]> git.ipfire.org Git - thirdparty/dracut.git/blame - lsinitrd.sh
dmsquash-live-root: Remove obsolete osmin.img processing.
[thirdparty/dracut.git] / lsinitrd.sh
CommitLineData
3b403b32 1#!/bin/bash
cc02093d
HH
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#
074d12c1 19
f7bccf37
HH
20usage()
21{
dde2db3d 22 {
7d9bb76a
HH
23 echo "Usage: ${0##*/} [options] [<initramfs file> [<filename> [<filename> [...] ]]]"
24 echo "Usage: ${0##*/} [options] -k <kernel version>"
dde2db3d 25 echo
7d9bb76a
HH
26 echo "-h, --help print a help message and exit."
27 echo "-s, --size sort the contents of the initramfs by size."
05d2a145 28 echo "-m, --mod list modules."
7d9bb76a 29 echo "-f, --file <filename> print the contents of <filename>."
97bbba69 30 echo "--unpack unpack the initramfs, instead of displaying the contents."
143420bc
KS
31 echo " If optional filenames are given, will only unpack specified files,"
32 echo " else the whole image will be unpacked. Won't unpack anything from early cpio part."
97bbba69 33 echo "--unpackearly unpack the early microcode part of the initramfs."
143420bc 34 echo " Same as --unpack, but only unpack files from early cpio part."
97bbba69 35 echo "-v, --verbose unpack verbosely."
7d9bb76a 36 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
dde2db3d
HH
37 echo
38 } >&2
f7bccf37
HH
39}
40
b208aad5
HH
41
42[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
43
3ea5d2e2 44sorted=0
05d2a145 45modules=0
97bbba69 46unset verbose
7d9bb76a
HH
47declare -A filenames
48
49unset POSIXLY_CORRECT
50TEMP=$(getopt \
97bbba69 51 -o "vshmf:k:" \
7d9bb76a
HH
52 --long kver: \
53 --long file: \
05d2a145 54 --long mod \
7d9bb76a
HH
55 --long help \
56 --long size \
97bbba69
HH
57 --long unpack \
58 --long unpackearly \
59 --long verbose \
7d9bb76a
HH
60 -- "$@")
61
62if (( $? != 0 )); then
63 usage
64 exit 1
65fi
66
67eval set -- "$TEMP"
68
69while (($# > 0)); do
70 case $1 in
97bbba69
HH
71 -k|--kver) KERNEL_VERSION="$2"; shift;;
72 -f|--file) filenames[${2#/}]=1; shift;;
73 -s|--size) sorted=1;;
74 -h|--help) usage; exit 0;;
75 -m|--mod) modules=1;;
76 -v|--verbose) verbose="--verbose";;
77 --unpack) unpack=1;;
78 --unpackearly) unpackearly=1;;
79 --) shift;break;;
80 *) usage; exit 1;;
3ea5d2e2 81 esac
7d9bb76a 82 shift
3ea5d2e2 83done
3ea5d2e2 84
7d9bb76a 85[[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
dde2db3d 86
7d9bb76a 87if [[ $1 ]]; then
dde2db3d
HH
88 image="$1"
89 if ! [[ -f "$image" ]]; then
90 {
91 echo "$image does not exist"
92 echo
93 } >&2
94 usage
95 exit 1
96 fi
dde2db3d 97else
727e68d0
HH
98 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
99
7d9bb76a
HH
100 if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
101 && [[ $MACHINE_ID ]] \
102 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
727e68d0
HH
103 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
104 else
d928724c 105 image="/boot/initramfs-${KERNEL_VERSION}.img"
727e68d0 106 fi
dde2db3d
HH
107fi
108
7d9bb76a
HH
109shift
110while (($# > 0)); do
111 filenames[${1#/}]=1;
112 shift
113done
727e68d0 114
dde2db3d
HH
115if ! [[ -f "$image" ]]; then
116 {
117 echo "No <initramfs file> specified and the default image '$image' cannot be accessed!"
118 echo
119 } >&2
120 usage
121 exit 1
122fi
074d12c1 123
cbf32008
MR
124TMPDIR="$(mktemp -d -t lsinitrd.XXXXXX)"
125trap "rm -rf '$TMPDIR'" EXIT
126
d3be9275
127dracutlibdirs() {
128 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
129 echo "$d/$1"
130 done
131}
132
b208aad5
HH
133extract_files()
134{
135 (( ${#filenames[@]} == 1 )) && nofileinfo=1
3721635b 136 for f in "${!filenames[@]}"; do
b208aad5
HH
137 [[ $nofileinfo ]] || echo "initramfs:/$f"
138 [[ $nofileinfo ]] || echo "========================================================================"
8379784a 139 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --to-stdout "$f" 2>/dev/null
b208aad5
HH
140 ((ret+=$?))
141 [[ $nofileinfo ]] || echo "========================================================================"
142 [[ $nofileinfo ]] || echo
143 done
144}
145
05d2a145
HB
146list_modules()
147{
148 echo "dracut modules:"
d3be9275
149 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
150 $(dracutlibdirs modules.txt) 2>/dev/null
05d2a145
HB
151 ((ret+=$?))
152}
153
b208aad5
HH
154list_files()
155{
156 echo "========================================================================"
157 if [ "$sorted" -eq 1 ]; then
bce6823a 158 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
b208aad5 159 else
bce6823a 160 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -k9
b208aad5
HH
161 fi
162 ((ret+=$?))
163 echo "========================================================================"
164}
165
1ff306a3
KS
166list_squash_content()
167{
168 SQUASH_IMG="squash/root.img"
cbf32008
MR
169 SQUASH_TMPFILE="$TMPDIR/initrd.root.sqsh"
170
1ff306a3
KS
171 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --to-stdout -- \
172 $SQUASH_IMG > "$SQUASH_TMPFILE" 2>/dev/null
173 if [[ -s $SQUASH_TMPFILE ]]; then
174 echo "Squashed content ($SQUASH_IMG):"
175 echo "========================================================================"
176 unsquashfs -ll "$SQUASH_TMPFILE" | tail -n +4
177 echo "========================================================================"
178 fi
179}
180
97bbba69
HH
181unpack_files()
182{
f81c864e
KS
183 if (( ${#filenames[@]} > 0 )); then
184 for f in "${!filenames[@]}"; do
185 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose $f
186 ((ret+=$?))
187 done
188 else
189 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose
190 ((ret+=$?))
191 fi
97bbba69
HH
192}
193
cbf32008
MR
194read -N 2 bin < "$image"
195if [ "$bin" = "MZ" ]; then
196 command -v objcopy > /dev/null || { echo "Need 'objcopy' to unpack an UEFI executable."; exit 1; }
197 objcopy \
198 --dump-section .linux="$TMPDIR/vmlinuz" \
199 --dump-section .initrd="$TMPDIR/initrd.img" \
200 --dump-section .cmdline="$TMPDIR/cmdline.txt" \
201 --dump-section .osrel="$TMPDIR/osrel.txt" \
202 "$image" /dev/null
203 uefi="$image"
204 image="$TMPDIR/initrd.img"
205 [ -f "$image" ] || exit 1
206fi
b208aad5 207
97bbba69 208if (( ${#filenames[@]} <= 0 )) && [[ -z "$unpack" ]] && [[ -z "$unpackearly" ]]; then
32dfd416 209 if [ -n "$uefi" ]; then
cbf32008
MR
210 echo -n "initrd in UEFI: $uefi: "
211 du -h $image | while read a b || [ -n "$a" ]; do echo $a;done
212 if [ -f "$TMPDIR/osrel.txt" ]; then
213 name=$(sed -En '/^PRETTY_NAME/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
214 id=$(sed -En '/^ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
215 build=$(sed -En '/^BUILD_ID/ s/^\w+=["'"'"']?([^"'"'"'$]*)["'"'"']?/\1/p' "$TMPDIR/osrel.txt")
216 echo "OS Release: $name (${id}-${build})"
217 fi
218 if [ -f "$TMPDIR/vmlinuz" ]; then
219 version=$(strings -n 20 "$TMPDIR/vmlinuz" | sed -En '/[0-9]+\.[0-9]+\.[0-9]+/ { p; q 0 }')
220 echo "Kernel Version: $version"
221 fi
222 if [ -f "$TMPDIR/cmdline.txt" ]; then
223 echo "Command line:"
224 sed -En 's/\s+/\n/g; s/\x00/\n/; p' "$TMPDIR/cmdline.txt"
225 fi
226 else
227 echo -n "Image: $image: "
228 du -h $image | while read a b || [ -n "$a" ]; do echo $a;done
229 fi
230
b208aad5
HH
231 echo "========================================================================"
232fi
233
884e1cda 234read -N 6 bin < "$image"
b208aad5
HH
235case $bin in
236 $'\x71\xc7'*|070701)
18a5011f 237 CAT="cat --"
b208aad5 238 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
ce62465c
TM
239 # Debian mkinitramfs does not create the file 'early_cpio', so let's check if firmware files exist
240 [[ "$is_early" ]] || is_early=$(cpio --list --verbose --quiet --to-stdout -- 'kernel/*/microcode/*.bin' < "$image" 2>/dev/null)
b208aad5 241 if [[ "$is_early" ]]; then
f81c864e
KS
242 if [[ -n "$unpack" ]]; then
243 # should use --unpackearly for early CPIO
244 :
245 elif [[ -n "$unpackearly" ]]; then
97bbba69
HH
246 unpack_files
247 elif (( ${#filenames[@]} > 0 )); then
b208aad5
HH
248 extract_files
249 else
250 echo "Early CPIO image"
251 list_files
252 fi
98fd0693
HH
253 if [[ -d "$dracutbasedir/skipcpio" ]]; then
254 SKIP="$dracutbasedir/skipcpio/skipcpio"
255 else
256 SKIP="$dracutbasedir/skipcpio"
257 fi
b208aad5
HH
258 if ! [[ -x $SKIP ]]; then
259 echo
260 echo "'$SKIP' not found, cannot display remaining contents!" >&2
261 echo
262 exit 0
263 fi
264 fi
265 ;;
266esac
267
3ce14286
HD
268if [[ $SKIP ]] ; then
269 bin="$($SKIP "$image" | { read -N 6 bin && echo "$bin" ; })"
270else
271 read -N 6 bin < "$image"
272fi
273case $bin in
274 $'\x1f\x8b'*)
275 CAT="zcat --"
276 ;;
277 BZh*)
278 CAT="bzcat --"
279 ;;
280 $'\x71\xc7'*|070701)
18a5011f 281 CAT="cat --"
3ce14286
HD
282 ;;
283 $'\x02\x21'*)
284 CAT="lz4 -d -c"
285 ;;
286 $'\x89'LZO$'\0'*)
287 CAT="lzop -d -c"
288 ;;
ebfd53e1 289 $'\x28\xB5\x2F\xFD'*)
7dbadcc7
TPG
290 CAT="zstd -d -c"
291 ;;
3ce14286
HD
292 *)
293 if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
294 CAT="xzcat --single-stream --"
fd9f9024 295 else
3ce14286
HD
296 CAT="xzcat --"
297 fi
298 ;;
299esac
66fe35eb 300
b208aad5
HH
301skipcpio()
302{
303 $SKIP "$@" | $ORIG_CAT
304}
305
306if [[ $SKIP ]]; then
307 ORIG_CAT="$CAT"
308 CAT=skipcpio
309fi
310
986b12d3 311if (( ${#filenames[@]} > 1 )); then
cbf32008 312 TMPFILE="$TMPDIR/initrd.cpio"
986b12d3 313 $CAT "$image" 2>/dev/null > $TMPFILE
986b12d3
KS
314 pre_decompress()
315 {
316 cat $TMPFILE
317 }
318 CAT=pre_decompress
319fi
320
884e1cda
HH
321ret=0
322
97bbba69
HH
323if [[ -n "$unpack" ]]; then
324 unpack_files
325elif (( ${#filenames[@]} > 0 )); then
b208aad5 326 extract_files
3ea5d2e2 327else
d3be9275
328 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
329 $(dracutlibdirs 'dracut-*') 2>/dev/null)
4460416a 330 ((ret+=$?))
ae01bd18 331 echo "Version: $version"
b208aad5 332 echo
05d2a145
HB
333 if [ "$modules" -eq 1 ]; then
334 list_modules
335 echo "========================================================================"
336 else
337 echo -n "Arguments: "
d3be9275
338 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
339 $(dracutlibdirs build-parameter.txt) 2>/dev/null
05d2a145
HB
340 echo
341 list_modules
342 list_files
1ff306a3 343 list_squash_content
05d2a145 344 fi
3ea5d2e2 345fi
4460416a 346
884e1cda 347exit $ret