Implement and make use of robust list functions.
[live-boot-grml.git] / scripts / live-helpers
index 0e1b691..2fcf441 100644 (file)
@@ -15,7 +15,6 @@ else
        udevinfo='udevinfo'
 fi
 
-root_overlay_label="full-ov"
 old_root_overlay_label="live-rw"
 old_home_overlay_label="home-rw"
 custom_overlay_label="custom-ov"
@@ -261,7 +260,6 @@ Arguments ()
                                ;;
 
                        persistence-subtext=*)
-                               root_overlay_label="${root_overlay_label}-${ARGUMENT#persistence-subtext=}"
                                old_root_overlay_label="${old_root_overlay_label}-${ARGUMENT#persistence-subtext=}"
                                old_home_overlay_label="${old_home_overlay_label}-${ARGUMENT#persistence-subtext=}"
                                custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistence-subtext=}"
@@ -390,7 +388,7 @@ Arguments ()
        then
                PERSISTENCE_ENCRYPTION="none"
                export PERSISTENCE_ENCRYPTION
-       elif echo ${PERSISTENCE_ENCRYPTION} | grep -qe "\<luks\>"
+       elif is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION}
        then
                if ! modprobe dm-crypt
                then
@@ -420,6 +418,27 @@ Arguments ()
        fi
 }
 
+is_in_list_separator_helper () {
+       local sep=${1}
+       shift
+       local element=${1}
+       shift
+       local list=${*}
+       echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
+}
+
+is_in_space_sep_list () {
+       local element=${1}
+       shift
+       is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
+}
+
+is_in_comma_sep_list () {
+       local element=${1}
+       shift
+       is_in_list_separator_helper "," "${element}" "${*}"
+}
+
 sys2dev ()
 {
        sysdev=${1#/sys}
@@ -451,9 +470,9 @@ storage_devices()
        do
                fulldevname=$(sys2dev "${sysblock}")
 
-               if echo "${black_listed_devices}" | grep -qe "\<${fulldevname}\>" || \
+               if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
                        [ -n "${white_listed_devices}" ] && \
-                       echo "${white_listed_devices}" | grep -qve "\<${fulldevname}\>"
+                       ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
                then
                        # skip this device entirely
                        continue
@@ -463,7 +482,7 @@ storage_devices()
                do
                        devname=$(sys2dev "${dev}")
 
-                       if echo "${black_listed_devices}" | grep -qe "\<${devname}\>"
+                       if is_in_space_sep_list ${devname} ${black_listed_devices}
                        then
                                # skip this subdevice
                                continue
@@ -738,23 +757,16 @@ 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}" > live-boot.log && return 0 )
+               ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
        fi
 }
 
 mount_persistence_media ()
 {
        local device=${1}
-       local backing=""
+       local probe=${2}
 
-       # We can't mount into ${rootmnt}/live before ${rootmnt} has been
-       # mounted since that would cover our mountpoint.
-       if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
-       then
-               backing="/$(basename ${device})-backing"
-       else
-               backing="${rootmnt}/live/persistence/$(basename ${device})"
-       fi
+       local backing="/live/persistence/$(basename ${device})"
 
        mkdir -p "${backing}"
        local old_backing="$(where_is_mounted ${device})"
@@ -771,7 +783,8 @@ mount_persistence_media ()
                        echo ${backing}
                        return 0
                else
-                       log_warning_msg "Failed to mount persistence media ${device}"
+                       [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
+                       rmdir "${backing}"
                        return 1
                fi
        elif [ "${backing}" != "${old_backing}" ]
@@ -781,7 +794,8 @@ mount_persistence_media ()
                        echo ${backing}
                        return 0
                else
-                       log_warning_msg "Failed to move persistence media ${device}"
+                       [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
+                       rmdir "${backing}"
                        return 1
                fi
        fi
@@ -917,7 +931,7 @@ probe_for_file_name ()
        local dev="${3}"
 
        local ret=""
-       local backing="$(mount_persistence_media ${dev})"
+       local backing="$(mount_persistence_media ${dev} probe)"
        if [ -z "${backing}" ]
        then
            return
@@ -980,7 +994,9 @@ find_persistence_media ()
        local white_listed_devices="${3}"
        local ret=""
 
-       for dev in $(storage_devices "" "${white_listed_devices}")
+       local black_listed_devices="$(what_is_mounted_on /live/image)"
+
+       for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
        do
                local result=""
 
@@ -989,7 +1005,7 @@ find_persistence_media ()
                # in order to probe any filesystem it contains, like we do
                # below. activate_custom_mounts() also depends on that any luks
                # device already has been opened.
-               if echo ${PERSISTENCE_ENCRYPTION} | grep -qe "\<luks\>" && \
+               if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
                   is_luks_partition ${dev}
                then
                        if luks_device=$(open_luks_device "${dev}")
@@ -999,14 +1015,14 @@ find_persistence_media ()
                                # skip $dev since we failed/chose not to open it
                                continue
                        fi
-               elif echo ${PERSISTENCE_ENCRYPTION} | grep -qve "\<none\>"
+               elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
                then
                        # skip $dev since we don't allow unencrypted storage
                        continue
                fi
 
                # Probe for matching GPT partition names or filesystem labels
-               if echo ${PERSISTENCE_STORAGE} | grep -qe "\<filesystem\>"
+               if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
                then
                        result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
                        if [ -n "${result}" ]
@@ -1024,7 +1040,7 @@ find_persistence_media ()
                fi
 
                # Probe for files with matching name on mounted partition
-               if echo ${PERSISTENCE_STORAGE} | grep -qe "\<file\>"
+               if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
                then
                        result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
                        if [ -n "${result}" ]
@@ -1266,7 +1282,7 @@ do_union ()
 
 get_custom_mounts ()
 {
-       # Side-effect: leaves $devices with live-persistence.conf mounted in ${rootmnt}/live/persistence
+       # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
        # Side-effect: prints info to file $custom_mounts
 
        local custom_mounts=${1}
@@ -1299,7 +1315,7 @@ get_custom_mounts ()
 
                if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
                then
-                       cp ${include_list} ${rootmnt}/live/persistence/${persistence_list}.${device_name}
+                       cp ${include_list} /live/persistence/${persistence_list}.${device_name}
                fi
 
                while read dir options # < ${include_list}
@@ -1310,22 +1326,22 @@ get_custom_mounts ()
                                continue
                        fi
 
-                       if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
+                       if trim_path ${dir} | grep -q -e "^[^/]" -e "^/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 \"/live\" (or any sub-directory therein) or \"/\" (for the latter, use ${root_overlay_label}-type persistence)"
+                               log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
                                continue
                        fi
 
                        local opt_source=""
-                       local opt_linkfiles=""
+                       local opt_link=""
                        for opt in $(echo ${options} | tr ',' ' ');
                        do
                                case "${opt}" in
                                        source=*)
                                                opt_source=${opt#source=}
                                                ;;
-                                       linkfiles)
-                                               opt_linkfiles="yes"
+                                       link)
+                                               opt_link="yes"
                                                ;;
                                        union|bind)
                                                ;;
@@ -1350,7 +1366,7 @@ get_custom_mounts ()
 
                        local full_source="$(trim_path ${backing}/${source})"
                        local full_dest="$(trim_path ${rootmnt}/${dir})"
-                       if [ -n "${opt_linkfiles}" ]
+                       if [ -n "${opt_link}" ]
                        then
                                echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
                        else
@@ -1394,30 +1410,35 @@ activate_custom_mounts ()
        while read device source dest options # < ${custom_mounts}
        do
                local opt_bind="yes"
-               local opt_linkfiles=""
+               local opt_link=""
                local opt_union=""
                for opt in $(echo ${options} | tr ',' ' ');
                do
                        case "${opt}" in
                                bind)
                                        opt_bind="yes"
-                                       unset opt_linkfiles opt_union
+                                       unset opt_link opt_union
                                        ;;
-                               linkfiles)
-                                       opt_linkfiles="yes"
+                               link)
+                                       opt_link="yes"
                                        unset opt_bind opt_union
                                        ;;
                                union)
                                        opt_union="yes"
-                                       unset opt_bind opt_linkfiles
+                                       unset opt_bind opt_link
                                        ;;
                        esac
                done
 
                if [ -n "$(what_is_mounted_on "${dest}")" ]
                then
-                       log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
-                       continue
+                       if [ "${dest}" = "${rootmnt}" ]
+                       then
+                               umount "${dest}"
+                       else
+                               log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
+                               continue
+                       fi
                fi
 
                if [ ! -d "${dest}" ]
@@ -1454,10 +1475,10 @@ activate_custom_mounts ()
                        if [ -n "${PERSISTENCE_READONLY}" ]
                        then
                                continue
-                       elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
+                       elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
                        then
                                # unions and don't need to be bootstrapped
-                               # linkfiles dirs can't be bootstrapped in a sensible way
+                               # link dirs can't be bootstrapped in a sensible way
                                mkdir -p "${source}"
                                chown_ref "${dest}" "${source}"
                                chmod_ref "${dest}" "${source}"
@@ -1472,9 +1493,9 @@ activate_custom_mounts ()
                # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
                # ignore the loop below and set rofs_dest_backing=$dest
                local rofs_dest_backing=""
-               if [ -n "${opt_linkfiles}"]
+               if [ -n "${opt_link}"]
                then
-                       for d in ${rootmnt}/live/rofs/*
+                       for d in /live/rofs/*
                        do
                                if [ -n "${rootmnt}" ]
                                then
@@ -1491,13 +1512,13 @@ activate_custom_mounts ()
                        done
                fi
 
-               if [ -n "${opt_linkfiles}" ] && [ -z "${PERSISTENCE_READONLY}" ]
+               if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
                then
                        link_files ${source} ${dest} ${rootmnt}
-               elif [ -n "${opt_linkfiles}" ] && [ -n "${PERSISTENCE_READONLY}" ]
+               elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
                then
-                       mkdir -p ${rootmnt}/live/persistence
-                       local links_source=$(mktemp -d ${rootmnt}/live/persistence/links-source-XXXXXX)
+                       mkdir -p /live/persistence
+                       local links_source=$(mktemp -d /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
@@ -1505,14 +1526,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.
-                       if [ -n "${rootmnt}" ]
-                       then
-                               local cow_dir="/overlay/live/persistence/$(basename ${links_source})"
-                       else
-                               # This is happens if persistence is activated
-                               # post boot
-                               local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
-                       fi
+                       local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
                        mkdir -p ${cow_dir}
                        chown_ref "${source}" "${cow_dir}"
                        chmod_ref "${source}" "${cow_dir}"
@@ -1531,13 +1545,13 @@ activate_custom_mounts ()
                        # is non-empty (and necessary) only for unions
                        if [ -n "${rootmnt}" ]
                        then
-                               local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/overlay/|")"
+                               local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
                        else
                                # This is happens if persistence is activated
                                # post boot
                                local cow_dir="/live/overlay/${dest}"
                        fi
-                       if [ -e "${cow_dir}" ] && [ -z "${opt_linkfiles}" ]
+                       if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
                        then
                                # If an earlier custom mount has files here
                                # it will "block" the current mount's files
@@ -1562,9 +1576,11 @@ activate_custom_mounts ()
        echo ${used_devices}
 }
 
-fix_home_rw_compatibility ()
+fix_backwards_compatibility ()
 {
        local device=${1}
+       local dir=${2}
+       local opt=${3}
 
        if [ -n "${PERSISTENCE_READONLY}" ]
        then
@@ -1580,8 +1596,8 @@ fix_home_rw_compatibility ()
        local include_list="${backing}/${persistence_list}"
        if [ ! -r "${include_list}" ]
        then
-               echo "# home-rw backwards compatibility:
-/home source=." > "${include_list}"
+               echo "# persistence backwards compatibility:
+${dir} ${opt},source=." > "${include_list}"
        fi
 }