Debian Codegraph @

dracut/modules.d/59kiwi-lib/kiwi-luks-lib.sh

raw
1 #!/bin/bash
2
3 # shellcheck disable=SC1091
4 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
5 type set_root_map >/dev/null 2>&1 || . /lib/kiwi-lib.sh
6 type wait_for_storage_device >/dev/null 2>&1 || . /lib/kiwi-partitions-lib.sh
7 type ask_for_password >/dev/null 2>&1 || . /lib/dracut-crypt-lib.sh
8 type run_progress_dialog >/dev/null 2>&1 || . /lib/kiwi-dialog-lib.sh
9
10 function luks_system {
11 declare kiwi_RootPart=${kiwi_RootPart}
12 local disk=$1
13 local device
14 device=$(get_partition_node_name "${disk}" "${kiwi_RootPart}")
15 if cryptsetup isLuks "${device}" &>/dev/null;then
16 return 0
17 fi
18 return 1
19 }
20
21 function deactivate_luks {
22 /usr/lib/systemd/systemd-cryptsetup detach luks
23 }
24
25 function reencrypt_luks {
26 declare kiwi_RootPart=${kiwi_RootPart}
27 local disk=$1
28 local keyslot=/root/.luks.slot
29 local header_checksum_origin=/root/.luks.header
30 local header_checksum_cur=/root/.luks.header.cur
31 local keyfile=/root/.root.keyfile
32 local new_keyfile=/run/.kiwi_reencrypt.keyfile
33 local passphrase_file=/root/.slotpass
34 local progress=/dev/install_progress
35 local load_text="Reencrypting..."
36 local title_text="LUKS"
37 local shasum
38 local device
39 device=$(get_partition_node_name "${disk}" "${kiwi_RootPart}")
40 read -r header_checksum_origin < "${header_checksum_origin}"
41 read -r keyslot < "${keyslot}"
42
43 # Checksum test if luks header is still the image origin header
44 shasum=$(shasum_tool)
45 cryptsetup luksHeaderBackup \
46 "${device}" --header-backup-file "${header_checksum_cur}"
47 header_checksum_cur=$(
48 "${shasum}" "${header_checksum_cur}" |\
49 cut -f1 -d" "; rm -f "${header_checksum_cur}"
50 )
51 if [ "${header_checksum_origin}" == "${header_checksum_cur}" ];then
52 if getargbool 0 rd.kiwi.oem.luks.reencrypt_randompass; then
53 # reset insecure built time passphrase with a random
54 # onetime passphrase that will be stored in memory at $new_keyfile
55 # This action require that the boot process uses $new_keyfile
56 # and sets a retrievable keyfile information for subsequent
57 # boot processes of this system
58 (umask 077; touch "${new_keyfile}")
59 tr -dc '[:graph:]' 2>/dev/null < /dev/urandom |\
60 head -c 32 > "${new_keyfile}"
61 chmod 0400 "${new_keyfile}"
62 cryptsetup \
63 --key-file "${passphrase_file}" \
64 --key-slot "${keyslot}" \
65 luksChangeKey "${device}" "${new_keyfile}"
66 cp "${new_keyfile}" "${passphrase_file}"
67 if [ -e "${keyfile}" ]; then
68 # if there is a keyfile it's referenced in the crypttab
69 # of this initrd instance in memory. Make sure all subsequent
70 # tasks e.g. luks resize have permissions to complete while
71 # inside of this initrd instance
72 cp "${new_keyfile}" "${keyfile}"
73 fi
74 fi
75 # reencrypt
76 setup_progress_fifo ${progress}
77 (
78 # reencrypt, this will overwrite all key slots
79 set_device_lock "${device}" cryptsetup reencrypt \
80 --progress-frequency 1 \
81 --key-file "${passphrase_file}" \
82 --key-slot "${keyslot}" \
83 "${device}" 2>&1 | sed -u 's/.* \([0-9]*\)[0-9.]*%.*/\1/'
84 ) >"${progress}" &
85 run_progress_dialog "${load_text}" "${title_text}"
86 if [ -e "${keyfile}" ] && [ ! -e "${new_keyfile}" ];then
87 # re-add keyfile if present and no other keyfile was created
88 cryptsetup --key-file "${passphrase_file}" luksAddKey \
89 "${device}" "${keyfile}"
90 fi
91 fi
92 }
93
94 function resize_luks {
95 cryptsetup resize luks
96 }
97
98 function activate_luks {
99 declare kiwi_luks_empty_passphrase=${kiwi_luks_empty_passphrase}
100 local device=$1
101 if [ "${kiwi_luks_empty_passphrase}" = "true" ];then
102 # There is no keyfile and kiwi has created the luks pool with
103 # an empty key. Therefore it can be opened without interaction
104 # but this requires to manually call luksOpen since
105 # with systemd-cryptsetup we saw it still asking for
106 # a passphrase
107 set_device_lock "${device}" cryptsetup \
108 --key-file /dev/zero \
109 --keyfile-size 32 \
110 luksOpen "${device}" luks
111 else
112 # There is a keyfile and we need to get prompted to enter the passphrase
113 systemctl start systemd-cryptsetup@luks
114 fi
115 wait_for_storage_device "/dev/mapper/luks"
116 set_root_map "/dev/mapper/luks"
117 }