]> git.ipfire.org Git - thirdparty/dracut.git/blob - lsinitrd.sh
Fix a missing space in example configs
[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 {
23 echo "Usage: ${0##*/} [options] [<initramfs file> [<filename> [<filename> [...] ]]]"
24 echo "Usage: ${0##*/} [options] -k <kernel version>"
25 echo
26 echo "-h, --help print a help message and exit."
27 echo "-s, --size sort the contents of the initramfs by size."
28 echo "-m, --mod list modules."
29 echo "-f, --file <filename> print the contents of <filename>."
30 echo "--unpack unpack the initramfs, instead of displaying the contents."
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."
33 echo "--unpackearly unpack the early microcode part of the initramfs."
34 echo " Same as --unpack, but only unpack files from early cpio part."
35 echo "-v, --verbose unpack verbosely."
36 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
37 echo
38 } >&2
39 }
40
41
42 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
43
44 sorted=0
45 modules=0
46 unset verbose
47 declare -A filenames
48
49 unset POSIXLY_CORRECT
50 TEMP=$(getopt \
51 -o "vshmf:k:" \
52 --long kver: \
53 --long file: \
54 --long mod \
55 --long help \
56 --long size \
57 --long unpack \
58 --long unpackearly \
59 --long verbose \
60 -- "$@")
61
62 if (( $? != 0 )); then
63 usage
64 exit 1
65 fi
66
67 eval set -- "$TEMP"
68
69 while (($# > 0)); do
70 case $1 in
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;;
81 esac
82 shift
83 done
84
85 [[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
86
87 if [[ $1 ]]; then
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
97 else
98 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
99
100 if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
101 && [[ $MACHINE_ID ]] \
102 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
103 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
104 else
105 image="/boot/initramfs-${KERNEL_VERSION}.img"
106 fi
107 fi
108
109 shift
110 while (($# > 0)); do
111 filenames[${1#/}]=1;
112 shift
113 done
114
115 if ! [[ -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
122 fi
123
124 dracutlibdirs() {
125 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
126 echo "$d/$1"
127 done
128 }
129
130 extract_files()
131 {
132 (( ${#filenames[@]} == 1 )) && nofileinfo=1
133 for f in "${!filenames[@]}"; do
134 [[ $nofileinfo ]] || echo "initramfs:/$f"
135 [[ $nofileinfo ]] || echo "========================================================================"
136 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --to-stdout "$f" 2>/dev/null
137 ((ret+=$?))
138 [[ $nofileinfo ]] || echo "========================================================================"
139 [[ $nofileinfo ]] || echo
140 done
141 }
142
143 list_modules()
144 {
145 echo "dracut modules:"
146 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
147 $(dracutlibdirs modules.txt) 2>/dev/null
148 ((ret+=$?))
149 }
150
151 list_files()
152 {
153 echo "========================================================================"
154 if [ "$sorted" -eq 1 ]; then
155 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
156 else
157 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -k9
158 fi
159 ((ret+=$?))
160 echo "========================================================================"
161 }
162
163 list_squash_content()
164 {
165 SQUASH_IMG="squash/root.img"
166 SQUASH_TMPFILE="$(mktemp -t --suffix=.root.sqsh lsinitrd.XXXXXX)"
167 trap "rm -f '$SQUASH_TMPFILE'" EXIT
168 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --to-stdout -- \
169 $SQUASH_IMG > "$SQUASH_TMPFILE" 2>/dev/null
170 if [[ -s $SQUASH_TMPFILE ]]; then
171 echo "Squashed content ($SQUASH_IMG):"
172 echo "========================================================================"
173 unsquashfs -ll "$SQUASH_TMPFILE" | tail -n +4
174 echo "========================================================================"
175 fi
176 }
177
178 unpack_files()
179 {
180 if (( ${#filenames[@]} > 0 )); then
181 for f in "${!filenames[@]}"; do
182 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose $f
183 ((ret+=$?))
184 done
185 else
186 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose
187 ((ret+=$?))
188 fi
189 }
190
191
192 if (( ${#filenames[@]} <= 0 )) && [[ -z "$unpack" ]] && [[ -z "$unpackearly" ]]; then
193 echo "Image: $image: $(du -h $image | while read a b || [ -n "$a" ]; do echo $a;done)"
194 echo "========================================================================"
195 fi
196
197 read -N 6 bin < "$image"
198 case $bin in
199 $'\x71\xc7'*|070701)
200 CAT="cat --"
201 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
202 if [[ "$is_early" ]]; then
203 if [[ -n "$unpack" ]]; then
204 # should use --unpackearly for early CPIO
205 :
206 elif [[ -n "$unpackearly" ]]; then
207 unpack_files
208 elif (( ${#filenames[@]} > 0 )); then
209 extract_files
210 else
211 echo "Early CPIO image"
212 list_files
213 fi
214 if [[ -d "$dracutbasedir/skipcpio" ]]; then
215 SKIP="$dracutbasedir/skipcpio/skipcpio"
216 else
217 SKIP="$dracutbasedir/skipcpio"
218 fi
219 if ! [[ -x $SKIP ]]; then
220 echo
221 echo "'$SKIP' not found, cannot display remaining contents!" >&2
222 echo
223 exit 0
224 fi
225 fi
226 ;;
227 esac
228
229 if [[ $SKIP ]] ; then
230 bin="$($SKIP "$image" | { read -N 6 bin && echo "$bin" ; })"
231 else
232 read -N 6 bin < "$image"
233 fi
234 case $bin in
235 $'\x1f\x8b'*)
236 CAT="zcat --"
237 ;;
238 BZh*)
239 CAT="bzcat --"
240 ;;
241 $'\x71\xc7'*|070701)
242 CAT="cat --"
243 ;;
244 $'\x02\x21'*)
245 CAT="lz4 -d -c"
246 ;;
247 $'\x89'LZO$'\0'*)
248 CAT="lzop -d -c"
249 ;;
250 $'\x28\xB5\x2F\xFD'*)
251 CAT="zstd -d -c"
252 ;;
253 *)
254 if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
255 CAT="xzcat --single-stream --"
256 else
257 CAT="xzcat --"
258 fi
259 ;;
260 esac
261
262 skipcpio()
263 {
264 $SKIP "$@" | $ORIG_CAT
265 }
266
267 if [[ $SKIP ]]; then
268 ORIG_CAT="$CAT"
269 CAT=skipcpio
270 fi
271
272 if (( ${#filenames[@]} > 1 )); then
273 TMPFILE="$(mktemp -t --suffix=.cpio lsinitrd.XXXXXX)"
274 $CAT "$image" 2>/dev/null > $TMPFILE
275 trap "rm -f '$TMPFILE'" EXIT
276 pre_decompress()
277 {
278 cat $TMPFILE
279 }
280 CAT=pre_decompress
281 fi
282
283 ret=0
284
285 if [[ -n "$unpack" ]]; then
286 unpack_files
287 elif (( ${#filenames[@]} > 0 )); then
288 extract_files
289 else
290 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
291 $(dracutlibdirs 'dracut-*') 2>/dev/null)
292 ((ret+=$?))
293 echo "Version: $version"
294 echo
295 if [ "$modules" -eq 1 ]; then
296 list_modules
297 echo "========================================================================"
298 else
299 echo -n "Arguments: "
300 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
301 $(dracutlibdirs build-parameter.txt) 2>/dev/null
302 echo
303 list_modules
304 list_files
305 list_squash_content
306 fi
307 fi
308
309 exit $ret