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 ######################################################################
53 if [[ "$@" == "" ]]; then
58 if [[ ! $(id -u) == 0 ]]; then
59 echo "This command should be run as root (no offense, but you are $(id -un))." >&2
63 ######################################################################
69 # mount | grep ^'/dev/dm-[0-9]*' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
71 mount | grep ^'/dev/mapper' | sed -e 's/^.* on \([^ ]*\) .*$/\1/' | \
77 \ls /dev/mapper | grep -v ^control$ | \
79 echo "cryptsetup luksClose ${line[0]}"
80 cryptsetup luksClose "${line[0]}"
83 losetup -a | sed -e "s/:.*$//" | \
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" ]]; then
119 LOOP_SRC="$(losetup -f)"
120 losetup "${LOOP_SRC}" "$1"
121 cryptsetup luksOpen "${LOOP_SRC}" crypt-src
122 DIR_MOUNT_SRC="$(mktemp -d /tmp/sync-luks.XXXXXX)"
123 mount -o ro /dev/mapper/crypt-src "${DIR_MOUNT_SRC}"
125 LOOP_DST="$(losetup -f)"
126 losetup "${LOOP_DST}" "$2"
127 cryptsetup luksOpen "${LOOP_DST}" crypt-dst
128 DIR_MOUNT_DST="$(mktemp -d /tmp/sync-luks.XXXXXX)"
129 mount /dev/mapper/crypt-dst "${DIR_MOUNT_DST}"
131 ######################################################################
132 # First, show the changes
134 echo "**********************************************************************"
137 rsync -n --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
139 ######################################################################
140 # Ask for confirmation and synchronize
142 echo "**********************************************************************"
143 echo "* Press 'y' to synchronize, anything else to cancel."
147 if [[ "${KEY}" == "y" ]]; then
149 rsync --itemize-changes --delete --progress -axz "${DIR_MOUNT_SRC}/" "${DIR_MOUNT_DST}/"
151 echo "No synchronization."
154 umount "${DIR_MOUNT_SRC}" && rmdir "${DIR_MOUNT_SRC}" && unset DIR_MOUNT_SRC
155 cryptsetup luksClose crypt-src
156 losetup -d "${LOOP_SRC}" && unset LOOP_SRC
158 umount "${DIR_MOUNT_DST}" && rmdir "${DIR_MOUNT_DST}" && unset DIR_MOUNT_DST
159 cryptsetup luksClose crypt-dst
160 losetup -d "${LOOP_DST}" && unset LOOP_DST
167 ######################################################################
173 if [[ "$1" == "-f" ]]; then
178 if [[ ! -a "$1" ]]; then
179 echo "Cannot find file \`$1'." >&2
183 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
185 if [[ -f "$1" ]]; then
186 LOOP_DST="$(losetup -f)"
187 losetup "${LOOP_DST}" "$1"
193 cryptsetup luksOpen "${DEVICE}" crypt-dst
195 fsck ${force} /dev/mapper/crypt-dst
199 cryptsetup luksClose crypt-dst
201 if [[ "${LOOP_DST}" ]]; then
202 losetup -d "${LOOP_DST}" && unset LOOP_DST
209 ######################################################################
213 if [[ "$1" == "umount" ]]; then
219 mount_point=$(echo $1 | sed -e "s;/*$;;")
220 device=$(grep ^/ /etc/fstab | awk '{ print $2" "$1 }' | grep ^${mount_point} | cut -f 2 -d " ")
222 if [[ ${device} =~ ^/dev/mapper ]]; then
224 mapped_device=${device/'/dev/mapper/'/}
226 if [[ ${umount} ]]; then
228 Attempting to unmount
231 umount ${mount_point} && cryptdisks_stop "${mapped_device}"
239 cryptdisks_start "${mapped_device}" && mount ${mount_point}
244 echo "\`${device}' does not look like a LUKS one"
252 ######################################################################
259 echo "Unknown argument \`$1', aborting." >&2