]> git.ipfire.org Git - thirdparty/dracut.git/blame - lsinitrd.sh
TEST-15-BTRFSRAID: install /etc/os-release
[thirdparty/dracut.git] / lsinitrd.sh
CommitLineData
3b403b32 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
4#
5# Copyright 2005-2010 Harald Hoyer <harald@redhat.com>
6# Copyright 2005-2010 Red Hat, Inc. All rights reserved.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
074d12c1 21
f7bccf37
HH
22usage()
23{
dde2db3d 24 {
7d9bb76a
HH
25 echo "Usage: ${0##*/} [options] [<initramfs file> [<filename> [<filename> [...] ]]]"
26 echo "Usage: ${0##*/} [options] -k <kernel version>"
dde2db3d 27 echo
7d9bb76a
HH
28 echo "-h, --help print a help message and exit."
29 echo "-s, --size sort the contents of the initramfs by size."
30 echo "-f, --file <filename> print the contents of <filename>."
31 echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
dde2db3d
HH
32 echo
33 } >&2
f7bccf37
HH
34}
35
815b9c16
HH
36
37[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
38
3ea5d2e2 39sorted=0
7d9bb76a
HH
40declare -A filenames
41
42unset POSIXLY_CORRECT
43TEMP=$(getopt \
44 -o "shf:k:" \
45 --long kver: \
46 --long file: \
47 --long help \
48 --long size \
49 -- "$@")
50
51if (( $? != 0 )); then
52 usage
53 exit 1
54fi
55
56eval set -- "$TEMP"
57
58while (($# > 0)); do
59 case $1 in
60 -k|--kver) KERNEL_VERSION="$2"; shift;;
61 -f|--file) filenames[${2#/}]=1; shift;;
62 -s|--size) sorted=1;;
63 -h|--help) usage; exit 0;;
64 --) shift;break;;
65 *) usage; exit 1;;
3ea5d2e2 66 esac
7d9bb76a 67 shift
3ea5d2e2 68done
3ea5d2e2 69
7d9bb76a 70[[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
dde2db3d 71
7d9bb76a 72if [[ $1 ]]; then
dde2db3d
HH
73 image="$1"
74 if ! [[ -f "$image" ]]; then
75 {
76 echo "$image does not exist"
77 echo
78 } >&2
79 usage
80 exit 1
81 fi
dde2db3d 82else
727e68d0
HH
83 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
84
7d9bb76a
HH
85 if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
86 && [[ $MACHINE_ID ]] \
87 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
727e68d0
HH
88 image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
89 else
d928724c 90 image="/boot/initramfs-${KERNEL_VERSION}.img"
727e68d0 91 fi
dde2db3d
HH
92fi
93
7d9bb76a
HH
94shift
95while (($# > 0)); do
96 filenames[${1#/}]=1;
97 shift
98done
727e68d0 99
dde2db3d
HH
100if ! [[ -f "$image" ]]; then
101 {
102 echo "No <initramfs file> specified and the default image '$image' cannot be accessed!"
103 echo
104 } >&2
105 usage
106 exit 1
107fi
074d12c1 108
815b9c16
HH
109extract_files()
110{
111 (( ${#filenames[@]} == 1 )) && nofileinfo=1
112 for f in ${!filenames[@]}; do
113 [[ $nofileinfo ]] || echo "initramfs:/$f"
114 [[ $nofileinfo ]] || echo "========================================================================"
115 $CAT $image | cpio --extract --verbose --quiet --to-stdout $f 2>/dev/null
116 ((ret+=$?))
117 [[ $nofileinfo ]] || echo "========================================================================"
118 [[ $nofileinfo ]] || echo
119 done
120}
121
122list_files()
123{
124 echo "========================================================================"
125 if [ "$sorted" -eq 1 ]; then
126 $CAT "$image" | cpio --extract --verbose --quiet --list | sort -n -k5
127 else
128 $CAT "$image" | cpio --extract --verbose --quiet --list | sort -k9
129 fi
130 ((ret+=$?))
131 echo "========================================================================"
132}
133
134
135if (( ${#filenames[@]} <= 0 )); then
136 echo "Image: $image: $(du -h $image | while read a b; do echo $a;done)"
137 echo "========================================================================"
138fi
139
884e1cda 140read -N 6 bin < "$image"
815b9c16
HH
141case $bin in
142 $'\x71\xc7'*|070701)
143 CAT="cat --"
144 is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
145 if [[ "$is_early" ]]; then
146 if (( ${#filenames[@]} > 0 )); then
147 extract_files
148 else
149 echo "Early CPIO image"
150 list_files
151 fi
152 SKIP="$dracutbasedir/skipcpio"
153 if ! [[ -x $SKIP ]]; then
154 echo
155 echo "'$SKIP' not found, cannot display remaining contents!" >&2
156 echo
157 exit 0
158 fi
159 fi
160 ;;
161esac
162
815b9c16 163
70311156
HH
164CAT=$({
165 if [[ $SKIP ]]; then
166 $SKIP "$image"
167 else
168 cat "$image"
169 fi } | {
170 read -N 6 bin
171 case $bin in
172 $'\x1f\x8b'*)
173 echo "zcat --"
174 ;;
175 BZh*)
176 echo "bzcat --"
177 ;;
178 $'\x71\xc7'*|070701)
179 echo "cat --"
180 ;;
07c4a2fc 181 $'\x02\x21'*)
70311156
HH
182 echo "lz4 -d -c"
183 ;;
491dbfe0
TG
184 $'\x89'LZO$'\0'*)
185 echo "lzop -d -c"
186 ;;
70311156
HH
187 *)
188 if echo "test"|xz|xzcat --single-stream >/dev/null 2>&1; then
189 echo "xzcat --single-stream --"
190 else
191 echo "xzcat --"
192 fi
193 ;;
194 esac
195 })
66fe35eb 196
815b9c16
HH
197skipcpio()
198{
199 $SKIP "$@" | $ORIG_CAT
200}
201
202if [[ $SKIP ]]; then
203 ORIG_CAT="$CAT"
204 CAT=skipcpio
205fi
206
884e1cda
HH
207ret=0
208
4460416a 209if (( ${#filenames[@]} > 0 )); then
815b9c16 210 extract_files
3ea5d2e2 211else
815b9c16 212 version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- 'lib/dracut/dracut-*' 'usr/lib/dracut/dracut-*' 2>/dev/null)
4460416a
HH
213 ((ret+=$?))
214 echo "$version with dracut modules:"
a0b85db1 215 $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- '*lib/dracut/modules.txt' 2>/dev/null
4460416a 216 ((ret+=$?))
815b9c16 217 list_files
3ea5d2e2 218fi
4460416a 219
884e1cda 220exit $ret