]> code.delx.au - monosys/commitdiff
reboot-no-passphrase: support arbitrary devnames
authorJames Bunton <jamesbunton@delx.net.au>
Sun, 9 Feb 2020 02:41:21 +0000 (13:41 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Sun, 9 Feb 2020 02:41:21 +0000 (13:41 +1100)
bin/reboot-no-passphrase
etc/systemd/system/disable-crypto_keyfile@.service

index 0f9b56d3b12d16b69c9fffeec216ef7a2de3b8b9..139f0e0c58ebe0cc1bb223de89d8869cdce31314 100755 (executable)
@@ -5,7 +5,7 @@
 # 1. dd if=/dev/urandom of=/crypto_keyfile.bin bs=1 count=512
 # 2. Add /crypto_keyfile.bin to FILES in /etc/mkinitcpio.conf
 # 3. mkinitcpio -p linux
-# 4. Enable the disable-crypto_keyfiles@<root-disk-id>.service
+# 4. systemctl enable disable-crypto_keyfiles@$(systemd-escape /dev/disk/by-id/xxx).service
 # 5. Run this script when you want to reboot without a passphrase
 
 
@@ -17,13 +17,16 @@ if [ ! -f "$crypto_keyfile" ]; then
     exit 1
 fi
 
-found_devices=""
-for disk_id in $(ls /etc/systemd/system/basic.target.wants/disable-crypto_keyfile@*.service | cut -d'@' -f2 | cut -d. -f1); do
-    found=1
-    found_devices="${found_devices} /dev/disk/by-id/${disk_id}"
-done
+readarray -t devnames < <(
+    find \
+        /etc/systemd/system/basic.target.wants/ \
+        -maxdepth 1 \
+        -name 'disable-crypto_keyfile@*' \
+        -printf '%f\0' \
+    | xargs -0 -n1 systemd-escape -u --instance
+)
 
-if [ -z "$found_devices" ]; then
+if [ ${#devnames[@]} = 0 ]; then
     echo "Failed to find your encrypted device. You must have disable-crypto_keyfile@.service enabled."
     exit 1
 fi
@@ -31,9 +34,9 @@ fi
 echo -n "Enter password for devices: "
 read -r -s pw
 echo ""
-for device_filename in $found_devices; do
-    echo "Adding key to $device_filename"
-    sudo cryptsetup luksAddKey "$device_filename" "$crypto_keyfile" --key-slot 7 <<EOF
+for devname in "${devnames[@]}"; do
+    echo "Adding key to $devname"
+    sudo cryptsetup luksAddKey "$devname" "$crypto_keyfile" --key-slot 7 <<EOF
 ${pw}
 EOF
 done
index f4bce3d12b196a3d9fbf1ee9792b61695e12caa7..06b8ceab219dd029d70445b383ba3acc4125f8a6 100644 (file)
@@ -4,7 +4,7 @@ After=basic.target
 
 [Service]
 Type=oneshot
-ExecStart=/sbin/cryptsetup luksRemoveKey /dev/disk/by-id/%i /crypto_keyfile.bin --key-slot 7
+ExecStart=/sbin/cryptsetup luksRemoveKey %I /crypto_keyfile.bin --key-slot 7
 SuccessExitStatus=2
 
 [Install]