開機自動掛載加密USB

開機自動掛載加密USB

我已經關注了這些步驟在 Lubuntu 13.10 上設定加密 USB 硬碟,以及這些使用密鑰檔案而不是密碼來解鎖它。我現在有一個磁碟 /dev/disk/by-uuid/32f692ec-e22c-49d3-b6cf-f90e605a5b48 這是原始加密設備, /dev/backup/backup 這是明文磁碟, /mnt/backup 其中檔案系統已安裝。該磁碟是透過USB3適配器連接的普通SATA硬碟。

我在使用或安裝該磁碟時沒有遇到任何問題;問題在於嘗試在啟動時自動安裝它。我按照文章的建議將其添加到我的 fstab 和 crypttab 中:

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/sda2_crypt /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=8afa992c-9174-4a70-b9a0-1ac25b42dbaf /boot           ext2    defaults        0       2
/dev/mapper/sdb2_crypt /home           ext4    defaults        0       2
/dev/mapper/sdb1_crypt /var            ext4    defaults        0       2
/dev/backup/backup     /mnt/backup     ext4    defaults        0       3

$ cat /etc/crypttab
sda2_crypt UUID=6450d0e6-539b-4fa0-a9e1-f06edfbb5ba9 none luks
sdb1_crypt UUID=f30e6efe-8e58-42b6-aed0-dc7999510744 /root/keyfile luks
sdb2_crypt UUID=17f1f107-8f05-4dbd-8172-4d595411d874 /root/keyfile luks
backup     UUID=32f692ec-e22c-49d3-b6cf-f90e605a5b48 /root/keyfile luks

(也許 /etc/crypttab 中的「backup」名稱不正確?我沒有 /dev/sda2_crypt。)

這樣,它只是在啟動時掛起,提示“/mnt/backup 的磁碟未準備好或不存在”。

我想那時磁碟可能還沒準備好,所以我嘗試將其安裝在 /etc/rc.local 中:

$ cat /etc/rc.local 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

logger Mounting backup disk...
ls -l /dev/disk/by-uuid/ > /home/rena/disk-uuid.txt
stat /root/keyfile >> /home/rena/disk-uuid.txt
whoami >> /home/rena/disk-uuid.txt
cryptsetup --verbose --key-file=/root/keyfile luksOpen /dev/disk/by-uuid/32f692ec-e22c-49d3-b6cf-f90e605a5b48 backup
logger Unlocked OK.
mount /dev/backup/backup /mnt/backup/
logger Mounted OK.

exit 0

我可以從系統日誌輸出中看到它解鎖正常,但從未安裝。看起來整個 rc.local 腳本在到達該行時就死掉了。我還可以從 ls、stat 和 whoami 中看到該磁碟確實存在於列表中,金鑰檔案可以訪問,並且腳本正在以 root 身份運行。所以我不知道為什麼安裝會失敗。

最奇怪的部分是,如果我從 rc,local 中刪除所有這些,然後在啟動後在終端機中輸入命令:

sudo cryptsetup --verbose --key-file=/root/keyfile luksOpen /dev/disk/by-uuid/32f692ec-e22c-49d3-b6cf-f90e605a5b48 backup
sudo mount /dev/backup/backup /mnt/backup/

安裝得很好。那麼為什麼它在 rc.local 和 fstab/crypttab 中失敗呢?

相關內容