3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation. #
8 # This program is distributed in the hope that it will be useful, but #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
11 # General Public License for more details. #
13 # You should have received a copy of the GNU General Public License #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports #
18 #########################################################################
23 function print_help () {
25 $(basename $0) [--help | clean | sync <source file> <dest file> | fsck [-f] <file|device> | mount <dir> | umount <dir>]
29 1. umounts all the volumes using a /dev/dm-* device
30 2. LUKS-close all the volumes appearing in /dev/mapper
31 3. Delete all the loop devices
35 Mounts both files as LUKS volumes, runs a dry-run rsync, and asks for
36 interactive confirmation before synchronizing.
40 LUKS-open the provided file and run fsck on it.
44 Automagically figures out from /etc/fstab what is the /dev/mapper/
45 device associated to the dir, and both LUKS-opens/mounts or
46 umount/LUKS-closes it.
51 ######################################################################
59 if [[ ! $(id -u) == 0 ]]
61 echo "This command should be run as root (no offense, but you are $(id -un))." >&2
65 ######################################################################
71 # mount | grep ^'/dev/dm-[0-9]*' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
73 mount | grep ^'/dev/mapper' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
80 \ls /dev/mapper | grep -v ^control$ | \
83 echo "cryptsetup luksClose ${line[0]}"
84 cryptsetup luksClose "${line[0]}"
87 losetup -a | sed -e "s/:.*$//" | \
90 echo "losetup -d ${line}"
98 ######################################################################
104 [[ -f "$1" ]] && [[ -f "$2" ]] || (echo "$(basename $0) sync <source file> <dest file>" >&2 && exit 1)
106 [[ -e "/dev/mapper/crypt-src" ]] && (echo "/dev/mapper/crypt-src already exists." >&2 && exit 1)
108 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
110 ######################################################################
113 echo "Please confirm that $2 can be modified (press 'y')"
117 if [[ ! "${KEY}" == "y" ]]
125 LOOP_SRC="$(losetup -f)"
126 losetup "${LOOP_SRC}" "$1"
127 cryptsetup luksOpen "${LOOP_SRC}" crypt-src
128 DIR_MOUNT_SRC="$(mktemp -d /tmp/sync-luks.XXXXXX)"
129 mount -o ro /dev/mapper/crypt-src "${DIR_MOUNT_SRC}"
131 LOOP_DST="$(losetup -f)"
132 losetup "${LOOP_DST}" "$2"
133 cryptsetup luksOpen "${LOOP_DST}" crypt-dst
134 DIR_MOUNT_DST="$(mktemp -d /tmp/sync-luks.XXXXXX)"
135 mount /dev/mapper/crypt-dst "${DIR_MOUNT_DST}"
137 ######################################################################
138 # First, show the changes
140 echo "**********************************************************************"
143 rsync -n --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
145 ######################################################################
146 # Ask for confirmation and synchronize
148 echo "**********************************************************************"
149 echo "* Press 'y' to synchronize, anything else to cancel."
153 if [[ "${KEY}" == "y" ]]
156 rsync --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
158 echo "No synchronization."
161 umount "${DIR_MOUNT_SRC}" && rmdir "${DIR_MOUNT_SRC}" && unset DIR_MOUNT_SRC
162 cryptsetup luksClose crypt-src
163 losetup -d "${LOOP_SRC}" && unset LOOP_SRC
165 umount "${DIR_MOUNT_DST}" && rmdir "${DIR_MOUNT_DST}" && unset DIR_MOUNT_DST
166 cryptsetup luksClose crypt-dst
167 losetup -d "${LOOP_DST}" && unset LOOP_DST
174 ######################################################################
180 if [[ "$1" == "-f" ]]
188 echo "Cannot find file \`$1'." >&2
192 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
196 LOOP_DST="$(losetup -f)"
197 losetup "${LOOP_DST}" "$1"
203 cryptsetup luksOpen "${DEVICE}" crypt-dst
205 fsck ${force} /dev/mapper/crypt-dst
209 cryptsetup luksClose crypt-dst
211 if [[ "${LOOP_DST}" ]]
213 losetup -d "${LOOP_DST}" && unset LOOP_DST
220 ######################################################################
224 if [[ "$1" == "umount" ]]
231 mount_point=$(echo $1 | sed -e "s;/*$;;")
232 device=$(grep ^/ /etc/fstab | awk '{ print $2" "$1 }' | grep ^${mount_point} | cut -f 2 -d " ")
234 if [[ ${device} =~ ^/dev/mapper ]]
237 mapped_device=${device/'/dev/mapper/'/}
242 Attempting to unmount
245 umount ${mount_point} && cryptdisks_stop "${mapped_device}"
253 cryptdisks_start "${mapped_device}" && mount ${mount_point}
258 echo "\`${device}' does not look like a LUKS one"
266 ######################################################################
273 echo "Unknown argument \`$1', aborting." >&2