forked from jotaki/Extract-Kernel-Initramfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
138 lines (120 loc) · 3.72 KB
/
main.sh
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
####
# main invocation of this script
####
[ -z $SOURCED ] && \
function main()
{
local opt= mode=query output= image=
local imgo= tmpfile= offsets=
local imgfile=
while getopts :qxdo: opt; do
case "$opt" in
q)
mode=query
;;
x)
mode=cpio
;;
d)
mode=extract
;;
o)
[ -z $OPTARG ] && show_help
output=$OPTARG
;;
*)
show_help
;;
esac
done
## get kernel image
shift $((OPTIND-1))
image=$1
imgo="$PWD/$(basename "$image")"
## verify parameters
[ -z "$image" ] && show_help
[ "$mode" = "extract" ] && \
output=${output:-$imgo.root} ||
output=${output:-$imgo.cpio}
## verify output doesn't exist
if [ -e "$output" -a "$mode" != "query" ]; then
echo_error "The output path '$output' exists."
echo_error "Please remove '$output' by executing: "
builtin echo " rm -fr '$output'" >&2
exit 1
fi
## a header.
echo "Extract Kernel Initramfs (eki) v$VERSION"
echo "Based on http://tinyurl.com/nclkczx and " \
"http://tinyurl.com/49aos4h"
echo "Source available at" \
"https://github.com/jotaki/Extract-Kernel-Initramfs"
echo "============================="
echo "Bootstrapping..."
tmpfile=$(xmktemp kernel)
[ $? -eq 0 ] || exit 1
imgfile=$(xmktemp initramfs)
if [ $? -ne 0 ]; then
rm -rf -- "$tmpfile"
exit 1
fi
echo "Bootstrapping complete"
## "decompress" kernel image (really extracts it)
uncompress_kernel "$image" > "$tmpfile"
[ $? -eq 0 ] || cleantemp "$tmpfile" "$imgfile"
## find potential archive offsets
offsets=$(detect_compression "$tmpfile")
[ $? -eq 0 ] || cleantemp "$tmpfile" "$imgfile"
## attempt to open/verify archive
find_archive "$tmpfile" "$offsets" "$imgfile"
[ $? -eq 0 ] || cleantemp "$tmpfile" "$imgfile"
echo_debug "Inspecting mode..."
case "$mode" in
query)
echo_debug " > Querying archive"
cpio -t < "$imgfile" | less
echo_debug " > Finished"
;;
cpio)
echo_debug " > Copying file '$imgfile' to '$output'"
cp -af "$imgfile" "$output"
echo_debug " > Finished"
;;
extract)
if [ -f "$output" ]; then
echo_error "Invalid usage, expected directory at '$output'"
break;
fi
echo_debug "Creating directory '$output'"
mkdir -p "$output"
echo_debug "Changing directory to '$output'"
cd "$output" 2>/dev/null >&2
echo_debug "Extracting '$imgfile' to '$output'"
cpio --quiet -i \
--make-directories \
--preserve-modification-time \
--no-absolute-filenames 2>/dev/null >&2 \
< "$imgfile"
echo_debug "Image file extracted"
echo_debug "Changing directory to '$OLDPWD'"
cd - 2>/dev/null >&2
;;
*)
echo "Not sure what to do in this mode."
echo "Mode provided: '$mode'"
;;
esac
## inform the user where to find the archive/root
[ "$mode" != "query" ] && echo "Resulting output available in '$output'"
## Keep files?
if [ -z "$KEEP_FILES" ]; then
cleantemp "$tmpfile" "$imgfile"
else
echo "You have enabled KEEP_FILES"
echo "Left over files at:"
echo " > $tmpfile"
echo " > $imgfile"
fi
}
# source protect
[ -z $SOURCED ] && main $@