]> git.ipfire.org Git - thirdparty/dracut.git/blob - lsinitrd.sh
test: increase test VM memory from 512M to 1024M to avoid OOM killer
[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 if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
113 MACHINE_ID="Default"
114 elif [[ -s /etc/machine-id ]]; then
115 read -r MACHINE_ID < /etc/machine-id
116 [[ $MACHINE_ID == "uninitialized" ]] && MACHINE_ID="Default"
117 else
118 MACHINE_ID="Default"
119 fi
120
121 if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
122 && [[ $MACHINE_ID ]] \
123 && [[ -d /efi/${MACHINE_ID} || -L /efi/${MACHINE_ID} ]]; then
124 image="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
125 elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
126 && [[ $MACHINE_ID ]] \
127 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]]; then
128 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
129 elif [[ -d /boot/efi/loader/entries || -L /boot/efi/loader/entries ]] \
130 && [[ $MACHINE_ID ]] \
131 && [[ -d /boot/efi/${MACHINE_ID} || -L /boot/efi/${MACHINE_ID} ]]; then
132 image="/boot/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
133 elif [[ -f /lib/modules/${KERNEL_VERSION}/initrd ]]; then
134 image="/lib/modules/${KERNEL_VERSION}/initrd"
135 elif [[ -f /lib/modules/${KERNEL_VERSION}/initramfs.img ]]; then
136 image="/lib/modules/${KERNEL_VERSION}/initramfs.img"
137 elif [[ -f /boot/initramfs-${KERNEL_VERSION}.img ]]; then
138 image="/boot/initramfs-${KERNEL_VERSION}.img"
139 elif [[ $MACHINE_ID ]] \
140 && mountpoint -q /efi; then
141 image="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
142 elif [[ $MACHINE_ID ]] \
143 && mountpoint -q /boot/efi; then
144 image="/boot/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
145 else
146 image=""
147 fi
148 fi
149
150 shift
151 while (($# > 0)); do
152 filenames[${1#/}]=1
153 shift
154 done
155
156 if ! [[ -f $image ]]; then
157 {
158 echo "No <initramfs file> specified and the default image '$image' cannot be accessed!"
159 echo
160 } >&2
161 usage
162 exit 1
163 fi
164
165 TMPDIR="$(mktemp -d -t lsinitrd.XXXXXX)"
166 # shellcheck disable=SC2064
167 trap "rm -rf '$TMPDIR'" EXIT
168
169 dracutlibdirs() {
170 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
171 echo "$d/$1"
172 done
173 }
174
175 extract_files() {
176 ((${#filenames[@]} == 1)) && nofileinfo=1
177 for f in "${!filenames[@]}"; do
178 [[ $nofileinfo ]] || echo "initramfs:/$f"
179 [[ $nofileinfo ]] || echo "========================================================================"
180 # shellcheck disable=SC2001
181 [[ $f == *"\\x"* ]] && f=$(echo "$f" | sed 's/\\x.\{2\}/????/g')
182 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --to-stdout "$f" 2> /dev/null
183 ((ret += $?))
184 [[ $nofileinfo ]] || echo "========================================================================"
185 [[ $nofileinfo ]] || echo
186 done
187 }
188
189 list_modules() {
190 echo "dracut modules:"
191 # shellcheck disable=SC2046
192 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
193 $(dracutlibdirs modules.txt) 2> /dev/null
194 ((ret += $?))
195 }
196
197 list_files() {
198 echo "========================================================================"
199 if [ "$sorted" -eq 1 ]; then
200 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
201 else
202 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -k9
203 fi
204 ((ret += $?))
205 echo "========================================================================"
206 }
207
208 list_squash_content() {
209 SQUASH_IMG="squash-root.img"
210 SQUASH_TMPFILE="$TMPDIR/initrd.root.sqsh"
211
212 $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --to-stdout -- \
213 $SQUASH_IMG > "$SQUASH_TMPFILE" 2> /dev/null
214 if [[ -s $SQUASH_TMPFILE ]]; then
215 echo "Squashed content ($SQUASH_IMG):"
216 echo "========================================================================"
217 unsquashfs -ll "$SQUASH_TMPFILE" | tail -n +4
218 echo "========================================================================"
219 fi
220 }
221
222 unpack_files() {
223 if ((${#filenames[@]} > 0)); then
224 for f in "${!filenames[@]}"; do
225 # shellcheck disable=SC2001
226 [[ $f == *"\\x"* ]] && f=$(echo "$f" | sed 's/\\x.\{2\}/????/g')
227 $CAT "$image" 2> /dev/null | cpio -id --quiet $verbose "$f"
228 ((ret += $?))
229 done
230 else
231 $CAT "$image" 2> /dev/null | cpio -id --quiet $verbose
232 ((ret += $?))
233 fi
234 }
235
236 read -r -N 2 bin < "$image"
237 if [ "$bin" = "MZ" ]; then
238 command -v objcopy > /dev/null || {
239 echo "Need 'objcopy' to unpack an UEFI executable."
240 exit 1
241 }
242 objcopy \
243 --dump-section .linux="$TMPDIR/vmlinuz" \
244 --dump-section .initrd="$TMPDIR/initrd.img" \
245 --dump-section .cmdline="$TMPDIR/cmdline.txt" \
246 --dump-section .osrel="$TMPDIR/osrel.txt" \
247 "$image" /dev/null
248 uefi="$image"
249 image="$TMPDIR/initrd.img"
250 [ -f "$image" ] || exit 1
251 fi
252
253 if ((${#filenames[@]} <= 0)) && [[ -z $unpack ]] && [[ -z $unpackearly ]]; then
254 if [ -n "$uefi" ]; then
255 echo -n "initrd in UEFI: $uefi: "
256 du -h "$image" | while read -r a _ || [ -n "$a" ]; do echo "$a"; done
257 if [ -f "$TMPDIR/osrel.txt" ]; then
258 name=$(sed -En '/^PRETTY_NAME/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
259 id=$(sed -En '/^ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
260 build=$(sed -En '/^BUILD_ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
261 echo "OS Release: $name (${id}-${build})"
262 fi
263 if [ -f "$TMPDIR/vmlinuz" ]; then
264 version=$(strings -n 20 "$TMPDIR/vmlinuz" | sed -En '/[0-9]+\.[0-9]+\.[0-9]+/ { p; q 0 }')
265 echo "Kernel Version: $version"
266 fi
267 if [ -f "$TMPDIR/cmdline.txt" ]; then
268 echo "Command line:"
269 sed -En 's/\s+/\n/g; s/\x00/\n/; p' "$TMPDIR/cmdline.txt"
270 fi
271 else
272 echo -n "Image: $image: "
273 du -h "$image" | while read -r a _ || [ -n "$a" ]; do echo "$a"; done
274 fi
275
276 echo "========================================================================"
277 fi
278
279 read -r -N 6 bin < "$image"
280 case $bin in
281 $'\x71\xc7'* | 070701)
282 CAT="cat --"
283 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2> /dev/null)
284 # Debian mkinitramfs does not create the file 'early_cpio', so let's check if firmware files exist
285 [[ "$is_early" ]] || is_early=$(cpio --list --verbose --quiet --to-stdout -- 'kernel/*/microcode/*.bin' < "$image" 2> /dev/null)
286 if [[ "$is_early" ]]; then
287 if [[ -n $unpack ]]; then
288 # should use --unpackearly for early CPIO
289 :
290 elif [[ -n $unpackearly ]]; then
291 unpack_files
292 elif ((${#filenames[@]} > 0)); then
293 extract_files
294 else
295 echo "Early CPIO image"
296 list_files
297 fi
298 if [[ -d "$dracutbasedir/src/skipcpio" ]]; then
299 SKIP="$dracutbasedir/src/skipcpio/skipcpio"
300 else
301 SKIP="$dracutbasedir/skipcpio"
302 fi
303 if ! [[ -x $SKIP ]]; then
304 echo
305 echo "'$SKIP' not found, cannot display remaining contents!" >&2
306 echo
307 exit 0
308 fi
309 fi
310 ;;
311 esac
312
313 if [[ $SKIP ]]; then
314 bin="$($SKIP "$image" | { read -r -N 6 bin && echo "$bin"; })"
315 else
316 read -r -N 6 bin < "$image"
317 fi
318 case $bin in
319 $'\x1f\x8b'*)
320 CAT="zcat --"
321 ;;
322 BZh*)
323 CAT="bzcat --"
324 ;;
325 $'\x71\xc7'* | 070701)
326 CAT="cat --"
327 ;;
328 $'\x02\x21'*)
329 CAT="lz4 -d -c"
330 ;;
331 $'\x89'LZO$'\0'*)
332 CAT="lzop -d -c"
333 ;;
334 $'\x28\xB5\x2F\xFD'*)
335 CAT="zstd -d -c"
336 ;;
337 *)
338 if echo "test" | xz | xzcat --single-stream > /dev/null 2>&1; then
339 CAT="xzcat --single-stream --"
340 else
341 CAT="xzcat --"
342 fi
343 ;;
344 esac
345
346 type "${CAT%% *}" > /dev/null 2>&1 || {
347 echo "Need '${CAT%% *}' to unpack the initramfs."
348 exit 1
349 }
350
351 skipcpio() {
352 $SKIP "$@" | $ORIG_CAT
353 }
354
355 if [[ $SKIP ]]; then
356 ORIG_CAT="$CAT"
357 CAT=skipcpio
358 fi
359
360 if ((${#filenames[@]} > 1)); then
361 TMPFILE="$TMPDIR/initrd.cpio"
362 $CAT "$image" 2> /dev/null > "$TMPFILE"
363 pre_decompress() {
364 cat "$TMPFILE"
365 }
366 CAT=pre_decompress
367 fi
368
369 ret=0
370
371 if [[ -n $unpack ]]; then
372 unpack_files
373 elif ((${#filenames[@]} > 0)); then
374 extract_files
375 else
376 # shellcheck disable=SC2046
377 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
378 $(dracutlibdirs 'dracut-*') 2> /dev/null)
379 ((ret += $?))
380 echo "Version: $version"
381 echo
382 if [ "$modules" -eq 1 ]; then
383 list_modules
384 echo "========================================================================"
385 else
386 echo -n "Arguments: "
387 # shellcheck disable=SC2046
388 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
389 $(dracutlibdirs build-parameter.txt) 2> /dev/null
390 echo
391 list_modules
392 list_files
393 list_squash_content
394 fi
395 fi
396
397 exit "$ret"