Skip to content
Newer
Older
100755 129 lines (108 sloc) 3.22 KB
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
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 May 15, 2011
26 shopt -s extglob
27
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
28 die() {
29 local mesg=$1; shift
30 printf "error: $fmt\n" "$@"
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 May 15, 2011
39 -a analyze contents
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
40 -h display this help
8d2b835 @falconindy switch verbosity. be quiet by default
authored May 10, 2011
41 -v more verbose output
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
42 -x extract image to disk
43
44 USAGE
45 exit 1
46 }
47
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored May 15, 2011
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
8d2b835 @falconindy switch verbosity. be quiet by default
authored May 11, 2011
57 declare verbose=
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
58 declare list='--list'
59
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored May 15, 2011
60 while getopts ':ahvx' flag; do
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
61 case $flag in
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored May 15, 2011
62 a) analyze=1 ;;
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
63 h) usage ;;
8d2b835 @falconindy switch verbosity. be quiet by default
authored May 11, 2011
64 v) verbose='--verbose' ;;
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
65 x) unset list ;;
66 \?) die "invalid option -- '$OPTARG'" ;;
67 esac
68 done
69 shift $(( OPTIND - 1 ))
70
71 declare image=$1
72
73 [[ $image ]] || usage
74 [[ -f $image ]] || die "$image: No such file"
75
f79f235 @falconindy lsinitramfs: add -a option for analyze
authored May 15, 2011
76 if (( analyze )); then
77 declare -a binaries explicitmod modules foundhooks hooks
78
79 # read contents of image
80 while read -r line; do
81 if [[ $line = *.ko?(.gz) ]]; then # module
82 line=${line##*/}
83 modules+=("${line%.ko?(.gz)}")
84 continue
85 elif [[ $line = ./hooks/* ]]; then
86 foundhooks+=("${line##*/}")
87 elif [[ $line = *@(/bin|/sbin)* ]]; then
88 binaries+=("${line#.}")
89 fi
90 done < <(bsdcpio -i --quiet --list <"$image")
91
92 # extract and read config
93 while read -r -a line; do
94 case "${line[0]}" in
95 "%HOOKS%")
96 for hook in "${line[@]:1}"; do
97 in_array "$hook" "${foundhooks[@]}" && hooks=("$hook")
98 done
99 ;;
100 "%MODULES%") explicitmod=("${line[@]:2}") ;;
101 esac
102 done < <(bsdtar Oxf "$image" config)
103
104 # print results
105 printf '==> Included modules:\n'
106 printf ' %s\n' "${modules[@]}"
107 printf '\n'
108
109 printf '==> Included binaries:\n'
110 printf ' %s\n' "${binaries[@]}"
111 printf '\n'
112
113 if (( ${#hooks[*]} )); then
114 printf '==> Hook run order:\n'
115 printf ' %s\n' "${hooks[@]}"
116 printf '\n'
117 fi
118
119 if (( ${#explicitmod[*]} )); then
120 printf '==> Explicitly loaded modules:\n'
121 printf ' %s\n' "${explicitmod[@]}"
122 printf '\n'
123 fi
124 else
125 bsdcpio -i --quiet $verbose $list <"$image"
126 fi
127
fedd108 @falconindy add new util: lsinitramfs
authored May 10, 2011
128
Something went wrong with that request. Please try again.