Debian Codegraph @

debian/scripts/suspend/cryptsetup-suspend-wrapper

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`.
2
3 # Wrapper for cryptsetup-suspend(7)
4 #
5 # Copyright © 2019-2020 Tim <[email protected]>
6 # © 2019-2020 Jonas Meurer <[email protected]>
7 # © 2020-2022 Guilhem Moulin <[email protected]>
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 set -ue
23 PATH="/usr/sbin:/usr/bin:/sbin:/binscip-shell filesystem . . `/usr/sbin:/usr/bin:/sbin:/bin`."
24 export PATH
25
26 # import cryptsetup shell functions
27 [scip-shell system . . `[`. -f /lib/cryptsetup/functionsscip-shell filesystem . . `/lib/cryptsetup/functions`. ] || return 0
28 . /lib/cryptsetup/functionsscip-shell source . . `/lib/cryptsetup/functions`.
29
30 INITRAMFS_MNT="/run/cryptsetup/cryptsetup-suspend-initramfsscip-shell filesystem . . `/run/cryptsetup/cryptsetup-suspend-initramfs`."
31 SYSTEM_SLEEP_PATH="/lib/systemd/system-sleepscip-shell filesystem . . `/lib/systemd/system-sleep`."
32 CONFIG_FILE="/etc/cryptsetup/suspend.confscip-shell filesystem . . `/etc/cryptsetup/suspend.conf`."
33 unset -v INITRAMFS_DIR
34
35 read_configscip-shell shell shell-project 0.0.0 read_config().() {
36 # define defaults
37 export UNLOCK_SESSIONS="false"
38 export KEEP_INITRAMFS="false"
39
40 # read config file if it exists
41 # shellcheck source=/etc/cryptsetup/suspend.conf
42 [scip-shell system . . `[`. -f "$CONFIG_FILE" ] && . "$CONFIG_FILE" || truescip-shell system . . true.
43 }
44
45 # run_dir ARGS...
46 # Run all executable scripts in directory SYSTEM_SLEEP_PATH with arguments ARGS
47 # mimic systemd behavior
48 run_dirscip-shell shell shell-project 0.0.0 run_dir().() {
49 local p
50 [scip-shell system . . `[`. -d "$SYSTEM_SLEEP_PATH" ] || return 0
51 for p in "$SYSTEM_SLEEP_PATH"/*; do
52 if [scip-shell system . . `[`. -x "$p" ]; then
53 "$p" "$@" || return $?
54 fi
55 done
56 }
57
58 log_errorscip-shell shell shell-project 0.0.0 log_error().() {
59 # arg1 should be message
60 echoscip-shell system . . echo. "Error: $1" | systemd-cat -t cryptsetup-suspend -p err
61 echoscip-shell system . . echo. "Error: $1" >&2
62 }
63
64 mount_initramfsscip-shell shell shell-project 0.0.0 mount_initramfs().() {
65 local k v u IFS MemAvailable=0 SwapFree=0 new="n"
66 # update-initramfs(8) hardcodes /boot also: there is a `-b bootdir`
67 # option but no config file to put it to
68 local INITRAMFS="/boot/initrd.img-$(unamescip-shell system . . uname. -r)" p
69 if [scip-shell system . . `[`. ! -f "$INITRAMFS" ]; then
70 log_errorscip-shell shell shell-project 0.0.0 log_error(). "No initramfs found at $INITRAMFS"
71 exit 1
72 fi
73
74 if [scip-shell system . . `[`. -d "$INITRAMFS_MNT" ] && [scip-shell system . . `[`. ! "$INITRAMFS" -ot "$INITRAMFS_MNT" ]; then
75 # need to unpack again: initramfs is newer than what we unpacked earlier
76 if mountpointscip-shell system . . mountpoint. -q "$INITRAMFS_MNT"; then
77 umountscip-shell system . . umount. "$INITRAMFS_MNT"
78 fi
79 rmdirscip-shell system . . rmdir. "$INITRAMFS_MNT" || exit 1
80 fi
81
82 if [scip-shell system . . `[`. ! -d "$INITRAMFS_MNT" ]; then
83 # we need at about 300 MiB on ubuntu, 200 on debian
84 # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
85 while IFS=" " read -r k v u; do
86 # /proc/meminfo format is documented in proc(5)
87 case "$u" in
88 MB) u=1048576;;
89 kB) u=1024;;
90 *) u=1;;
91 esac
92 case "$k" in
93 "MemAvailable:") MemAvailable=$((v*u));;
94 "SwapFree:") SwapFree=$((v*u));;
95 esac
96 done </proc/meminfoscip-shell filesystem . . `/proc/meminfo`.
97 if [scip-shell system . . `[`. $((MemAvailable+SwapFree)) -lt $((300*1024*1024)) ]; then
98 log_errorscip-shell shell shell-project 0.0.0 log_error(). "Not enough memory available. Please close some programs or add swap space to suspend successfully."
99 exit 1
100 fi
101
102 mkdirscip-shell system . . mkdir. -m0700 "$INITRAMFS_MNT"
103 mountscip-shell system . . mount. -t ramfs -o nodev,mode=0700 ramfs "$INITRAMFS_MNT"
104
105 # extract initrd.img to initramfs dir
106 unmkinitramfs "$INITRAMFS" "$INITRAMFS_MNT"
107 new="y"
108 fi
109
110 INITRAMFS_DIR="$INITRAMFS_MNT"
111 if [scip-shell system . . `[`. ! -f "$INITRAMFS_DIR/sbin/cryptsetup" ]; then
112 # unmkinitramfs(8) extracts microcode and actual initramfs into different folders
113 for p in "$INITRAMFS_MNT"/*/sbin/cryptsetup; do
114 if [scip-shell system . . `[`. -f "$p" ] && [scip-shell system . . `[`. -d "${p%"/sbin/cryptsetup"}/usr" ]; then
115 INITRAMFS_DIR="${p%"/sbin/cryptsetup"}"
116 fi
117 done
118 if [scip-shell system . . `[`. ! -f "$INITRAMFS_DIR/sbin/cryptsetup" ]; then
119 log_errorscip-shell shell shell-project 0.0.0 log_error(). "Directory $INITRAMFS_MNT has unexpected content" >&2
120 exit 1
121 fi
122 fi
123
124 if [scip-shell system . . `[`. "$new" = "y" ]; then
125 for p in /devscip-shell filesystem . . `/dev`. /procscip-shell filesystem . . `/proc`. /runscip-shell filesystem . . `/run`. /sysscip-shell filesystem . . `/sys`.; do
126 if [scip-shell system . . `[`. ! -d "$INITRAMFS_DIR$p" ]; then
127 mkdirscip-shell system . . mkdir. -m0755 "$INITRAMFS_DIR$p"
128 fi
129 done
130
131 # copy our binary to ramdisk
132 installscip-shell system . . install. -m0755 -t "$INITRAMFS_DIR/bin" /lib/cryptsetup/scripts/suspend/cryptsetup-suspendscip-shell filesystem . . `/lib/cryptsetup/scripts/suspend/cryptsetup-suspend`.
133
134 # copy all firmware files to ramdisk to prevent dead-lock
135 # see https://salsa.debian.org/mejo/cryptsetup-suspend/issues/38)
136 # TODO we should try to identify which firmwares need to be loaded
137 # and only copy those
138 if [scip-shell system . . `[`. -d /lib/firmwarescip-shell filesystem . . `/lib/firmware`. ] && [scip-shell system . . `[`. ! -d "$INITRAMFS_DIR/lib/firmware" ]; then
139 cpscip-shell system . . cp. -dR -T -- /lib/firmwarescip-shell filesystem . . `/lib/firmware`. "$INITRAMFS_DIR/lib/firmware"
140 fi
141 fi
142
143 # from initramfs-tools-core's /usr/share/initramfs-tools/init
144 mountscip-shell system . . mount. -t devtmpfs -o noexec,nosuid,mode=0755 udev "$INITRAMFS_DIR/dev"
145 mountscip-shell system . . mount. -t proc -o nodev,noexec,nosuid proc "$INITRAMFS_DIR/proc"
146 mountscip-shell system . . mount. -t ramfs -o nodev,noexec,nosuid,mode=0755 ramfs "$INITRAMFS_DIR/run"
147 mountscip-shell system . . mount. -t sysfs -o nodev,noexec,nosuid sysfs "$INITRAMFS_DIR/sys"
148
149 [scip-shell system . . `[`. -d "$INITRAMFS_DIR/dev/pts" ] || mkdirscip-shell system . . mkdir. -m0755 "$INITRAMFS_DIR/dev/pts"
150 mountscip-shell system . . mount. -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts "$INITRAMFS_DIR/dev/pts" || truescip-shell system . . true.
151
152 # remount read-only, private and unbindable
153 mountscip-shell system . . mount. -oremount,ro --make-rprivate --make-runbindable "$INITRAMFS_MNT"
154 }
155
156 umount_initramfsscip-shell shell shell-project 0.0.0 umount_initramfs().() {
157 if [scip-shell system . . `[`. -d "${INITRAMFS_DIR-}" ]; then
158 umountscip-shell system . . umount. -- "$INITRAMFS_DIR/dev/pts"
159 umountscip-shell system . . umount. -- "$INITRAMFS_DIR/dev"
160 umountscip-shell system . . umount. -- "$INITRAMFS_DIR/proc"
161 umountscip-shell system . . umount. -- "$INITRAMFS_DIR/run"
162 umountscip-shell system . . umount. -- "$INITRAMFS_DIR/sys"
163 fi
164 if [scip-shell system . . `[`. "$KEEP_INITRAMFS" != "true" ] || [scip-shell system . . `[`. -z "${INITRAMFS_DIR+x}" ]; then
165 # always unmount if we error out before setting INITRAMFS_DIR
166 umountscip-shell system . . umount. -- "$INITRAMFS_MNT"
167 rmdirscip-shell system . . rmdir. -- "$INITRAMFS_MNT"
168 fi
169 }
170
171 CGROUP_FREEZER=
172 freeze_cgroupscip-shell shell shell-project 0.0.0 freeze_cgroup().() {
173 local c="$1" v
174 # freeze cgroup if non-frozen
175 if [scip-shell system . . `[`. -f "$c" ] && v="$(catscip-shell system . . cat. <"$c")" && [scip-shell system . . `[`. $v -eq 0 ]; then
176 echoscip-shell system . . echo. 1 >"$c"
177 CGROUP_FREEZER="$c${CGROUP_FREEZER:+" $CGROUP_FREEZER"}"
178 fi
179 }
180 freeze_cgroupsscip-shell shell shell-project 0.0.0 freeze_cgroups().() {
181 local mycgroup c
182
183 # freeze all machines/containers and user cgroups
184 freeze_cgroupscip-shell shell shell-project 0.0.0 freeze_cgroup(). "$hierarchy/machine.slice/cgroup.freeze"
185 freeze_cgroupscip-shell shell shell-project 0.0.0 freeze_cgroup(). "$hierarchy/user.slice/cgroup.freeze"
186
187 # get my second level cgroup
188 mycgroup="$(grepscip-shell system . . grep. -m1 "^0::" /proc/self/cgroupscip-shell filesystem . . `/proc/self/cgroup`. | cutscip-shell system . . cut. -sd/ -f3)"
189
190 # freeze all system cgroups except ours and systemd-suspend
191 for c in "$hierarchy"/system.slice/*/cgroup.freeze; do
192 if [scip-shell system . . `[`. "$c" != "$hierarchy/system.slice/$mycgroup/cgroup.freeze" ] && \
193 [ "${c#"$hierarchy/system.slice/systemd-suspend."}" = "$c" ]; then
194 freeze_cgroupscip-shell shell shell-project 0.0.0 freeze_cgroup(). "$c"
195 fi
196 done
197
198 # freeze systemd itself
199 freeze_cgroupscip-shell shell shell-project 0.0.0 freeze_cgroup(). "$hierarchy/init.scope/cgroup.freeze"
200 }
201
202 thaw_cgroupsscip-shell shell shell-project 0.0.0 thaw_cgroups().() {
203 local c
204 for c in $CGROUP_FREEZER; do
205 echoscip-shell system . . echo. 0 >"$c"
206 done
207 }
208
209 populate_ACTIVE_DEVICESscip-shell shell shell-project 0.0.0 populate_ACTIVE_DEVICES().() {
210 local DEV MAJ MIN
211 if ! dm_blkdevname "$CRYPTTAB_NAME" >/dev/nullscip-shell filesystem . . `/dev/null`.; then
212 # silently ignore unmapped devices
213 return 0
214 elif [scip-shell system . . `[`. "$(dmsetup info --noheadings -c -o subsystem -- "$CRYPTTAB_NAME")" != "CRYPT" ]; then
215 cryptsetup_message "ERROR: $CRYPTTAB_NAME: Subsystem mismatch"
216 return 1
217 elif ! _resolve_device "$CRYPTTAB_SOURCE"; then
218 cryptsetup_message "ERROR: $CRYPTTAB_NAME: Missing source $CRYPTTAB_SOURCE"
219 return 1
220 elif [scip-shell system . . `[`. "$(dmsetup info -c --noheadings -o devnos_used -- "$CRYPTTAB_NAME" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" != "$MAJ:$MIN" ]; then
221 cryptsetup_message "ERROR: $CRYPTTAB_NAME: Source mismatch"
222 return 1
223 fi
224
225 if ! crypttab_parse_options --quiet; then
226 cryptsetup_message "ERROR: $CRYPTTAB_NAME: Unable to parse options field"
227 return 1
228 elif [scip-shell system . . `[`. "$CRYPTTAB_TYPE" != "luks" ]; then
229 # XXX does it even work with detached headers?
230 cryptsetup_message "WARNING: $CRYPTTAB_NAME: unable to suspend non-LUKS device"
231 return 0
232 fi
233
234 # XXX that's not robust since $CRYPTTAB_NAME might contain spaces or
235 # special characters; we need to create a NUL-delimited list in a
236 # file instead
237 ACTIVE_DEVICES="${ACTIVE_DEVICES:+"$ACTIVE_DEVICES "}$CRYPTTAB_NAME"
238 }
239
240 clean_upscip-shell shell shell-project 0.0.0 clean_up().() {
241 # we always want to run through the whole cleanup
242 set +e
243
244 # thaw all frozen cgroups
245 thaw_cgroupsscip-shell shell shell-project 0.0.0 thaw_cgroups().
246
247 # Run post-suspend scripts
248 run_dirscip-shell shell shell-project 0.0.0 run_dir(). post suspend
249
250 umount_initramfsscip-shell shell shell-project 0.0.0 umount_initramfs().
251
252 # unlock sessions
253 if [scip-shell system . . `[`. "$UNLOCK_SESSIONS" = "true" ]; then
254 loginctl unlock-sessions
255 fi
256 }
257
258 ## Main script
259
260 # check unified cgroups hierarchy
261 # https://github.com/systemd/systemd/blob/master/docs/CGROUP_DELEGATION.md
262 if [scip-shell system . . `[`. -d /sys/fs/cgroup/system.slicescip-shell filesystem . . `/sys/fs/cgroup/system.slice`. ]; then
263 hierarchy="/sys/fs/cgroupscip-shell filesystem . . `/sys/fs/cgroup`."
264 elif [scip-shell system . . `[`. -d /sys/fs/cgroup/unified/system.slicescip-shell filesystem . . `/sys/fs/cgroup/unified/system.slice`. ]; then
265 # hybrid cgroup hierarchy
266 hierarchy="/sys/fs/cgroup/unifiedscip-shell filesystem . . `/sys/fs/cgroup/unified`."
267 else
268 log_errorscip-shell shell shell-project 0.0.0 log_error(). "No unified cgroups hierarchy"
269 exit 1
270 fi
271
272 # check that not run as user
273 # XXX: We should catch also cases where libpam-systemd is not installed
274 if grepscip-shell system . . grep. -Eq '^[0-9]+:[^:]*:/user\.slice/' /proc/self/cgroupscip-shell filesystem . . `/proc/self/cgroup`.; then
275 log_errorscip-shell shell shell-project 0.0.0 log_error(). "Don't run this script as user"
276 exit 1
277 fi
278
279 # always thaw cgroups, re-mount filesystems and remove initramfs at the end of the script
280 trap clean_up EXIT
281
282 read_configscip-shell shell shell-project 0.0.0 read_config().
283
284 # extract temporary filesystem to switch to
285 mount_initramfsscip-shell shell shell-project 0.0.0 mount_initramfs().
286
287 # Run pre-suspend scripts
288 run_dirscip-shell shell shell-project 0.0.0 run_dir(). pre suspend
289
290 # populate list of active crypt devices
291 ACTIVE_DEVICES=""
292 crypttab_foreach_entry populate_ACTIVE_DEVICES
293
294 # freeze all cgroups but us
295 freeze_cgroupsscip-shell shell shell-project 0.0.0 freeze_cgroups().
296
297 # No longer fail in case of errors
298 set +e
299
300 # change into ramdisk
301 devices_remaining="$(chrootscip-shell system . . chroot. "$INITRAMFS_DIR" /bin/shscip-shell filesystem . . `/bin/sh`. -c "
302 # suspend active luks devices (in reverse order) and system
303 /bin/cryptsetup-suspend --reverse $ACTIVE_DEVICES
304
305 TABFILE=\"/cryptroot/crypttab\"
306 . /lib/cryptsetup/functions
307
308 # resume active luks devices (only initramfs devices)
309 for dev in $ACTIVE_DEVICES; do
310 if crypttab_find_entry --quiet \"\$dev\"; then
311 DM_DISABLE_UDEV=y resume_device \"\$dev\" || sleep 5
312 else
313 # write remaining devices to FD3
314 printf \"%s \" \"\$dev\" >&3
315 fi
316 done
317 " 3>&- 3>&1 >&2)"
318
319 # resume remaining active luks devices (non-initramfs devices)
320 for dev in $devices_remaining; do
321 if crypttab_find_entry --quiet "$dev"; then
322 # explicitely disable udev support, cf. #1020553
323 # XXX this is not ideal since udev might be required in some situations
324 # (detached header or key material on removable device comes to mind)
325 DM_DISABLE_UDEV=y resume_device "$dev" || truescip-shell system . . true.
326 else
327 log_errorscip-shell shell shell-project 0.0.0 log_error(). "'$dev' not found in /etc/crypttab"
328 fi
329 done