1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#!/bin/sh
# file: /usr/share/bootcd/bootcdproberoot
# copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2001-2020)
# license: GNU General Public License, version 3
# description: bootcdproberoot -
# try to detect the root device and set it as new
# wait for usb, but stop waiting if device is recognized
# uses /sys/block/<name>/dev
#
getdsk()
{
sed -n "s/^.*\<root=\(iso:\)*\([^ :]*\)\>.*$/\2/p"
}
getiso()
{
sed -n "s/^.*\<root=iso:\([^ :]*\):\([^ ]*\)\>.*$/\2/p"
}
getbootcdid()
{
sed -n "s/^.*\<BOOTCDID=\([^ ]*\).*$/\1/p"
}
# for cmdline in \
# "root=auto" \
# "root=auto BOOTCDID=<uuid>" \
# "root=iso:auto:/mydir/cdimage17.iso" \
# "root=/dev/sdc" \
# "root=/dev/sdc1" \
# "root=iso:/dev/sdc1:/mydir/cdimage17.iso" \
# "before root=auto behind" \
# "before root=iso:auto:/mydir/cdimage17.iso behind" \
# "before root=/dev/sdc behind" \
# "before root=/dev/sdc1 behind" \
# "before root=iso:/dev/sdc1:/mydir/cdimage17.iso behind"
# do
# echo "cmdline=<$cmdline> dsk=<$(echo $cmdline | getdsk)> iso=<$(echo $cmdline | getiso)>"
# done
# exit 0
dsk="$(cat /proc/cmdline | getdsk)"
iso="$(cat /proc/cmdline | getiso)"
BOOTCDID="$(cat /proc/cmdline | getbootcdid)"
# search for this file on the cd
bootcdfile="/etc/bootcd/thisbootcd.conf"
if [ "$iso" ]; then
bootcdfile="$iso"
fi
ROOT=""
mkdir -p /mnt /dev
for try in 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1; do
[ $try -eq 20 ] && echo "bootcdproberoot: searching ..." >&2
if [ "$dsk" = "auto" ]; then
# /sys/class/block seems a better option for sata/scsi[?]/usb stuff.
# /sys/class/block does not exist on 2.6.26-2-amd64
# devnames="$(ls /sys/class/block | grep -e "^sr" -e "^sd" -e "^hd")"
# ls may (in stretch 2016/05/12) return lines like
# lrwxrwxrwx 1 0 0 0 sr0 -> ../../devices/pci0000:00/0000:00:01.1/ata2/hos\
# t1/arget1:0:0/1:0:0:0/block/sr0 in busybox"
# devnames="$( (cd /sys/class/block; echo *) )"
# floppy is too slow
devnames="$(/bin/ls /sys/class/block | grep -v "^fd")"
else
[ $try -eq 20 ] && echo "bootcdproberoot: only check device from commandline" >&2
devnames="$(basename $dsk)"
fi
# make sure sr0 (if found) will be tried first
devnames="$(echo "$devnames" | sed -E -e "s/(.*) sr0\>/sr0 \1/")"
for devname in $devnames; do
echo "bootcdproberoot: searching on devices $devname" >&2
# try to mount the selected devices to /mnt and look for bootcdfile
if [ -e /dev/$devname ]; then
# give other filesystems than iso9660 a chance, but also try iso9660
# as last option, because auto does not always work
# next line does not always work, so we still need for-next-loop
# ret=$(mount -t ext2,ext3,ext4,iso9660,ntfs,auto -n /dev/$devname /mnt >/dev/null 2>&1; echo $?)
for i in ext2 ext3 ext4 iso9660 ntfs auto; do
ret=$(mount -t $i -n /dev/$devname /mnt >/dev/null 2>&1; echo $?)
[ $ret = 0 ] && break
done
if [ $ret != 0 ]; then
echo "bootcdproberoot: $devname is not mountable." >&2
else
if [ ! -e "/mnt/$bootcdfile" ]; then
echo "bootcdproberoot: $bootcdfile not found on $devname" >&2
umount /mnt
elif [ "$BOOTCDID" ]; then
if [ ! "$(grep "^BOOTCDID=$BOOTCDID$" /mnt/$bootcdfile)" ]; then
echo "bootcdproberoot: BOOTCDID=$BOOTCDID not found in $bootcdfile on $devname" >&2
umount /mnt
else
echo "bootcdproberoot: found BOOTCDID=$BOOTCDID in $bootcdfile on $devname" >&2
ROOT=/dev/$devname
echo "ROOT=$ROOT" >/conf/param.conf
umount /mnt
break 2
fi
else
echo "bootcdproberoot: found $bootcdfile on $devname" >&2
ROOT=/dev/$devname
echo "ROOT=$ROOT" >/conf/param.conf
umount /mnt
break 2
fi
fi
fi
done
[ $try -eq 20 ] && echo "bootcdproberoot: waiting max 20 sec for usb" >&2
echo -n "$try " >&2
sleep 1
done
echo "" >&2
if [ ! "$ROOT" ]; then
echo "bootcdproberoot: could not find new root device" >&2
echo "bootcdproberoot: search yourself, try to mount, umount, write result and exit" >&2
echo "bootcdproberoot: exp: echo \"ROOT=/dev/sr0\" > /conf/param.conf" >&2
# For DEBUG /bin/sh is enabled
/bin/sh
fi
|