-
Notifications
You must be signed in to change notification settings - Fork 10
/
create_initramfs.sh
executable file
·294 lines (254 loc) · 8.75 KB
/
create_initramfs.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
if grep -q debug <<< "$@"; then
set -o xtrace
fi
set -o errexit
set -o pipefail
set -o nounset
#---------
# VARIABLES
#----------
# Defaults, can be overridden with env or args
ARCH=""
OVERLAY=""
OVERLAY_DST=""
INIT="true"
PRINTK="false"
TTY_DEV="hvc0 tty0"
CURPWD="$(pwd)"
DIR="/tmp/$(date +%-s%N)"
CACHE_DIR="${HOME}/.cache/initramfs"
CLEAN="true"
WGET="wget -q"
#URLs of scripts and files
rcS_url="https://raw.githubusercontent.com/buildroot/buildroot/master/package/initscripts/init.d/rcS"
rcK_url="https://raw.githubusercontent.com/buildroot/buildroot/master/package/initscripts/init.d/rcK"
network_url="https://raw.githubusercontent.com/buildroot/buildroot/master/package/ifupdown-scripts/S40network"
urandom_url="https://raw.githubusercontent.com/buildroot/buildroot/master/package/urandom-scripts/S20urandom"
passwd_url="https://raw.githubusercontent.com/buildroot/buildroot/master/system/skeleton/etc/passwd"
shadow_url="https://raw.githubusercontent.com/buildroot/buildroot/master/system/skeleton/etc/shadow"
group_url="https://raw.githubusercontent.com/buildroot/buildroot/master/system/skeleton/etc/group"
fstab_url="https://raw.githubusercontent.com/csmart/buildroot/master/system/skeleton/etc/fstab"
inittab_url="https://raw.githubusercontent.com/buildroot/buildroot/master/package/busybox/inittab"
# Get the distro we're running on
ID=""
source /etc/os-release 2>/dev/null
#----------
# FUNCTIONS
#----------
# Print usage and quit
usage() {
cat << EOF
Usage: $0 --arch <string> [options]
Required options:
--arch <string> architecture, e.g. aarch64, arm, ppc64, ppc64le, i386, x86_64
Options:
--dir <dir> directory to use for building and output of initramfs
--debug enable set -x
--noclean don't clean the build directory (we clean by default)
--noinit don't use busybox init system, boot to /bin/sh instead
--overlay <dir> files to copy over top of initramfs filesystem
--overlay-dst <dir> dir to copy overlay files into on initramfs, defaults to /
--printk enable printk on console
--tty <string> the tty(s) to run getting on, defaults to "tty0 hvc0"
--help show this help message
Short Options:
-a <string> Same as --arch
-d <dir> Same as --dir
-o <dir> Same as --overlay
-O <dir> Same as --overlay-dst
-t <string> Same as --tty
-h Same as --help
EOF
exit 1
}
#------------------------
# PARSE COMMAND LINE ARGS
#------------------------
CMD_LINE=$(getopt -o a:c:d:ht:o:O:i: --longoptions arch:,noclean,debug,dir:,help,printk,tty:,overlay:,overlay-dst:,noinit -n "$0" -- "$@")
eval set -- "${CMD_LINE}"
while true ; do
case "${1}" in
-a|--arch)
ARCH="${2}"
shift 2
;;
--noclean)
CLEAN="false"
shift
;;
--debug)
set -x
shift
;;
-d|--dir)
DIR="${2}"
shift 2
;;
-o|--overlay)
OVERLAY="${2}"
shift 2
;;
-o|--overlay-dst)
OVERLAY_DST="${2}"
shift 2
;;
--noinit)
INIT="false"
shift
;;
--printk)
PRINTK="true"
shift
;;
-t|--tty)
TTY_DEV="${2}"
shift 2
;;
-h|--help)
usage
;;
--)
shift
break
;;
*)
usage
;;
esac
done
[[ "$(type rpm2cpio 2>/dev/null)" ]] || { echo "Please install rpm2cpio" ; exit 1 ; }
[[ "$(type xz 2>/dev/null)" ]] || { echo "Please install xz" ; exit 1 ; }
# Get a busybox RPM from Fedora
if [[ "${ARCH}" == "ppc64" ]] ; then
pkg=busybox-1.26.2-3.fc27.ppc64.rpm
pkgurl=${pkgurl:-https://dl.fedoraproject.org/pub/archive/fedora-secondary/releases/28/Everything/ppc64/os/Packages/b/}${pkg}
elif [[ "${ARCH}" == "ppc64le" ]] ; then
pkg=busybox-1.32.0-2.fc33.ppc64le.rpm
pkgurl=https://dl.fedoraproject.org/pub/fedora-secondary/releases/33/Everything/ppc64le/os/Packages/b/$pkg
elif [[ "${ARCH}" == "i386" ]] ; then
pkg=busybox-1.28.3-3.fc30.i686.rpm
pkgurl=https://dl.fedoraproject.org/pub/fedora-secondary/releases/30/Everything/i386/os/Packages/b/$pkg
elif [[ "${ARCH}" == "x86_64" ]] ; then
pkg=busybox-1.32.0-2.fc33.x86_64.rpm
pkgurl=https://dl.fedoraproject.org/pub/fedora/linux/releases/33/Everything/x86_64/os/Packages/b/${pkg}
elif [[ "${ARCH}" == "arm" ]] ; then
pkg=busybox-1.32.0-2.fc33.armv7hl.rpm
pkgurl=https://dl.fedoraproject.org/pub/fedora/linux/releases/33/Everything/armhfp/os/Packages/b/${pkg}
elif [[ "${ARCH}" == "arm64" || "${ARCH}" == aarch64 ]] ; then
pkg=busybox-1.32.0-2.fc33.aarch64.rpm
pkgurl=https://dl.fedoraproject.org/pub/fedora/linux/releases/33/Everything/aarch64/os/Packages/b/${pkg}
else
usage
fi
#-------------
# DO THE BUILD
#-------------
# Prepare work dir
mkdir -p "${DIR}" "${CACHE_DIR}" && cd "${DIR}"
echo "Downloading busybox..."
# Get pre-build static busybox from Fedora
${WGET} -c ${pkgurl} -O "${CACHE_DIR}/${pkg}" || { echo "Failed to download the busybox RPM" ; exit 1 ; }
cp "${CACHE_DIR}/${pkg}" .
# Extract rpm to get busybox binary
if [[ "${ID}" == "arch" ]]; then
rpm2cpio ${pkg} |xz -d |cpio -idm 2>/dev/null
else
rpm2cpio ${pkg} |cpio -idm 2>/dev/null
fi
# Create initramfs file structure
mkdir -p "${DIR}/initramfs/"{bin,dev,etc,proc,run,sbin,sys,tmp}
[[ "${INIT}" == "true" ]] && mkdir -p "${DIR}/initramfs/etc/init.d"
# Copy busybox into our initramfs
cp -a "${DIR}/sbin/busybox" "${DIR}/initramfs/bin/busybox"
chmod a+x "${DIR}/initramfs/bin/busybox"
# Create our base init script
cat > "${DIR}/initramfs/init" << EOF
#!/bin/busybox sh
# Create utils links to busybox
/bin/busybox --install -s /bin
/bin/busybox --install -s /sbin
# devfs required for getty
mount -t devtmpfs none /dev
EOF
# init needs to executable
chmod a+x "${DIR}/initramfs/init"
if [[ "${INIT}" == "false" ]]; then
# No init system, so we need to set up a few more mounts
echo "mount -t proc proc /proc" >> "${DIR}/initramfs/init"
echo "mount -t sysfs sysfs /sys" >> "${DIR}/initramfs/init"
if [[ "${PRINTK}" != "true" ]]; then
{
echo "# Silence kernel output"
echo "echo 0 > /proc/sys/kernel/printk"
echo "clear"
} >> "${DIR}/initramfs/init"
fi
# Just execute a shell
{
echo "echo SYSTEM BOOTED ; /bin/sh"
echo "umount -a 2>/dev/null"
echo "poweroff -f"
} >> "${DIR}/initramfs/init"
else
# Run busybox instead of sh as init
echo 'exec /bin/init $*' >> "${DIR}/initramfs/init"
echo "Downloading init scripts..."
# Get our init start script
${WGET} ${rcS_url} -O "${DIR}/initramfs/etc/init.d/rcS" || { echo "Failed to download the system scripts" ; exit 1 ; }
chmod a+x "${DIR}/initramfs/etc/init.d/rcS"
# Get our init kill sript
${WGET} ${rcK_url} -O "${DIR}/initramfs/etc/init.d/rcK" || { echo "Failed to download the system scripts" ; exit 1 ; }
chmod a+x "${DIR}/initramfs/etc/init.d/rcK"
# Get basic startup files
${WGET} ${network_url} -O "${DIR}/initramfs/etc/init.d/S40network" || { echo "Failed to download the system scripts" ; exit 1 ; }
mkdir -p "${DIR}/initramfs/etc/network"
touch "${DIR}/initramfs/etc/network/interfaces"
${WGET} ${urandom_url} -O "${DIR}/initramfs/etc/init.d/S20urandom" || { echo "Failed to download the system scripts" ; exit 1 ; }
# Disable printk
if [[ "${PRINTK}" != "true" ]] ; then
cat > "${DIR}/initramfs/etc/init.d/S10printk" << \EOF
#!/bin/sh
case "$1" in
start|"")
echo 0 > /proc/sys/kernel/printk
clear
;;
esac
EOF
fi
chmod a+x "${DIR}/initramfs/etc/init.d/"*
# Get busybox inittab
${WGET} ${inittab_url} -O "${DIR}/initramfs/etc/inittab" || { echo "Failed to download the system scripts" ; exit 1 ; }
for tty in $(echo "${TTY_DEV}" | tr ' ' '\n' |sort |uniq); do
echo "${tty}::respawn:/sbin/getty -L ${tty} 115200 vt100" >> "${DIR}/initramfs/etc/inittab"
done
# Get basic passwd and shadow files
echo "Downloading system files..."
${WGET} ${passwd_url} -O "${DIR}/initramfs/etc/passwd" || { echo "Failed to download the system scripts" ; exit 1 ; }
${WGET} ${shadow_url} -O "${DIR}/initramfs/etc/shadow" || { echo "Failed to download the system scripts" ; exit 1 ; }
${WGET} ${group_url} -O "${DIR}/initramfs/etc/group" || { echo "Failed to download the system scripts" ; exit 1 ; }
${WGET} ${fstab_url} -O "${DIR}/initramfs/etc/fstab" || { echo "Failed to download the system scripts" ; exit 1 ; }
# set a hostname based on ARCH
echo "${ARCH}" > "${DIR}/initramfs/etc/hostname"
fi
# Copy over anything from overlay into initramfs
if [[ "${OVERLAY}" != "" ]]; then
if [[ -d "${OVERLAY}" ]]; then
echo "Copying in files from ${OVERLAY}"
mkdir -p "${DIR}/initramfs/${OVERLAY_DST}"
cp -a "${OVERLAY}/." "${DIR}/initramfs/${OVERLAY_DST}/"
else
echo "Your specified overlay directory doesn't seem to be correct, skipping."
fi
fi
# Create initramfs image
echo "Creating initramfs..."
cd "${DIR}/initramfs"
find . | cpio -H newc -o > "${DIR}/initramfs-${ARCH}.cpio" 2>/dev/null
gzip -qf "${DIR}/initramfs-${ARCH}.cpio"
cd "${CURPWD}"
# Clean if we're meant to
[[ "${CLEAN}" == "true" ]] && rm -Rf "${DIR:?}"/{initramfs,busybox*rpm,usr,sbin}
# Notify of completion
echo -e "\nCreated initramfs at ${DIR}/initramfs-${ARCH}.cpio.gz\n"