c82f27ed31171be4c21db2b600216b40fda92f97
[platform/upstream/cryptsetup.git] / misc / dracut_90reencrypt / reencrypt.sh
1 #!/bin/sh
2 #
3 # $1=$device [$2=keyfile|none [$3=keyslot|any [$4=size]]]
4 #
5
6 [ -d /sys/module/dm_crypt ] || modprobe dm_crypt
7
8 [ -d /sys/module/loop ] || modprobe loop
9
10 [ -f /tmp/reencrypted ] && exit 0
11
12 . /lib/dracut-lib.sh
13
14 # if device name is /dev/dm-X, convert to /dev/mapper/name
15 if [ "${1##/dev/dm-}" != "$1" ]; then
16     device="/dev/mapper/$(dmsetup info -c --noheadings -o name "$1")"
17 else
18     device="$1"
19 fi
20
21 PARAMS="$device -T 1 --use-fsync -B 32"
22 if [ "$3" != "any" ]; then
23     PARAMS="$PARAMS -S $3"
24 fi
25
26 if [ -n "$4" ]; then
27     PARAMS="$PARAMS --device-size $4"
28 fi
29
30 reenc_readkey() {
31     local keypath="${1#*:}"
32     local keydev="${1%%:*}"
33
34     local mntp="/tmp/reencrypted-mount-tmp"
35     mkdir "$mntp"
36     mount -r "$keydev" "$mntp" && cat "$mntp/$keypath"
37     umount "$mntp"
38     rm -r "$mntp"
39 }
40
41 reenc_run() {
42     local cwd=$(pwd)
43     local _prompt="LUKS password for REENCRYPTING $device"
44     cd /tmp
45     if [ "$1" = "none" ] ; then
46         if [ "$2" != "any" ]; then
47                 _prompt="$_prompt, using keyslot $2"
48         fi
49         /bin/plymouth ask-for-password \
50         --prompt "$_prompt" \
51         --command="/sbin/cryptsetup-reencrypt $PARAMS"
52     else
53         info "REENCRYPT using key $1"
54         reenc_readkey "$1" | /sbin/cryptsetup-reencrypt -d - $PARAMS
55     fi
56     _ret=$?
57     cd $cwd
58 }
59
60 info "REENCRYPT $device requested"
61 # flock against other interactive activities
62 { flock -s 9;
63     reenc_run $2 $3
64 } 9>/.console_lock
65
66 if [ $_ret -eq 0 ]; then
67     # do not ask again
68     >> /tmp/reencrypted
69     warn "Reencryption of device $device has finished successfully. Use previous"
70     warn "initramfs image (without reencrypt module) to boot the system. When"
71     warn "you leave the emergency shell, the system will reboot."
72
73     emergency_shell -n "(reboot)"
74     [ -x /usr/bin/systemctl ] && /usr/bin/systemctl reboot
75     [ -x /sbin/shutdown ] && /sbin/shutdown -r now
76 fi
77
78 # panic the kernel otherwise
79 exit 1