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, then rsync.
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 umounts/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/mapper' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | while read line
77 find /dev/mapper -type l | while read line
79 echo "cryptsetup luksClose ${line}"
80 cryptsetup luksClose "${line}"
83 losetup -a | sed -e "s/:.*$//" | while read line
85 echo "losetup -d ${line}"
93 ######################################################################
99 [[ -f "$1" ]] && [[ -f "$2" ]] || (echo "$(basename $0) sync <source file> <dest file>" >&2 && exit 1)
101 [[ -e "/dev/mapper/crypt-src" ]] && (echo "/dev/mapper/crypt-src already exists." >&2 && exit 1)
103 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
105 ######################################################################
108 echo "Please confirm that $2 can be modified (press 'y')"
112 if [[ ! "${KEY}" == "y" ]]
120 LOOP_SRC="$(losetup -f)"
121 losetup "${LOOP_SRC}" "$1"
122 cryptsetup luksOpen "${LOOP_SRC}" crypt-src
123 DIR_MOUNT_SRC="$(mktemp -d /tmp/sync-luks.XXXXXX)"
124 mount -o ro /dev/mapper/crypt-src "${DIR_MOUNT_SRC}"
126 LOOP_DST="$(losetup -f)"
127 losetup "${LOOP_DST}" "$2"
128 cryptsetup luksOpen "${LOOP_DST}" crypt-dst
129 DIR_MOUNT_DST="$(mktemp -d /tmp/sync-luks.XXXXXX)"
130 mount /dev/mapper/crypt-dst "${DIR_MOUNT_DST}"
132 ######################################################################
133 # First, show the changes
135 echo "**********************************************************************"
138 rsync -n --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
140 ######################################################################
141 # Ask for confirmation and synchronize
143 echo "**********************************************************************"
144 echo "* Press 'y' to synchronize, anything else to cancel."
148 if [[ "${KEY}" == "y" ]]
151 rsync --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
153 echo "No synchronization."
156 umount "${DIR_MOUNT_SRC}" && rmdir "${DIR_MOUNT_SRC}" && unset DIR_MOUNT_SRC
157 cryptsetup luksClose crypt-src
158 losetup -d "${LOOP_SRC}" && unset LOOP_SRC
160 umount "${DIR_MOUNT_DST}" && rmdir "${DIR_MOUNT_DST}" && unset DIR_MOUNT_DST
161 cryptsetup luksClose crypt-dst
162 losetup -d "${LOOP_DST}" && unset LOOP_DST
169 ######################################################################
175 if [[ "$1" == "-f" ]]
183 echo "Cannot find file \`$1'." >&2
187 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
191 LOOP_DST="$(losetup -f)"
192 losetup "${LOOP_DST}" "$1"
198 cryptsetup luksOpen "${DEVICE}" crypt-dst
200 fsck ${force} /dev/mapper/crypt-dst
204 cryptsetup luksClose crypt-dst
206 if [[ "${LOOP_DST}" ]]
208 losetup -d "${LOOP_DST}" && unset LOOP_DST
215 ######################################################################
219 if [[ "$1" == "umount" ]]
226 mount_point=$(echo $1 | sed -e "s;/*$;;")
227 device=$(grep ^/ /etc/fstab | awk '{ print $2" "$1 }' | grep ^${mount_point} | cut -f 2 -d " ")
229 if [[ ${device} =~ ^/dev/mapper ]]
232 mapped_device=${device/'/dev/mapper/'/}
237 Attempting to unmount
240 umount ${mount_point} && cryptdisks_stop "${mapped_device}"
248 cryptdisks_start "${mapped_device}" && mount ${mount_point}
253 echo "\`${device}' does not look like a luks one"
261 ######################################################################
268 echo "Unknown argument \`$1', aborting." >&2