Closed8
LUKS で HDD を暗号化

LUKS で暗号化された HDD のセットアップを行った手順のメモ。

デバイスの確認
lsblk
でデバイスを確認する。
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 14.6T 0 disk
nvme0n1 259:0 0 232.9G 0 disk
|-nvme0n1p1 259:1 0 487M 0 part /boot
|-nvme0n1p2 259:2 0 1K 0 part
`-nvme0n1p5 259:3 0 232.4G 0 part
`-nvme0n1p5_crypt 254:0 0 232.4G 0 crypt
|-nas--vg-root 254:1 0 231.4G 0 lvm /
`-nas--vg-swap_1 254:2 0 976M 0 lvm [SWAP]
/dev/sda
が今回の対象デバイス。

パーティションの作成
fdisk でパーティションを作成する。
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
The size of this disk is 14.6 TiB (16000900661248 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
Created a new DOS disklabel with disk identifier 0x19d33b89.
p
で現在のパーティションテーブルを出力し、空の HDD を選択しているか確認する。
Command (m for help): p
Disk /dev/sda: 14.55 TiB, 16000900661248 bytes, 31251759104 sectors
Disk model: TOSHIBA MG08ACA1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x19d33b89
今回使用する HDD は16TBなので GPT を使用する。 g
で空の GPT パーティションテーブルを作成する。
Command (m for help): g
Created a new GPT disklabel (GUID: 6A4344D4-1893-674E-858B-D9D40283EF3A).
パーティションを作成する。
1つのパーティションにすべての容量を使用する。
ommand (m for help): n
Partition number (1-128, default 1):
First sector (2048-31251759070, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-31251759070, default 31251759070):
Created a new partition 1 of type 'Linux filesystem' and of size 14.6 TiB.
w
で変更をディスクに書き込む。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
/dev/sda1
としてパーティションが作成された。
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 14.6T 0 disk
`-sda1 8:1 0 14.6T 0 part
nvme0n1 259:0 0 232.9G 0 disk
|-nvme0n1p1 259:1 0 487M 0 part /boot
|-nvme0n1p2 259:2 0 1K 0 part
`-nvme0n1p5 259:3 0 232.4G 0 part
`-nvme0n1p5_crypt 254:0 0 232.4G 0 crypt
|-nas--vg-root 254:1 0 231.4G 0 lvm /
`-nas--vg-swap_1 254:2 0 976M 0 lvm [SWAP]

LUKS コンテナの作成
cryptsetup で LUKS コンテナを作成する。
$ sudo cryptsetup luksFormat /dev/sda1
WARNING!
========
This will overwrite data on /dev/sda1 irrevocably.
Are you sure? (Type 'yes' in capital letters): YES
Enter passphrase for /dev/sda1:
Verify passphrase:

LUKS コンテナを開く
作成した LUKS コンテナを開いて利用可能にする。
$ sudo cryptsetup luksOpen /dev/sda1 sda1_crypt
Enter passphrase for /dev/sda1:
復号されたコンテンが /dev/mapper/sda1_crypt
で使えるようになった。
$ ls -l /dev/mapper/sda1_crypt
lrwxrwxrwx 1 root root 7 Aug 6 19:55 /dev/mapper/sda1_crypt -> ../dm-4

ファイルシステムの作成
ext4 でファイルシステムを作成する。
$ sudo mkfs.ext4 /dev/mapper/sda1_crypt
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 3906465531 4k blocks and 488308736 inodes
Filesystem UUID: 1dc139c0-68d7-4781-a60b-7763788fdd59
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544, 1934917632,
2560000000, 3855122432
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

ファイルシステムのマウント
/mnt/hdd
に /dev/mapper/sda1_crypt
をマウントする。
$ sudo mount /dev/mapper/sda1_crypt /mnt/hdd
書き込みと読み込みができることを確認する。
$ echo 'Hello, world!' | sudo tee /mnt/hdd/message > /dev/null
$ cat /mnt/hdd/message
Hello, world!

自動マウントの設定
システム自動時に自動的に LUKS コンテナの復号とパーティションのマウントが行われるようにする。
復号に使うパスフレーズを暗号化されたルートシステムの /path/to/key
に配置し /etc/crypttab
にエントリを追加する。
/etc/crypttab
+hdd_crypt UUID=027cc178-4fa1-4547-ab67-6619a2f4343d /path/to/key luks
/etc/fstab
にエントリを追加する。
/etc/crypttab
+/dev/mapper/hdd_crypt /mnt/hdd ext4 defaults 0 2
このスクラップは2023/08/06にクローズされました