-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkinitramfs.sh
executable file
·175 lines (155 loc) · 4.04 KB
/
mkinitramfs.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
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
#!/bin/sh
# root device
ROOTDEV="mmcblk0p7"
# mount options
MOUNTOPTS="ro"
# the init program
INIT="/sbin/init"
# compression
COMPRESSION="gz"
# firmware archive
FW_ARCHIVE="./firmware_all.tar.gz"
# output file name
OUTFILE="initramfs.cpio"
help() {
echo "Usage: " $0 " [arguments]"
echo "Available options:"
echo " -h print this message"
echo " -o FILENAME write into FILENAME (uncompressed, default: $OUTFILE)"
echo " -d DEVICE the partition containing rootfs (default: $ROOTDEV)"
echo " -i INIT the init program to launch (default: $INIT)"
echo " -c COMP the compression format (default: $COMPRESSION)"
echo " -f FIRMWARE the path to a firmware archive (default: $FW_ARCHIVE)"
echo " -m MOUNTOPTS the mount opts (default: $MOUNTOPTS)"
echo ""
echo "By default, rootfs is on /data (mmcblk0p7), not in any subdir."
echo "Keep in mind that using a subdirectory might not always work right"
echo "and is considered EXPERIMENTAL, so use at your own risk. It exists"
echo "only to cover the case when you want to dual boot your system with"
echo "Android."
echo "The -d option allows you to choose the partition on which your root"
echo "filesystem is present. You might want to use this if you repartition"
echo "your device or if you installed the OS into /system (mmdcblk0p4)."
echo "Overriding -i is only for if your rootfs doesn't use /sbin/init."
echo "The choice of compression algorithmss is 'gz', 'none'."
}
while getopts o:d:i:c:f:m:h OPT; do
case $OPT in
o) OUTFILE=$OPTARG ;;
d) ROOTDEV=$OPTARG ;;
i) INIT=$OPTARG ;;
c) COMPRESSION=$OPTARG ;;
f) FW_ARCHIVE=$OPTARG ;;
m) MOUNTOPTS=$OPTARG ;;
h) help; exit 0 ;;
\?)
echo "Unrecognized option: $OPTARG"
help
exit 1
;;
esac
done
if [ ! -f "$FW_ARCHIVE" ]; then
echo "firmware archive does not exist, exitting..."
exit 1
fi
case $COMPRESSION in
none)
CMPCMD=""
OUTFILE_COMP="$OUTFILE"
;;
lz4)
CMPCMD="lz4c"
OUTFILE_COMP="$OUTFILE.lz4"
;;
gz)
CMPCMD="gzip"
OUTFILE_COMP="$OUTFILE.gz"
;;
xz)
CMPCMD="xz"
OUTFILE_COMP="$OUTFILE.xz"
;;
*)
echo "Unknown compression algorithm: $COMPRESSION"
help
exit 1
esac
compress() {
case $COMPRESSION in
gz) gzip -9 -c "$1" > "$2" ;;
*) true ;;
esac
if [ $? -ne 0 ]; then
echo "Compression failed, exitting..."
rm -f "$1" "$2"
rm -rf output
exit 1
fi
}
if [ -n "$CMPCMD" ] && [ ! -x "$(command -v $CMPCMD)" ]; then
echo "Compression tool '$CMPCMD' not found, exitting..."
exit 1
fi
echo "Generating initramfs for rootfs on partition $ROOTDEV with init $INIT..."
echo ""
# fetch busybox
./get_busybox.sh
if [ $? -ne 0 ]; then
echo "Failed getting busybox, exitting..."
exit 1
fi
# cleanup
echo ""
echo "Cleanup..."
rm -f "$OUTFILE" "$OUTFILE_COMP"
rm -rf output
# pre-populate ramdisk structure
echo "Creating directory structure..."
mkdir output
mkdir output/dev
mkdir output/lib
mkdir output/sys
mkdir -p output/mnt/root
# config file
echo "Generating config file..."
cat << EOF >> output/conf
export ROOTDEV="${ROOTDEV}"
export MOUNTOPTS="${MOUNTOPTS}"
export INIT="${INIT}"
EOF
echo "Copying binaries..."
cp skel/init downloaded/busybox output
echo "Extracting firmware..."
cd output
tar xvf "../$FW_ARCHIVE"
if [ $? -ne 0 ]; then
echo "Extracting firmware failed, exitting..."
cd ..
rm -rf output
exit 1
fi
cd ..
# list contents first
echo ""
echo "initramfs contents:"
cd output
find .
# create the ramdisk
echo ""
echo "Creating initramfs..."
find . | cpio -o -H newc > ../$OUTFILE
cd ..
# compress
if [ -n "$CMPCMD" ]; then
echo "Compressing initramfs..."
fi
compress "$OUTFILE" "$OUTFILE_COMP"
# cleanup
echo ""
echo "Final cleanup..."
rm -rf output
if [ "$OUTFILE" != "$OUTFILE_COMP" ]; then
rm -f "$OUTFILE"
fi
echo "Initramfs created: $OUTFILE_COMP"