File: uswsusp.config

package info (click to toggle)
uswsusp 1.0%2B20120915-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 1,440
  • sloc: ansic: 7,164; sh: 566; makefile: 223; perl: 65
file content (319 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
set -e

# uswsusp.config -- configure script for uswsusp debian package
#
# Copyright 2006, 2007	Tim Dijkstra <tim@famdijkstra.org>
# Copyright 2012	Rodolfo García Peñas <kix@kix.es>
#  04-2012 Full rewrited
# Released under GPLv2
CONFIGNAME=uswsusp.conf
CONFIGFILE=/etc/$CONFIGNAME
SWAPOFFSET=/usr/sbin/swap-offset

# If we don't have /proc or /sys we can't work, this will probably
# be a chroot or so.
function check_filesystems
{
	mountpoint -q /sys || {
		echo "/sys not mounted. Can't create $CONFIGNAME" >> /dev/stderr;
		exit 0; }
	mountpoint -q /proc || {
		echo "/proc not mounted. Can't create $CONFIGNAME" >> /dev/stderr;
		exit 0; }
}

# This function returns the full path in /dev/disk/by-uuid
# for a given /dev device. If not found, returns the /dev device.
function dev_to_uuid
{
	local link
	local path
	local uuid

	if [ -d /dev/disk/by-uuid ]; then
		uuid=$tmpswap
		for path in /dev/disk/by-uuid/*; do
			link=$(readlink -f "$path")
			if [ "$link" = "$tmpswap" ]; then
				uuid=${path}
				break;
			fi
		done

		tmpswap=$uuid
	fi
}

function get_list_of_swaps
{
	SWAPLIST=
	SWAPFIRST=
	KOMMA=
	tmpswap=

	# Swap file support is from 2.6.20
	if dpkg --compare-versions `uname -r` lt 2.6.20; then
		NO_FILE=' && $2!="file"'
	fi

	# List of swaps, listed by size
	for tmpswap in `sort -nr -k 3 /proc/swaps | awk '$2!="Type" '"$NO_FILE"' {print $1}'`; do
		dev_to_uuid
		SWAPLIST=${SWAPLIST}${KOMMA}${tmpswap}

		if [ -z "$KOMMA" ]; then
			SWAPFIRST=${tmpswap}
			KOMMA=", "
		fi
	done
}

# This function checks snapshot support
function check_snapshot_support
{
	if [ ! -e /sys/class/misc/snapshot/dev ]; then
		db_input critical uswsusp/no_snapshot || true
		db_fset uswsusp/no_snapshot hit true
		db_go || true
		exit 0
	fi
}

# This function sets the max size of image
function set_image_size
{
	IMAGESIZE=`awk '$1 == "MemTotal:" {printf("%lu\n", $2*1024*0.46)}' /proc/meminfo 2> /dev/null`
	if [ -n "$IMAGESIZE" ]; then
		db_set uswsusp/image_size $IMAGESIZE
	fi
}

# Read the configuration file
function read_config_file
{
# Luckily the parser in s2disk is quite strict
	for i in $LOWQ $MEDQ $HIGHQ $NOTQ; do
		VAL=`sed -n 's/^[[:space:]]*'"${i//_/ }"'[[:space:]]*[=:][[:space:]]*\([^[:space:]]*\)/\1/ p' $CONFIGFILE`

		# For boolean values it only checks for [yY]
		db_metaget uswsusp/${i} type
		TYPE=$RET

		if [ "boolean" = "$TYPE" ]; then
			if [ "$VAL" = "y" -o "$VAL" = "Y" ];  then
				db_set uswsusp/${i} true
			else
				db_set uswsusp/${i} false
			fi
		else
			db_set uswsusp/${i} "$VAL"
		fi
	done
}

function get_partition_to_use
{
	if [ -z "$USERSWAP" ]; then
		# The user didn't set one: Set the first as default
		SWAPDEFAULT=$SWAPFIRST
	elif echo "$SWAPLIST" | grep -q -e '\(^\|, \)'$USERSWAP'\(,\|$\)'; then
		# Valid swap partition
		SWAPDEFAULT=$USERSWAP
	elif [ -n "$USEROFFSET" ] && [ -x $SWAPOFFSET ]; then
		# Offset was specified
		# If we don't have swap-offset, this probably means uswsusp
		# was removed before, leaving the config file. Now we don't
		# have a way to check the offset. Best is to ask if this is OK

		devid=`printf "%x%02x" $(stat -c "0x%t 0x%T" $USERSWAP 2> /dev/null)`

		while read name type rest; do
			[ "$type" == "file" ]  || continue;
			[ "$(stat -c '%D' $name)" == "$devid" ] || continue;
			[ "$USEROFFSET" == "$(swap-offset $name | cut -c17- )" ] || continue;

			SWAPDEFAULT=$name
			break
		done < /proc/swaps
	fi
}

# Some people want to use a swap partition that is only mounted
# during suspend. This means we can't find it in /proc/swaps, but
# it is a valid option non the less. To not remove their changes
# we add it to the SWAPLIST, but not after we confirmed this is
# what they want.
function get_partition_mounted_to_use
{
	# This can be:
	#   Offset is given, no sbin/swap-offset
	#   Offset is given, USERSWAP:USEROFFSET not in /proc/swaps
	#   No offset, USERSWAP not in /proc/swaps
	db_input critical uswsusp/continue_without_swap || true
	db_get uswsusp/continue_without_swap
	if [ "$RET" = "true" ]; then
		SWAPLIST=${SWAPLIST}${KOMMA}${USERSWAP} ;
		SWAPDEFAULT=${USERSWAP}
		db_fset uswsusp/continue_without_swap hit true
	fi
}

function setup_priority_questions
{
	for i in $LOWQ; do
		db_input low uswsusp/${i} || true
	done

	for i in $MEDQ; do
		db_input medium uswsusp/${i} || true
	done

	for i in $HIGHQ; do
		db_input high uswsusp/${i} || true
	done

	# Ask questions
	db_go || true
}

function set_encryption
{
	# First ask filename
	while [ 1 ] ; do
		db_input low uswsusp/RSA_key_file || true
		db_go || true
		db_get uswsusp/RSA_key_file || true
		KEYFILE=$RET

		if [ -n "$KEYFILE" ]; then
			break
		fi

		db_reset uswsusp/RSA_key_file
	done

	# Then ask if we should generate it, default to yes if they don't have one yet
	if [ -e "$KEYFILE" ]; then
		db_set uswsusp/create_RSA_key false
	else
		db_set uswsusp/create_RSA_key true
	fi

	db_input low uswsusp/create_RSA_key || true
	db_go || true

	# If they want it created, ask nr bits and passphrase (twice)
	db_get uswsusp/create_RSA_key
	if [ "$RET" = "true" ]; then
		while [ 1 ]; do
			db_input low uswsusp/RSA_key_bits || true
			db_go || true
			db_get uswsusp/RSA_key_bits

			if [ -z "$RET" ] || [ $RET -ge 1024 -a $RET -le 4096 ]; then
				break;
			fi

			db_reset uswsusp/RSA_key_bits
		done

		P1=A; P2=B;

		while [ "$P1" != "$P2" ]; do
			db_input critical uswsusp/RSA_passphrase || true
			db_input critical uswsusp/RSA_passphrase_v || true
			db_go || true

			db_get uswsusp/RSA_passphrase
			if [ -n "$RET" ]; then
				P1=$RET;
			fi

			db_get uswsusp/RSA_passphrase_v
			if [ -n "$RET" ]; then
				P2=$RET;
			fi
		done
	fi
}

# MAIN SCRIPT #
. /usr/share/debconf/confmodule

LOWQ="snapshot_device compute_checksum compress early_writeout image_size suspend_loglevel max_loglevel shutdown_method"
MEDQ="resume_device encrypt"
HIGHQ=""
NOTQ="RSA_key_file shutdown_method resume_offset"

# Check if filesystems /proc and /sys are mounted
check_filesystems

# Reset these flags
db_fset uswsusp/no_swap hit false
db_fset uswsusp/no_snapshot hit false
db_fset uswsusp/continue_without_swap hit false

# Try to detect snapshot support
check_snapshot_support

# Get the list of swap devices
get_list_of_swaps

# Set the maximum size for image
set_image_size

# If config file, read it
if [ -e "$CONFIGFILE" ]; then
	read_config_file
fi

# Check resume_device and resume_offset from the config file
db_get uswsusp/resume_device
# If the file is a device file, translate to uuid file
# Else we can get an error about file is not active.
# debconf error uswsusp/continue_without_swap
tmpswap=$RET
dev_to_uuid
USERSWAP=$tmpswap

db_get uswsusp/resume_offset
USEROFFSET=$RET

# Get the swap partition to use
get_partition_to_use

# Some people want to use a swap partition that is only mounted
# during suspend. This means we can't find it in /proc/swaps, but
# it is a valid option non the less. To not remove their changes
# we add it to the SWAPLIST, but not after we confirmed this is
# what they want.
if [ -z "$SWAPDEFAULT" ]; then
	get_partition_mounted_to_use
fi

# If we still do not have a SWAPLIST, something is wrong
if [ -z "$SWAPLIST" ]; then
	db_input critical uswsusp/no_swap || true
	db_fset uswsusp/no_swap hit true
	db_go || true
	exit 0
fi
db_subst uswsusp/resume_device list $SWAPLIST
db_set uswsusp/resume_device $SWAPDEFAULT

# If we're still here, reset the seen flags on error messages
# Maybe they had problems before, but not anymore...
db_fset uswsusp/no_swap seen false
db_fset uswsusp/no_snapshot seen false

# Stetup questions according to  priority
setup_priority_questions

# Only if they want encryption, ask the RSA question
db_get uswsusp/encrypt
if [ "$RET" = "false" ]; then
	db_set uswsusp/create_RSA_key false
elif [ "$RET" = "true" ]; then
	set_encryption
fi