]> git.ipfire.org Git - thirdparty/dracut.git/blob - lsinitrd.sh
add support for Zstandard
[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 "--unpackearly unpack the early microcode part of the initramfs."
32 echo "-v, --verbose unpack verbosely."
33 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
34 echo
35 } >&2
36 }
37
38
39 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
40
41 sorted=0
42 modules=0
43 unset verbose
44 declare -A filenames
45
46 unset POSIXLY_CORRECT
47 TEMP=$(getopt \
48 -o "vshmf:k:" \
49 --long kver: \
50 --long file: \
51 --long mod \
52 --long help \
53 --long size \
54 --long unpack \
55 --long unpackearly \
56 --long verbose \
57 -- "$@")
58
59 if (( $? != 0 )); then
60 usage
61 exit 1
62 fi
63
64 eval set -- "$TEMP"
65
66 while (($# > 0)); do
67 case $1 in
68 -k|--kver) KERNEL_VERSION="$2"; shift;;
69 -f|--file) filenames[${2#/}]=1; shift;;
70 -s|--size) sorted=1;;
71 -h|--help) usage; exit 0;;
72 -m|--mod) modules=1;;
73 -v|--verbose) verbose="--verbose";;
74 --unpack) unpack=1;;
75 --unpackearly) unpackearly=1;;
76 --) shift;break;;
77 *) usage; exit 1;;
78 esac
79 shift
80 done
81
82 [[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
83
84 if [[ $1 ]]; then
85 image="$1"
86 if ! [[ -f "$image" ]]; then
87 {
88 echo "$image does not exist"
89 echo
90 } >&2
91 usage
92 exit 1
93 fi
94 else
95 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
96
97 if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
98 && [[ $MACHINE_ID ]] \
99 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
100 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
101 else
102 image="/boot/initramfs-${KERNEL_VERSION}.img"
103 fi
104 fi
105
106 shift
107 while (($# > 0)); do
108 filenames[${1#/}]=1;
109 shift
110 done
111
112 if ! [[ -f "$image" ]]; then
113 {
114 echo "No <initramfs file> specified and the default image '$image' cannot be accessed!"
115 echo
116 } >&2
117 usage
118 exit 1
119 fi
120
121 dracutlibdirs() {
122 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
123 echo "$d/$1"
124 done
125 }
126
127 extract_files()
128 {
129 (( ${#filenames[@]} == 1 )) && nofileinfo=1
130 for f in "${!filenames[@]}"; do
131 [[ $nofileinfo ]] || echo "initramfs:/$f"
132 [[ $nofileinfo ]] || echo "========================================================================"
133 $CAT $image | cpio --extract --verbose --quiet --to-stdout $f 2>/dev/null
134 ((ret+=$?))
135 [[ $nofileinfo ]] || echo "========================================================================"
136 [[ $nofileinfo ]] || echo
137 done
138 }
139
140 list_modules()
141 {
142 echo "dracut modules:"
143 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
144 $(dracutlibdirs modules.txt) 2>/dev/null
145 ((ret+=$?))
146 }
147
148 list_files()
149 {
150 echo "========================================================================"
151 if [ "$sorted" -eq 1 ]; then
152 $CAT "$image" | cpio --extract --verbose --quiet --list | sort -n -k5
153 else
154 $CAT "$image" | cpio --extract --verbose --quiet --list | sort -k9
155 fi
156 ((ret+=$?))
157 echo "========================================================================"
158 }
159
160 unpack_files()
161 {
162 $CAT "$image" | cpio -id --quiet $verbose
163 ((ret+=$?))
164 }
165
166
167 if (( ${#filenames[@]} <= 0 )) && [[ -z "$unpack" ]] && [[ -z "$unpackearly" ]]; then
168 echo "Image: $image: $(du -h $image | while read a b || [ -n "$a" ]; do echo $a;done)"
169 echo "========================================================================"
170 fi
171
172 read -N 6 bin < "$image"
173 case $bin in
174 $'\x71\xc7'*|070701)
175 CAT="cat --"
176 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
177 if [[ "$is_early" ]]; then
178 if [[ -n "$unpackearly" ]]; then
179 unpack_files
180 elif (( ${#filenames[@]} > 0 )); then
181 extract_files
182 else
183 echo "Early CPIO image"
184 list_files
185 fi
186 if [[ -d "$dracutbasedir/skipcpio" ]]; then
187 SKIP="$dracutbasedir/skipcpio/skipcpio"
188 else
189 SKIP="$dracutbasedir/skipcpio"
190 fi
191 if ! [[ -x $SKIP ]]; then
192 echo
193 echo "'$SKIP' not found, cannot display remaining contents!" >&2
194 echo
195 exit 0
196 fi
197 fi
198 ;;
199 esac
200
201 if [[ $SKIP ]] ; then
202 bin="$($SKIP "$image" | { read -N 6 bin && echo "$bin" ; })"
203 else
204 read -N 6 bin < "$image"
205 fi
206 case $bin in
207 $'\x1f\x8b'*)
208 CAT="zcat --"
209 ;;
210 BZh*)
211 CAT="bzcat --"
212 ;;
213 $'\x71\xc7'*|070701)
214 CAT="cat --"
215 ;;
216 $'\x02\x21'*)
217 CAT="lz4 -d -c"
218 ;;
219 $'\x89'LZO$'\0'*)
220 CAT="lzop -d -c"
221 ;;
222 $'0xFD2FB528'*)
223 CAT="zstd -d -c"
224 ;;
225 *)
226 if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
227 CAT="xzcat --single-stream --"
228 else
229 CAT="xzcat --"
230 fi
231 ;;
232 esac
233
234 skipcpio()
235 {
236 $SKIP "$@" | $ORIG_CAT
237 }
238
239 if [[ $SKIP ]]; then
240 ORIG_CAT="$CAT"
241 CAT=skipcpio
242 fi
243
244 ret=0
245
246 if [[ -n "$unpack" ]]; then
247 unpack_files
248 elif (( ${#filenames[@]} > 0 )); then
249 extract_files
250 else
251 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
252 $(dracutlibdirs 'dracut-*') 2>/dev/null)
253 ((ret+=$?))
254 echo "Version: $version"
255 echo
256 if [ "$modules" -eq 1 ]; then
257 list_modules
258 echo "========================================================================"
259 else
260 echo -n "Arguments: "
261 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
262 $(dracutlibdirs build-parameter.txt) 2>/dev/null
263 echo
264 list_modules
265 list_files
266 fi
267 fi
268
269 exit $ret