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