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