← Documents Documentation/power/swsusp-dmcrypt.rst GitHub 원문 ↗

Linux 6.18.37 · Power

How to use dm-crypt and swsusp together

Initrd에서 고정 device-mapper 번호와 외부 key로 encrypted swap resume을 구성하는 예제입니다.

Source pathDocumentation/power/swsusp-dmcrypt.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

swsusp-dmcrypt.rst:1-140

Initrd에서 고정 device-mapper 번호와 외부 key로 encrypted swap resume을 구성하는 예제입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 =======================================
2 How to use dm-crypt and swsusp together
3 =======================================
4
5 Author: Andreas Steinmetz <[email protected]>
6
7
8
9 Some prerequisites:
10 You know how dm-crypt works. If not, visit the following web page:
11 http://www.saout.de/misc/dm-crypt/
12 You have read Documentation/power/swsusp.rst and understand it.
13 You did read Documentation/admin-guide/initrd.rst and know how an initrd works.
14 You know how to create or how to modify an initrd.
15
16 Now your system is properly set up, your disk is encrypted except for
17 the swap device(s) and the boot partition which may contain a mini
18 system for crypto setup and/or rescue purposes. You may even have
19 an initrd that does your current crypto setup already.
20
21 At this point you want to encrypt your swap, too. Still you want to
22 be able to suspend using swsusp. This, however, means that you
23 have to be able to either enter a passphrase or that you read
24 the key(s) from an external device like a pcmcia flash disk
25 or an usb stick prior to resume. So you need an initrd, that sets
26 up dm-crypt and then asks swsusp to resume from the encrypted
27 swap device.
28
29 The most important thing is that you set up dm-crypt in such
30 a way that the swap device you suspend to/resume from has
31 always the same major/minor within the initrd as well as
32 within your running system. The easiest way to achieve this is
33 to always set up this swap device first with dmsetup, so that
34 it will always look like the following::
35
36 brw------- 1 root root 254, 0 Jul 28 13:37 /dev/mapper/swap0
37
38 Now set up your kernel to use /dev/mapper/swap0 as the default
39 resume partition, so your kernel .config contains::
40
41 CONFIG_PM_STD_PARTITION="/dev/mapper/swap0"
42
43 Prepare your boot loader to use the initrd you will create or
44 modify. For lilo the simplest setup looks like the following
45 lines::
46
47 image=/boot/vmlinuz
48 initrd=/boot/initrd.gz
49 label=linux
50 append="root=/dev/ram0 init=/linuxrc rw"
51
52 Finally you need to create or modify your initrd. Lets assume
53 you create an initrd that reads the required dm-crypt setup
54 from a pcmcia flash disk card. The card is formatted with an ext2
55 fs which resides on /dev/hde1 when the card is inserted. The
56 card contains at least the encrypted swap setup in a file
57 named "swapkey". /etc/fstab of your initrd contains something
58 like the following::
59
60 /dev/hda1 /mnt ext3 ro 0 0
61 none /proc proc defaults,noatime,nodiratime 0 0
62 none /sys sysfs defaults,noatime,nodiratime 0 0
63
64 /dev/hda1 contains an unencrypted mini system that sets up all
65 of your crypto devices, again by reading the setup from the
66 pcmcia flash disk. What follows now is a /linuxrc for your
67 initrd that allows you to resume from encrypted swap and that
68 continues boot with your mini system on /dev/hda1 if resume
69 does not happen::
70
71 #!/bin/sh
72 PATH=/sbin:/bin:/usr/sbin:/usr/bin
73 mount /proc
74 mount /sys
75 mapped=0
76 noresume=`grep -c noresume /proc/cmdline`
77 if [ "$*" != "" ]
78 then
79 noresume=1
80 fi
81 dmesg -n 1
82 /sbin/cardmgr -q
83 for i in 1 2 3 4 5 6 7 8 9 0
84 do
85 if [ -f /proc/ide/hde/media ]
86 then
87 usleep 500000
88 mount -t ext2 -o ro /dev/hde1 /mnt
89 if [ -f /mnt/swapkey ]
90 then
91 dmsetup create swap0 /mnt/swapkey > /dev/null 2>&1 && mapped=1
92 fi
93 umount /mnt
94 break
95 fi
96 usleep 500000
97 done
98 killproc /sbin/cardmgr
99 dmesg -n 6
100 if [ $mapped = 1 ]
101 then
102 if [ $noresume != 0 ]
103 then
104 mkswap /dev/mapper/swap0 > /dev/null 2>&1
105 fi
106 echo 254:0 > /sys/power/resume
107 dmsetup remove swap0
108 fi
109 umount /sys
110 mount /mnt
111 umount /proc
112 cd /mnt
113 pivot_root . mnt
114 mount /proc
115 umount -l /mnt
116 umount /proc
117 exec chroot . /sbin/init $* < dev/console > dev/console 2>&1
118
119 Please don't mind the weird loop above, busybox's msh doesn't know
120 the let statement. Now, what is happening in the script?
121 First we have to decide if we want to try to resume, or not.
122 We will not resume if booting with "noresume" or any parameters
123 for init like "single" or "emergency" as boot parameters.
124
125 Then we need to set up dmcrypt with the setup data from the
126 pcmcia flash disk. If this succeeds we need to reset the swap
127 device if we don't want to resume. The line "echo 254:0 > /sys/power/resume"
128 then attempts to resume from the first device mapper device.
129 Note that it is important to set the device in /sys/power/resume,
130 regardless if resuming or not, otherwise later suspend will fail.
131 If resume starts, script execution terminates here.
132
133 Otherwise we just remove the encrypted swap device and leave it to the
134 mini system on /dev/hda1 to set the whole crypto up (it is up to
135 you to modify this to your taste).
136
137 What then follows is the well known process to change the root
138 file system and continue booting from there. I prefer to unmount
139 the initrd prior to continue booting but it is up to you to modify
140 this.
141

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

암호화 swap resume 전제

1-28

이 절차는 dm-crypt 작동 방식을 알고 `Documentation/power/swsusp.rst`와 `Documentation/admin-guide/initrd.rst`를 읽었으며 initrd를 만들거나 수정할 수 있다는 전제에서 시작합니다. 참고 URL은 `http://www.saout.de/misc/dm-crypt/`입니다.

Disk의 swap device와 boot partition을 제외한 영역이 이미 암호화되어 있고 boot partition에는 crypto setup 또는 rescue용 mini system이 있을 수 있습니다.

Swap도 암호화하면서 swsusp를 쓰려면 resume 전에 passphrase를 입력하거나 PCMCIA flash disk·USB stick 같은 외부 device에서 key를 읽어야 합니다. 따라서 initrd가 dm-crypt mapping을 만든 뒤 encrypted swap device에서 swsusp resume을 요청해야 합니다.

Encrypted swap resume
boot initrdobtain passphrase or external keydm-crypt mappingrequest swsusp resumecontinue boot if no image

Root filesystem 전환 전에 initrd가 key와 mapping을 준비합니다.

=======================================
How to use dm-crypt and swsusp together
=======================================

Author: Andreas Steinmetz <[email protected]>



Some prerequisites:
You know how dm-crypt works. If not, visit the following web page:
http://www.saout.de/misc/dm-crypt/
You have read Documentation/power/swsusp.rst and understand it.
You did read Documentation/admin-guide/initrd.rst and know how an initrd works.
You know how to create or how to modify an initrd.

Now your system is properly set up, your disk is encrypted except for
the swap device(s) and the boot partition which may contain a mini
system for crypto setup and/or rescue purposes. You may even have
an initrd that does your current crypto setup already.

At this point you want to encrypt your swap, too. Still you want to
be able to suspend using swsusp. This, however, means that you
have to be able to either enter a passphrase or that you read
the key(s) from an external device like a pcmcia flash disk
or an usb stick prior to resume. So you need an initrd, that sets
up dm-crypt and then asks swsusp to resume from the encrypted
swap device.

고정 major/minor와 boot 설정

29-51

가장 중요한 조건은 suspend/resume 대상 swap device가 initrd와 실행 중인 system에서 항상 같은 major/minor를 갖는 것입니다. 가장 쉬운 방법은 `dmsetup`에서 이 swap mapping을 항상 먼저 만들어 `/dev/mapper/swap0`이 `254, 0`이 되게 하는 것입니다.

Kernel `.config`에는 `CONFIG_PM_STD_PARTITION="/dev/mapper/swap0"`을 설정합니다. Boot loader도 새 initrd를 사용해야 하며 원문은 lilo의 `image`, `initrd`, `label`, `append="root=/dev/ram0 init=/linuxrc rw"` 예제를 제공합니다.

안정적인 resume device
항목
Mapper path/dev/mapper/swap0
Major/minor254:0
Kernel configCONFIG_PM_STD_PARTITION=/dev/mapper/swap0
Initrdswap0 mapping을 첫 번째로 생성

Kernel 설정과 initrd mapping이 동일한 device number를 가리켜야 합니다.

The most important thing is that you set up dm-crypt in such
a way that the swap device you suspend to/resume from has
always the same major/minor within the initrd as well as
within your running system. The easiest way to achieve this is
to always set up this swap device first with dmsetup, so that
it will always look like the following::

  brw-------  1 root root 254, 0 Jul 28 13:37 /dev/mapper/swap0

Now set up your kernel to use /dev/mapper/swap0 as the default
resume partition, so your kernel .config contains::

  CONFIG_PM_STD_PARTITION="/dev/mapper/swap0"

Prepare your boot loader to use the initrd you will create or
modify. For lilo the simplest setup looks like the following
lines::

  image=/boot/vmlinuz
  initrd=/boot/initrd.gz
  label=linux
  append="root=/dev/ram0 init=/linuxrc rw"

예제 initrd 구성

52-70

예제 initrd는 PCMCIA flash disk에서 dm-crypt setup을 읽습니다. Card는 삽입 시 `/dev/hde1`인 ext2 filesystem이고 encrypted swap setup을 `swapkey` 파일로 담습니다.

Initrd의 `/etc/fstab`은 unencrypted mini system이 있는 `/dev/hda1`을 `/mnt`에 read-only ext3로, `/proc`과 `/sys`를 각각 proc·sysfs로 mount합니다. `/dev/hda1`의 mini system도 같은 flash disk setup으로 나머지 crypto device를 구성합니다.

이어지는 `/linuxrc`는 encrypted swap에서 resume을 시도하고, resume하지 않으면 `/dev/hda1` mini system으로 root를 전환해 boot를 계속합니다.

예제 storage 배치
PCMCIA /dev/hde1 ext2 -> swapkeyinitrd /linuxrcdmsetup swap0resume attemptfallback /dev/hda1 mini system

외부 key media와 unencrypted mini system의 역할을 분리합니다.

Finally you need to create or modify your initrd. Lets assume
you create an initrd that reads the required dm-crypt setup
from a pcmcia flash disk card. The card is formatted with an ext2
fs which resides on /dev/hde1 when the card is inserted. The
card contains at least the encrypted swap setup in a file
named "swapkey". /etc/fstab of your initrd contains something
like the following::

  /dev/hda1   /mnt    ext3      ro                            0 0
  none        /proc   proc      defaults,noatime,nodiratime   0 0
  none        /sys    sysfs     defaults,noatime,nodiratime   0 0

/dev/hda1 contains an unencrypted mini system that sets up all
of your crypto devices, again by reading the setup from the
pcmcia flash disk. What follows now is a /linuxrc for your
initrd that allows you to resume from encrypted swap and that
continues boot with your mini system on /dev/hda1 if resume
does not happen::

linuxrc 원문과 실행 단계

71-118

Script는 `/proc`과 `/sys`를 mount하고 `mapped=0`으로 시작합니다. Kernel command line에 `noresume`이 있거나 init에 `single`, `emergency` 같은 인수가 전달되면 resume하지 않도록 표시합니다.

`cardmgr`를 시작하고 busybox `msh`에 `let`이 없어 1부터 0까지 명시한 loop로 PCMCIA media를 기다립니다. `/dev/hde1`을 read-only로 mount해 `/mnt/swapkey`가 있으면 `dmsetup create swap0`을 실행하고 성공 시 `mapped=1`로 둡니다.

Mapping이 있고 resume을 원하지 않으면 `mkswap /dev/mapper/swap0`으로 image를 초기화합니다. 이어 `echo 254:0 > /sys/power/resume`으로 첫 device-mapper device에서 resume을 시도하고 mapping을 제거합니다.

Resume이 시작되지 않으면 `/sys`를 unmount하고 mini system을 mount한 뒤 `pivot_root`, `chroot`를 거쳐 실제 init을 실행합니다. 원문 shell script 전체는 아래 원문 블록에 그대로 보존됩니다.

linuxrc 제어 흐름
mount proc/sysdetect noresume or init argswait for key mediadmsetup create swap0optional mkswapecho 254:0 > /sys/power/resumeremove mappingpivot_root + chroot init

Resume 성공 시 echo 지점에서 복원되고, 실패하면 mini system boot로 진행합니다.

  #!/bin/sh
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  mount /proc
  mount /sys
  mapped=0
  noresume=`grep -c noresume /proc/cmdline`
  if [ "$*" != "" ]
  then
    noresume=1
  fi
  dmesg -n 1
  /sbin/cardmgr -q
  for i in 1 2 3 4 5 6 7 8 9 0
  do
    if [ -f /proc/ide/hde/media ]
    then
      usleep 500000
      mount -t ext2 -o ro /dev/hde1 /mnt
      if [ -f /mnt/swapkey ]
      then
        dmsetup create swap0 /mnt/swapkey > /dev/null 2>&1 && mapped=1
      fi
      umount /mnt
      break
    fi
    usleep 500000
  done
  killproc /sbin/cardmgr
  dmesg -n 6
  if [ $mapped = 1 ]
  then
    if [ $noresume != 0 ]
    then
      mkswap /dev/mapper/swap0 > /dev/null 2>&1
    fi
    echo 254:0 > /sys/power/resume
    dmsetup remove swap0
  fi
  umount /sys
  mount /mnt
  umount /proc
  cd /mnt
  pivot_root . mnt
  mount /proc
  umount -l /mnt
  umount /proc
  exec chroot . /sbin/init $* < dev/console > dev/console 2>&1

Resume 시도와 fallback의 핵심

119-140

이상한 형태의 loop는 busybox `msh`가 `let` 문을 지원하지 않기 때문입니다. 첫 판단은 resume 시도 여부이며 command line에 `noresume` 또는 `single`, `emergency` 같은 init parameter가 있으면 시도하지 않습니다.

외부 PCMCIA data로 dm-crypt를 구성한 뒤 resume하지 않을 경우 swap device를 reset합니다. `echo 254:0 > /sys/power/resume`은 첫 device-mapper device에서 resume을 시도합니다.

Resume 여부와 무관하게 `/sys/power/resume`에 device를 설정하는 것이 중요합니다. 생략하면 나중의 suspend가 실패합니다. Image resume이 시작되면 script는 그 지점에서 더 진행하지 않습니다.

Resume하지 않으면 encrypted swap mapping을 제거하고 `/dev/hda1` mini system이 전체 crypto setup을 맡게 합니다. 이후 root filesystem을 바꾸고 boot를 계속하며, initrd를 먼저 unmount할지는 구현자가 조정할 수 있습니다.

linuxrc 결과
조건결과
Valid image + resume 허용swsusp restore 시작, script 종료
noresume 또는 init 인수mkswap 후 일반 boot
image 없음/실패mapping 제거, /dev/hda1 mini system으로 전환

Resume image 유무와 boot parameter에 따라 두 경로로 나뉩니다.

Please don't mind the weird loop above, busybox's msh doesn't know
the let statement. Now, what is happening in the script?
First we have to decide if we want to try to resume, or not.
We will not resume if booting with "noresume" or any parameters
for init like "single" or "emergency" as boot parameters.

Then we need to set up dmcrypt with the setup data from the
pcmcia flash disk. If this succeeds we need to reset the swap
device if we don't want to resume. The line "echo 254:0 > /sys/power/resume"
then attempts to resume from the first device mapper device.
Note that it is important to set the device in /sys/power/resume,
regardless if resuming or not, otherwise later suspend will fail.
If resume starts, script execution terminates here.

Otherwise we just remove the encrypted swap device and leave it to the
mini system on /dev/hda1 to set the whole crypto up (it is up to
you to modify this to your taste).

What then follows is the well known process to change the root
file system and continue booting from there. I prefer to unmount
the initrd prior to continue booting but it is up to you to modify
this.