summaryrefslogtreecommitdiff
path: root/update-initramfs
blob: bd5a4ab1ca529be9389db16afe0def8c967cbac9 (plain)
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
#!/bin/sh

STATEDIR=/var/lib/initramfs-tools
BOOTDIR=/boot

set -e

usage()
{
	if [ -n "${1}" ]; then
		printf "${@}\n\n" >&2
	fi
	cat >&2 << EOF
Usage: ${0} [OPTION]...

Options:
 -k [version]	Specify kernel version or ALL
 -c		Create a new initramfs
 -u		Update an existing initramfs
 -d		Remove an existing initramfs
 -t		Take over a custom initramfs with this one
 -v		Be verbose
 -h		This message

EOF
	exit 1
}

mild_panic()
{
	if [ -n "${1}" ]; then
		printf "${@}\n" >&2
	fi
	exit 0
}

panic()
{
	if [ -n "${1}" ]; then
		printf "${@}\n" >&2
	fi
	exit 1
}

verbose()
{
	if [ "${verbose}" = 1 ]; then
		printf "${@}\n"
	fi
}

version_exists()
{
	[ -e "${STATEDIR}/${1}" ]
	return $?
}

set_initramfs()
{
	initramfs="${BOOTDIR}/initrd.img-${version}"
}

generate_initramfs()
{
	verbose "Generating ${initramfs}"
	mkinitramfs -o ${initramfs} ${version}
	set_sha1
}

compare_sha1()
{
	sha1sum ${initramfs} | diff ${STATEDIR}/${version} - >/dev/null 2>&1
	return $?
}

# Note that this must overwrite so that updates work.
set_sha1()
{
	sha1sum ${initramfs} > ${STATEDIR}/${version}
}

delete_sha1()
{
	rm -f ${STATEDIR}/${version}
}

get_sorted_versions()
{
	version_list=""

	for gsv_x in ${STATEDIR}/*; do
		gsv_x=$(basename ${gsv_x})
		if [ "${gsv_x}" = '*' ]; then
			verbose "Nothing to do, exiting."
			exit 0
		fi
		worklist=""
		for gsv_i in $version_list; do
			if dpkg --compare-versions "${gsv_x}" '>' "${gsv_i}"; then
				worklist="${worklist} ${gsv_x} ${gsv_i}"
				gsv_x=""
			else
				worklist="${worklist} ${gsv_i}"
			fi
		done
		if [ "${gsv_x}" != "" ]; then
			worklist="${worklist} ${gsv_x}"
		fi
		version_list=${worklist}
	done

	verbose "Available versions: ${version_list}"
}

set_linked_version()
{
	if [ -L /initrd.img ]; then
		linktarget=$(basename $(readlink /initrd.img))
	fi

	if [ -L /boot/initrd.img ]; then
		linktarget=$(basename $(readlink /boot/initrd.img))
	fi

	if [ -z "${linktarget}" ]; then
		return
	fi

	version="${linktarget##initrd.img-}"
}

set_highest_version()
{
	get_sorted_versions
	set - ${version_list}
	version=${1}
}

create()
{
	if [ -z "${version}" ]; then
		usage "Create mode requires a version argument"
	fi

	set_initramfs

	if [ "${takeover}" = 0 ]; then
		if version_exists ${version}; then
			panic "Cannot create version ${version}: already exists"
		fi
	
		if [ -e ${initramfs} ]; then
			panic "${initramfs} already exists, cannot create."
		fi
	fi

	generate_initramfs
}

update()
{
	if [ -z "${version}" ]; then
		set_linked_version
	fi

	if [ -z "${version}" ]; then
		set_highest_version
	fi

	if [ "${version}" = "all" ]; then
		: FIXME check for --yes, and if not ask are you sure
		get_sorted_versions
		for u_version in ${version_list}; do
			if [ "${verbose}" = "1" ]; then
				vflag="-v"
			fi
			# Don't stop if one version doesn't work.
			set +e
			${0} ${vflag} -u -k ${u_version}
			set -e
		done
		exit 0
	fi

	set_initramfs

	altered_check

	generate_initramfs

}

delete()
{
	if [ -z "${version}" ]; then
		usage "Delete mode requires a version argument"
	fi

	set_initramfs

	if [ ! -e ${initramfs} ]; then
		panic "Cannot delete ${initramfs}, doesn't exist."
	fi
	
	if ! version_exists ${version}; then
		panic "Cannot delete version ${version}: Not created by this utility."
	fi

	altered_check

	delete_sha1

	rm -f "${initramfs}"
}


altered_check()
{
	if [ "${takeover}" = 0 ]; then
		if ! compare_sha1; then
			delete_sha1
			mild_panic "${initramfs} was been altered.  Cannot update."
		fi
	fi
}

# Defaults
verbose=0
yes=0
takeover=0

##

while getopts "k:cudyvht?" flag; do
	case "${flag}" in
	k)
		version="${OPTARG}"	
		;;
	c)
		mode="c"
		;;
	d)
		mode="d"
		;;
	u)
		mode="u"
		;;
	v)
		verbose="1"
		;;
	y)
		yes="1"
		;;
	t)
		takeover="1"
		;;
	h|?)
		usage
		;;
	esac
done

# Validate arguments

if [ -z "${mode}" ]; then
	usage "You must specify at least one of -c, -u, or -d."
fi

case "${mode}" in
	c)
		create
		;;
	d)
		delete
		;;
	u)
		update
		;;
esac