Release new version 1:20210208+grml.5
[live-boot-grml.git] / components / 9990-misc-helpers.sh
index 570a12a..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 ]
@@ -76,7 +112,7 @@ is_nice_device ()
 {
        sysfs_path="${1#/sys}"
 
-       if udevadm test-builtin path_id "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
+       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]$'
@@ -95,51 +131,111 @@ is_nice_device ()
 
 check_dev ()
 {
+       local force fix
        sysdev="${1}"
        devname="${2}"
        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))
-                       done
+                               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
-                       echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
-               else
-                       fs_type=$(get_fstype "${ISO_DEVICE}")
-                       if is_supported_fs ${fs_type}
+                       # 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
-                               mkdir /live/fromiso
-                               mount -t $fs_type "$ISO_DEVICE" /live/fromiso
-                               ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
-                               loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
-                               devname="${loopdevname}"
+                               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}" -o 'ro' "${FROMISO}" '/run/live/fromiso'
+                                       if [ "$?" != 0 ]
+                                       then
+                                               echo "Warning: unable to mount ${FROMISO} (type ${fs_type})." >>/boot.log
+                                       fi
+                                       devname="/run/live/fromiso"
+                               fi
                        else
-                               echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
+                               echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
                        fi
+               else
+                       # 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
@@ -153,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
@@ -161,24 +260,77 @@ check_dev ()
                                # Adding lvm support
                                if [ -x /scripts/local-top/lvm2 ]
                                then
-                                       ROOT="$device" resume="" /scripts/local-top/lvm2
+                                       ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
                                fi
                                ;;
 
                        /dev/md*)
                                # Adding raid support
-                               if [ -x /scripts/local-top/mdadm ]
+                               if [ -x /scripts/local-block/mdadm ]
                                then
-                                       cp /conf/conf.d/md /conf/conf.d/md.orig
-                                       echo "MD_DEVS=$device " >> /conf/conf.d/md
-                                       /scripts/local-top/mdadm
-                                       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
@@ -195,6 +347,33 @@ check_dev ()
        then
                devuid=$(blkid -o value -s UUID "$devname")
                [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
+
+               for _PARAMETER in ${LIVE_BOOT_CMDLINE}
+               do
+                       case "${_PARAMETER}" in
+                               forcefsck)
+                                       FORCEFSCK="true"
+                                       ;;
+                       esac
+               done
+
+               if [ "${PERSISTENCE_FSCK}" = "true" ] ||  [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
+               then
+                       force=""
+                       if [ "$FORCEFSCK" = "true" ]
+                       then
+                               force="-f"
+                       fi
+
+                       fix="-a"
+                       if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
+                       then
+                               fix="-y"
+                       fi
+
+                       fsck $fix $force ${devname} >> fsck.log 2>&1
+               fi
+
                mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
                [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
 
@@ -203,9 +382,9 @@ check_dev ()
                        if [ -f ${mountpoint}/${FINDISO} ]
                        then
                                umount ${mountpoint}
-                               mkdir -p /live/findiso
-                               mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
-                               loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
+                               mkdir -p /run/live/findiso
+                               mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
+                               loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
                                devname="${loopdevname}"
                                mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
                        else
@@ -214,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
@@ -310,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
 
@@ -420,13 +586,27 @@ is_supported_fs ()
                return 1
        fi
 
+       # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
+       if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
+       then
+               return 1
+       fi
+
        # Try to look if it is already supported by the kernel
+       # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
+       if [ "${fstype}" = "ntfs" ]; then
+               if type ntfs-3g >/dev/null 2>&1; then
+                       return 0
+               else
+                       return 1
+               fi
+       fi
        if grep -q ${fstype} /proc/filesystems
        then
                return 0
        else
                # Then try to add support for it the gentle way using the initramfs capabilities
-               modprobe ${fstype}
+               modprobe -q -b ${fstype}
                if grep -q ${fstype} /proc/filesystems
                then
                        return 0
@@ -449,7 +629,7 @@ is_supported_fs ()
 
 get_fstype ()
 {
-       /sbin/blkid -s TYPE -o value $1 2>/dev/null
+       blkid -s TYPE -o value $1 2>/dev/null
 }
 
 where_is_mounted ()
@@ -507,7 +687,7 @@ base_path ()
 {
        testpath="${1}"
        mounts="$(awk '{print $2}' /proc/mounts)"
-       testpath="$(busybox realpath ${testpath})"
+       testpath="$(realpath ${testpath})"
 
        while true
        do
@@ -625,7 +805,7 @@ setup_loop ()
                                        echo "${passphrase}" > /tmp/passphrase
                                        unset passphrase
                                        exec 9</tmp/passphrase
-                                       /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
+                                       losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
                                        error=${?}
                                        exec 9<&-
                                        rm -f /tmp/passphrase
@@ -679,13 +859,14 @@ 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
 }
 
 # Try to mount $device to the place expected by live-boot. If $device
-# is already mounted somewhere, move it to the expected place. If
-# we're only probing $device (to check if it has custom persistence)
+# is already mounted somewhere, move it to the expected place. If $device
+# ends with a "/" this is a directory path.
+# If we're only probing $device (to check if it has custom persistence)
 # $probe should be set, which suppresses warnings upon failure. On
 # success, print the mount point for $device.
 mount_persistence_media ()
@@ -694,7 +875,21 @@ mount_persistence_media ()
        device=${1}
        probe=${2}
 
-       backing="/live/persistence/$(basename ${device})"
+       # get_custom_mounts() might call this with a directory path instead
+       # of a block device path. This means we have found sub-directory path
+       # underneath /run/live/persistence, so we're done
+       if [ -d "${device}" ]
+       then
+               echo "${device}"
+               return 0
+       fi
+
+       if [ ! -b "${device}" ]
+       then
+               return 1
+       fi
+
+       backing="/run/live/persistence/$(basename ${device})"
 
        mkdir -p "${backing}"
        old_backing="$(where_is_mounted ${device})"
@@ -706,7 +901,7 @@ mount_persistence_media ()
                then
                        mount_opts="ro,noatime"
                fi
-               if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
+               if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
                then
                        echo ${backing}
                        return 0
@@ -717,15 +912,24 @@ mount_persistence_media ()
                fi
        elif [ "${backing}" != "${old_backing}" ]
        then
-               if mount --move ${old_backing} ${backing} >/dev/null
+               if ! mount -o move ${old_backing} ${backing} >/dev/null
                then
-                       echo ${backing}
-                       return 0
-               else
                        [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
                        rmdir "${backing}"
                        return 1
                fi
+               mount_opts="rw,noatime"
+               if [ -n "${PERSISTENCE_READONLY}" ]
+               then
+                       mount_opts="ro,noatime"
+               fi
+               if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
+               then
+                       log_warning_msg "Failed to remount persistence media ${device} writable"
+                       # Don't unmount or rmdir the new mountpoint in this case
+               fi
+               echo ${backing}
+               return 0
        else
                # This means that $device has already been mounted on
                # the place expected by live-boot, so we're done.
@@ -748,7 +952,7 @@ close_persistence_media ()
 
        if is_active_luks_mapping ${device}
        then
-               /sbin/cryptsetup luksClose ${device}
+               cryptsetup luksClose ${device}
        fi
 }
 
@@ -762,7 +966,7 @@ open_luks_device ()
                opts="${opts} --readonly"
        fi
 
-       if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
+       if cryptsetup status "${name}" >/dev/null 2>&1
        then
                re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
                opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
@@ -779,10 +983,31 @@ open_luks_device ()
 
        load_keymap
 
+       # check for plymouth
+       if [ -x /bin/plymouth ]
+       then
+               _PLYMOUTH="true"
+       fi
+
+       case "${_PLYMOUTH}" in
+               true)
+                       plymouth --ping
+
+                       cryptkeyscript="plymouth ask-for-password --prompt"
+                       # Plymouth will add a : if it is a non-graphical prompt
+                       cryptkeyprompt="Please unlock disk ${dev}"
+                       ;;
+
+               *)
+                       cryptkeyscript="/lib/cryptsetup/askpass"
+                       cryptkeyprompt="Please unlock disk ${dev}: "
+                       ;;
+       esac
+
        while true
        do
-               /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
-                       /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
+               $cryptkeyscript "$cryptkeyprompt" | \
+                       cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
 
                if [ 0 -eq ${?} ]
                then
@@ -792,11 +1017,28 @@ open_luks_device ()
                fi
 
                echo >&6
-               echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
-               read answer
+               retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
+
+               case "${_PLYMOUTH}" in
+                       true)
+                               plymouth display-message --text "${retryprompt}"
+                               answer=$(plymouth watch-keystroke --keys="YNyn")
+                               ;;
+
+                       *)
+                               echo -n "${retryprompt} " >&6
+                               read answer
+                               ;;
+               esac
 
                if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
                then
+                       case "${_PLYMOUTH}" in
+                               true)
+                                       plymouth display-message --text ""
+                                       ;;
+                       esac
+
                        return 2
                fi
        done
@@ -806,14 +1048,14 @@ get_gpt_name ()
 {
     local dev
     dev="${1}"
-    /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
+    blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
 }
 
 is_gpt_device ()
 {
     local dev
     dev="${1}"
-    [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
+    [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
 }
 
 probe_for_gpt_name ()
@@ -853,7 +1095,7 @@ probe_for_fs_label ()
 
        for label in ${overlays}
        do
-               if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
+               if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
                then
                        echo "${label}=${dev}"
                fi
@@ -894,6 +1136,39 @@ probe_for_file_name ()
        fi
 }
 
+probe_for_directory_name ()
+{
+       local overlays dev ret backing
+       overlays="${1}"
+       dev="${2}"
+
+       ret=""
+       backing="$(mount_persistence_media ${dev} probe)"
+       if [ -z "${backing}" ]
+       then
+           return
+       fi
+
+       for label in ${overlays}
+       do
+               path=${backing}/${PERSISTENCE_PATH}/${label}
+               if [ -d "${path}" ]
+               then
+                       # in this case the "device" ends with a "/"
+                       ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
+               fi
+       done
+
+       if [ -n "${ret}" ]
+       then
+               echo ${ret}
+       else
+               # unmount and remove mountpoint
+               umount ${backing} > /dev/null 2>&1 || true
+               rmdir ${backing} > /dev/null 2>&1 || true
+       fi
+}
+
 find_persistence_media ()
 {
        # Scans devices for overlays, and returns a whitespace
@@ -918,7 +1193,17 @@ find_persistence_media ()
        white_listed_devices="${2}"
        ret=""
 
-       black_listed_devices="$(what_is_mounted_on /live/medium) $(what_is_mounted_on /live/findiso) $(what_is_mounted_on /live/fromiso)"
+       #
+       # The devices that are hosting the actual live rootfs should not be
+       # used for persistence storage since otherwise you might mount a
+       # parent directory on top of a sub-directory of the same filesystem
+       # in one union together.
+       #
+       black_listed_devices=""
+       for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
+       do
+               black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
+       done
 
        for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
        do
@@ -969,18 +1254,18 @@ find_persistence_media ()
                        result=$(probe_for_file_name "${overlays}" ${dev})
                        if [ -n "${result}" ]
                        then
-                               local loopdevice
+                               local loopdevice
                                loopdevice=${result##*=}
-                               if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
+                               if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
                                then
-                                       local luksfile
+                                       local luksfile
                                        luksfile=""
                                        if luksfile=$(open_luks_device "${loopdevice}")
                                        then
-                                               result=${result%%=*}
+                                               result=${result%%=*}
                                                result="${result}=${luksfile}"
                                        else
-                                               losetup -d $loopdevice
+                                               losetup -d $loopdevice
                                                result=""
                                        fi
                                fi
@@ -989,10 +1274,21 @@ find_persistence_media ()
                        fi
                fi
 
+               # Probe for directory with matching name on mounted partition
+               if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
+               then
+                       result=$(probe_for_directory_name "${overlays}" ${dev})
+                       if [ -n "${result}" ]
+                       then
+                               ret="${ret} ${result}"
+                               continue
+                       fi
+               fi
+
                # Close luks device if it isn't used
                if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
                then
-                       /sbin/cryptsetup luksClose "${luks_device}"
+                       cryptsetup luksClose "${luks_device}"
                fi
        done
 
@@ -1023,13 +1319,13 @@ get_mac ()
 is_luks_partition ()
 {
        device="${1}"
-       /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
+       cryptsetup isLuks "${device}" 1>/dev/null 2>&1
 }
 
 is_active_luks_mapping ()
 {
        device="${1}"
-       /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
+       cryptsetup status "${device}" 1>/dev/null 2>&1
 }
 
 get_luks_backing_device ()
@@ -1047,6 +1343,10 @@ removable_dev ()
 
        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
@@ -1092,6 +1392,10 @@ non_removable_dev ()
 
        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
@@ -1176,23 +1480,8 @@ do_union ()
                        rw_opt="rw"
                        ro_opt="rr+wh"
                        noxino_opt="noxino"
-                       ;;
-
-               unionfs-fuse)
-                       rw_opt="RW"
-                       ro_opt="RO"
-                       ;;
 
-               *)
-                       rw_opt="rw"
-                       ro_opt="ro"
-                       ;;
-       esac
-
-       case "${UNIONTYPE}" in
-               unionfs-fuse)
-                       unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
-                       unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
+                       unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
                        if [ -n "${unionro}" ]
                        then
                                for rofs in ${unionro}
@@ -1200,43 +1489,33 @@ do_union ()
                                        unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
                                done
                        fi
-                       ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
-                       unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
-                       ( mkdir -p /run/sendsigs.omit.d
-                       pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
-                       ;;
-
-               overlayfs)
-                       # XXX: can multiple unionro be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
-                       # XXX: and can unionro be optional? i.e. can overlayfs skip lowerdir?
-                       if echo ${unionro} | grep -q " "
-                       then
-                               panic "Multiple lower filesystems are currently not supported with overlayfs (unionro = ${unionro})."
-                       elif [ -z "${unionro}"  ]
-                       then
-                               panic "Overlayfs needs at least one lower filesystem (read-only branch)."
-                       fi
-                       unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}"
-                       mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
                        ;;
 
-               *)
-                       unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
-                       if [ -n "${unionro}" ]
+               overlay)
+                       # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
+                       if [ -z "${unionro}" ]
                        then
-                               for rofs in ${unionro}
-                               do
-                                       unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
-                               done
+                               panic "overlay needs at least one lower filesystem (read-only branch)."
                        fi
-                       mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
+                       # Multiple lower layers can now be given using the the colon (":") as a
+                       # separator character between the directory names.
+                       unionro="$(echo ${unionro} | sed -e 's| |:|g')"
+                       # overlayfs requires:
+                       # + a workdir to become mounted
+                       # + workdir and upperdir to reside under the same mount
+                       # + workdir and upperdir to be in separate directories
+                       mkdir -p "${unionrw}/rw"
+                       mkdir -p "${unionrw}/work"
+                       unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
                        ;;
        esac
+
+       mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
 }
 
 get_custom_mounts ()
 {
-       # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
+       # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
        # Side-effect: prints info to file $custom_mounts
 
        local custom_mounts devices bindings links
@@ -1250,11 +1529,6 @@ get_custom_mounts ()
 
        for device in ${devices}
        do
-               if [ ! -b "${device}" ]
-               then
-                       continue
-               fi
-
                local device_name backing include_list
                device_name="$(basename ${device})"
                backing=$(mount_persistence_media ${device})
@@ -1272,7 +1546,7 @@ get_custom_mounts ()
 
                if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
                then
-                       cp ${include_list} /live/persistence/${persistence_list}.${device_name}
+                       cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
                fi
 
                while read dir options # < ${include_list}
@@ -1283,9 +1557,9 @@ get_custom_mounts ()
                                continue
                        fi
 
-                       if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
+                       if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
                        then
-                               log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/lib/live\" or any of its sub-directories."
+                               log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/run/live\" or any of its sub-directories."
                                continue
                        fi
 
@@ -1304,7 +1578,7 @@ get_custom_mounts ()
                                        union|bind)
                                                ;;
                                        *)
-                                               log_warning_msg "Skipping custom mount with unkown option: ${opt}"
+                                               log_warning_msg "Skipping custom mount with unknown option: ${opt}"
                                                continue 2
                                                ;;
                                esac
@@ -1349,7 +1623,7 @@ get_custom_mounts ()
        prev_dest=""
        # This sort will ensure that a source /a comes right before a source
        # /a/b so we only need to look at the previous source
-       sort -k2 -b ${custom_mounts} |
+       [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
        while read device source dest options
        do
                if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
@@ -1457,7 +1731,7 @@ activate_custom_mounts ()
                rootfs_dest_backing=""
                if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
                then
-                       for d in /live/rootfs/*
+                       for d in /run/live/rootfs/*
                        do
                                if [ -n "${rootmnt}" ]
                                then
@@ -1475,11 +1749,11 @@ activate_custom_mounts ()
                local cow_dir links_source
                if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
                then
-                       link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
+                       link_files ${source} ${dest} ""
                elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
                then
-                       mkdir -p ${rootmnt}/lib/live/mount/persistence
-                       links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
+                       mkdir -p /run/live/persistence
+                       links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
                        chown_ref ${source} ${links_source}
                        chmod_ref ${source} ${links_source}
                        # We put the cow dir in the below strange place to
@@ -1487,7 +1761,7 @@ activate_custom_mounts ()
                        # has its own directory and isn't nested with some
                        # other custom mount (if so that mount's files would
                        # be linked, causing breakage.
-                       cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
+                       cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
                        mkdir -p ${cow_dir}
                        chown_ref "${source}" "${cow_dir}"
                        chmod_ref "${source}" "${cow_dir}"
@@ -1498,13 +1772,13 @@ activate_custom_mounts ()
                        do_union ${dest} ${source} ${rootfs_dest_backing}
                elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
                then
-                       mount --bind "${source}" "${dest}"
+                       mount -bind "${source}" "${dest}"
                elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
                then
                        # bind-mount and union mount are handled the same
                        # in read-only mode, but note that rootfs_dest_backing
                        # is non-empty (and necessary) only for unions
-                       cow_dir="/live/overlay/${dest}"
+                       cow_dir="/run/live/overlay/${dest}"
                        if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
                        then
                                # If an earlier custom mount has files here
@@ -1515,6 +1789,13 @@ activate_custom_mounts ()
                        mkdir -p ${cow_dir}
                        chown_ref "${source}" "${cow_dir}"
                        chmod_ref "${source}" "${cow_dir}"
+                       if [ "${UNIONTYPE}" = "overlay" ]
+                       then
+                               # When we use overlay we add the "/rw" postfix to our source when using it
+                               # as upper layer. Therefore we also have to add it here when using it as
+                               # the lower layer.
+                               source="${source}/rw"
+                       fi
                        do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
                fi