First pass coding style updates on live-persistence.
authorDaniel Baumann <daniel@debian.org>
Mon, 10 Dec 2012 18:54:11 +0000 (19:54 +0100)
committerDaniel Baumann <daniel@debian.org>
Mon, 10 Dec 2012 19:25:40 +0000 (20:25 +0100)
bin/live-persistence

index c8a67e1..93d81d3 100755 (executable)
@@ -1,6 +1,13 @@
 #!/bin/sh
 
-NAME="${0##*/}"
+## live-tools(7) - System Support Scripts
+##
+## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
+## This is free software, and you are welcome to redistribute it
+## under certain conditions; see COPYING for details.
+
+
+set -e
 
 # We're gonna mess with stderr's file descriptor below, so we save a
 # reference to it in fd 3 if we want to print to stderr
@@ -12,26 +19,32 @@ error ()
        exit 1
 }
 
+# Exit if user is unprivileged
 if [ "$(id -u)" -ne 0 ]
 then
-       error "need root privileges"
+       echo "E: need root privileges" >&3
+
+       exit 1
 fi
 
-if ! dpkg -s live-boot >/dev/null 2>&1
+# Exit if live-boot is not installed
+if [ ! -e /lib/live/boot ]
 then
-       error "package 'live-boot' must be installed"
+       echo "E: live-boot not installed" >&3
+
+       exit 1
 fi
 
 # import Cmdline_old()
-. /lib/live/boot/9990-cmdline-old \
-       || error 'Could not source /lib/live/boot/9990-cmdline-old'
+. /lib/live/boot/9990-cmdline-old || error 'Could not source /lib/live/boot/9990-cmdline-old'
 
 # Set variable names needed by get_custom_mounts() etc.,
 # and now initialized by live-boot in a file that we certainly
 # don't want to source.
-export persistence_list="persistence.conf"
-export old_persistence_list="live-persistence.conf"
-export custom_overlay_label="persistence"
+persistence_list="persistence.conf"
+old_persistence_list="live-persistence.conf"
+custom_overlay_label="persistence"
+export persistence_list old_persistence_list custom_overlay_label
 
 # This will import the following functions and variables used below:
 #   activate_custom_mounts()
@@ -42,21 +55,20 @@ export custom_overlay_label="persistence"
 #   removable_usb_dev()
 #   storage_devices()
 #   where_is_mounted()
-. /lib/live/boot/9990-misc-helpers.sh \
-       || error 'Could not source /lib/live/boot/9990-misc-helpers.sh'
+. /lib/live/boot/9990-misc-helpers.sh || error 'Could not source /lib/live/boot/9990-misc-helpers.sh'
 
 usage ()
 {
-       echo "Usage: ${NAME} [OPTION]... list [LABEL]...
+       echo "Usage: live-persistence [OPTION]... list [LABEL]...
 List (on stdout) all partitions with names among LABEL(s) that are compatible
 with live-boot's overlay persistence, and that are adhering to live-boot's
 persistence filters (e.g. persistence-media). If no LABEL is given the default
 in live-boot is used ('${custom_overlay_label}').
-   or: ${NAME} [OPTION]... activate VOLUME...
+   or: live-persistence [OPTION]... activate VOLUME...
 Activates persistence on the given VOLUME(s) (specified via block device).
 Successes and failures are written to stdout. There are no checks for whether
 the given volumes adhere to live-boot's options.
-   or: ${NAME} [OPTION]... close VOLUME...
+   or: live-persistence [OPTION]... close VOLUME...
 Deactivates persistence on the given VOLUME(s) (specified via block device).
 
 Note: The 'activate' and 'stop' actions only support partition-backed volumes
@@ -90,10 +102,10 @@ warning ()
 
 dbus_udisks_get_attribute ()
 {
-       local dev attribute re
        dev="${1}"
        attribute="${2}"
        re='^[[:space:]]*variant[[:space:]]\+string[[:space:]]\+"\(.*\)"$'
+
        dbus-send --system --print-reply --dest=org.freedesktop.UDisks \
                /org/freedesktop/UDisks/devices/$(basename ${dev}) \
                org.freedesktop.DBus.Properties.Get \
@@ -107,19 +119,18 @@ dbus_udisks_get_attribute ()
 # instead (which should be available in user-space).
 get_gpt_name ()
 {
-       local dev
        dev="${1}"
+
        dbus_udisks_get_attribute ${dev} partition-label
 }
 
 is_gpt_device ()
 {
-       local dev
        dev="${1}"
+
        [ "$(dbus_udisks_get_attribute ${dev} partition-scheme)" = "gpt" ]
 }
 
-
 # We override live-boot's logging facilities to get more useful error messages
 log_warning_msg ()
 {
@@ -147,13 +158,15 @@ fix_gconf_dirs ()
                then
                        continue
                fi
-               local ownership
+
                ownership=$(stat -c "%u:%g" "${home}")
+
                find "${home}/.gconf" -type d | while IFS="" read -r dir
                do
-                       local conf
                        conf="${dir}/%gconf.xml"
-                       if [ ! -e "${conf}" ]; then
+
+                       if [ ! -e "${conf}" ]
+                       then
                                touch "${conf}"
                                chown ${ownership} "${conf}"
                        fi
@@ -163,31 +176,37 @@ fix_gconf_dirs ()
 
 list_volumes ()
 {
-       local labels whitelistdev
        labels=${@}
        whitelistdev=""
+
        case "${PERSISTENCE_MEDIA}" in
                removable)
                        whitelistdev="$(removable_dev)"
+
                        [ -z "${whitelistdev}" ] && return
                        ;;
                removable-usb)
                        whitelistdev="$(removable_usb_dev)"
+
                        [ -z "${whitelistdev}" ] && return
                        ;;
                *)
                        whitelistdev=""
                        ;;
        esac
+
        volumes=$(find_persistence_media "${labels}" "${whitelistdev}")
+
        for vol in $volumes
        do
                # drop the "$label=" prefix
                vol=${vol#*=}
+
                if [ -n "${GPT_PARTITIONS_ONLY}" ] && ! is_gpt_device ${vol}
                then
                        continue
                fi
+
                echo $vol
        done
 
@@ -196,7 +215,6 @@ list_volumes ()
 
 activate_volumes ()
 {
-       local volumes ret open_volumes successes failures
        volumes=${@}
        ret=0
        open_volumes=""
@@ -211,12 +229,15 @@ activate_volumes ()
                if [ ! -b "${vol}" ]
                then
                        warning "${vol} is not a block device"
+
                        failures="${failures} ${vol}"
                        ret=1
+
                        continue
                fi
-               local luks_vol
+
                luks_vol=""
+
                if /sbin/cryptsetup isLuks ${vol} >/dev/null
                then
                        if luks_vol=$(open_luks_device "${vol}")
@@ -231,12 +252,15 @@ activate_volumes ()
        done
 
        custom_mounts="$(mktemp /tmp/custom_mounts-XXXXXX.list)"
+
        get_custom_mounts ${custom_mounts} ${open_volumes}
+
        if [ -s "${custom_mounts}" ]
        then
                activate_custom_mounts ${custom_mounts} >/dev/null
                fix_gconf_dirs
        fi
+
        rm -f ${custom_mounts}
 
        for vol in ${open_volumes}
@@ -253,6 +277,7 @@ activate_volumes ()
        if [ -n "${successes}" ]
        then
                echo "Successes:"
+
                for vol in ${successes}
                do
                        echo "  - ${vol}"
@@ -262,11 +287,13 @@ activate_volumes ()
        if [ -n "${failures}" ]
        then
                echo "Failures:"
+
                for vol in ${failures}
                do
                        echo "  - ${vol}"
                done
        fi
+
        exit ${ret}
 }
 
@@ -283,10 +310,11 @@ get_readonly_link_dir ()
 
 close_volumes ()
 {
-       local volumes custom_mounts
        volumes=${@}
        custom_mounts="$(mktemp /tmp/custom_mounts-XXXXXX.list)"
+
        get_custom_mounts ${custom_mounts} ${volumes}
+
        while read device source dest options # < ${custom_mounts}
        do
                if is_in_comma_sep_list link ${options}
@@ -308,16 +336,18 @@ close_volumes ()
                        umount ${dest}
                fi
        done < ${custom_mounts}
+
        rm -f ${custom_mounts}
+
        for vol in ${volumes}
        do
-               local backing
                backing=$(where_is_mounted ${vol})
+
                umount ${backing}
        done
 }
 
-main ()
+Main ()
 {
        # tracing get's activated by Cmdline_old() if "debug" is in
        # /proc/cmdline which will flood stderr, which we don't want
@@ -330,59 +360,75 @@ main ()
        set +x
        exec 2>&3
 
-       export PERSISTENCE="true"
-       export NOPERSISTENCE=""
+       PERSISTENCE="true"
+       NOPERSISTENCE=""
+       export PERSISTENCE NOPERSISTENCE
 
        # FIXME: we don't yet support file-backed custom persistence
-       export PERSISTENCE_STORAGE="filesystem"
+       PERSISTENCE_STORAGE="filesystem"
+       export PERSISTENCE_STORAGE
 
        # Should be set empty since live-boot already changed root for us
-       export rootmnt=""
+       rootmnt=""
+       export rootmnt
 
        SHORTOPTS="e:,g,h,l:,m:,r,w,u:"
        LONGOPTS="encryption:,gpt-only,help,log-file:,media:,read-only,read-write,union:"
-       OPTS=$(getopt -o ${SHORTOPTS} --longoptions ${LONGOPTS} -n "${NAME}" -- "$@")
+       OPTS=$(getopt -o ${SHORTOPTS} --longoptions ${LONGOPTS} -n "live-persistence" -- "$@")
+
        if [ "$?" -ne 0 ]
        then
-               echo "Try '${NAME} --help' for more information."
+               echo "Try 'live-persistence --help' for more information."
                exit 1
        fi
+
        eval set -- "$OPTS"
-       while [ $# -gt 0 ]; do
+
+       while [ $# -gt 0 ]
+       do
                case $1 in
                        -e|--encryption)
                                shift
                                export PERSISTENCE_ENCRYPTION="${1}"
                                ;;
+
                        -g|--gpt-only)
                                GPT_PARTITIONS_ONLY="true"
                                ;;
+
                        -h|--help)
                                usage
                                exit 0
                                ;;
+
                        -l|--log-file)
                                shift
                                DEBUG_LOG_FILE="${1}"
                                ;;
+
                        -m|--media)
                                shift
                                export PERSISTENCE_MEDIA="${1}"
                                ;;
+
                        -r|--read-only)
                                export PERSISTENCE_READONLY="true"
                                ;;
+
                        -w|--read-write)
                                export PERSISTENCE_READONLY=""
                                ;;
+
                        -u|--union)
                                shift
                                export UNIONTYPE="${1}"
                                ;;
+
                        --)
                                shift
                                break
                                ;;
+
                        *)
                                error "unrecognized option: ${1}"
                                ;;
@@ -396,34 +442,41 @@ main ()
                set -x
        fi
 
-       local action labels volumes
        action="${1}"
        shift
+
        case "${action}" in
                list)
                        labels=${@}
+
                        if [ -z "${labels}" ]
                        then
                                # use default label in live-boot
                                labels=${custom_overlay_label}
                        fi
+
                        list_volumes ${labels}
                        ;;
+
                activate|close)
                        volumes=${@}
+
                        if [ -z "${volumes}" ]
                        then
                                error "you must specify at least one volume"
                        fi
+
                        ${action}_volumes "${volumes}"
                        ;;
+
                "")
                        error "no action specified"
                        ;;
+
                *)
                        error "unrecognized action: ${action}"
                        ;;
        esac
 }
 
-main "${@}"
+Main ${@}