]> git.ipfire.org Git - thirdparty/dracut.git/blame - lsinitrd.sh
lsinitrd: allow to only unpack certain files
[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
HH
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."
7d9bb76a 33 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
dde2db3d
HH
34 echo
35 } >&2
f7bccf37
HH
36}
37
b208aad5
HH
38
39[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
40
3ea5d2e2 41sorted=0
05d2a145 42modules=0
97bbba69 43unset verbose
7d9bb76a
HH
44declare -A filenames
45
46unset POSIXLY_CORRECT
47TEMP=$(getopt \
97bbba69 48 -o "vshmf:k:" \
7d9bb76a
HH
49 --long kver: \
50 --long file: \
05d2a145 51 --long mod \
7d9bb76a
HH
52 --long help \
53 --long size \
97bbba69
HH
54 --long unpack \
55 --long unpackearly \
56 --long verbose \
7d9bb76a
HH
57 -- "$@")
58
59if (( $? != 0 )); then
60 usage
61 exit 1
62fi
63
64eval set -- "$TEMP"
65
66while (($# > 0)); do
67 case $1 in
97bbba69
HH
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;;
3ea5d2e2 78 esac
7d9bb76a 79 shift
3ea5d2e2 80done
3ea5d2e2 81
7d9bb76a 82[[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
dde2db3d 83
7d9bb76a 84if [[ $1 ]]; then
dde2db3d
HH
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
dde2db3d 94else
727e68d0
HH
95 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
96
7d9bb76a
HH
97 if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
98 && [[ $MACHINE_ID ]] \
99 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
727e68d0
HH
100 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
101 else
d928724c 102 image="/boot/initramfs-${KERNEL_VERSION}.img"
727e68d0 103 fi
dde2db3d
HH
104fi
105
7d9bb76a
HH
106shift
107while (($# > 0)); do
108 filenames[${1#/}]=1;
109 shift
110done
727e68d0 111
dde2db3d
HH
112if ! [[ -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
119fi
074d12c1 120
d3be9275
121dracutlibdirs() {
122 for d in lib64/dracut lib/dracut usr/lib64/dracut usr/lib/dracut; do
123 echo "$d/$1"
124 done
125}
126
b208aad5
HH
127extract_files()
128{
129 (( ${#filenames[@]} == 1 )) && nofileinfo=1
3721635b 130 for f in "${!filenames[@]}"; do
b208aad5
HH
131 [[ $nofileinfo ]] || echo "initramfs:/$f"
132 [[ $nofileinfo ]] || echo "========================================================================"
8379784a 133 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --to-stdout "$f" 2>/dev/null
b208aad5
HH
134 ((ret+=$?))
135 [[ $nofileinfo ]] || echo "========================================================================"
136 [[ $nofileinfo ]] || echo
137 done
138}
139
05d2a145
HB
140list_modules()
141{
142 echo "dracut modules:"
d3be9275
143 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
144 $(dracutlibdirs modules.txt) 2>/dev/null
05d2a145
HB
145 ((ret+=$?))
146}
147
b208aad5
HH
148list_files()
149{
150 echo "========================================================================"
151 if [ "$sorted" -eq 1 ]; then
bce6823a 152 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
b208aad5 153 else
bce6823a 154 $CAT "$image" 2>/dev/null | cpio --extract --verbose --quiet --list | sort -k9
b208aad5
HH
155 fi
156 ((ret+=$?))
157 echo "========================================================================"
158}
159
97bbba69
HH
160unpack_files()
161{
f81c864e
KS
162 if (( ${#filenames[@]} > 0 )); then
163 for f in "${!filenames[@]}"; do
164 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose $f
165 ((ret+=$?))
166 done
167 else
168 $CAT "$image" 2>/dev/null | cpio -id --quiet $verbose
169 ((ret+=$?))
170 fi
97bbba69
HH
171}
172
b208aad5 173
97bbba69 174if (( ${#filenames[@]} <= 0 )) && [[ -z "$unpack" ]] && [[ -z "$unpackearly" ]]; then
6d58fa27 175 echo "Image: $image: $(du -h $image | while read a b || [ -n "$a" ]; do echo $a;done)"
b208aad5
HH
176 echo "========================================================================"
177fi
178
884e1cda 179read -N 6 bin < "$image"
b208aad5
HH
180case $bin in
181 $'\x71\xc7'*|070701)
18a5011f 182 CAT="cat --"
b208aad5
HH
183 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
184 if [[ "$is_early" ]]; then
f81c864e
KS
185 if [[ -n "$unpack" ]]; then
186 # should use --unpackearly for early CPIO
187 :
188 elif [[ -n "$unpackearly" ]]; then
97bbba69
HH
189 unpack_files
190 elif (( ${#filenames[@]} > 0 )); then
b208aad5
HH
191 extract_files
192 else
193 echo "Early CPIO image"
194 list_files
195 fi
98fd0693
HH
196 if [[ -d "$dracutbasedir/skipcpio" ]]; then
197 SKIP="$dracutbasedir/skipcpio/skipcpio"
198 else
199 SKIP="$dracutbasedir/skipcpio"
200 fi
b208aad5
HH
201 if ! [[ -x $SKIP ]]; then
202 echo
203 echo "'$SKIP' not found, cannot display remaining contents!" >&2
204 echo
205 exit 0
206 fi
207 fi
208 ;;
209esac
210
3ce14286
HD
211if [[ $SKIP ]] ; then
212 bin="$($SKIP "$image" | { read -N 6 bin && echo "$bin" ; })"
213else
214 read -N 6 bin < "$image"
215fi
216case $bin in
217 $'\x1f\x8b'*)
218 CAT="zcat --"
219 ;;
220 BZh*)
221 CAT="bzcat --"
222 ;;
223 $'\x71\xc7'*|070701)
18a5011f 224 CAT="cat --"
3ce14286
HD
225 ;;
226 $'\x02\x21'*)
227 CAT="lz4 -d -c"
228 ;;
229 $'\x89'LZO$'\0'*)
230 CAT="lzop -d -c"
231 ;;
ebfd53e1 232 $'\x28\xB5\x2F\xFD'*)
7dbadcc7
TPG
233 CAT="zstd -d -c"
234 ;;
3ce14286
HD
235 *)
236 if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
237 CAT="xzcat --single-stream --"
fd9f9024 238 else
3ce14286
HD
239 CAT="xzcat --"
240 fi
241 ;;
242esac
66fe35eb 243
b208aad5
HH
244skipcpio()
245{
246 $SKIP "$@" | $ORIG_CAT
247}
248
249if [[ $SKIP ]]; then
250 ORIG_CAT="$CAT"
251 CAT=skipcpio
252fi
253
884e1cda
HH
254ret=0
255
97bbba69
HH
256if [[ -n "$unpack" ]]; then
257 unpack_files
258elif (( ${#filenames[@]} > 0 )); then
b208aad5 259 extract_files
3ea5d2e2 260else
d3be9275
261 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
262 $(dracutlibdirs 'dracut-*') 2>/dev/null)
4460416a 263 ((ret+=$?))
ae01bd18 264 echo "Version: $version"
b208aad5 265 echo
05d2a145
HB
266 if [ "$modules" -eq 1 ]; then
267 list_modules
268 echo "========================================================================"
269 else
270 echo -n "Arguments: "
d3be9275
271 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- \
272 $(dracutlibdirs build-parameter.txt) 2>/dev/null
05d2a145
HB
273 echo
274 list_modules
275 list_files
276 fi
3ea5d2e2 277fi
4460416a 278
884e1cda 279exit $ret