Refactoring device scanning in find_{cow_device,files}.
[live-boot-grml.git] / scripts / live-helpers
index a935001..90fa3b4 100644 (file)
@@ -1,4 +1,4 @@
-# live-initramfs helper functions, used by live-initramfs on boot and by live-snapshot
+# live-boot helper functions, used by live-boot on boot and by live-snapshot
 
 if [ ! -x "/bin/fstype" ]
 then
@@ -6,10 +6,19 @@ then
        export PATH="${PATH}:/usr/lib/klibc/bin"
 fi
 
+# handle upgrade path from old udev (using udevinfo) to
+# recent versions of udev (using udevadm info)
+if [ -x /sbin/udevadm ]
+then
+       udevinfo='/sbin/udevadm info'
+else
+       udevinfo='udevinfo'
+fi
+
 sys2dev ()
 {
        sysdev=${1#/sys}
-       echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
+       echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
 }
 
 subdevices ()
@@ -17,7 +26,7 @@ subdevices ()
        sysblock=${1}
        r=""
 
-       for dev in "${sysblock}" "${sysblock}"/*
+       for dev in "${sysblock}"/* "${sysblock}"
        do
                if [ -e "${dev}/dev" ]
                then
@@ -28,6 +37,38 @@ subdevices ()
        echo ${r}
 }
 
+storage_devices()
+{
+       black_listed_devices="${1}"
+       white_listed_devices="${2}"
+
+       for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
+       do
+               fulldevname=$(sys2dev "${sysblock}")
+
+               if echo "${black_listed_devices}" | grep -qw "${fulldevname}" || \
+                       [ -n "${white_listed_devices}" ] && \
+                       echo "${white_listed_devices}" | grep -vqw "${fulldevname}"
+               then
+                       # skip this device entirely
+                       continue
+               fi
+
+               for dev in $(subdevices "${sysblock}")
+               do
+                       devname=$(sys2dev "${dev}")
+
+                       if echo "${black_listed_devices}" | grep -qw "${devname}"
+                       then
+                               # skip this subdevice
+                               continue
+                       else
+                               echo "${devname}"
+                       fi
+               done
+       done
+}
+
 is_supported_fs ()
 {
        fstype="${1}"
@@ -67,25 +108,7 @@ is_supported_fs ()
 
 get_fstype ()
 {
-       local FSTYPE
-       local FSSIZE
-
-       # fstype misreports LUKS devices
-       if is_luks "${1}"
-       then
-           /lib/udev/vol_id -t ${1} 2>/dev/null
-           return
-       fi
-
-       eval $(fstype < ${1} 2>/dev/null)
-
-       if [ "${FSTYPE}" != "unknown" ]
-       then
-               echo ${FSTYPE}
-               return 0
-       fi
-
-       /lib/udev/vol_id -t ${1} 2>/dev/null
+       /sbin/blkid -s TYPE -o value $1 2>/dev/null
 }
 
 where_is_mounted ()
@@ -95,7 +118,7 @@ where_is_mounted ()
        if grep -q "^${device} " /proc/mounts
        then
                # return the first found
-               grep "^${device} " /proc/mounts | cut -f2 -d ' '
+               grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
        fi
 }
 
@@ -188,16 +211,13 @@ setup_loop ()
        local encryption=${5}
        local readonly=${6}
 
-       modprobe -q -b "${module}"
+       # the output of setup_loop is evaluated in other functions,
+       # modprobe leaks kernel options like "libata.dma=0"
+       # as "options libata dma=0" on stdout, causing serious
+       # problems therefor, so instead always avoid output to stdout
+       modprobe -q -b "${module}" 1>/dev/null
 
-       if [ -x /sbin/udevadm ]
-       then
-               # lenny
-               udevadm settle
-       else
-               # etch
-               udevsettle
-       fi
+       udevadm settle
 
        for loopdev in ${pattern}
        do
@@ -206,7 +226,7 @@ setup_loop ()
                        dev=$(sys2dev "${loopdev}")
                        options=''
 
-                       if [ -n ${readonly} ]
+                       if [ -n "${readonly}" ]
                        then
                                if losetup --help 2>&1 | grep -q -- "-r\b"
                                then
@@ -275,14 +295,19 @@ try_mount ()
 
        if [ -n "${old_mountp}" ]
        then
-               mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
+               if [ "${opts}" != "ro" ]
+               then
+                       mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
+               fi
+
                mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
        else
                if [ -z "${fstype}" ]
                then
                        fstype=$(get_fstype "${dev}")
                fi
-               mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
+               mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
+               ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
        fi
 }
 
@@ -290,101 +315,135 @@ find_cow_device ()
 {
        # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
        #  in the latter case the partition containing the file is left mounted
-       #  if is not in black_listed_devices
+       #  if is not in black_listed_devices.
+       #  Additionally, if the white_listed_devices list is non-empty, the
+       #  parent block device of the returned device must be part of this list.
        pers_label="${1}"
        cow_backing="/${pers_label}-backing"
        black_listed_devices="${2}"
+       white_listed_devices="${3}"
 
-       for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
-       do
-               for dev in $(subdevices "${sysblock}")
-               do
-                       devname=$(sys2dev "${dev}")
+       if [ -z "${PERSISTENT_PATH}" ]
+       then
+               pers_fpath=${cow_backing}/${pers_label}
+       else
+               pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
+       fi
 
-                       if echo "${black_listed_devices}" | grep -q "${devname}"
+       for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
+       do
+               # Checking for a luks device
+               if [ "${PERSISTENT_ENCRYPTION}" = "luks" ] && [ -e /sbin/cryptsetup ]
+               then
+                       if ! modprobe dm-crypt
                        then
-                               # skip this device enterely
-                               break
+                               log_warning_msg "Unable to load module dm-crypt"
+                               continue
                        fi
 
-                       if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
+                       if ! /sbin/cryptsetup isLuks ${dev}
                        then
-                               echo "${devname}"
-                               return
+                               # we only look for encrypted subdevices
+                               continue
                        fi
 
-                       if [ "${PERSISTENT}" = "nofiles" ]
-                       then
-                               # do not mount the device to find for image files
-                               # just skip this
-                               break
-                       fi
+                       while true
+                       do
+                               load_keymap
 
-                       case "$(get_fstype ${devname})" in
-                               vfat|ext2|ext3|jffs2)
-                                       mkdir -p "${cow_backing}"
-                                       if ! try_mount "${devname}" "${cow_backing}" "rw"
-                                       then
-                                               break
-                                       fi
+                               /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${dev}: " | /sbin/cryptsetup -T 1 luksOpen ${dev} $(basename ${dev}) --key-file=-
+                               error=${?}
+
+                               dev="/dev/mapper/$(basename ${dev})"
+
+                               if [ 0 -eq ${error} ]
+                               then
+                                       unset error
+                                       break
+                               fi
+
+                               echo >&6
+                               echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
+                               read answer
+
+                               if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
+                               then
+                                       unset answer
+                                       # skip to next subdevice
+                                       continue 2
+                               fi
+                       done
+               fi
+
+               if echo ${PERSISTENT_STORAGE} | grep -qw filesystem && [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${pers_label}" ]
+               then
+                       echo "${dev}"
+                       return 0
+               fi
 
-                                       if [ -f "${cow_backing}/${pers_label}" ]
+               if ! echo ${PERSISTENT_STORAGE} | grep -qw file
+               then
+                       # do not mount the device to find for image files
+                       # just skip this
+                       continue
+               fi
+
+               case "$(get_fstype ${dev})" in
+                       vfat|ext2|ext3|ext4|jffs2)
+                               mkdir -p "${cow_backing}"
+                               if try_mount "${dev}" "${cow_backing}" "rw"
+                               then
+                                       if [ -f "${pers_fpath}" ]
                                        then
-                                               echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
+                                               echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
                                                return 0
                                        else
-                                               umount ${cow_backing}
+                                               umount ${cow_backing} > /dev/null 2>&1 || true
                                        fi
-                                       ;;
-                               *)
-                                       ;;
-                       esac
-               done
+                               fi
+                               ;;
+                       *)
+                               ;;
+               esac
        done
+       return 1
 }
 
 find_files ()
 {
        # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
+       #  if is not in black_listed_devices.
+       #  Additionally, if the white_listed_devices list is non-empty, the
+       #  parent block device of the returned device must be part of this list.
        # FIXME: merge with above function
 
        filenames="${1}"
        snap_backing="/snap-backing"
        black_listed_devices="${2}"
+       white_listed_devices="${3}"
 
-       for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
+       for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
        do
-               for dev in $(subdevices "${sysblock}")
-               do
-                       devname=$(sys2dev "${dev}")
-                       devfstype="$(get_fstype ${devname})"
+               devfstype="$(get_fstype ${dev})"
 
-                       if echo "${black_listed_devices}" | grep -q "${devname}"
-                       then
-                               # skip this device enterely
-                               break
-                       fi
+               if is_supported_fs ${devfstype}
+               then
+                       mkdir -p "${snap_backing}"
 
-                       if is_supported_fs ${devfstype}
+                       if try_mount "${dev}" "${snap_backing}" "ro" "${devfstype}"
                        then
-                               mkdir -p "${snap_backing}"
-
-                               if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
-                               then
-                                       for filename in ${filenames}
-                                       do
-                                               if [ -f "${snap_backing}/${filename}" ]
-                                               then
-                                                       echo "${devname} ${snap_backing} ${filename}"
-                                                       umount ${snap_backing}
-                                                       return 0
-                                               fi
-                                       done
-                               fi
-
-                               umount ${snap_backing}
+                               for filename in ${filenames}
+                               do
+                                       if [ -f "${snap_backing}/${filename}" ]
+                                       then
+                                               echo "${dev} ${snap_backing} ${filename}"
+                                               umount ${snap_backing}
+                                               return 0
+                                       fi
+                               done
                        fi
-               done
+                       umount ${snap_backing}
+               fi
        done
 }
 
@@ -418,3 +477,73 @@ is_luks()
     fi
 
 }
+
+removable_dev ()
+{
+       output_format="${1}"
+       want_usb="${2}"
+       ret=
+
+       for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
+       do
+               dev_ok=
+               if [ "$(cat ${sysblock}/removable)" = "1" ]
+               then
+                       if [ -z "${want_usb}" ]
+                       then
+                               dev_ok="yes"
+                       else
+                               if readlink ${sysblock} | grep -q usb
+                               then
+                                       dev_ok="yes"
+                               fi
+                       fi
+               fi
+
+               if [ "${dev_ok}" = "yes" ]
+               then
+                       case "${output_format}" in
+                               sys)
+                                       ret="${ret} ${sysblock}"
+                                       ;;
+                               *)
+                                       devname=$(sys2dev "${sysblock}")
+                                       ret="${ret} ${devname}"
+                                       ;;
+                       esac
+               fi
+       done
+
+       echo "${ret}"
+}
+
+removable_usb_dev ()
+{
+       output_format="${1}"
+
+       removable_dev "${output_format}" "want_usb"
+}
+
+non_removable_dev ()
+{
+       output_format="${1}"
+       ret=
+
+       for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
+       do
+               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}"
+}