dm-crypt/システム設定

提供: ArchWiki
2017年12月24日 (日) 00:25時点におけるKusakata (トーク | 投稿記録)による版 (戻るリンクの削除)
ナビゲーションに移動 検索に移動

ヒント: リモートで root などのブートファイルシステムのロックを解除する必要がある場合 (ヘッドレスマシンや遠隔地のサーバーなど)、Dm-crypt/特記事項#root などのパーティションのリモート解除の指示に従って下さい。

mkinitcpio

システムの暗号化をする場合、mkinitcpio を正しく設定した後に initial ramdisk を再生成する必要があります。特にシナリオによっては、以下のフックを有効しなくてはなりません:

  • encrypt: root パーティションや、root よりもにマウントする必要があるパーティションを暗号化する場合に必ず必要。他の場合は必要ありません。/etc/crypttab などのシステム初期化スクリプトが暗号化されたパーティションの鍵を解除してくれます。encrypt フックを使用する場合、udev フックよりも後に記述する必要があります。
  • sd-encrypt: systemd 版の encrypt フックで encrypt フックの代わりに使うことができます。必ず systemd フックと一緒に使用する必要があります。
  • keymap: 英語以外のキーマップで暗号化パスワードを入力できるようになります。encrypt フックより前に記述してください。キーマップの設定は /etc/vconsole.conf で行います。詳しくはキーマップ#永続的な設定を参照。
  • keyboard: 初期ユーザー空間で USB キーボードを使えるようにするために必須です。

他のフックについてはここでは触れません。

サンプル

encrypt フックを使用する /etc/mkinitcpio.conf の設定例:

/etc/mkinitcpio.conf
...
HOOKS="base udev autodetect keyboard keymap consolefont modconf block encrypt lvm2 filesystems fsck"
...

sd-encrypt フックを使用する systemd ベースの initramfs の設定:

/etc/mkinitcpio.conf
...
HOOKS="base systemd autodetect keyboard sd-vconsole modconf block sd-encrypt sd-lvm2 filesystems fsck"
...

ブートローダー

暗号化された root パーティションを起動できるようにするには、以下のカーネルパラメータのサブセットを設定する必要があります: 設定する方法はカーネルパラメータの使用しているブートローダーの手順を見て下さい。例えば GRUB を使用しているならブート設定を生成する前に /etc/default/grub にパラメータを追加するのが良いでしょう。

必要なカーネルパラメータは encrypt フックと sd-encrypt フックのどちらを使用するかによって変わります。

encrypt フックを使う

cryptdevice

このパラメータはコールドブート時に暗号化された root が含まれたデバイスのロックを解除するためにパスフレーズを要求するシステムプロンプトを作成します。どのデバイスに暗号化されたシステムが含まれているのか確認するために encrypt フックによってパースされます:

cryptdevice=device:dmname
  • device は暗号化されたデバイスのパスです。永続的なブロックデバイスの命名を使うことを推奨します。
  • dmname は復号化後にデバイスに与えられる device-mapper の名前です。/dev/mapper/dmname として使用できるようになります。
  • LVM に 暗号化されたルートが含まれる場合、最初に LVM が有効になって暗号化されたルートの論理ボリュームを含むボリュームグループがデバイスとして使用できるようになります。それからボリュームグループがルートにマッピングされます。パラメータは cryptdevice=/dev/vgname/lvname:dmname という形式になります。

root

root= パラメータは実際の (暗号化を解除した後の) root ファイルシステムの device を指定します:

root=device
  • ファイルシステムが復号化されたデバイスファイル上に直接フォーマットされている場合、/dev/mapper/dmname となります。
  • LVM を先に有効にして暗号化した論理ルートボリュームを含めている場合も、上記と同じになります。
  • root ファイルシステムが完全に暗号化された LVM の論理ボリュームに含まれている場合、device mapper は root=/dev/mapper/volumegroup-logicalvolume となります。
ヒント: GRUB を使用する場合はこのパラメータを手動で指定する必要はありません。grub-mkconfig を実行するだけで復号化された root ファイルシステムの UUID の確認が自動的に行われ、grub.cfg で指定されます。

resume

resume=device
  • device は suspend2disk のために使われる復号化された (スワップ) ファイルシステムのデバイスファイルです。スワップが別のパーティション上にある場合、/dev/mapper/swap という形式になります。Dm-crypt/スワップの暗号化を参照。

cryptkey

This parameter specifies the location of a keyfile and is required by the encrypt hook for reading such a keyfile to unlock the cryptdevice (unless a key is in the default location, see below). It can have three parameter sets, depending on whether the keyfile exists as a file in a particular device, a bitstream starting on a specific location, or a file in the initramfs.

For a file in a device the format is:

cryptkey=device:fstype:path
  • device is the raw block device where the key exists.
  • fstype is the filesystem type of device (or auto).
  • path is the absolute path of the keyfile within the device.

例: cryptkey=/dev/usbstick:vfat:/secretkey

For a bitstream on a device the key's location is specified with the following:

cryptkey=device:offset:size 

where the offset is in bytes and the size in bits. Example: cryptkey=/dev/sdZ:0:512 reads a 512 bit keyfile starting at the beginning of the device.

For a file included in the initramfs the format is[1]:

cryptkey=rootfs:path

Example: cryptkey=rootfs:/secretkey

Also note that if cryptkey is not specified, it defaults to /crypto_keyfile.bin (in the initramfs).[2]

Dm-crypt/デバイスの暗号化#キーファイルを参照。

crypto

This parameter is specific to pass dm-crypt plain mode options to the encrypt hook.

It takes the form

crypto=<hash>:<cipher>:<keysize>:<offset>:<skip>

引数は cryptsetup のオプションと直接関連します。Dm-crypt/デバイスの暗号化#plain モードの暗号化オプションを見て下さい。

For a disk encrypted with just plain default options, the crypto arguments must be specified, but each entry can be left blank:

crypto=::::

A specific example of arguments is

crypto=sha512:twofish-xts-plain64:512:0:

sd-encrypt フックを使う

以下の luks は全て rd.luks に置き換えられます。luks パラメータはメインシステムと initrd の両方には反映されます。rd.luks パラメータは initrd にだけ反映されます。他のオプションなど詳細については systemd-cryptsetup-generator(8) を参照してください。

ヒント: /etc/crypttab.initramfs ファイルが存在する場合、mkinitcpio/etc/crypttab として initramfs に追加します。
ノート: ルートファイルシステムで luks.* カーネルパラメータを使用し、さらに /etc/crypttab でスワップを記述すると systemd は "Not creating device 'swap' because it was not specified on the kernel command line." というエラーを吐きます。この問題を解決するには代わりに rd.luks.* パラメータを使ってください。

luks.uuid

luks.uuid=UUID

Specify the UUID of the device to be decrypted on boot with this flag. If the UUID is in /etc/crypttab, the options listed there will be used.

luks.name

luks.name=UUID=name

Specify the name of the mapped device after the LUKS partition is open. For example, specifying UUID=cryptroot causes the unlocked device to be located at /dev/mapper/cryptroot. If this is not specified the mapped device will be located at /dev/mapper/luks-UUID where UUID is the UUID of the LUKS partition.

This is equivalent to the second parameter of encrypt's cryptdevice.

luks.options

luks.options=UUID=options

または:

luks.options=options

Specify options for the device listed after UUID or, if not specified, for all UUIDs not specified elsewhere (e.g., crypttab).

This is roughly equivalent to the third parameter of encrypt's cryptdevice.

Follows a similar format to options in crypttab - options are separated by commas, options with values are specified using option=value.

例:

luks.options=timeout=10s,swap,cipher=aes-cbc-essiv:sha256,size=256

luks.key

luks.key=mykeyfile

luks.UUID で指定したデバイスを復号化するのに使用するパスワードファイルの場所を指定します。encrypt フックのパラメータのようにデフォルトの場所というものは存在しません。

crypttab

/etc/crypttab (encrypted device table) ファイルにはシステムの起動時にロックを解除する暗号化デバイスのリストを記述します。fstab と似ています。crypttab ファイルを使うことで暗号化したスワップデバイスやセカンダリファイルシステムを自動でマウントすることが可能です。

crypttabfstab よりもに読み込まれるので、ファイルシステムの中身がマウントされる前に dm-crypt コンテナのロックを解除できます。ただし crypttab が読み込まれるのはシステムが起動したなので、root を暗号化する場合に、mkinitcpio フックやブートローダーのオプションでロックを解除するのを代替することはできません。ブート時の crypttab の処理は systemd-cryptsetup-generator によって自動的に行われるため、特に有効化する必要はありません。

詳しくは crypttab(5) を見てください。デバイスの UUID を使って設定する方法は#起動時にマウントセクションを見てください。

ノート: systemd-bootsd-encrypt フックを使用する場合、ルートパーティション以外のパスフレーズがルートパーティションのパスフレーズと同じであれば、パスフレーズがキャッシュされるためルートパーティション以外に crypttab を配置すr必要はありません。詳しくは こちらのスレッド を参照。
警告: dm-cryptplain モード (--type plain) を使用する場合、crypttab エントリを処理する際に systemd によって問題が発生することがあります:
  • For --type plain devices with a keyfile, it is necessary to add the hash=plain option to crypttab due to a systemd incompatibility. Do not use systemd-cryptsetup manually for device creation to work around it.
  • It may be further required to add the plain option explicitly to force systemd-cryptsetup to recognize a --type plain) device at boot. GitHub issue in question.
警告: nofail オプションが指定された場合、パスワードの入力中にパスワード入力画面が消えてしまうことがあります。nofail はキーファイルを使用する場合にのみ使ってください。
/etc/crypttab
# Example crypttab file. Fields are: name, underlying device, passphrase, cryptsetup options.
# Mount /dev/lvm/swap re-encrypting it with a fresh key each reboot
swap	/dev/lvm/swap	/dev/urandom	swap,cipher=aes-xts-plain64,size=256
# Mount /dev/lvm/tmp as /dev/mapper/tmp using plain dm-crypt with a random passphrase, making its contents unrecoverable after it is dismounted.
tmp	/dev/lvm/tmp	/dev/urandom	tmp,cipher=aes-xts-plain64,size=256 
# Mount /dev/lvm/home as /dev/mapper/home using LUKS, and prompt for the passphrase at boot time.
home   /dev/lvm/home
# Mount /dev/sdb1 as /dev/mapper/backup using LUKS, with a passphrase stored in a file.
backup /dev/sdb1       /home/alice/backup.key

起動時にマウント

起動時に暗号化されたドライブをマウントしたい場合、デバイスの UUID を /etc/crypttab に入力してください。UUID は lsblk -f コマンドで確認して以下のように crypttab に追加します:

/etc/crypttab
externaldrive         UUID=2f9a8428-ac69-478a-88a2-4aa458565431        none    luks,timeout=180

The first parameter is your preferred device mapper's name for the encrypted drive. The option none will trigger a prompt during boot to type the passphrase for unlocking the partition. The timeout option defines a timeout in seconds for entering the decryption password during boot.

A keyfile can also be set up and referenced instead of none. This results in an automatic unlocking, if the keyfile is accessible during boot. Since LUKS offers the option to have multiple keys, the chosen option can also be changed later.

Use the device mapper's name you've defined in /etc/crypttab in /etc/fstab as follows:

/etc/fstab
/dev/mapper/externaldrive      /mnt/backup               ext4    defaults,errors=remount-ro  0  2

Since /dev/mapper/externaldrive already is the result of a unique partition mapping, there is no need to specify an UUID for it. In any case, the mapper with the filesystem will have a different UUID than the partition it is encrypted in.

スタックされたブロックデバイスのマウント

The systemd generators also automatically process stacked block devices at boot.

For example, you can create a RAID setup, use cryptsetup on it and create an LVM logical volume with respective filesystem inside the encrypted block device. A resulting:

$ lsblk -f
─sdXX                  linux_raid_member    
│ └─md0                 crypto_LUKS   
│   └─cryptedbackup     LVM2_member 
│     └─vgraid-lvraid   ext4              /mnt/backup
└─sdYY                  linux_raid_member    
  └─md0                 crypto_LUKS       
    └─cryptedbackup     LVM2_member 
      └─vgraid-lvraid   ext4              /mnt/backup

will ask for the passphrase and mount automatically at boot.

Given you specify the correct corresponding crypttab (e.g. UUID for the crypto_LUKS device) and fstab (/dev/mapper/vgraid-lvraid) entries, there is no need to add additional mkinitcpio hooks/configuration, because /etc/crypttab processing applies to non-root mounts only. One exception is when the mdadm_udev hook is used already (e.g. for the root device). In this case /etc/madadm.conf and the initramfs need updating to achieve the correct root raid is picked first.

トラブルシューティング

起動時にシステムがフリーズしたりパスワードのプロンプトが表示されない

Plymouth を使用する場合、適切なモジュールを使用するか、Plymouth を無効化してください。正しく設定しないと Plymouth によってパスワードプロンプトが隠れてしまい、システムを起動できなくなります。詳しくは Plymouth#plymouth フックを参照。