@@ -18,11 +18,142 @@ users:
18
18
{{- end}}
19
19
20
20
write_files :
21
+ - content : |
22
+ #!/bin/sh
23
+ # This script pretends that /bin/ash can be used as /bin/bash, so all following
24
+ # cloud-init scripts can use `#!/bin/bash` and `set -o pipefail`.
25
+ test -f /etc/alpine-release || exit 0
26
+
27
+ # Redirect bash to ash (built with CONFIG_ASH_BASH_COMPAT) and hope for the best :)
28
+ # It does support `set -o pipefail`, but not `[[`.
29
+ # /bin/bash can't be a symlink because /bin/ash is a symlink to /bin/busybox
30
+ cat >/bin/bash <<'EOF'
31
+ #!/bin/sh
32
+ exec /bin/ash "$@"
33
+ EOF
34
+ chmod +x /bin/bash
35
+ owner: root:root
36
+ path: /var/lib/cloud/scripts/per-boot/00-alpine-ash-as-bash.boot.sh
37
+ permissions: '0755'
21
38
- content : |
22
39
#!/bin/bash
23
40
set -eux -o pipefail
24
41
25
- {{- if .Containerd.User}}
42
+ # Restrict the rest of this script to Alpine until it has been tested with other distros
43
+ test -f /etc/alpine-release || exit 0
44
+
45
+ # Data directories that should be persisted across reboots
46
+ DATADIRS="/home /usr/local /etc/containerd /var/lib/containerd"
47
+
48
+ # When running from RAM try to move persistent data to data-volume
49
+ # FIXME: the test for tmpfs mounts is probably Alpine-specific
50
+ if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
51
+ mkdir -p /mnt/data
52
+ if [ -e /dev/disk/by-label/data-volume ]; then
53
+ mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
54
+ else
55
+ # Find an unpartitioned disk and create data-volume
56
+ DISKS=$(lsblk --list --noheadings --output name,type | awk '$2 == "disk" {print $1}')
57
+ for DISK in ${DISKS}; do
58
+ IN_USE=false
59
+ # Looking for a disk that is not mounted or partitioned
60
+ for PART in $(awk '/^\/dev\// {gsub("/dev/", ""); print $1}' /proc/mounts); do
61
+ if [ "${DISK}" == "${PART}" -o -e /sys/block/${DISK}/${PART} ]; then
62
+ IN_USE=true
63
+ break
64
+ fi
65
+ done
66
+ if [ "${IN_USE}" == "false" ]; then
67
+ echo 'type=83' | sfdisk --label dos /dev/${DISK}
68
+ PART=$(lsblk --list /dev/${DISK} --noheadings --output name,type | awk '$2 == "part" {print $1}')
69
+ mkfs.ext4 -L data-volume /dev/${PART}
70
+ mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
71
+ for DIR in ${DATADIRS}; do
72
+ DEST="/mnt/data$(dirname ${DIR})"
73
+ mkdir -p ${DIR} ${DEST}
74
+ mv ${DIR} ${DEST}
75
+ done
76
+ break
77
+ fi
78
+ done
79
+ fi
80
+ for DIR in ${DATADIRS}; do
81
+ if [ -d /mnt/data${DIR} ]; then
82
+ [ -e ${DIR} ] && rm -rf ${DIR}
83
+ ln -s /mnt/data${DIR} ${DIR}
84
+ fi
85
+ done
86
+ fi
87
+ owner: root:root
88
+ path: /var/lib/cloud/scripts/per-boot/05-persistent-data-volume.boot.sh
89
+ permissions: '0755'
90
+ - content : |
91
+ #!/sbin/openrc-run
92
+ supervisor=supervise-daemon
93
+
94
+ name="lima-guestagent"
95
+ description="Forward ports to the lima-hostagent"
96
+
97
+ export XDG_RUNTIME_DIR="/run/user/{{.UID}}"
98
+ command=/usr/local/bin/lima-guestagent
99
+ command_args="daemon"
100
+ command_background=true
101
+ command_user="{{.User}}:{{.User}}"
102
+ pidfile="${XDG_RUNTIME_DIR}/lima-guestagent.pid"
103
+ owner: root:root
104
+ path: /var/lib/lima-guestagent/lima-guestagent.openrc
105
+ permissions: '0755'
106
+ - content : |
107
+ #!/bin/bash
108
+ set -eux -o pipefail
109
+
110
+ # This script prepares Alpine for lima; there is nothing in here for other distros
111
+ test -f /etc/alpine-release || exit 0
112
+
113
+ # Configure apk repos
114
+ BRANCH=edge
115
+ VERSION_ID=$(awk -F= '$1=="VERSION_ID" {print $2}' /etc/os-release)
116
+ case ${VERSION_ID} in
117
+ *_alpha*|*_beta*) BRANCH=edge;;
118
+ *.*.*) BRANCH=v${VERSION_ID%.*};;
119
+ esac
120
+
121
+ for REPO in main community; do
122
+ URL="https://dl-cdn.alpinelinux.org/alpine/${BRANCH}/${REPO}"
123
+ if ! grep -q "^${URL}$" /etc/apk/repositories; then
124
+ echo "${URL}" >> /etc/apk/repositories
125
+ fi
126
+ done
127
+
128
+ # Alpine doesn't use PAM so we need to explicitly allow public key auth
129
+ usermod -p '*' ""{{.User}}""
130
+
131
+ # Alpine disables TCP forwarding, which is needed by the lima-guestagent
132
+ sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
133
+ rc-service sshd reload
134
+
135
+ # Create directory for the lima-guestagent socket (normally done by systemd)
136
+ mkdir -p /run/user/{{.UID}}
137
+ chown "{{.User}}" /run/user/{{.UID}}
138
+ chmod 700 /run/user/{{.UID}}
139
+
140
+ # Install the openrc lima-guestagent service script
141
+ mv /var/lib/lima-guestagent/lima-guestagent.openrc /etc/init.d/lima-guestagent
142
+
143
+ # `limactl stop` tells acpid to powerdown
144
+ rc-update add acpid
145
+ rc-service acpid start
146
+ owner: root:root
147
+ path: /var/lib/cloud/scripts/per-boot/10-alpine-prep.boot.sh
148
+ permissions: '0755'
149
+ {{- if .Containerd.User}}
150
+ - content : |
151
+ #!/bin/bash
152
+ set -eux -o pipefail
153
+
154
+ # This script does not work unless systemd is available
155
+ command -v systemctl 2>&1 >/dev/null || exit 0
156
+
26
157
# Set up env
27
158
for f in .profile .bashrc; do
28
159
if ! grep -q "# Lima BEGIN" "/home/{{.User}}.linux/$f"; then
@@ -66,7 +197,14 @@ write_files:
66
197
67
198
# Start systemd session
68
199
loginctl enable-linger "{{.User}}"
69
- {{- end}}
200
+ owner: root:root
201
+ # We do not use per-once.
202
+ path: /var/lib/cloud/scripts/per-boot/20-rootless-base.boot.sh
203
+ permissions: '0755'
204
+ {{- end}}
205
+ - content : |
206
+ #!/bin/bash
207
+ set -eux -o pipefail
70
208
71
209
# Create mount points
72
210
{{- range $val := .Mounts}}
@@ -81,16 +219,22 @@ write_files:
81
219
umount /mnt/lima-cidata
82
220
83
221
# Launch the guestagent service
84
- until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
85
- sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" lima-guestagent install-systemd
222
+ if [ -f /etc/alpine-release ]; then
223
+ rc-update add lima-guestagent default
224
+ rc-service lima-guestagent start
225
+ else
226
+ until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
227
+ sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" lima-guestagent install-systemd
228
+ fi
86
229
owner: root:root
87
230
# We do not use per-once.
88
- path: /var/lib/cloud/scripts/per-boot/00 -base.boot.sh
231
+ path: /var/lib/cloud/scripts/per-boot/25-guestagent -base.boot.sh
89
232
permissions: '0755'
90
233
{{- if or .Mounts .Containerd.System .Containerd.User }}
91
234
- content : |
92
235
#!/bin/bash
93
236
set -eux -o pipefail
237
+
94
238
# Install minimum dependencies
95
239
if command -v apt-get 2>&1 >/dev/null; then
96
240
export DEBIAN_FRONTEND=noninteractive
@@ -119,6 +263,15 @@ write_files:
119
263
ln -s fusermount3 /usr/bin/fusermount
120
264
fi
121
265
{{- end}}
266
+ elif command -v apk 2>&1 >/dev/null; then
267
+ : {{/* make sure the "elif" block is never empty */}}
268
+ {{- if .Mounts}}
269
+ if ! command -v sshfs 2>&1 >/dev/null; then
270
+ apk update
271
+ apk add sshfs
272
+ fi
273
+ modprobe fuse
274
+ {{- end}}
122
275
fi
123
276
# Modify /etc/fuse.conf to allow "-o allow_root"
124
277
{{- if .Mounts }}
@@ -127,13 +280,17 @@ write_files:
127
280
fi
128
281
{{- end}}
129
282
owner: root:root
130
- path: /var/lib/cloud/scripts/per-boot/10 -install-packages.boot.sh
283
+ path: /var/lib/cloud/scripts/per-boot/30 -install-packages.boot.sh
131
284
permissions: '0755'
132
285
{{- end}}
133
286
{{- if or .Containerd.System .Containerd.User}}
134
287
- content : |
135
288
#!/bin/bash
136
289
set -eux -o pipefail
290
+
291
+ # This script does not work unless systemd is available
292
+ command -v systemctl 2>&1 >/dev/null || exit 0
293
+
137
294
if [ ! -x /usr/local/bin/nerdctl ]; then
138
295
mkdir -p -m 600 /mnt/lima-cidata
139
296
mount -t iso9660 -o ro /dev/disk/by-label/cidata /mnt/lima-cidata
@@ -191,7 +348,7 @@ write_files:
191
348
fi
192
349
{{- end}}
193
350
owner: root:root
194
- path: /var/lib/cloud/scripts/per-boot/20 -install-containerd.boot.sh
351
+ path: /var/lib/cloud/scripts/per-boot/40 -install-containerd.boot.sh
195
352
permissions: '0755'
196
353
{{- end}}
197
354
{{- if .Provision}}
@@ -208,7 +365,7 @@ write_files:
208
365
{{- end}}
209
366
{{- end}}
210
367
owner: root:root
211
- path: /var/lib/cloud/scripts/per-boot/30 -execute-provision-scripts.boot.sh
368
+ path: /var/lib/cloud/scripts/per-boot/50 -execute-provision-scripts.boot.sh
212
369
permissions: '0755'
213
370
{{- end}}
214
371
{{- range $i, $val := .Provision}}
0 commit comments