Use systemd service to mount installation media

We can't use `.bash_profile` to do this as it is spawned
on all terminals simultaneously and causes a race condition.
This commit is contained in:
themylogin
2022-01-22 17:41:09 +01:00
parent 83d40468cb
commit f66b967f53
5 changed files with 206 additions and 190 deletions

View File

@@ -0,0 +1,14 @@
[Unit]
Description=Mount installation media
DefaultDependencies=no
Before=local-fs.target
[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutStartSec=300
ExecStart=/sbin/mount-cd
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1 @@
../mount-cd.service

View File

@@ -1,192 +1,6 @@
#!/bin/sh
# Inspired by https://salsa.debian.org/live-team/live-boot/-/blob/master/components/9990-misc-helpers.sh
mountpoint=/cdrom
is_live_path()
{
FILE="${1}/TrueNAS-SCALE.update"
if [ -f "${FILE}" ]
then
return 0
fi
return 1
}
is_nice_device ()
{
sysfs_path="${1#/sys}"
if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
then
return 0
elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
then
return 0
elif echo ${sysfs_path} | grep -q "^/block/dm-"
then
return 0
elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
then
return 0
fi
return 1
}
check_dev ()
{
local force fix
sysdev="${1}"
devname="${2}"
skip_uuid_check="${3}"
if [ -z "${devname}" ]
then
devname=$(sys2dev "${sysdev}")
fi
mount "${devname}" $mountpoint || return 1
if is_live_path $mountpoint
then
echo $mountpoint
return 0
else
umount $mountpoint
fi
return 1
}
find_livefs ()
{
# scan of block devices to find the installation media
# prefer removable devices over non-removable devices, so scan them first
devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
for sysblock in $devices_to_scan
do
devname=$(sys2dev "${sysblock}")
[ -e "$devname" ] || continue
if /lib/udev/cdrom_id ${devname} > /dev/null
then
if check_dev "null" "${devname}"
then
return 0
fi
elif is_nice_device "${sysblock}"
then
for dev in $(subdevices "${sysblock}")
do
if check_dev "${dev}"
then
return 0
fi
done
fi
done
return 1
}
sys2dev ()
{
sysdev=${1#/sys}
echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
}
subdevices ()
{
sysblock=${1}
r=""
for dev in "${sysblock}"/* "${sysblock}"
do
if [ -e "${dev}/dev" ]
then
r="${r} ${dev}"
fi
done
echo ${r}
}
removable_dev ()
{
output_format="${1}"
want_usb="${2}"
ret=
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
do
if [ ! -d "${sysblock}" ]; then
continue
fi
dev_ok=
if [ "$(cat ${sysblock}/removable)" = "1" ]
then
if [ -z "${want_usb}" ]
then
dev_ok="true"
else
if readlink ${sysblock} | grep -q usb
then
dev_ok="true"
fi
fi
fi
if [ "${dev_ok}" = "true" ]
then
case "${output_format}" in
sys)
ret="${ret} ${sysblock}"
;;
*)
devname=$(sys2dev "${sysblock}")
ret="${ret} ${devname}"
;;
esac
fi
done
echo "${ret}"
}
non_removable_dev ()
{
output_format="${1}"
ret=
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
do
if [ ! -d "${sysblock}" ]; then
continue
fi
if [ "$(cat ${sysblock}/removable)" = "0" ]
then
case "${output_format}" in
sys)
ret="${ret} ${sysblock}"
;;
*)
devname=$(sys2dev "${sysblock}")
ret="${ret} ${devname}"
;;
esac
fi
done
echo "${ret}"
}
mkdir -p "$mountpoint"
if find_livefs
if -f /cdrom/TrueNAS-SCALE.update
then
until /sbin/truenas-install; do true; done
else

189
conf/cd-files/sbin/mount-cd Executable file
View File

@@ -0,0 +1,189 @@
#!/bin/sh
# Inspired by https://salsa.debian.org/live-team/live-boot/-/blob/master/components/9990-misc-helpers.sh
mountpoint=/cdrom
is_live_path()
{
FILE="${1}/TrueNAS-SCALE.update"
if [ -f "${FILE}" ]
then
return 0
fi
return 1
}
is_nice_device ()
{
sysfs_path="${1#/sys}"
if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
then
return 0
elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
then
return 0
elif echo ${sysfs_path} | grep -q "^/block/dm-"
then
return 0
elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
then
return 0
fi
return 1
}
check_dev ()
{
local force fix
sysdev="${1}"
devname="${2}"
skip_uuid_check="${3}"
if [ -z "${devname}" ]
then
devname=$(sys2dev "${sysdev}")
fi
mount "${devname}" $mountpoint || return 1
if is_live_path $mountpoint
then
echo $mountpoint
return 0
else
umount $mountpoint
fi
return 1
}
find_livefs ()
{
# scan of block devices to find the installation media
# prefer removable devices over non-removable devices, so scan them first
devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
for sysblock in $devices_to_scan
do
devname=$(sys2dev "${sysblock}")
[ -e "$devname" ] || continue
if /lib/udev/cdrom_id ${devname} > /dev/null
then
if check_dev "null" "${devname}"
then
return 0
fi
elif is_nice_device "${sysblock}"
then
for dev in $(subdevices "${sysblock}")
do
if check_dev "${dev}"
then
return 0
fi
done
fi
done
return 1
}
sys2dev ()
{
sysdev=${1#/sys}
echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
}
subdevices ()
{
sysblock=${1}
r=""
for dev in "${sysblock}"/* "${sysblock}"
do
if [ -e "${dev}/dev" ]
then
r="${r} ${dev}"
fi
done
echo ${r}
}
removable_dev ()
{
output_format="${1}"
want_usb="${2}"
ret=
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
do
if [ ! -d "${sysblock}" ]; then
continue
fi
dev_ok=
if [ "$(cat ${sysblock}/removable)" = "1" ]
then
if [ -z "${want_usb}" ]
then
dev_ok="true"
else
if readlink ${sysblock} | grep -q usb
then
dev_ok="true"
fi
fi
fi
if [ "${dev_ok}" = "true" ]
then
case "${output_format}" in
sys)
ret="${ret} ${sysblock}"
;;
*)
devname=$(sys2dev "${sysblock}")
ret="${ret} ${devname}"
;;
esac
fi
done
echo "${ret}"
}
non_removable_dev ()
{
output_format="${1}"
ret=
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
do
if [ ! -d "${sysblock}" ]; then
continue
fi
if [ "$(cat ${sysblock}/removable)" = "0" ]
then
case "${output_format}" in
sys)
ret="${ret} ${sysblock}"
;;
*)
devname=$(sys2dev "${sysblock}")
ret="${ret} ${devname}"
;;
esac
fi
done
echo "${ret}"
}
mkdir -p "$mountpoint"
find_livefs

View File

@@ -1,4 +1,3 @@
import distutils.dir_util
import glob
import itertools
import os
@@ -51,8 +50,7 @@ def make_iso_file():
f.write('truenas.local')
# Copy the CD files
distutils.dir_util._path_created = {}
distutils.dir_util.copy_tree(CD_FILES_DIR, CHROOT_BASEDIR, preserve_symlinks=True)
run(f'rsync -av {CD_FILES_DIR}/ {CHROOT_BASEDIR}/', shell=True)
# Create the CD assembly dir
if os.path.exists(CD_DIR):