Release new version 1:20210208+grml.5
[live-boot-grml.git] / components / 9990-misc-helpers.sh
index 7994ecd..d14b072 100755 (executable)
@@ -15,6 +15,42 @@ is_live_path()
        return 1
 }
 
+grml_match_bootid()
+{
+       path="$1"
+
+       if [ -n "$IGNORE_BOOTID" ] ; then
+               echo " * Ignoring verification of bootid.txt as requested via ignore_bootid.">>/boot.log
+               return 0
+       fi
+
+       if [ -n "$BOOTID" ] && ! [ -r "${path}/conf/bootid.txt" ] ; then
+               echo "  * Warning: bootid=... specified but no bootid.txt found on currently requested device.">>/boot.log
+               return 1
+       fi
+
+       [ -r "${path}/conf/bootid.txt" ] || return 0
+
+       bootid_conf=$(cat "${path}/conf/bootid.txt")
+
+       if [ -z "$BOOTID" -a -z "$IGNORE_BOOTID" ]
+       then
+               echo " * Warning: bootid.txt found but ignore_bootid / bootid=.. bootoption missing...">>/boot.log
+               return 1
+       fi
+
+       if [ "$BOOTID" = "$bootid_conf" ]
+       then
+               echo " * Successfully verified /conf/bootid.txt from ISO, continuing... ">>/boot.log
+       else
+               echo " * Warning: BOOTID of ISO does not match. Retrying and continuing search...">>/boot.log
+               return 1
+       fi
+
+       return 0
+}
+
+
 matches_uuid ()
 {
        if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
@@ -101,37 +137,85 @@ check_dev ()
        skip_uuid_check="${3}"
 
        # support for fromiso=.../isofrom=....
+       local iso_device=''
        if [ -n "$FROMISO" ]
        then
-               ISO_DEVICE=$(dirname $FROMISO)
-               if ! [ -b $ISO_DEVICE ]
+               fs_type="${FROMISO%%:*}"
+               fs_type_auto='1'
+               iso_device="${FROMISO}"
+               if echo "${fs_type}" | grep -q '[^[:alnum:]_-]'; then
+                       # Not a valid file system name. Treat as part of the
+                       # path, and, especially, use autodetection.
+                       fs_type=''
+               else
+                       # Looks like a file system specification, treat it
+                       # like that.
+                       fs_type_auto='0'
+                       iso_device="${iso_device#*:}"
+               fi
+               iso_device=$(dirname "${iso_device}")
+               if ! [ -b "${iso_device}" ]
                then
                        # to support unusual device names like /dev/cciss/c0d0p1
                        # as well we have to identify the block device name, let's
                        # do that for up to 15 levels
-                       i=15
-                       while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
+                       i='15'
+                       while [ -n "${iso_device}" ] && [ "${i}" -gt '0' ]
                        do
-                               ISO_DEVICE=$(dirname ${ISO_DEVICE})
-                               [ -b "$ISO_DEVICE" ] && break
-                               i=$(($i -1))
+                               iso_device=$(dirname "${iso_device}")
+                               if [ -b "${iso_device}" ]; then
+                                       # Proper device, we're done.
+                                       break
+                               else
+                                       # Otherwise, it *could* be a proper device
+                                       # that just hasn't been started yet - think
+                                       # lvm or RAID.
+                                       case "${iso_device}" in
+                                               '/dev/mapper/'*|'/dev/md/'*)
+                                                       # We're looking for paths like
+                                                       # /dev/{mapper,md}/foo here.
+                                                       if printf '%s\n' "${iso_device}" | \
+                                                          grep -Eqs '^/dev/(mapper|md)/[^/]+$'; then
+                                                               # Okay, looks like a device we'll
+                                                               # have to start later on, done here.
+                                                               break
+                                                       fi
+                                                       ;;
+                                               '/dev/md'*)
+                                                       # This is different from the path above.
+                                                       # Here, we're looking for something like
+                                                       # /dev/md0, /dev/md126 and the like.
+                                                       if printf '%s\n' "${iso_device}" | \
+                                                          grep -Eqs '^/dev/md[[:digit:]]+$'; then
+                                                               # Syntax matches, keep as device
+                                                               # name for later when RAIDs were
+                                                               # started.
+                                                               break
+                                                       fi
+                                                       ;;
+                                       esac
+                               fi
+                               i="$(($i - 1))"
                        done
                fi
 
-               if [ "$ISO_DEVICE" = "/" ]
+               if [ "${iso_device}" = '/' ]
                then
                        # not a block device, check if it's an iso file, for
                        # example an ISO when booting on an ONIE system
                        if echo "${FROMISO}" | grep -q "\.iso$"
                        then
-                               fs_type=$(get_fstype "${FROMISO}")
+                               if [ '0' -ne "${fs_type_auto}" ]; then
+                                       # Autodetect fs type if not overridden.
+                                       fs_type=$(get_fstype "${FROMISO}")
+                               fi
                                if is_supported_fs ${fs_type}
                                then
                                        mkdir /run/live/fromiso
-                                       mount -t $fs_type "${FROMISO}" /run/live/fromiso
+                                       mount -t "${fs_type}" -o 'ro' "${FROMISO}" '/run/live/fromiso'
                                        if [ "$?" != 0 ]
                                        then
-                                               echo "Warning: unable to mount ${FROMISO}." >>/boot.log
+                                               echo "Warning: unable to mount ${FROMISO} (type ${fs_type})." >>/boot.log
                                        fi
                                        devname="/run/live/fromiso"
                                fi
@@ -139,25 +223,19 @@ check_dev ()
                                echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
                        fi
                else
-                       fs_type=$(get_fstype "${ISO_DEVICE}")
-                       if is_supported_fs ${fs_type}
-                       then
-                               mkdir /run/live/fromiso
-                               mount -t $fs_type "$ISO_DEVICE" /run/live/fromiso
-                               ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
-                               loopdevname=$(setup_loop "/run/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
-                               devname="${loopdevname}"
-                       else
-                               echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
-                       fi
+                       # Otherwise, it must be a block device, so record that as the devname.
+                       devname="${iso_device}"
+               fi
+       else
+               # Usual handling in the non-fromiso case.
+               if [ -z "${devname}" ]
+               then
+                       devname=$(sys2dev "${sysdev}")
                fi
        fi
 
-       if [ -z "${devname}" ]
-       then
-               devname=$(sys2dev "${sysdev}")
-       fi
-
+       # Common code path again: if devname is a directory, we can just go ahead
+       # and bind-mount it to our target mount point.
        if [ -d "${devname}" ]
        then
                mount -o bind "${devname}" $mountpoint || continue
@@ -171,7 +249,10 @@ check_dev ()
                fi
        fi
 
+       # Check if we have to start logical volumes or RAID arrays.
+       # Do this in the common code path so it's only executed once.
        IFS=","
+       local device=''
        for device in ${devname}
        do
                case "$device" in
@@ -185,18 +266,71 @@ check_dev ()
 
                        /dev/md*)
                                # Adding raid support
-                               if [ -x /scripts/local-top/mdadm ]
+                               if [ -x /scripts/local-block/mdadm ]
                                then
-                                       [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
-                                       echo "MD_DEVS=$device " >> /conf/conf.d/md
-                                       /scripts/local-top/mdadm >>/boot.log
-                                       [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
+                                       # Before actually doing anything, make sure that no auto-generated
+                                       # mdadm.conf is around in the initramfs.
+                                       # Short explanation of why this is needed:
+                                       # /usr/share/initramfs-tools/hooks/mdadm is executed as part of
+                                       # mkinitramfs, which generates an /etc/mdadm/mdadm.conf file if one
+                                       # doesn't exist yet (which should always be the case in our new
+                                       # chroot) based on the host system configuration. Eventually, this
+                                       # is copied into the generated initramfs and then causes issues on
+                                       # other systems.
+                                       # It's safe to delete the file within the initramfs. The actual
+                                       # squashfs root will not be affected by that (and, additionally,
+                                       # a FAI script makes sure that the mdadm.conf in there gets wiped
+                                       # of any auto-generated arrays).
+                                       rm -rf '/etc/mdadm/mdadm.conf'
+
+                                       # Back in the day, when there was still a local-top mdadm script, we
+                                       # used to select specific devices to be auto-assembled.
+                                       # This functionality was dropped in the local-block script, so just
+                                       # scan and assemble all RAID devices.
+                                       /scripts/local-block/mdadm >>/boot.log
                                fi
                                ;;
                esac
        done
        unset IFS
 
+       # Back to fromiso handling, if necessary.
+       # We should now have the necessary lv/RAID devices available, so try to use them.
+       if [ -n "$FROMISO" ]
+       then
+               if [ "${iso_device}" != '/' ]
+               then
+                       # Need to extract actual ISO file path later on,
+                       # initialize first.
+                       iso_name="${FROMISO}"
+
+                       if [ '0' -ne "${fs_type_auto}" ]; then
+                               # Try to auto-detect file system if not
+                               # explicitly provided.
+                               fs_type=$(get_fstype "${iso_device}")
+                       else
+                               # Delete file system type override.
+                               iso_name="${iso_name#*:}"
+                       fi
+                       # At this point, the backing device should always be
+                       # at the very front, so remove that - leaving only the
+                       # ISO file path.
+                       iso_name="$(echo "${iso_name}" | sed "s|^${iso_device}||")"
+                       if is_supported_fs ${fs_type}
+                       then
+                               mkdir /run/live/fromiso
+                               mount -t "${fs_type}" -o 'ro' "${iso_device}" '/run/live/fromiso'
+                               loopdevname=$(setup_loop "/run/live/fromiso/${iso_name}" "loop" "/sys/block/loop*" "" '')
+                               devname="${loopdevname}"
+
+                               # Reset device variable, we won't need it.
+                               device=''
+                       else
+                               echo "Warning: unable to mount ${iso_device} (type ${fs_type})." >>/boot.log
+                       fi
+               fi
+       fi
+
        [ -n "$device" ] && devname="$device"
 
        [ -e "$devname" ] || continue
@@ -259,7 +393,7 @@ check_dev ()
                fi
 
                if is_live_path ${mountpoint} && \
-                       ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
+                       ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
                then
                        echo ${mountpoint}
                        return 0
@@ -355,19 +489,6 @@ find_livefs ()
                                        return 0
                                fi
                        done
-               elif [ "${fstype}" = "squashfs" -o \
-                       "${fstype}" = "btrfs" -o \
-                       "${fstype}" = "ext2" -o \
-                       "${fstype}" = "ext3" -o \
-                       "${fstype}" = "ext4" -o \
-                       "${fstype}" = "jffs2" ]
-               then
-                       # This is an ugly hack situation, the block device has
-                       # an image directly on it.  It's hopefully
-                       # live-boot, so take it and run with it.
-                       ln -s "${devname}" "${devname}.${fstype}"
-                       echo "${devname}.${fstype}"
-                       return 0
                fi
        done
 
@@ -738,7 +859,7 @@ try_mount ()
                        fstype=$(get_fstype "${dev}")
                fi
                mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
-               ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
+               ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" >> boot.log && return 0 )
        fi
 }