Skip to content

Commit 0f25d1d

Browse files
committed
Add support for Alpine guest OS
Requires Alpine image with cloud-init, but configures lima-guestagent service with openrc and pretends /bin/ash can stand in for /bin/bash. Does not work with containerd (yet). Signed-off-by: Jan Dubois <[email protected]>
1 parent 36b2ab6 commit 0f25d1d

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

pkg/cidata/user-data.TEMPLATE

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,75 @@ users:
1818
{{- end}}
1919

2020
write_files:
21+
- content: |
22+
#!/sbin/openrc-run
23+
supervisor=supervise-daemon
24+
25+
name="lima-guestagent"
26+
description="Forward ports to the lima-hostagent"
27+
28+
export XDG_RUNTIME_DIR="/run/user/{{.UID}}"
29+
command=/usr/local/bin/lima-guestagent
30+
command_args="daemon"
31+
command_background=true
32+
command_user="{{.User}}:{{.User}}"
33+
pidfile="${XDG_RUNTIME_DIR}/lima-guestagent.pid"
34+
owner: root:root
35+
path: /var/lib/lima-guestagent/lima-guestagent.openrc
36+
permissions: '0755'
37+
- content: |
38+
#!/bin/sh
39+
# This script prepares Alpine for lima; there is nothing in here for other distros
40+
test -f /etc/alpine-release || exit
41+
42+
# Since we are on Alpine, we can now assume /bin/sh is /bin/ash
43+
set -eux -o pipefail
44+
45+
# Redirect bash to ash (built with CONFIG_ASH_BASH_COMPAT) and hope for the best :)
46+
# (it does support `set -o pipefail`, but not `[[`)
47+
# /bin/bash can't be a symlink because /bin/ash is a symlink to /bin/busybox
48+
cat >/bin/bash <<'EOF'
49+
#!/bin/sh
50+
exec /bin/ash "$@"
51+
EOF
52+
chmod +x /bin/bash
53+
54+
# Configure apk repos
55+
branch=edge
56+
VERSION_ID=$(awk -F= '$1=="VERSION_ID" {print $2}' /etc/os-release)
57+
case $VERSION_ID in
58+
*_alpha*|*_beta*) branch=edge;;
59+
*.*.*) branch=v${VERSION_ID%.*};;
60+
esac
61+
62+
for repo in main community; do
63+
url="https://dl-cdn.alpinelinux.org/alpine/${branch}/${repo}"
64+
if ! grep -q "^${url}$" /etc/apk/repositories; then
65+
echo "${url}" >> /etc/apk/repositories
66+
fi
67+
done
68+
69+
# Alpine doesn't use PAM so we need to explicitly allow public key auth
70+
usermod -p '*' ""{{.User}}""
71+
72+
# Alpine disables TCP forwarding, which is needed by the lima-guestagent
73+
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
74+
rc-service sshd reload
75+
76+
# Create directory for the lima-guestagent socket (normally done by systemd)
77+
mkdir -p /run/user/{{.UID}}
78+
chown "{{.User}}" /run/user/{{.UID}}
79+
chmod 700 /run/user/{{.UID}}
80+
81+
# Install the openrc lima-guestagent service script
82+
mv /var/lib/lima-guestagent/lima-guestagent.openrc /etc/init.d/lima-guestagent
83+
84+
# `limactl stop` tells acpid to powerdown
85+
rc-update add acpid
86+
rc-service acpid start
87+
owner: root:root
88+
path: /var/lib/cloud/scripts/per-boot/00-alpine-prep.boot.sh
89+
permissions: '0755'
2190
- content: |
2291
#!/bin/bash
2392
set -eux -o pipefail
@@ -81,11 +150,16 @@ write_files:
81150
umount /mnt/lima-cidata
82151
83152
# 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
153+
if [ -f /etc/alpine-release ]; then
154+
rc-update add lima-guestagent default
155+
rc-service lima-guestagent start
156+
else
157+
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
158+
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" lima-guestagent install-systemd
159+
fi
86160
owner: root:root
87161
# We do not use per-once.
88-
path: /var/lib/cloud/scripts/per-boot/00-base.boot.sh
162+
path: /var/lib/cloud/scripts/per-boot/10-base.boot.sh
89163
permissions: '0755'
90164
{{- if or .Mounts .Containerd.System .Containerd.User }}
91165
- content: |
@@ -119,6 +193,15 @@ write_files:
119193
ln -s fusermount3 /usr/bin/fusermount
120194
fi
121195
{{- end}}
196+
elif command -v apk 2>&1 >/dev/null; then
197+
: {{/* make sure the "elif" block is never empty */}}
198+
{{- if .Mounts}}
199+
if ! command -v sshfs 2>&1 >/dev/null; then
200+
apk update
201+
apk add sshfs
202+
fi
203+
modprobe fuse
204+
{{- end}}
122205
fi
123206
# Modify /etc/fuse.conf to allow "-o allow_root"
124207
{{- if .Mounts }}
@@ -127,7 +210,7 @@ write_files:
127210
fi
128211
{{- end}}
129212
owner: root:root
130-
path: /var/lib/cloud/scripts/per-boot/10-install-packages.boot.sh
213+
path: /var/lib/cloud/scripts/per-boot/20-install-packages.boot.sh
131214
permissions: '0755'
132215
{{- end}}
133216
{{- if or .Containerd.System .Containerd.User}}
@@ -191,7 +274,7 @@ write_files:
191274
fi
192275
{{- end}}
193276
owner: root:root
194-
path: /var/lib/cloud/scripts/per-boot/20-install-containerd.boot.sh
277+
path: /var/lib/cloud/scripts/per-boot/30-install-containerd.boot.sh
195278
permissions: '0755'
196279
{{- end}}
197280
{{- if .Provision}}
@@ -208,7 +291,7 @@ write_files:
208291
{{- end}}
209292
{{- end}}
210293
owner: root:root
211-
path: /var/lib/cloud/scripts/per-boot/30-execute-provision-scripts.boot.sh
294+
path: /var/lib/cloud/scripts/per-boot/40-execute-provision-scripts.boot.sh
212295
permissions: '0755'
213296
{{- end}}
214297
{{- range $i, $val := .Provision}}

pkg/hostagent/requirements.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fi
100100
script: `#!/bin/bash
101101
set -eux -o pipefail
102102
sock="/run/user/$(id -u)/lima-guestagent.sock"
103-
if ! timeout 30s bash -c "until [[ -S \"${sock}\" ]]; do sleep 3; done"; then
103+
if ! timeout 30s bash -c "until [ -S \"${sock}\" ]; do sleep 3; done"; then
104104
echo >&2 "lima-guestagent is not installed yet"
105105
exit 1
106106
fi

0 commit comments

Comments
 (0)