-
Notifications
You must be signed in to change notification settings - Fork 1
/
initramfs-extract
executable file
·186 lines (138 loc) · 4.16 KB
/
initramfs-extract
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
################################################################################
### Head: Usage
##
##
## $ initramfs-extract
## $ initramfs-extract initrd.img initrd-root
##
##
### Tail: Usage
################################################################################
################################################################################
### Head: Link
##
##
## * https://github.com/samwhelp/tool-initramfs-extract/blob/main/src/asset/bin/initramfs-extract
## * https://unix.stackexchange.com/questions/163346/why-is-it-that-my-initrd-only-has-one-directory-namely-kernel
## * https://www.ubuntu-tw.org/modules/newbb/viewtopic.php?post_id=362052#forumpost362052
##
##
### Tail: Link
################################################################################
################################################################################
### Head: Init
##
set -e ## for Exit immediately if a command exits with a non-zero status.
THE_BASE_DIR_PATH="$(cd -- "$(dirname -- "$0")" ; pwd)"
THE_CMD_FILE_NAME="$(basename "$0")"
##
### Tail: Init
################################################################################
################################################################################
### Head: Util / Debug
##
util_error_echo () {
echo "$@" 1>&2
}
##
### Head: Util / Debug
################################################################################
################################################################################
### Head: Util / initramfs
##
util_initramfs_extract_core () {
if [[ $(wc -c "$1" | cut -d ' ' -f1) -eq 0 ]]; then
return
fi
local type=$(cat "$1" | file -)
local temp_file=$(date +%s.%N)
util_error_echo -e "\n$type"
if [[ "$type" =~ .*cpio.* ]]; then
cat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*gzip.* ]]; then
zcat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*XZ.* ]]; then
xzcat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*bzip2.* ]]; then
bzcat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*Zstandard.* ]]; then
zstdcat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*LZMA.* ]]; then
lzcat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*LZ4.* ]]; then
lz4cat "$1" | (cpio -id; cat > "$temp_file")
elif [[ "$type" =~ .*lzop.* ]]; then
util_error_echo "TODO"
return
else
return
fi
util_initramfs_extract_core $temp_file
rm $temp_file
}
util_initramfs_extract () {
local initrd_file_path="$(makesure_initrd_file_path "$1")"
local rootfs_dir_path="$(makesure_rootfs_dir_path "$2")"
if ! [ -a "$initrd_file_path" ]; then
util_error_echo
util_error_echo "##"
util_error_echo "## File Not Exist: $initrd_file_path"
util_error_echo "##"
util_error_echo
return
fi
mkdir -p "$rootfs_dir_path"
util_error_echo
util_error_echo "##"
util_error_echo "## cd $rootfs_dir_path"
util_error_echo "##"
util_error_echo
cd "$rootfs_dir_path"
util_error_echo
util_error_echo "##"
util_error_echo "## start extract"
util_error_echo "##"
util_error_echo
util_initramfs_extract_core "$initrd_file_path"
##
## $ man cd
##
util_error_echo
util_error_echo "##"
util_error_echo "## cd $OLDPWD"
util_error_echo "##"
util_error_echo
cd "$OLDPWD"
}
makesure_initrd_file_path () {
local initrd_file_path="$1"
if [ -z "$initrd_file_path" ]; then
initrd_file_path='initrd.img'
#util_error_echo "Use default initrd_file_path: $initrd_file_path"
fi
initrd_file_path="$(realpath "$initrd_file_path")"
echo "$initrd_file_path"
}
makesure_rootfs_dir_path () {
local rootfs_dir_path="$1"
if [ -z "$rootfs_dir_path" ]; then
rootfs_dir_path='initrd-root'
#util_error_echo "Use default rootfs_dir_name: $rootfs_dir_path"
fi
rootfs_dir_path="$(realpath "$rootfs_dir_path")"
echo "$rootfs_dir_path"
}
##
### Tail: Util / initramfs
################################################################################
################################################################################
### Head: Main
##
__main__ () {
util_initramfs_extract "$1" "$2"
}
__main__ "$@"
##
### Tail: Main
################################################################################