1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
From 7d9bb76ac7cf6996318a0cfbc8576d8d307bff3e Mon Sep 17 00:00:00 2001
From: Harald Hoyer <harald@redhat.com>
Date: Tue, 16 Apr 2013 10:44:56 +0200
Subject: [PATCH] lsinitrd: switch to getopt and add "-f" and "-k" parameter
---
lsinitrd.1.asc | 10 +++++++++-
lsinitrd.sh | 61 +++++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/lsinitrd.1.asc b/lsinitrd.1.asc
index fd98161..4293910 100644
--- a/lsinitrd.1.asc
+++ b/lsinitrd.1.asc
@@ -10,7 +10,9 @@ lsinitrd - tool to show the contents of an initramfs image
SYNOPSIS
--------
-*lsinitrd* ['OPTION...'] [<image>]
+*lsinitrd* ['OPTION...'] [<image> [<filename> [<filename> [...] ]]]
+
+*lsinitrd* ['OPTION...'] -k <kernel-version>
DESCRIPTION
-----------
@@ -26,6 +28,12 @@ OPTIONS
**-s, --size**::
sort the contents of the initramfs by size.
+**-f, --file** _<filename>_::
+ print the contents of <filename>.
+
+**-k, --kver** _<kernel version>_::
+ inspect the initramfs of <kernel version>.
+
AVAILABILITY
------------
The lsinitrd command is part of the dracut package and is available from
diff --git a/lsinitrd.sh b/lsinitrd.sh
index 1b27393..42e30d9 100755
--- a/lsinitrd.sh
+++ b/lsinitrd.sh
@@ -22,29 +22,51 @@
usage()
{
{
- echo "Usage: ${0##*/} [-s] [<initramfs file> [<filename>]]"
+ echo "Usage: ${0##*/} [options] [<initramfs file> [<filename> [<filename> [...] ]]]"
+ echo "Usage: ${0##*/} [options] -k <kernel version>"
echo
- echo "-h, --help print a help message and exit."
- echo "-s, --size sort the contents of the initramfs by size."
+ echo "-h, --help print a help message and exit."
+ echo "-s, --size sort the contents of the initramfs by size."
+ echo "-f, --file <filename> print the contents of <filename>."
+ echo "-k, --kver <kernel version> inspect the initramfs of <kernel version>."
echo
} >&2
}
-[[ $# -le 2 ]] || { usage ; exit 1 ; }
-
sorted=0
-while getopts "s" opt; do
- case $opt in
- s) sorted=1;;
- h) usage; exit 0;;
- \?) usage; exit 1;;
+declare -A filenames
+
+unset POSIXLY_CORRECT
+TEMP=$(getopt \
+ -o "shf:k:" \
+ --long kver: \
+ --long file: \
+ --long help \
+ --long size \
+ -- "$@")
+
+if (( $? != 0 )); then
+ usage
+ exit 1
+fi
+
+eval set -- "$TEMP"
+
+while (($# > 0)); do
+ case $1 in
+ -k|--kver) KERNEL_VERSION="$2"; shift;;
+ -f|--file) filenames[${2#/}]=1; shift;;
+ -s|--size) sorted=1;;
+ -h|--help) usage; exit 0;;
+ --) shift;break;;
+ *) usage; exit 1;;
esac
+ shift
done
-shift $((OPTIND-1))
-KERNEL_VERSION="$(uname -r)"
+[[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
-if [[ "$1" ]]; then
+if [[ $1 ]]; then
image="$1"
if ! [[ -f "$image" ]]; then
{
@@ -57,13 +79,20 @@ if [[ "$1" ]]; then
else
[[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
- if [[ $MACHINE_ID ]] && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
+ if [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
+ && [[ $MACHINE_ID ]] \
+ && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]] ; then
image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
else
image="/boot/initramfs-${KERNEL_VERSION}.img"
fi
fi
+shift
+while (($# > 0)); do
+ filenames[${1#/}]=1;
+ shift
+done
if ! [[ -f "$image" ]]; then
{
@@ -93,8 +122,8 @@ elif [[ "$FILE_T" =~ :\ data ]]; then
CAT="xzcat $XZ_SINGLE_STREAM"
fi
-if [[ $# -eq 2 ]]; then
- $CAT $image | cpio --extract --verbose --quiet --to-stdout ${2#/} 2>/dev/null
+if (( ${#filenames[@]} > 0 )); then
+ $CAT $image | cpio --extract --verbose --quiet --to-stdout ${!filenames[@]} 2>/dev/null
exit $?
fi
|