Fix FSF address in license text according to
[platform/upstream/cryptsetup.git] / misc / luks-header-from-active
1 #!/bin/bash
2
3 # Try to get LUKS info and master key from active mapping and prepare parameters for cryptsetup.
4 #
5 # Copyright (C) 2010,2011 Milan Broz <asi@ucw.cz>
6 #
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.
10 #
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14 #
15
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/\ .*//") ; }
19
20 which xxd >/dev/null || fail "You need xxd (part of vim package) installed to convert key."
21
22 [ -z "$2" ] && fail "Recover LUKS header from active mapping, use:\n $0 crypt_mapped_device mk_file_name"
23
24 DEVICE=$1
25 MK_FILE=$2
26
27 [ -z "$(field 4)" ] && fail "Mapping $1 not active or it is not crypt target."
28
29 # FIXME:
30 # - add UUID
31 # - check for CRYPT-LUKS1-* DM-UUID
32
33 CIPHER=$(field_cryptsetup cipher)
34 OFFSET=$(field_cryptsetup offset)
35 REAL_DEVICE=$(field_cryptsetup device)
36 KEY_SIZE=$(field_cryptsetup keysize)
37 KEY=$(field 5)
38
39 [ -z "$CIPHER" -o -z "$OFFSET" -o "$OFFSET" -le 383 -o -z "$KEY" ] && fail "Incompatible device, sorry."
40
41 echo "Generating master key to file $MK_FILE."
42 echo -E -n $KEY| xxd -r -p >$MK_FILE
43
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"