「Dm-crypt/スワップの暗号化」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
5行目: 5行目:
 
[[Dm-crypt]] に戻る。
 
[[Dm-crypt]] に戻る。
   
必要に応じて、以下で説明する様々な方法を使ってスワップパーティションを暗号化することができます。再起動時にスワップパーティションを (新しい暗号を使って) 初期化することで、過去にスワップアウトされた重要なファイルの断片がそのまま残ってしまうのを防ぐことができ、強固なデータの保護が可能になります。ただし、スワップを暗号化すると基本的にハイバネートが使えなくなってしまいます。
+
必要に応じて、以下で説明する様々な方法を使って[[スワップ]]パーティションを暗号化することができます。再起動時にスワップパーティションを (新しい暗号を使って) 初期化することで、過去にスワップアウトされた重要なファイルの断片がそのまま残ってしまうのを防ぐことができ、強固なデータの保護が可能になります。ただし、スワップを暗号化すると基本的にハイバネートが使えなくなってしまいます。
   
 
== suspend-to-disk を使用しない ==
 
== suspend-to-disk を使用しない ==
   
suspend-to-disk が必要ない場合、起動時にランダムなーでスワップパーティションを号化することができます。起動するたびにパーティの中身はされます。plain dm-crypt を使って {{ic|mkswap}} を呼び出すよう {{ic|/etc/crypttab}} を設定します: [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#2-setup point cryptsetup FAQ 2.3] と {{ic|man (5) crypttab}} の "swap" オプションを見て下さい
+
suspend-to-disk (ハイバネーション) が必要ない場合、{{ic|/etc/crypttab}} を設定することで plain dm-crypt を使って起動時にランダムなパスワでスワップパーティションを号化することができます。ランダムなスワドはャットダウ時にされるため、スワップデバイス内のデータは二度とアクセスできなくなります。
   
デフォルトの {{ic|/etc/crypttab}} に初めからスワップの暗号化についての行が含れていので、アンコメントして {{ic|<device>}} パラメータあなたの使用しているスワップデバイスの[[永続的なブロックデバイスの命名|名前]]に変更することで使えます
+
{{ic|/etc/crypttab}} の {{ic|swap}} からまる行をアンコメントするだけで機能を有効にできます。{{ic|<device>}} パラメータあなたの使用しているスワップデバイスの名前に変更してください例:
   
{{hc|/etc/crypttab|# <name> <device> <password> <options>
+
{{hc|/etc/crypttab|
  +
# <name> <device> <password> <options>
# swap /dev/sdaX /dev/urandom <nowiki>swap,cipher=aes-cbc-essiv:sha256,size=256</nowiki>}}
 
  +
swap /dev/sd''X#'' /dev/urandom <nowiki>swap,cipher=aes-cbc-essiv:sha256,size=256</nowiki>}}
   
  +
上記の設定で {{ic|/dev/sd''X#''}} が {{ic|/dev/mapper/swap}} にスワップパーティションとしてマッピングされ、通常のスワップと同じように {{ic|/etc/fstab}} に追加できるようになります。既にスワップパーティションを使っていた場合、無効化するか [[fstab]] エントリを再利用してデバイスを {{ic|/dev/mapper/swap}} に変更してください。大抵の場合はデフォルトのオプションで問題ありません。他のオプションやカラムの説明については {{ic|man 5 crypttab}} および [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#2-setup point cryptsetup FAQ 2.3] を見てください。
Where:
 
; <name>: Represents the name to state in the first column of {{ic|/etc/fstab}} (as "{{ic|/dev/mapper/<name>}}").
 
; <device>: Should be the persistent device name for the swap device.
 
; <password>: {{ic|/dev/urandom}} sets the dm-crypt master key to be randomized on every volume recreation.
 
; <options>: The {{ic|swap}} option runs mkswap after cryptographic's are setup.
 
   
  +
{{Warning|名前付きデバイスの中身は永久に削除されます。スワップデバイスに対してカーネルのシンプルな命名を使うのは危険です。名前の順番が起動時に変わってしまう可能性があるためです (例: {{ic|/dev/sda}}, {{ic|/dev/sdb}})。以下の方法があります:
{{Warning|Make sure to use either {{ic|by-id}}, {{ic|by-path}} or [[LVM]] logical volumes' [[永続的なブロックデバイスの命名|persistent device naming]] for the {{ic|<device>}} array (especially if there are multiple storage drives in the system), as it might happen that their usual kernel naming order (sda, sdb,...) changes upon boots and thus the swap would be created over a valuable file system, destroying all its content. Because of the recreation and re-encryption of the swap device on every boot with {{ic|mkswap}}, labels and UUIDs cannot be used (see [[永続的なブロックデバイスの命名#by-uuid|naming by UUID]] and [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#2-setup cryptsetup FAQ 2.3]).}}
 
  +
* {{ic|by-id}} と {{ic|by-path}} を使用する。ただし、どちらもハードウェアを変更すると変わる可能性があります。[[永続的なブロックデバイスの命名#by-id と by-path]] を参照。
  +
* [[LVM]] の論理ボリュームの名前を使用する。
  +
* [[#UUID と LABEL]] に書かれている方法を使う。ラベルや [[永続的なブロックデバイスの命名#by-uuid|UUID]] は {{ic|mkswap}} によって起動時にスワップデバイスを再作成・暗号化するため直接使われることはありません [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#2-setup]。
  +
}}
   
  +
カーネルの命名の代わりに {{ic|by-id}} による命名を使う場合、まずスワップデバイスを確認してください:
For example, {{ic|by-id}} persistent device naming is first identified for the chosen device:
 
   
 
{{hc|# ls -l <nowiki>/dev/disk/*/* |</nowiki> grep sdaX|
 
{{hc|# ls -l <nowiki>/dev/disk/*/* |</nowiki> grep sdaX|
31行目: 32行目:
 
}}
 
}}
   
  +
そして出力された行の中から適切なデバイスを選択して以下のように設定してください:
and then used as a persistent reference for the {{ic|/dev/sdaX}} example partition (if two results are returned as above, choose either one of them):
 
   
  +
{{hc|/etc/crypttab|
{{hc|/etc/crypttab|# <name> <device> <password> <options>
 
  +
# <name> <device> <password> <options>
swap /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX /dev/urandom <nowiki>swap,cipher=aes-cbc-essiv:sha256,size=256</nowiki>}}
 
  +
swap /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX /dev/urandom <nowiki>swap,cipher=aes-cbc-essiv:sha256,size=256</nowiki>}}
   
  +
再起動後 {{ic|swapon -s}} を実行することで任意のデバイスマッパーエントリ (例: {{ic|/dev/dm-1}}) が表示され、{{ic|lsblk}} コマンドで {{ic|FSTYPE}} カラムの '''crypt''' が表示されます。起動時に新しく暗号化するため、{{ic|/dev/mapper/swap}} の UUID は毎回変わります。
This will map {{ic|/dev/sdaX}} to {{ic|/dev/mapper/swap}} as a swap partition that can be added in {{ic|/etc/fstab}} like a normal swap. If you had a non-encrypted swap partition before, do not forget to disable it - or re-use its [[fstab]] entry by changing the device to {{ic|/dev/mapper/swap}}.
 
   
  +
{{Note|
After a reboot to activate the encrypted swap, you will note that running {{ic|swapon -s}} shows an arbitrary device mapper entry (e.g. {{ic|/dev/dm-1}}) for it, while the {{ic|lsblk}} command shows '''crypt''' in the {{ic|FSTYPE}} column. Due to fresh encryption each boot, the UUID for {{ic|/dev/mapper/swap}} will change every time.
 
  +
* スワップに選択したパーティションが LUKS パーティションだった場合、crypttab はスワップパーティションを作成するのにパーティションを上書きしません。crypttab で間違ってスワップパーティションを指定したときにデータが消失しないようにするための安全機構です。そのようなパーティションを使うときは [[dm-crypt/ドライブの準備#LUKS ヘッダーを消去|LUKS ヘッダーを上書き]]してください。
  +
* ルートファイルシステムで {{ic|sd-encrypt}} フックと {{ic|luks.*}} カーネルパラメータを使用する場合、systemd は "Not creating device 'swap' because it was not specified on the kernel command line." とエラーを吐きます。問題を解決するには {{ic|rd.luks.*}} パラメータを使ってください。}}
   
  +
=== UUID と LABEL ===
If the partition chosen for swap was previously a LUKS partition, crypttab will not overwrite the partition to create a swap partition. This is a safety measure to prevent data loss from accidental mis-identification of the swap partition in crypttab. In order to use such a partition the [[dm-crypt/ドライブの準備#LUKS ヘッダーを消去|LUKS header must be overwritten]] once.
 
  +
  +
{{Note|以下の方法は systemd と sd-encrypt init フックを使用している場合は機能しません。}}
  +
  +
{{ic|/dev/sdX#}} あるいは {{ic|/dev/disk/by-id/ata-SERIAL-partX}} などのシンプルなカーネルデバイス名で crypttab のスワップを使うのは危険です。デバイス名やパーティションレイアウトに少しでも変更があると {{ic|/etc/crypttab}} は次回の起動時に重要なデータをフォーマットしてしまいます。PARTUUID を使用した場合も同じです。
  +
  +
It is more reliable to identify the correct partition by giving it a genuine UUID or LABEL. By default that does not work because dm-crypt and {{ic|mkswap}} would simply overwrite any content on that partition which would remove the UUID and LABEL too; however, it is possible to specify a swap offset. This allows you to create a very small, empty, bogus filesystem with no other purpose than providing a persistent UUID or LABEL for the swap encryption.
  +
  +
Create a filesystem with label of your choice:
  +
  +
# mkfs.ext2 -L ''cryptswap'' /dev/sd''X#'' 1M
  +
  +
The unusual parameter after the device name limits the filesystem size to 1 MiB, leaving room for encrypted swap behind it.
  +
  +
{{hc|1=# blkid /dev/sdX#|
  +
2=/dev/sdX#: LABEL="cryptswap" UUID="b72c384e-bd3c-49aa-b7a7-a28ea81a2605" TYPE="ext2"}}
  +
  +
With this, {{ic|/dev/sdX#}} now can easily be identified either by UUID or LABEL, regardless of how its device name or even partition number might change in the future. All that's left is the {{ic|/etc/crypttab}} and {{ic|/etc/fstab}} entries:
  +
  +
{{hc|1=/etc/crypttab|2=
  +
# <name> <device> <password> <options>
  +
swap LABEL=''cryptswap'' /dev/urandom swap,offset=2048,cipher=aes-xts-plain64,size=256}}
  +
  +
Note the offset: it's 2048 sectors of 512 bytes, thus 1 MiB. This way the encrypted swap will not affect the filesystem LABEL/UUID, and data alignment works out as well.
  +
  +
{{hc|1=/etc/fstab|2=
  +
# <filesystem> <dir> <type> <options> <dump> <pass>
  +
/dev/mapper/swap none swap defaults 0 0
  +
}}
  +
  +
Using this setup, the cryptswap will only try to use the partition with the corresponding LABEL, regardless of what its device name may be. Should you decide to use the partition for something else, by formatting it the cryptswap LABEL would also be gone, so cryptswap won't overwrite it on your next boot.
   
 
== suspend-to-disk を使用する ==
 
== suspend-to-disk を使用する ==
   
  +
コンピュータをディスクにサスペンド (ハイバネート) して復帰できるようにするには、スワップ領域を残す必要があります。あらかじめ LUKS スワップパーティションを用意して、ディスクに保存するか起動時に手動で入力できるようにしなければなりません。
The following three methods are alternatives for setting up an encrypted swap for resume-from-disk. If you apply any of them, be aware that critical data swapped out by the system may potentially stay in the swap over a long period (i.e. until it is overwritten). To reduce this risk consider setting up a system job which re-encrypts swap, e.g. each time the system is going into a regular shut-down, along with the method of your choice.
 
  +
  +
以下の3つの方法はハイバネートのためにスワップの暗号化を設定する方法です。以下の方法を使う場合、スワップアウトされたデータがしばらくスワップに残り続ける可能性があるため注意してください。システムをシャットダウンするときにスワップを再暗号化するシステムジョブを設定することでリスクを減らすことができます。
   
 
=== LVM on LUKS ===
 
=== LVM on LUKS ===
59行目: 95行目:
 
=== mkinitcpio フック ===
 
=== mkinitcpio フック ===
   
  +
{{Note|このセクションは {{ic|encrypt}} フックを使用する場合にのみ適用され、シングルデバイスしかアンロックできません ({{Bug|23182}})。{{ic|sd-encrypt}} を使用することで複数のデバイスをアンロックできるようになりますが ([[Dm-crypt/システム#sd-encrypt フックを使う]]を参照)、スワップの自動認識は使えなくなります [https://github.com/systemd/systemd/issues/4878]。}}
To be able to resume after suspending the computer to disk (hibernate), it is required to keep the swap filesystem intact. Therefore, it is required to have a pre-existent LUKS swap partition, which can be stored on the disk or input manually at startup. Because the resume takes place before {{ic|/etc/crypttab}} can be used, it is required to create a hook in {{ic|/etc/mkinitcpio.conf}} to open the swap LUKS device before resuming.
 
  +
  +
If the swap device is on a different device from that of the root file system, it will not be opened by the {{ic|encrypt}} hook, i.e. the resume will take place before {{ic|/etc/crypttab}} can be used, therefore it is required to create a hook in {{ic|/etc/mkinitcpio.conf}} to open the swap LUKS device before resuming.
   
 
If you want to use a partition which is currently used by the system, you have to disable it first:
 
If you want to use a partition which is currently used by the system, you have to disable it first:
65行目: 103行目:
   
 
Also make sure you remove any line in {{ic|/etc/crypttab}} pointing to this device.
 
Also make sure you remove any line in {{ic|/etc/crypttab}} pointing to this device.
  +
  +
既存のスワップパーティションを再利用する場合、パーティションが GPT パーティションテーブルに載っているときはパーティションを削除してから再作成する必要があります。[[パーティショニング]]を見てください。削除することで systemd-gpt-auto-generator によって起動時にパーティションが有効にならないようにします。
   
 
The following setup has the disadvantage of having to insert an additional passphrase for the swap partition manually on every boot.
 
The following setup has the disadvantage of having to insert an additional passphrase for the swap partition manually on every boot.
   
{{Warning|1=Do not use this setup with a key file. Please read about the issue reported [https://wiki.archlinux.org/index.php?title=Talk:Dm-crypt&oldid=255742#Suspend_to_disk_instructions_are_insecure here]. Alternatively, use a gnupg-encrypted keyfile as per https://bbs.archlinux.org/viewtopic.php?id=120181}}
+
{{Warning|1=Do not use this setup with a key file if {{ic|/boot}} is unencrypted. Please read about the issue reported [https://wiki.archlinux.org/index.php?title=Talk:Dm-crypt&oldid=255742#Suspend_to_disk_instructions_are_insecure here]. Alternatively, use a gnupg-encrypted keyfile as per https://bbs.archlinux.org/viewtopic.php?id=120181}}
   
 
To format the encrypted container for the swap partition, create a keyslot for a user-memorizable passphrase.
 
To format the encrypted container for the swap partition, create a keyslot for a user-memorizable passphrase.
80行目: 120行目:
 
起動時にスワップをオープンにするフックを作成する必要があります。{{AUR|mkinitcpio-openswap}} を[[インストール]]して設定するか、以下の手順に従って下さい。open コマンドを記述したフックファイルを作成:
 
起動時にスワップをオープンにするフックを作成する必要があります。{{AUR|mkinitcpio-openswap}} を[[インストール]]して設定するか、以下の手順に従って下さい。open コマンドを記述したフックファイルを作成:
   
{{hc|/lib/initcpio/hooks/openswap|<nowiki>
+
{{hc|/etc/initcpio/hooks/openswap|<nowiki>
 
run_hook ()
 
run_hook ()
 
{
 
{
89行目: 129行目:
 
for opening the swap device by typing your password or
 
for opening the swap device by typing your password or
   
{{hc|/lib/initcpio/hooks/openswap|<nowiki>
+
{{hc|/etc/initcpio/hooks/openswap|<nowiki>
 
run_hook ()
 
run_hook ()
 
{
 
{
  +
## Optional: To avoid race conditions
  +
x=0;
  +
while [ ! -b /dev/mapper/<root-device> ] && [ $x -le 10 ]; do
  +
x=$((x+1))
  +
sleep .2
  +
done
  +
## End of optional
  +
 
mkdir crypto_key_device
 
mkdir crypto_key_device
 
mount /dev/mapper/<root-device> crypto_key_device
 
mount /dev/mapper/<root-device> crypto_key_device
99行目: 147行目:
 
</nowiki>}}
 
</nowiki>}}
   
for opening the swap device by loading a keyfile from a crypted root device
+
for opening the swap device by loading a keyfile from a crypted root device.
   
  +
On some computers race conditions may occur when mkinitcpio tries to mount the device before the decryption process and device enumeration is completed. The commented ''Optional'' block will delay the boot process up to 2 seconds until the root device is ready to mount.
{{Note|If swap is on a Solid State Disk (SSD) and Discard/TRIM is desired the option {{ic|--allow-discards}} has to get added to the cryptsetup line in the openswap hook above. See [[dm-crypt/特記事項#ソリッドステートドライブ (SSD) の Discard/TRIM のサポート|Discard/TRIM support for solid state disks (SSD)]] or [[SSD]] for more information on discard. Additionally you have to add the mount option 'discard' to your fstab entry for the swap device.''}}
 
  +
  +
{{Note|スワップがソリッドステートドライブ (SSD) 上にあり Discard/TRIM を使用する場合、{{ic|--allow-discards}} オプションを上記の openswap フックの cryptsetup 行に追加する必要があります。discard について詳しくは [[Dm-crypt/特記事項#ソリッドステートドライブ (SSD) の Discard/TRIM のサポート]]や [[SSD]] の記事を見てください。さらに 'discard' オプションをスワップデバイスの fstab エントリに追加してください。}}
   
 
Then create and edit the hook setup file:
 
Then create and edit the hook setup file:
{{hc|/lib/initcpio/install/openswap|<nowiki>
+
{{hc|/etc/initcpio/install/openswap|<nowiki>
 
build ()
 
build ()
 
{
 
{
137行目: 187行目:
 
=== スワップファイルを使う ===
 
=== スワップファイルを使う ===
   
  +
スワップファイルを使って既存のパーティションにスワップ領域を予約したり暗号化されたブロックデバイスのパーティションの中で設定することができます。スワップファイルから復帰するときは {{ic|resume}} フックにパスフレーズを入力してスワップファイルが存在するデバイスをアンロックします。
A swap file can be used to reserve swap-space within an existing partition and may also be setup inside an encrypted blockdevice's partition. When resuming from a swapfile the {{ic|resume}} hook must be supplied with the passphrase to unlock the device where the swap file is located.
 
   
 
{{Warning|[[Dm-crypt/ドライブの準備#Btrfs のサブボリューム|Btrfs]] はスワップファイルをサポートしていません。この警告を無視するとファイルシステムが壊れてしまう可能性があります。ループデバイスを使ってマウントすることで [[Btrfs#スワップファイル|Btrfs]] でスワップファイルを使うことはできますが、スワップのパフォーマンスが大分落ちます。}}
 
{{Warning|[[Dm-crypt/ドライブの準備#Btrfs のサブボリューム|Btrfs]] はスワップファイルをサポートしていません。この警告を無視するとファイルシステムが壊れてしまう可能性があります。ループデバイスを使ってマウントすることで [[Btrfs#スワップファイル|Btrfs]] でスワップファイルを使うことはできますが、スワップのパフォーマンスが大分落ちます。}}
143行目: 193行目:
 
To create it, first choose a mapped partition (e.g. {{ic|/dev/mapper/rootDevice}}) whose mounted filesystem (e.g. {{ic|/}}) contains enough free space to create a swapfile with the desired size.
 
To create it, first choose a mapped partition (e.g. {{ic|/dev/mapper/rootDevice}}) whose mounted filesystem (e.g. {{ic|/}}) contains enough free space to create a swapfile with the desired size.
   
Now [[スワップ#スワップファイルの作成|create the swap file]] (e.g. {{ic|/swapfile}}) inside the mounted filesystem of your chosen mapped partition. Be sure to activate it with {{ic|swapon}} and also add it to your {{ic|/etc/fstab}} file afterward. Note that the swapfile's previous contents remain transparent over reboots.
+
選択したパーティションのマウントしたファイルシステムに[[スワップ#スワップファイルの作成|スワップファイルを作成]] (例: {{ic|/swapfile}}) してください。そして {{ic|swapon}} でスワップファイルを有効にして {{ic|/etc/fstab}} ファイルに追加してください。スワップファイルの前のコンテンツは再起動しても残るので注意してください。
   
 
Set up your system to resume from your chosen mapped partition. For example, if you use [[GRUB]] with kernel hibernation support, add {{ic|resume<nowiki>=</nowiki>}}''your chosen mapped partition'' and {{ic|resume_offset<nowiki>=</nowiki>}}''see calculation command below'' to the kernel line in {{ic|/boot/grub/grub.cfg}}. A line with encrypted root partition can look like this:
 
Set up your system to resume from your chosen mapped partition. For example, if you use [[GRUB]] with kernel hibernation support, add {{ic|resume<nowiki>=</nowiki>}}''your chosen mapped partition'' and {{ic|resume_offset<nowiki>=</nowiki>}}''see calculation command below'' to the kernel line in {{ic|/boot/grub/grub.cfg}}. A line with encrypted root partition can look like this:
157行目: 207行目:
 
HOOKS="... encrypt '''resume''' ... filesystems ..."
 
HOOKS="... encrypt '''resume''' ... filesystems ..."
   
  +
USB キーボードを使って復号化パスワードを入力する場合、以下のように {{ic|encrypt}} フックの前に {{ic|keyboard}} モジュールが来るようにしてください。モジュールが後になっていると、復号化パスワードを入力できなくなってコンピュータを起動できなくなります ({{ic|keyboard}} を追加しても問題が起こる場合は {{ic|usbinput}} を試してみてください):
If you use a USB keyboard to enter your decryption password, then the {{ic|keyboard}} module '''must''' appear in front of the {{ic|encrypt}} hook, as shown below. Otherwise, you will not be able to boot your computer because you could not enter your decryption password to decrypt your Linux root partition! (If you still have this problem after adding {{ic|keyboard}}, try {{ic|usbinput}}, though this is deprecated.)
 
   
 
HOOKS="... '''keyboard''' encrypt ..."
 
HOOKS="... '''keyboard''' encrypt ..."
  +
  +
== 既知の問題 ==
  +
  +
* ログに "Stopped (with error) /dev/dm-1" と出力される場合 [https://github.com/systemd/systemd/issues/1620] を見てください。

2017年8月11日 (金) 21:39時点における版

Dm-crypt に戻る。

必要に応じて、以下で説明する様々な方法を使ってスワップパーティションを暗号化することができます。再起動時にスワップパーティションを (新しい暗号を使って) 初期化することで、過去にスワップアウトされた重要なファイルの断片がそのまま残ってしまうのを防ぐことができ、強固なデータの保護が可能になります。ただし、スワップを暗号化すると基本的にハイバネートが使えなくなってしまいます。

suspend-to-disk を使用しない

suspend-to-disk (ハイバネーション) が必要ない場合、/etc/crypttab を設定することで plain dm-crypt を使って起動時にランダムなパスワードでスワップパーティションを復号化することができます。ランダムなパスワードはシャットダウン時に破棄されるため、スワップデバイス内のデータには二度とアクセスできなくなります。

/etc/crypttabswap から始まる行をアンコメントするだけで機能を有効にできます。<device> パラメータはあなたの使用しているスワップデバイスの名前に変更してください。例:

/etc/crypttab
# <name>  <device>     <password>     <options>
swap      /dev/sdX#    /dev/urandom   swap,cipher=aes-cbc-essiv:sha256,size=256

上記の設定で /dev/sdX#/dev/mapper/swap にスワップパーティションとしてマッピングされ、通常のスワップと同じように /etc/fstab に追加できるようになります。既にスワップパーティションを使っていた場合、無効化するか fstab エントリを再利用してデバイスを /dev/mapper/swap に変更してください。大抵の場合はデフォルトのオプションで問題ありません。他のオプションやカラムの説明については man 5 crypttab および point cryptsetup FAQ 2.3 を見てください。

警告: 名前付きデバイスの中身は永久に削除されます。スワップデバイスに対してカーネルのシンプルな命名を使うのは危険です。名前の順番が起動時に変わってしまう可能性があるためです (例: /dev/sda, /dev/sdb)。以下の方法があります:
  • by-idby-path を使用する。ただし、どちらもハードウェアを変更すると変わる可能性があります。永続的なブロックデバイスの命名#by-id と by-path を参照。
  • LVM の論理ボリュームの名前を使用する。
  • #UUID と LABEL に書かれている方法を使う。ラベルや UUIDmkswap によって起動時にスワップデバイスを再作成・暗号化するため直接使われることはありません [1]

カーネルの命名の代わりに by-id による命名を使う場合、まずスワップデバイスを確認してください:

# ls -l /dev/disk/*/* | grep sdaX
lrwxrwxrwx 1 root root 10 Oct 12 16:54 /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX -> ../../sdaX
lrwxrwxrwx 1 root root 10 Oct 12 16:54 /dev/disk/by-id/wwn-0x60015ee0000b237f-partX -> ../../sdaX

そして出力された行の中から適切なデバイスを選択して以下のように設定してください:

/etc/crypttab
# <name>  <device>                                                         <password>     <options>
swap      /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX  /dev/urandom   swap,cipher=aes-cbc-essiv:sha256,size=256

再起動後 swapon -s を実行することで任意のデバイスマッパーエントリ (例: /dev/dm-1) が表示され、lsblk コマンドで FSTYPE カラムの crypt が表示されます。起動時に新しく暗号化するため、/dev/mapper/swap の UUID は毎回変わります。

ノート:
  • スワップに選択したパーティションが LUKS パーティションだった場合、crypttab はスワップパーティションを作成するのにパーティションを上書きしません。crypttab で間違ってスワップパーティションを指定したときにデータが消失しないようにするための安全機構です。そのようなパーティションを使うときは LUKS ヘッダーを上書きしてください。
  • ルートファイルシステムで sd-encrypt フックと luks.* カーネルパラメータを使用する場合、systemd は "Not creating device 'swap' because it was not specified on the kernel command line." とエラーを吐きます。問題を解決するには rd.luks.* パラメータを使ってください。

UUID と LABEL

ノート: 以下の方法は systemd と sd-encrypt init フックを使用している場合は機能しません。

/dev/sdX# あるいは /dev/disk/by-id/ata-SERIAL-partX などのシンプルなカーネルデバイス名で crypttab のスワップを使うのは危険です。デバイス名やパーティションレイアウトに少しでも変更があると /etc/crypttab は次回の起動時に重要なデータをフォーマットしてしまいます。PARTUUID を使用した場合も同じです。

It is more reliable to identify the correct partition by giving it a genuine UUID or LABEL. By default that does not work because dm-crypt and mkswap would simply overwrite any content on that partition which would remove the UUID and LABEL too; however, it is possible to specify a swap offset. This allows you to create a very small, empty, bogus filesystem with no other purpose than providing a persistent UUID or LABEL for the swap encryption.

Create a filesystem with label of your choice:

# mkfs.ext2 -L cryptswap /dev/sdX# 1M

The unusual parameter after the device name limits the filesystem size to 1 MiB, leaving room for encrypted swap behind it.

# blkid /dev/sdX#
/dev/sdX#: LABEL="cryptswap" UUID="b72c384e-bd3c-49aa-b7a7-a28ea81a2605" TYPE="ext2"

With this, /dev/sdX# now can easily be identified either by UUID or LABEL, regardless of how its device name or even partition number might change in the future. All that's left is the /etc/crypttab and /etc/fstab entries:

/etc/crypttab
# <name> <device>         <password>    <options>
swap     LABEL=cryptswap  /dev/urandom  swap,offset=2048,cipher=aes-xts-plain64,size=256

Note the offset: it's 2048 sectors of 512 bytes, thus 1 MiB. This way the encrypted swap will not affect the filesystem LABEL/UUID, and data alignment works out as well.

/etc/fstab
# <filesystem>    <dir>  <type>  <options>  <dump>  <pass>
/dev/mapper/swap  none   swap    defaults   0       0

Using this setup, the cryptswap will only try to use the partition with the corresponding LABEL, regardless of what its device name may be. Should you decide to use the partition for something else, by formatting it the cryptswap LABEL would also be gone, so cryptswap won't overwrite it on your next boot.

suspend-to-disk を使用する

コンピュータをディスクにサスペンド (ハイバネート) して復帰できるようにするには、スワップ領域を残す必要があります。あらかじめ LUKS スワップパーティションを用意して、ディスクに保存するか起動時に手動で入力できるようにしなければなりません。

以下の3つの方法はハイバネートのためにスワップの暗号化を設定する方法です。以下の方法を使う場合、スワップアウトされたデータがしばらくスワップに残り続ける可能性があるため注意してください。システムをシャットダウンするときにスワップを再暗号化するシステムジョブを設定することでリスクを減らすことができます。

LVM on LUKS

suspend-to-disk のサポートを暗号化されたスワップで実現するシンプルな方法は暗号化レイヤーで LVM を使うことです。暗号化されたパーティションにいくらでもファイルシステムを含めることができるようになります (root, swap, home, ...)。Dm-crypt/システム全体の暗号化#LVM on LUKS の手順に従ってから必要なカーネルパラメータを設定してください。

Assuming you have setup LVM on LUKS with a swap logical volume (at /dev/MyStorage/swap for example), all you need to do is add the resume mkinitcpio hook, and add the resume=/dev/MyStorage/swap kernel parameter to your boot loader. For GRUB, this can be done by appending it to the GRUB_CMDLINE_LINUX_DEFAULT variable in /etc/default/grub.

/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="... resume=/dev/MyStorage/swap"

then run grub-mkconfig -o /boot/grub/grub.cfg to update GRUB's configuration file. To add the mkinitcpio hook, edit the following line in mkinitcpio.conf

/etc/mkinitcpio.conf
HOOKS="... encrypt lvm2 resume ... filesystems ..."

then run mkinitcpio -p linux to update the initramfs image.

mkinitcpio フック

ノート: このセクションは encrypt フックを使用する場合にのみ適用され、シングルデバイスしかアンロックできません (FS#23182)。sd-encrypt を使用することで複数のデバイスをアンロックできるようになりますが (Dm-crypt/システム#sd-encrypt フックを使うを参照)、スワップの自動認識は使えなくなります [2]

If the swap device is on a different device from that of the root file system, it will not be opened by the encrypt hook, i.e. the resume will take place before /etc/crypttab can be used, therefore it is required to create a hook in /etc/mkinitcpio.conf to open the swap LUKS device before resuming.

If you want to use a partition which is currently used by the system, you have to disable it first:

# swapoff /dev/<device>

Also make sure you remove any line in /etc/crypttab pointing to this device.

既存のスワップパーティションを再利用する場合、パーティションが GPT パーティションテーブルに載っているときはパーティションを削除してから再作成する必要があります。パーティショニングを見てください。削除することで systemd-gpt-auto-generator によって起動時にパーティションが有効にならないようにします。

The following setup has the disadvantage of having to insert an additional passphrase for the swap partition manually on every boot.

警告: Do not use this setup with a key file if /boot is unencrypted. Please read about the issue reported here. Alternatively, use a gnupg-encrypted keyfile as per https://bbs.archlinux.org/viewtopic.php?id=120181

To format the encrypted container for the swap partition, create a keyslot for a user-memorizable passphrase.

Open the partition in /dev/mapper:

# cryptsetup open --type luks /dev/<device> swapDevice

Create a swap filesystem inside the mapped partition:

# mkswap /dev/mapper/swapDevice

起動時にスワップをオープンにするフックを作成する必要があります。mkinitcpio-openswapAURインストールして設定するか、以下の手順に従って下さい。open コマンドを記述したフックファイルを作成:

/etc/initcpio/hooks/openswap
 run_hook ()
 {
     cryptsetup open --type luks /dev/<device> swapDevice
 }

for opening the swap device by typing your password or

/etc/initcpio/hooks/openswap
 run_hook ()
 {
     ## Optional: To avoid race conditions
     x=0;
     while [ ! -b /dev/mapper/<root-device> ] && [ $x -le 10 ]; do
        x=$((x+1))
        sleep .2
     done
     ## End of optional

     mkdir crypto_key_device
     mount /dev/mapper/<root-device> crypto_key_device
     cryptsetup open --type luks --key-file crypto_key_device/<path-to-the-key> /dev/<device> swapDevice
     umount crypto_key_device
 }

for opening the swap device by loading a keyfile from a crypted root device.

On some computers race conditions may occur when mkinitcpio tries to mount the device before the decryption process and device enumeration is completed. The commented Optional block will delay the boot process up to 2 seconds until the root device is ready to mount.

ノート: スワップがソリッドステートドライブ (SSD) 上にあり Discard/TRIM を使用する場合、--allow-discards オプションを上記の openswap フックの cryptsetup 行に追加する必要があります。discard について詳しくは Dm-crypt/特記事項#ソリッドステートドライブ (SSD) の Discard/TRIM のサポートSSD の記事を見てください。さらに 'discard' オプションをスワップデバイスの fstab エントリに追加してください。

Then create and edit the hook setup file:

/etc/initcpio/install/openswap
build ()
{
   add_runscript
}
help ()
{
cat<<HELPEOF
  This opens the swap encrypted partition /dev/<device> in /dev/mapper/swapDevice
HELPEOF
}

Add the hook openswap in the HOOKS array in /etc/mkinitcpio.conf, before filesystem but after encrypt. Do not forget to add the resume hook after openswap.

HOOKS="... encrypt openswap resume filesystems ..."

Regenerate the boot image:

# mkinitcpio -p linux

Add the mapped partition to /etc/fstab by adding the following line:

/dev/mapper/swapDevice swap swap defaults 0 0

Set up your system to resume from /dev/mapper/swapDevice. For example, if you use GRUB with kernel hibernation support, add resume=/dev/mapper/swapDevice to the kernel line in /boot/grub/grub.cfg. A line with encrypted root and swap partitions can look like this:

kernel /vmlinuz-linux cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/swapDevice ro

To make the parameter persistent on kernel updates, add it to /etc/default/grub.

At boot time, the openswap hook will open the swap partition so the kernel resume may use it. If you use special hooks for resuming from hibernation, make sure they are placed after openswap in the HOOKS array. Please note that because of initrd opening swap, there is no entry for swapDevice in /etc/crypttab needed in this case.

スワップファイルを使う

スワップファイルを使って既存のパーティションにスワップ領域を予約したり暗号化されたブロックデバイスのパーティションの中で設定することができます。スワップファイルから復帰するときは resume フックにパスフレーズを入力してスワップファイルが存在するデバイスをアンロックします。

警告: Btrfs はスワップファイルをサポートしていません。この警告を無視するとファイルシステムが壊れてしまう可能性があります。ループデバイスを使ってマウントすることで Btrfs でスワップファイルを使うことはできますが、スワップのパフォーマンスが大分落ちます。

To create it, first choose a mapped partition (e.g. /dev/mapper/rootDevice) whose mounted filesystem (e.g. /) contains enough free space to create a swapfile with the desired size.

選択したパーティションのマウントしたファイルシステムにスワップファイルを作成 (例: /swapfile) してください。そして swapon でスワップファイルを有効にして /etc/fstab ファイルに追加してください。スワップファイルの前のコンテンツは再起動しても残るので注意してください。

Set up your system to resume from your chosen mapped partition. For example, if you use GRUB with kernel hibernation support, add resume=your chosen mapped partition and resume_offset=see calculation command below to the kernel line in /boot/grub/grub.cfg. A line with encrypted root partition can look like this:

kernel /vmlinuz-linux cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/rootDevice resume_offset=123456789 ro

The resume_offset of the swap-file points to the start (extent zero) of the file and can be identified like this:

# filefrag -v /swapfile | awk '{if($1=="0:"){print $4}}'

resume フックを etc/mkinitcpio.conf ファイルに追加してイメージを再生成してください:

HOOKS="... encrypt resume ... filesystems ..."

USB キーボードを使って復号化パスワードを入力する場合、以下のように encrypt フックの前に keyboard モジュールが来るようにしてください。モジュールが後になっていると、復号化パスワードを入力できなくなってコンピュータを起動できなくなります (keyboard を追加しても問題が起こる場合は usbinput を試してみてください):

HOOKS="... keyboard encrypt ..."

既知の問題

  • ログに "Stopped (with error) /dev/dm-1" と出力される場合 [3] を見てください。