3 # Try to get LUKS info and master key from active mapping and prepare parameters for cryptsetup.
5 # Copyright (C) 2010,2011 Milan Broz <asi@ucw.cz>
7 # This copyrighted material is made available to anyone wishing to use,
8 # modify, copy, or redistribute it subject to the terms and conditions
9 # of the GNU General Public License v.2.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software Foundation,
13 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 fail() { echo -e $1 ; exit 1 ; }
17 field() { echo $(dmsetup table --target crypt --showkeys $DEVICE | sed 's/.*: //' | cut -d' ' -f$1) ; }
18 field_cryptsetup() { echo $(cryptsetup status $DEVICE | grep $1 | sed "s/.*$1:\s*//;s/\ .*//") ; }
20 which xxd >/dev/null || fail "You need xxd (part of vim package) installed to convert key."
22 [ -z "$2" ] && fail "Recover LUKS header from active mapping, use:\n $0 crypt_mapped_device mk_file_name"
27 [ -z "$(field 4)" ] && fail "Mapping $1 not active or it is not crypt target."
31 # - check for CRYPT-LUKS1-* DM-UUID
33 CIPHER=$(field_cryptsetup cipher)
34 OFFSET=$(field_cryptsetup offset)
35 REAL_DEVICE=$(field_cryptsetup device)
36 KEY_SIZE=$(field_cryptsetup keysize)
39 [ -z "$CIPHER" -o -z "$OFFSET" -o "$OFFSET" -le 383 -o -z "$KEY" ] && fail "Incompatible device, sorry."
41 echo "Generating master key to file $MK_FILE."
42 echo -E -n $KEY| xxd -r -p >$MK_FILE
44 echo "You can now try to reformat LUKS device using:"
45 echo " cryptsetup luksFormat -c $CIPHER -s $KEY_SIZE --align-payload=$OFFSET --master-key-file=$MK_FILE $REAL_DEVICE"