Debian Codegraph @

hooks/resume

raw
1 #!/bin/sh
2
3 PREREQ=""
4
5 prereqs()
6 {
7 echo "$PREREQ"
8 }
9
10 case $1 in
11 # get pre-requisites
12 prereqs)
13 prereqs
14 exit 0
15 ;;
16 esac
17
18 . /usr/share/initramfs-tools/scripts/functions
19
20 # First check if a location is set and is a valid swap partition.
21 # If so, the config file will be copied in and there is nothing to do.
22 if [ -n "$RESUME" ] && [ "$RESUME" != auto ]; then
23 if [ "$RESUME" = none ]; then
24 exit 0
25 fi
26 if resume_dev_node="$(resolve_device "$RESUME")" && \
27 resume_dev_type=$(blkid -p -s TYPE -o value "$resume_dev_node") && \
28 [ "$resume_dev_type" = swap ]; then
29 exit 0
30 fi
31
32 echo >&2 "W: initramfs-tools configuration sets RESUME=$RESUME"
33 echo >&2 "W: but no matching swap device is available."
34 fi
35
36 # If we were not explicitly requested to select a device, or the -v
37 # option is used, report that we are doing so
38 report_auto()
39 {
40 test "${verbose?}" != y && test "$RESUME" = auto || echo "I: $*"
41 }
42
43 # Report in excruciating detail if the -v option is used
44 report_verbose()
45 {
46 test "${verbose?}" != y || echo "I: $*"
47 }
48
49 report_verbose "Configuration sets RESUME=$RESUME"
50
51 # Try to autodetect the RESUME partition, using biggest swap device that
52 # is not ephemeral. We need to be able to read the listed swap partitions.
53 resume_auto=
54 if ! ischroot && [ -r /proc/swaps ]; then
55 # shellcheck disable=SC2013
56 for resume_auto in $(grep ^/dev/ /proc/swaps | sort -rnk3 | cut -d " " -f 1); do
57 report_verbose "Checking swap device $resume_auto"
58 ephemeral=false
59 dm_name="$(dmsetup info -c --noheadings -o name "$resume_auto" 2>/dev/null)"
60
61 # dm-crypt is ephemeral if the key file is /dev/urandom
62 if [ -n "$dm_name" ] && [ -e /etc/crypttab ]; then
63 report_verbose "$resume_auto has device-mapper name $dm_name; checking crypttab"
64 # shellcheck disable=SC2034
65 while read -r cryptdev srcdev keyfile junk; do
66 report_verbose "Found cryptdev=$cryptdev keyfile=$keyfile"
67 if [ "$cryptdev" = "$dm_name" ] && [ "$keyfile" = /dev/urandom ]; then
68 report_verbose "Rejecting $resume_auto since it has no permanent key"
69 ephemeral=true
70 fi
71 done < /etc/crypttab
72 fi
73
74 # zram is ephemeral
75 case "$resume_auto" in
76 /dev/zram*)
77 report_verbose "Rejecting $resume_auto since it is zram"
78 ephemeral=true
79 ;;
80
81 /dev/zd[0-9]*)
82 report_verbose "Rejecting $resume_auto since it is zvol"
83 ephemeral=true
84 ;;
85 esac
86
87 $ephemeral || break
88 done
89
90 if $ephemeral; then
91 resume_auto=
92 fi
93 if [ -n "$resume_auto" ]; then
94 if [ -n "$dm_name" ]; then
95 resume_auto_canon="/dev/mapper/$dm_name"
96 elif UUID=$(blkid -s UUID -o value "$resume_auto"); then
97 resume_auto_canon="UUID=$UUID"
98 else
99 resume_auto_canon=
100 fi
101 report_auto "The initramfs will attempt to resume from $resume_auto"
102 if [ -n "$resume_auto_canon" ]; then
103 report_auto "($resume_auto_canon)"
104 resume_auto="$resume_auto_canon"
105 fi
106 report_auto "Set the RESUME variable to override this."
107 fi
108 fi
109
110 # Write selected resume device to intramfs conf.d
111 if [ "$RESUME" = auto ] || [ -n "$resume_auto" ]; then
112 # If we were explicitly requested to select a device, and we failed,
113 # report that
114 if [ -z "$resume_auto" ]; then
115 echo >&2 "W: initramfs-tools failed to select a resume device"
116 fi
117 echo "RESUME=${resume_auto}" > "${DESTDIR}/conf/conf.d/zz-resume-auto"
118 fi