Update.
[scripts.git] / luks_toolbox.sh
index ac4e3a2..aed8dc0 100755 (executable)
@@ -27,35 +27,37 @@ $(basename $0) [--help | clean | sync <source file> <dest file> | fsck [-f] <fil
 clean
 
   1. umounts all the volumes using a /dev/dm-* device
-  2. LUKS-close all the volumes appearing in /dev/mapper
+  2. luks-close all the volumes appearing in /dev/mapper
   3. Delete all the loop devices
 
 sync
 
-  Mounts both files as LUKS volumes, runs a dry-run rsync, and asks for
-  interactive confirmation before synchronizing.
+  Mounts both files as luks volumes, runs a dry-run rsync, and asks for
+  interactive confirmation, then rsync.
 
 fsck
 
-  LUKS-open the provided file and run fsck on it.
+  luks-open the provided file and run fsck on it.
 
 mount|umount
 
   Automagically figures out from /etc/fstab what is the /dev/mapper/
-  device associated to the dir, and both LUKS-opens/mounts or
-  umount/LUKS-closes it.
+  device associated to the dir, and both luks-opens/mounts or
+  umounts/luks-closes it.
 
 EOF
 }
 
 ######################################################################
 
-if [[ "$@" == "" ]]; then
+if [[ "$@" == "" ]]
+then
     print_help >&2
     exit 1
 fi
 
-if [[ ! $(id -u) == 0 ]]; then
+if [[ ! $(id -u) == 0 ]]
+then
     echo "This command should be run as root (no offense, but you are $(id -un))." >&2
     exit 1
 fi
@@ -66,25 +68,23 @@ case $1 in
 
     clean)
 
-        # mount | grep ^'/dev/dm-[0-9]*' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
+        mount | grep ^'/dev/mapper' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | while read line
+        do
+            echo "umount ${line}"
+            umount "${line}"
+        done
 
-        mount | grep ^'/dev/mapper' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
-            while read line; do
-                echo "umount ${line}"
-                umount ${line}
-            done
+        find /dev/mapper -type l | while read line
+        do
+            echo "cryptsetup luksClose ${line}"
+            cryptsetup luksClose "${line}"
+        done
 
-        \ls /dev/mapper | grep -v ^control$ | \
-            while read line; do
-                echo "cryptsetup luksClose ${line[0]}"
-                cryptsetup luksClose "${line[0]}"
-            done
-
-        losetup -a | sed -e "s/:.*$//" | \
-            while read line; do
-                echo "losetup -d ${line}"
-                losetup -d ${line}
-            done
+        losetup -a | sed -e "s/:.*$//" | while read line
+        do
+            echo "losetup -d ${line}"
+            losetup -d "${line}"
+        done
 
         exit 0
 
@@ -109,7 +109,8 @@ case $1 in
 
         read -n 1 KEY
 
-        if [[ ! "${KEY}" == "y" ]]; then
+        if [[ ! "${KEY}" == "y" ]]
+        then
             echo "Cancelled!"
             exit 1
         fi
@@ -144,7 +145,8 @@ case $1 in
 
         read -n 1 KEY
 
-        if [[ "${KEY}" == "y" ]]; then
+        if [[ "${KEY}" == "y" ]]
+        then
             echo
             rsync --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
         else
@@ -170,19 +172,22 @@ case $1 in
 
         shift
 
-        if [[ "$1" == "-f" ]]; then
+        if [[ "$1" == "-f" ]]
+        then
             force="-f"
             shift
         fi
 
-        if [[ ! -a "$1" ]]; then
+        if [[ ! -a "$1" ]]
+        then
             echo "Cannot find file \`$1'." >&2
             exit 1
         fi
 
         [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
 
-        if [[ -f "$1" ]]; then
+        if [[ -f "$1" ]]
+        then
             LOOP_DST="$(losetup -f)"
             losetup "${LOOP_DST}" "$1"
             DEVICE="${LOOP_DST}"
@@ -198,7 +203,8 @@ case $1 in
 
         cryptsetup luksClose crypt-dst
 
-        if [[ "${LOOP_DST}" ]]; then
+        if [[ "${LOOP_DST}" ]]
+        then
             losetup -d "${LOOP_DST}" && unset LOOP_DST
         fi
 
@@ -210,7 +216,8 @@ case $1 in
 
     mount|umount)
 
-        if [[ "$1" == "umount" ]]; then
+        if [[ "$1" == "umount" ]]
+        then
             umount=yes
         fi
 
@@ -219,11 +226,13 @@ case $1 in
         mount_point=$(echo $1 | sed -e "s;/*$;;")
         device=$(grep ^/ /etc/fstab | awk '{ print $2" "$1 }' | grep ^${mount_point} | cut -f 2 -d " ")
 
-        if [[ ${device} =~ ^/dev/mapper ]]; then
+        if [[ ${device} =~ ^/dev/mapper ]]
+        then
 
             mapped_device=${device/'/dev/mapper/'/}
 
-            if [[ ${umount} ]]; then
+            if [[ ${umount} ]]
+            then
                 cat <<EOF
 Attempting to unmount
    ${mount_point}.
@@ -241,7 +250,7 @@ EOF
 
         else
 
-            echo "\`${device}' does not look like a LUKS one"
+            echo "\`${device}' does not look like a luks one"
 
         fi