]> git.ipfire.org Git - thirdparty/dracut.git/blob - lsinitrd.sh
fix(integrity): properly set up EVM when using an x509 cert
[thirdparty/dracut.git] / lsinitrd.sh
1 #!/bin/bash
2 #
3 # Copyright 2005-2010 Harald Hoyer <harald@redhat.com>
4 # Copyright 2005-2010 Red Hat, Inc. All rights reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 usage() {
21 {
22 echo "Usage: ${0##*/} [options] [<initramfs file> [<filename> [<filename> [...] ]]]"
23 echo "Usage: ${0##*/} [options] -k <kernel version>"
24 echo
25 echo "-h, --help print a help message and exit."
26 echo "-s, --size sort the contents of the initramfs by size."
27 echo "-m, --mod list modules."
28 echo "-f, --file <filename> print the contents of <filename>."
29 echo "--unpack unpack the initramfs, instead of displaying the contents."
30 echo " If optional filenames are given, will only unpack specified files,"
31 echo " else the whole image will be unpacked. Won't unpack anything from early cpio part."
32 echo "--unpackearly unpack the early microcode part of the initramfs."
33 echo " Same as --unpack, but only unpack files from early cpio part."
34 echo "-v, --verbose unpack verbosely."
35 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
36 echo
37 } >&2
38 }
39
40 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
41
42 sorted=0
43 modules=0
44 unset verbose
45 declare -A filenames
46
47 unset POSIXLY_CORRECT
48 TEMP=$(getopt \
49 -o "vshmf:k:" \
50 --long kver: \
51 --long file: \
52 --long mod \
53 --long help \
54 --long size \
55 --long unpack \
56 --long unpackearly \
57 --long verbose \
58 -- "$@")
59
60 # shellcheck disable=SC2181
61 if (($? != 0)); then
62 usage
63 exit 1
64 fi
65
66 eval set -- "$TEMP"
67
68 while (($# > 0)); do
69 case $1 in
70 -k | --kver)
71 KERNEL_VERSION="$2"
72 shift
73 ;;
74 -f | --file)
75 filenames[${2#/}]=1
76 shift
77 ;;
78 -s | --size) sorted=1 ;;
79 -h | --help)
80 usage
81 exit 0
82 ;;
83 -m | --mod) modules=1 ;;
84 -v | --verbose) verbose="--verbose" ;;
85 --unpack) unpack=1 ;;
86 --unpackearly) unpackearly=1 ;;
87 --)
88 shift
89 break
90 ;;
91 *)
92 usage
93 exit 1
94 ;;
95 esac
96 shift
97 done
98
99 [[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
100
101 if [[ $1 ]]; then
102 image="$1"
103 if ! [[ -f $image ]]; then
104 {
105 echo "$image does not exist"
106 echo
107 } >&2
108 usage
109 exit 1
110 fi
111 else
112 [[ -f /etc/machine-id ]] && read -r MACHINE_ID < /etc/machine-id
113
114 if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
115 && [[ $MACHINE_ID ]] \
116 && [[ -d /efi/${MACHINE_ID} || -L /efi/${MACHINE_ID} ]]; then
117 image="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
118 elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
119 && [[ $MACHINE_ID ]] \
120 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]]; then
121 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
122 else
123 image="/boot/initramfs-${KERNEL_VERSION}.img"
124 fi
125 fi
126
127 shift
128 while (($# > 0)); do
129 filenames[${1#/}]=1
130 shift
131 done
132
133 if ! [[ -f $image ]]; then
134 {
135 echo "No <initramfs file> specified and the default image '$image' cannot be accessed!"
136 echo
137 } >&2
138 usage
139 exit 1
140 fi
141
142 TMPDIR="$(mktemp -d -t lsinitrd.XXXXXX)"
143 # shellcheck disable=SC2064
144 trap "rm -rf '$TMPDIR'" EXIT
145
146 dracutlibdirs() {
147 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
148 echo "$d/$1"
149 done
150 }
151
152 extract_files() {
153 ((${#filenames[@]} == 1)) && nofileinfo=1
154 for f in "${!filenames[@]}"; do
155 [[ $nofileinfo ]] || echo "initramfs:/$f"
156 [[ $nofileinfo ]] || echo "========================================================================"
157 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --to-stdout "$f" 2> /dev/null
158 ((ret += $?))
159 [[ $nofileinfo ]] || echo "========================================================================"
160 [[ $nofileinfo ]] || echo
161 done
162 }
163
164 list_modules() {
165 echo "dracut modules:"
166 # shellcheck disable=SC2046
167 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
168 $(dracutlibdirs modules.txt) 2> /dev/null
169 ((ret += $?))
170 }
171
172 list_files() {
173 echo "========================================================================"
174 if [ "$sorted" -eq 1 ]; then
175 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
176 else
177 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -k9
178 fi
179 ((ret += $?))
180 echo "========================================================================"
181 }
182
183 list_squash_content() {
184 SQUASH_IMG="squash-root.img"
185 SQUASH_TMPFILE="$TMPDIR/initrd.root.sqsh"
186
187 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --to-stdout -- \
188 $SQUASH_IMG > "$SQUASH_TMPFILE" 2> /dev/null
189 if [[ -s $SQUASH_TMPFILE ]]; then
190 echo "Squashed content ($SQUASH_IMG):"
191 echo "========================================================================"
192 unsquashfs -ll "$SQUASH_TMPFILE" | tail -n +4
193 echo "========================================================================"
194 fi
195 }
196
197 unpack_files() {
198 if ((${#filenames[@]} > 0)); then
199 for f in "${!filenames[@]}"; do
200 $CAT "$image" 2> /dev/null | cpio -id --quiet $verbose $f
201 ((ret += $?))
202 done
203 else
204 $CAT "$image" 2> /dev/null | cpio -id --quiet $verbose
205 ((ret += $?))
206 fi
207 }
208
209 read -r -N 2 bin < "$image"
210 if [ "$bin" = "MZ" ]; then
211 command -v objcopy > /dev/null || {
212 echo "Need 'objcopy' to unpack an UEFI executable."
213 exit 1
214 }
215 objcopy \
216 --dump-section .linux="$TMPDIR/vmlinuz" \
217 --dump-section .initrd="$TMPDIR/initrd.img" \
218 --dump-section .cmdline="$TMPDIR/cmdline.txt" \
219 --dump-section .osrel="$TMPDIR/osrel.txt" \
220 "$image" /dev/null
221 uefi="$image"
222 image="$TMPDIR/initrd.img"
223 [ -f "$image" ] || exit 1
224 fi
225
226 if ((${#filenames[@]} <= 0)) && [[ -z $unpack ]] && [[ -z $unpackearly ]]; then
227 if [ -n "$uefi" ]; then
228 echo -n "initrd in UEFI: $uefi: "
229 du -h "$image" | while read -r a _ || [ -n "$a" ]; do echo "$a"; done
230 if [ -f "$TMPDIR/osrel.txt" ]; then
231 name=$(sed -En '/^PRETTY_NAME/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
232 id=$(sed -En '/^ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
233 build=$(sed -En '/^BUILD_ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
234 echo "OS Release: $name (${id}-${build})"
235 fi
236 if [ -f "$TMPDIR/vmlinuz" ]; then
237 version=$(strings -n 20 "$TMPDIR/vmlinuz" | sed -En '/[0-9]+\.[0-9]+\.[0-9]+/ { p; q 0 }')
238 echo "Kernel Version: $version"
239 fi
240 if [ -f "$TMPDIR/cmdline.txt" ]; then
241 echo "Command line:"
242 sed -En 's/\s+/\n/g; s/\x00/\n/; p' "$TMPDIR/cmdline.txt"
243 fi
244 else
245 echo -n "Image: $image: "
246 du -h "$image" | while read -r a _ || [ -n "$a" ]; do echo "$a"; done
247 fi
248
249 echo "========================================================================"
250 fi
251
252 read -r -N 6 bin < "$image"
253 case $bin in
254 $'\x71\xc7'* | 070701)
255 CAT="cat --"
256 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2> /dev/null)
257 # Debian mkinitramfs does not create the file 'early_cpio', so let's check if firmware files exist
258 [[ "$is_early" ]] || is_early=$(cpio --list --verbose --quiet --to-stdout -- 'kernel/*/microcode/*.bin' < "$image" 2> /dev/null)
259 if [[ "$is_early" ]]; then
260 if [[ -n $unpack ]]; then
261 # should use --unpackearly for early CPIO
262 :
263 elif [[ -n $unpackearly ]]; then
264 unpack_files
265 elif ((${#filenames[@]} > 0)); then
266 extract_files
267 else
268 echo "Early CPIO image"
269 list_files
270 fi
271 if [[ -d "$dracutbasedir/skipcpio" ]]; then
272 SKIP="$dracutbasedir/skipcpio/skipcpio"
273 else
274 SKIP="$dracutbasedir/skipcpio"
275 fi
276 if ! [[ -x $SKIP ]]; then
277 echo
278 echo "'$SKIP' not found, cannot display remaining contents!" >&2
279 echo
280 exit 0
281 fi
282 fi
283 ;;
284 esac
285
286 if [[ $SKIP ]]; then
287 bin="$($SKIP "$image" | { read -r -N 6 bin && echo "$bin"; })"
288 else
289 read -r -N 6 bin < "$image"
290 fi
291 case $bin in
292 $'\x1f\x8b'*)
293 CAT="zcat --"
294 ;;
295 BZh*)
296 CAT="bzcat --"
297 ;;
298 $'\x71\xc7'* | 070701)
299 CAT="cat --"
300 ;;
301 $'\x02\x21'*)
302 CAT="lz4 -d -c"
303 ;;
304 $'\x89'LZO$'\0'*)
305 CAT="lzop -d -c"
306 ;;
307 $'\x28\xB5\x2F\xFD'*)
308 CAT="zstd -d -c"
309 ;;
310 *)
311 if echo "test" | xz | xzcat --single-stream > /dev/null 2>&1; then
312 CAT="xzcat --single-stream --"
313 else
314 CAT="xzcat --"
315 fi
316 ;;
317 esac
318
319 skipcpio() {
320 $SKIP "$@" | $ORIG_CAT
321 }
322
323 if [[ $SKIP ]]; then
324 ORIG_CAT="$CAT"
325 CAT=skipcpio
326 fi
327
328 if ((${#filenames[@]} > 1)); then
329 TMPFILE="$TMPDIR/initrd.cpio"
330 $CAT "$image" 2> /dev/null > "$TMPFILE"
331 pre_decompress() {
332 cat "$TMPFILE"
333 }
334 CAT=pre_decompress
335 fi
336
337 ret=0
338
339 if [[ -n $unpack ]]; then
340 unpack_files
341 elif ((${#filenames[@]} > 0)); then
342 extract_files
343 else
344 # shellcheck disable=SC2046
345 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
346 $(dracutlibdirs 'dracut-*') 2> /dev/null)
347 ((ret += $?))
348 echo "Version: $version"
349 echo
350 if [ "$modules" -eq 1 ]; then
351 list_modules
352 echo "========================================================================"
353 else
354 echo -n "Arguments: "
355 # shellcheck disable=SC2046
356 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
357 $(dracutlibdirs build-parameter.txt) 2> /dev/null
358 echo
359 list_modules
360 list_files
361 list_squash_content
362 fi
363 fi
364
365 exit "$ret"