-
Notifications
You must be signed in to change notification settings - Fork 28
/
genapkovl-lima.sh
executable file
·245 lines (190 loc) · 6.33 KB
/
genapkovl-lima.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
#!/bin/sh -e
HOSTNAME="$1"
if [ -z "$HOSTNAME" ]; then
echo "usage: $0 hostname"
exit 1
fi
cleanup() {
rm -rf "$tmp"
}
makefile() {
OWNER="$1"
PERMS="$2"
FILENAME="$3"
cat > "$FILENAME"
chown "$OWNER" "$FILENAME"
chmod "$PERMS" "$FILENAME"
}
rc_add() {
mkdir -p "$tmp"/etc/runlevels/"$2"
ln -sf /etc/init.d/"$1" "$tmp"/etc/runlevels/"$2"/"$1"
}
tmp="$(mktemp -d)"
trap cleanup EXIT
mkdir -p "$tmp"/etc
makefile root:root 0644 "$tmp"/etc/fstab <<EOF
# /etc/fstab
EOF
makefile root:root 0644 "$tmp"/etc/hostname <<EOF
$HOSTNAME
EOF
mkdir -p "$tmp"/etc/network
makefile root:root 0644 "$tmp"/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
mkdir -p "$tmp"/etc/apk
makefile root:root 0644 "$tmp"/etc/apk/world <<EOF
alpine-base
openssh-server-pam
EOF
rc_add devfs sysinit
rc_add dmesg sysinit
# cloud-init / lima-init require udev instead (for /dev/disk/...)
if [ "${LIMA_INSTALL_CLOUD_INIT}" != "true" -a "${LIMA_INSTALL_LIMA_INIT}" != "true" ]; then
rc_add mdev sysinit
rc_add hwdrivers sysinit
fi
rc_add modloop sysinit
rc_add hwclock boot
rc_add modules boot
rc_add sysctl boot
rc_add hostname boot
rc_add bootmisc boot
rc_add syslog boot
rc_add mount-ro shutdown
rc_add killprocs shutdown
rc_add savecache shutdown
rc_add networking default
rc_add sshd default
# lima-overlay must run with the boot filesystem, so has to run before
# cloud-init or lima-init because the boot script will remap /etc and
# /var/lib to the data volume.
mkdir -p "${tmp}/etc/init.d/"
makefile root:root 0755 "$tmp/etc/init.d/lima-overlay" << EOF
#!/sbin/openrc-run
depend() {
after localmount
before cloud-init-local lima-init-local sshd
provide lima-overlay
}
start() {
sed -i 's/#UsePAM no/UsePAM yes/g' /etc/ssh/sshd_config
echo "BUILD_ID=\"${LIMA_BUILD_ID}\"" >> /etc/os-release
echo "VARIANT_ID=\"${LIMA_VARIANT_ID}\"" >> /etc/os-release
eend 0
}
EOF
rc_add lima-overlay default
if [ "${LIMA_INSTALL_LIMA_INIT}" == "true" ]; then
rc_add lima-init default
rc_add lima-init-local default
mkdir -p "${tmp}/etc/init.d/"
cp /home/build/lima-init.openrc "${tmp}/etc/init.d/lima-init"
cp /home/build/lima-init-local.openrc "${tmp}/etc/init.d/lima-init-local"
mkdir -p "${tmp}/usr/bin/"
cp /home/build/lima-init.sh "${tmp}/usr/bin/lima-init"
cp /home/build/lima-network.awk "${tmp}/usr/bin/lima-network.awk"
echo e2fsprogs >> "$tmp"/etc/apk/world
echo lsblk >> "$tmp"/etc/apk/world
echo sfdisk >> "$tmp"/etc/apk/world
echo shadow >> "$tmp"/etc/apk/world
echo sudo >> "$tmp"/etc/apk/world
echo udev >> "$tmp"/etc/apk/world
rc_add udev sysinit
rc_add udev-postmount default
rc_add udev-trigger sysinit
rc_add machine-id sysinit
fi
if [ "${LIMA_INSTALL_CLOUD_INIT}" == "true" ]; then
echo cloud-init >> "$tmp"/etc/apk/world
echo e2fsprogs >> "$tmp"/etc/apk/world
echo sudo >> "$tmp"/etc/apk/world
rc_add cloud-init-local boot
rc_add cloud-config default
rc_add cloud-final default
rc_add cloud-init default
rc_add udev sysinit
rc_add udev-postmount default
rc_add udev-trigger sysinit
mkdir -p "${tmp}/etc/cloud/cloud.cfg.d/"
makefile root:root 0644 "$tmp/etc/cloud/cloud.cfg.d/10_lima.cfg" << EOF
datasource_list: [ NoCloud, None ]
EOF
fi
if [ "${LIMA_INSTALL_CLOUD_UTILS_GROWPART}" == "true" ]; then
echo cloud-utils-growpart >> "$tmp"/etc/apk/world
echo partx >> "$tmp"/etc/apk/world
fi
# /proc/sys/fs/binfmt_misc must exist for /etc/init.d/procfs to load
# the binfmt-misc kernel module, which will then mount the filesystem.
# This is needed for Rosetta to register.
mkdir -p "${tmp}/proc/sys/fs/binfmt_misc"
rc_add procfs default
if [ "${LIMA_INSTALL_BINFMT_MISC}" == "true" ]; then
# install qemu-aarch64 on x86_64 and vice versa
OTHERARCH=aarch64
if [ "$(uname -m)" == "${OTHERARCH}" ]; then
OTHERARCH=x86_64
fi
# Installing into /usr/bin instead of /usr/local/bin because that's
# where /etc/init.d/qemu-binfmt will be looking for it
mkdir -p "${tmp}/usr/bin/"
cp /binfmt/qemu-${OTHERARCH} "${tmp}/usr/bin/"
# Copy QEMU license into /usr/share/doc (using Debian naming convention)
mkdir -p "${tmp}/usr/share/doc/qemu/"
cp /home/build/qemu-copying "${tmp}/usr/share/doc/qemu/copyright"
mkdir -p "${tmp}/etc/init.d/"
APKBUILD=/home/build/aports/community/qemu-openrc/APKBUILD
PKGVER=$(awk '/^pkgver=/ {split($1, a, "="); print a[2]}' ${APKBUILD})
URL=$(awk '/^url=/ {split($1, a, "="); print a[2]}' ${APKBUILD} | tr -d '"' | sed 's/github/raw.githubusercontent/')
wget "${URL}/v${PKGVER}/qemu-binfmt.initd" -O "${tmp}/etc/init.d/qemu-binfmt"
chmod +x "${tmp}/etc/init.d/qemu-binfmt"
# qemu-binfmt doesn't include an entry for x86_64
magic="7f454c4602010100000000000000000002003e00"
mask="fffffffffffefe00fffffffffffffffffeffffff"
arch="x86_64"
sed -i "/^FMTS=/a \\\t${magic} ${mask} ${arch}" "${tmp}/etc/init.d/qemu-binfmt"
# qemu from tonistiigi/binfmt is patched to assume preserve-argv; set it here.
mkdir -p "${tmp}/etc/conf.d"
echo 'binfmt_flags="POCF"' > "${tmp}/etc/conf.d/qemu-binfmt"
rc_add qemu-binfmt default
fi
if [ "${LIMA_INSTALL_CA_CERTIFICATES}" == "true" ]; then
echo "ca-certificates" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_E2FSPROGS_EXTRA}" == "true" ]; then
echo "e2fsprogs-extra" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_GIT}" == "true" ]; then
echo "git" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_K3S}" == "true" ]; then
echo "k3s" >> "$tmp"/etc/apk/world
rc_add k3s default
fi
if [ "${LIMA_INSTALL_LOGROTATE}" == "true" ]; then
echo "logrotate" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_IPTABLES}" == "true" ]; then
echo "iptables ip6tables" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_OPENSSH_SFTP_SERVER}" == "true" ]; then
echo "openssh-sftp-server" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_SSHFS}" == "true" ]; then
echo "sshfs" >> "$tmp"/etc/apk/world
fi
if [ "${LIMA_INSTALL_TINI}" == "true" ]; then
echo tini-static >> "$tmp"/etc/apk/world
ln -sf /sbin/tini-static "$tmp"/usr/bin/tini
fi
if [ "${LIMA_INSTALL_TZDATA}" == "true" ]; then
echo tzdata >> "$tmp"/etc/apk/world
fi
mkdir -p "${tmp}/etc"
mkdir -p "${tmp}/proc"
mkdir -p "${tmp}/usr"
tar -c -C "$tmp" etc proc usr | gzip -9n > $HOSTNAME.apkovl.tar.gz