From f11eef01d5bcbd86a70ee0c1a3ae0f8278aa7cbf Mon Sep 17 00:00:00 2001 From: themylogin Date: Sat, 22 Jan 2022 11:54:44 +0100 Subject: [PATCH 1/2] Revert "Revert "Proper way to mount boot media"" --- conf/cd-files/root/.bash_profile | 195 ++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 4 deletions(-) diff --git a/conf/cd-files/root/.bash_profile b/conf/cd-files/root/.bash_profile index 63ee441..8f77deb 100755 --- a/conf/cd-files/root/.bash_profile +++ b/conf/cd-files/root/.bash_profile @@ -1,7 +1,194 @@ #!/bin/sh -# Mount the local CD/USB image to access install file -mkdir /cdrom -mount /dev/disk/by-label/ISOIMAGE /cdrom +# Inspired by https://salsa.debian.org/live-team/live-boot/-/blob/master/components/9990-misc-helpers.sh -until /sbin/truenas-install; do true; done +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}" +} + +if find_livefs +then + until /sbin/truenas-install; do true; done +else + read -p "No installation media found. Press enter to reboot..." answer + reboot +fi From 7a98a96f3b03a4f81b907faa8f587c48604a0991 Mon Sep 17 00:00:00 2001 From: themylogin Date: Sat, 22 Jan 2022 11:56:34 +0100 Subject: [PATCH 2/2] mkdir mountpoint --- conf/cd-files/root/.bash_profile | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/cd-files/root/.bash_profile b/conf/cd-files/root/.bash_profile index 8f77deb..468f16d 100755 --- a/conf/cd-files/root/.bash_profile +++ b/conf/cd-files/root/.bash_profile @@ -185,6 +185,7 @@ non_removable_dev () echo "${ret}" } +mkdir -p "$mountpoint" if find_livefs then until /sbin/truenas-install; do true; done