Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 162 lines (138 sloc) 4.309 kB
fedd108 @falconindy add new util: lsinitramfs
authored
1 #!/bin/bash
2 #
3 # lsinitramfs - dump the contents of an initramfs image
4 #
5 # Copyright (C) 2011 by Dave Reisner <d@falconindy.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
26 shopt -s extglob
27
fedd108 @falconindy add new util: lsinitramfs
authored
28 die() {
29 local mesg=$1; shift
46acac0 @falconindy lsinitramfs: fix variable name mismatch
authored
30 printf "error: $mesg\n" "$@"
fedd108 @falconindy add new util: lsinitramfs
authored
31 exit 1
32 }
33
34 usage() {
35 cat<<USAGE
36 usage: ${0##*/} [options] <initramfs>
37
38 Options:
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
39 -a analyze contents
fedd108 @falconindy add new util: lsinitramfs
authored
40 -h display this help
8d2b835 @falconindy switch verbosity. be quiet by default
authored
41 -v more verbose output
fedd108 @falconindy add new util: lsinitramfs
authored
42 -x extract image to disk
43
44 USAGE
45 exit 1
46 }
47
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
48 in_array() {
49 local item needle=$1; shift
50
51 for item; do
52 [[ "$item" == $needle ]] && return 0 # Found
53 done
54 return 1 # Not Found
55 }
56
1a5e61c @falconindy Add support for LZO compression via lzop
authored
57 decomp() {
58 ${compress:-cat} ${compress:+-cd} "$@"
59 }
60
8d2b835 @falconindy switch verbosity. be quiet by default
authored
61 declare verbose=
fedd108 @falconindy add new util: lsinitramfs
authored
62 declare list='--list'
63
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
64 while getopts ':ahvx' flag; do
fedd108 @falconindy add new util: lsinitramfs
authored
65 case $flag in
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
66 a) analyze=1 ;;
fedd108 @falconindy add new util: lsinitramfs
authored
67 h) usage ;;
8d2b835 @falconindy switch verbosity. be quiet by default
authored
68 v) verbose='--verbose' ;;
fedd108 @falconindy add new util: lsinitramfs
authored
69 x) unset list ;;
70 \?) die "invalid option -- '$OPTARG'" ;;
71 esac
72 done
73 shift $(( OPTIND - 1 ))
74
75 declare image=$1
76
77 [[ $image ]] || usage
78 [[ -f $image ]] || die "$image: No such file"
79
1a5e61c @falconindy Add support for LZO compression via lzop
authored
80 # read compression type
81 case "$(file -b "$image")" in
82 data|LZMA*) compress=lzma ;;
83 gzip*) compress=gzip ;;
84 bzip2*) compress=bzip2 ;;
85 lzop*) compress=lzop ;;
86 XZ*) compress=xz ;;
87 esac
88
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
89 if (( analyze )); then
90 declare -a binaries explicitmod modules foundhooks hooks
1a5e61c @falconindy Add support for LZO compression via lzop
authored
91 declare kernver ratio
efbaef2 @falconindy lsinitramfs: add -a option for analyze
authored
92
e35b298 @falconindy lsinitramfs: add compression ratio and decomp time
authored
93 # calculate compression ratio
94 if [[ $compress ]]; then
1a5e61c @falconindy Add support for LZO compression via lzop
authored
95 TIMEFORMAT=%R decomptime=$({ time decomp "$image" >/dev/null; } 2>&1 )
981e2c1 @falconindy lsinitramfs: display ratio as a decimal rather than a percent
authored
96 ratio=.$(( $(stat -c %s "$image") * 1000 /
97 $(decomp "$image" | bsdtar xOf - | wc -c) % 1000 ))
e35b298 @falconindy lsinitramfs: add compression ratio and decomp time
authored
98 fi
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
99
100 # read contents of image
101 while read -r line; do
102 if [[ $line = *.ko?(.gz) ]]; then # module
6cf8c15 @falconindy lsinitramfs: read kernel version and compression type
authored
103 if [[ -z $kernver ]]; then
104 [[ $line =~ /lib/modules/([^/]+)/ ]] && kernver=${BASH_REMATCH[1]}
105 fi
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
106 line=${line##*/}
107 modules+=("${line%.ko?(.gz)}")
108 continue
109 elif [[ $line = ./hooks/* ]]; then
110 foundhooks+=("${line##*/}")
f6d12d0 @falconindy lsinitramfs: dont include dirs in binary listing
authored
111 elif [[ $line = *@(/bin/|/sbin/)* ]]; then
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
112 binaries+=("${line#.}")
113 fi
1a5e61c @falconindy Add support for LZO compression via lzop
authored
114 done < <(decomp "$image" | bsdtar tf -)
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
115
116 # extract and read config
117 while read -r -a line; do
118 case "${line[0]}" in
119 "%HOOKS%")
120 for hook in "${line[@]:1}"; do
121 in_array "$hook" "${foundhooks[@]}" && hooks=("$hook")
122 done
123 ;;
124 "%MODULES%") explicitmod=("${line[@]:2}") ;;
125 esac
1a5e61c @falconindy Add support for LZO compression via lzop
authored
126 done < <(decomp "$image" | bsdtar xOf - config)
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
127
128 # print results
e35b298 @falconindy lsinitramfs: add compression ratio and decomp time
authored
129 printf '==> Image: %s\n' "$(readlink -e "$image")"
6cf8c15 @falconindy lsinitramfs: read kernel version and compression type
authored
130 printf '==> Kernel: %s\n' "${kernver:-unknown}"
e35b298 @falconindy lsinitramfs: add compression ratio and decomp time
authored
131
132 if [[ $compress ]]; then
133 printf '==> Compressed with: %s\n' "$compress"
134 printf ' -> Compression ratio: %s\n' "$ratio"
135 printf ' -> Estimated decompression time: %ss\n' "$decomptime"
136 fi
6cf8c15 @falconindy lsinitramfs: read kernel version and compression type
authored
137 printf '\n'
138
21b3ec9 @falconindy lsinitramfs: dont assume the image has modules
authored
139 if (( ${#modules[*]} )); then
140 printf '==> Included modules:\n'
141 for mod in "${modules[@]}"; do
142 printf ' %s' "$mod"
143 in_array "${mod//_/-}" "${explicitmod[@]//_/-}" && printf ' [explicit]'
144 printf '\n'
145 done
9c4602b @falconindy lsinitramfs: mark modules as explicit in included list
authored
146 printf '\n'
21b3ec9 @falconindy lsinitramfs: dont assume the image has modules
authored
147 fi
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
148
149 printf '==> Included binaries:\n'
150 printf ' %s\n' "${binaries[@]}"
151 printf '\n'
152
153 if (( ${#hooks[*]} )); then
154 printf '==> Hook run order:\n'
155 printf ' %s\n' "${hooks[@]}"
156 printf '\n'
157 fi
158 else
1a5e61c @falconindy Add support for LZO compression via lzop
authored
159 decomp "$image" | bsdcpio -i --quiet $verbose $list
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored
160 fi
161
Something went wrong with that request. Please try again.