Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 163 lines (139 sloc) 4.362 kb
fedd108 Dave Reisner 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 Dave Reisner lsinitramfs: add -a option for analyze
authored
26 shopt -s extglob
27
fedd108 Dave Reisner add new util: lsinitramfs
authored
28 die() {
29 local mesg=$1; shift
46acac0 Dave Reisner lsinitramfs: fix variable name mismatch
authored
30 printf "error: $mesg\n" "$@"
fedd108 Dave Reisner add new util: lsinitramfs
authored
31 exit 1
32 }
33
34 usage() {
35 cat<<USAGE
96bac76 Dave Reisner inject VERSION into geninit and lsinitramfs
authored
36 lsinitramfs %VERSION%
fedd108 Dave Reisner add new util: lsinitramfs
authored
37 usage: ${0##*/} [options] <initramfs>
38
39 Options:
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
40 -a analyze contents
fedd108 Dave Reisner add new util: lsinitramfs
authored
41 -h display this help
8d2b835 Dave Reisner switch verbosity. be quiet by default
authored
42 -v more verbose output
fedd108 Dave Reisner add new util: lsinitramfs
authored
43 -x extract image to disk
44
45 USAGE
46 exit 1
47 }
48
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
49 in_array() {
50 local item needle=$1; shift
51
52 for item; do
53 [[ "$item" == $needle ]] && return 0 # Found
54 done
55 return 1 # Not Found
56 }
57
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
58 decomp() {
59 ${compress:-cat} ${compress:+-cd} "$@"
60 }
61
8d2b835 Dave Reisner switch verbosity. be quiet by default
authored
62 declare verbose=
fedd108 Dave Reisner add new util: lsinitramfs
authored
63 declare list='--list'
64
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
65 while getopts ':ahvx' flag; do
fedd108 Dave Reisner add new util: lsinitramfs
authored
66 case $flag in
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
67 a) analyze=1 ;;
fedd108 Dave Reisner add new util: lsinitramfs
authored
68 h) usage ;;
8d2b835 Dave Reisner switch verbosity. be quiet by default
authored
69 v) verbose='--verbose' ;;
fedd108 Dave Reisner add new util: lsinitramfs
authored
70 x) unset list ;;
71 \?) die "invalid option -- '$OPTARG'" ;;
72 esac
73 done
74 shift $(( OPTIND - 1 ))
75
76 declare image=$1
77
78 [[ $image ]] || usage
79 [[ -f $image ]] || die "$image: No such file"
80
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
81 # read compression type
82 case "$(file -b "$image")" in
83 data|LZMA*) compress=lzma ;;
84 gzip*) compress=gzip ;;
85 bzip2*) compress=bzip2 ;;
86 lzop*) compress=lzop ;;
87 XZ*) compress=xz ;;
88 esac
89
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
90 if (( analyze )); then
91 declare -a binaries explicitmod modules foundhooks hooks
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
92 declare kernver ratio
efbaef2 Dave Reisner lsinitramfs: add -a option for analyze
authored
93
e35b298 Dave Reisner lsinitramfs: add compression ratio and decomp time
authored
94 # calculate compression ratio
95 if [[ $compress ]]; then
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
96 TIMEFORMAT=%R decomptime=$({ time decomp "$image" >/dev/null; } 2>&1 )
981e2c1 Dave Reisner lsinitramfs: display ratio as a decimal rather than a percent
authored
97 ratio=.$(( $(stat -c %s "$image") * 1000 /
98 $(decomp "$image" | bsdtar xOf - | wc -c) % 1000 ))
e35b298 Dave Reisner lsinitramfs: add compression ratio and decomp time
authored
99 fi
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
100
101 # read contents of image
102 while read -r line; do
103 if [[ $line = *.ko?(.gz) ]]; then # module
6cf8c15 Dave Reisner lsinitramfs: read kernel version and compression type
authored
104 if [[ -z $kernver ]]; then
105 [[ $line =~ /lib/modules/([^/]+)/ ]] && kernver=${BASH_REMATCH[1]}
106 fi
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
107 line=${line##*/}
108 modules+=("${line%.ko?(.gz)}")
109 continue
110 elif [[ $line = ./hooks/* ]]; then
111 foundhooks+=("${line##*/}")
f6d12d0 Dave Reisner lsinitramfs: dont include dirs in binary listing
authored
112 elif [[ $line = *@(/bin/|/sbin/)* ]]; then
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
113 binaries+=("${line#.}")
114 fi
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
115 done < <(decomp "$image" | bsdtar tf -)
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
116
117 # extract and read config
118 while read -r -a line; do
119 case "${line[0]}" in
120 "%HOOKS%")
121 for hook in "${line[@]:1}"; do
122 in_array "$hook" "${foundhooks[@]}" && hooks=("$hook")
123 done
124 ;;
125 "%MODULES%") explicitmod=("${line[@]:2}") ;;
126 esac
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
127 done < <(decomp "$image" | bsdtar xOf - config)
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
128
129 # print results
e35b298 Dave Reisner lsinitramfs: add compression ratio and decomp time
authored
130 printf '==> Image: %s\n' "$(readlink -e "$image")"
6cf8c15 Dave Reisner lsinitramfs: read kernel version and compression type
authored
131 printf '==> Kernel: %s\n' "${kernver:-unknown}"
e35b298 Dave Reisner lsinitramfs: add compression ratio and decomp time
authored
132
133 if [[ $compress ]]; then
134 printf '==> Compressed with: %s\n' "$compress"
135 printf ' -> Compression ratio: %s\n' "$ratio"
136 printf ' -> Estimated decompression time: %ss\n' "$decomptime"
137 fi
6cf8c15 Dave Reisner lsinitramfs: read kernel version and compression type
authored
138 printf '\n'
139
21b3ec9 Dave Reisner lsinitramfs: dont assume the image has modules
authored
140 if (( ${#modules[*]} )); then
141 printf '==> Included modules:\n'
142 for mod in "${modules[@]}"; do
143 printf ' %s' "$mod"
144 in_array "${mod//_/-}" "${explicitmod[@]//_/-}" && printf ' [explicit]'
145 printf '\n'
b7d7470 Dave Reisner lsinitramfs: sort and columnize module output
authored
146 done | sort | column -c$(tput cols)
9c4602b Dave Reisner lsinitramfs: mark modules as explicit in included list
authored
147 printf '\n'
21b3ec9 Dave Reisner lsinitramfs: dont assume the image has modules
authored
148 fi
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
149
150 printf '==> Included binaries:\n'
151 printf ' %s\n' "${binaries[@]}"
152 printf '\n'
153
154 if (( ${#hooks[*]} )); then
155 printf '==> Hook run order:\n'
156 printf ' %s\n' "${hooks[@]}"
157 printf '\n'
158 fi
159 else
1a5e61c Dave Reisner Add support for LZO compression via lzop
authored
160 decomp "$image" | bsdcpio -i --quiet $verbose $list
f79f235 Dave Reisner lsinitramfs: add -a option for analyze
authored
161 fi
162
Something went wrong with that request. Please try again.