Skip to content

Commit efd8d86

Browse files
committed
cidata: add lima.env file
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 933eda9 commit efd8d86

File tree

8 files changed

+125
-92
lines changed

8 files changed

+125
-92
lines changed

docs/internal.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The directory contains the following files:
6464

6565
- `user-data`: [Cloud-init user-data](https://cloudinit.readthedocs.io/en/latest/topics/format.html)
6666
- `meta-data`: [Cloud-init meta-data](https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html)
67+
- `lima.env`: the environment variables
6768
- `lima-guestagent`: Lima guest agent binary
6869
- `nerdctl-full.tgz`: [`nerdctl-full-<VERSION>-linux-<ARCH>.tar.gz`](https://github.com/containerd/nerdctl/releases)
6970
- `boot/*`: Boot scripts
@@ -77,3 +78,9 @@ The volume label is "cidata", as defined by [cloud-init NoCloud](https://cloudin
7778

7879
### Environment variables
7980
- `LIMA_CIDATA_MNT`: the mount point of the disk. `/mnt/lima-cidata`.
81+
- `LIMA_CIDATA_USER`: the user name string
82+
- `LIMA_CIDATA_UID`: the numeric UID
83+
- `LIMA_CIDATA_MOUNTS`: the number of the Lima mounts
84+
- `LIMA_CIDATA_MOUNTS_%d_MOUNTPOINT`: the N-th mount point of Lima mounts (N=0, 1, ...)
85+
- `LIMA_CIDATA_CONTAINERD_USER`: set to "1" if rootless containerd to be set up
86+
- `LIMA_CIDATA_CONTAINERD_SYSTEM`: set to "1" if system-wide containerd to be set up

pkg/cidata/cidata.TEMPLATE.d/boot/10-alpine-prep.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ for REPO in main community; do
2020
done
2121

2222
# Alpine doesn't use PAM so we need to explicitly allow public key auth
23-
usermod -p '*' ""{{.User}}""
23+
usermod -p '*' "${LIMA_CIDATA_USER}"
2424

2525
# Alpine disables TCP forwarding, which is needed by the lima-guestagent
2626
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
2727
rc-service sshd reload
2828

2929
# Create directory for the lima-guestagent socket (normally done by systemd)
30-
mkdir -p /run/user/{{.UID}}
31-
chown "{{.User}}" /run/user/{{.UID}}
32-
chmod 700 /run/user/{{.UID}}
30+
mkdir -p /run/user/${LIMA_CIDATA_UID}
31+
chown "${LIMA_CIDATA_USER}" /run/user/${LIMA_CIDATA_UID}
32+
chmod 700 /run/user/${LIMA_CIDATA_UID}
3333

3434
# Install the openrc lima-guestagent service script
3535
cat >/etc/init.d/lima-guestagent <<'EOF'
@@ -39,11 +39,11 @@ supervisor=supervise-daemon
3939
name="lima-guestagent"
4040
description="Forward ports to the lima-hostagent"
4141
42-
export XDG_RUNTIME_DIR="/run/user/{{.UID}}"
42+
export XDG_RUNTIME_DIR="/run/user/${LIMA_CIDATA_UID}"
4343
command=/usr/local/bin/lima-guestagent
4444
command_args="daemon"
4545
command_background=true
46-
command_user="{{.User}}:{{.User}}"
46+
command_user="${LIMA_CIDATA_USER}:${LIMA_CIDATA_USER}"
4747
pidfile="${XDG_RUNTIME_DIR}/lima-guestagent.pid"
4848
EOF
4949
chmod 755 /etc/init.d/lima-guestagent

pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ command -v systemctl 2>&1 >/dev/null || exit 0
66

77
# Set up env
88
for f in .profile .bashrc; do
9-
if ! grep -q "# Lima BEGIN" "/home/{{.User}}.linux/$f"; then
10-
cat >>"/home/{{.User}}.linux/$f" <<EOF
9+
if ! grep -q "# Lima BEGIN" "/home/${LIMA_CIDATA_USER}.linux/$f"; then
10+
cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
1111
# Lima BEGIN
1212
# Make sure iptables and mount.fuse3 are available
1313
PATH="$PATH:/usr/sbin:/sbin"
@@ -16,7 +16,7 @@ CONTAINERD_SNAPSHOTTER="fuse-overlayfs"
1616
export PATH CONTAINERD_SNAPSHOTTER
1717
# Lima END
1818
EOF
19-
chown "{{.User}}" "/home/{{.User}}.linux/$f"
19+
chown "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/$f"
2020
fi
2121
done
2222
# Enable cgroup delegation (only meaningful on cgroup v2)
@@ -42,8 +42,8 @@ fi
4242

4343
# Set up subuid
4444
for f in /etc/subuid /etc/subgid; do
45-
grep -qw "{{.User}}" $f || echo "{{.User}}:100000:65536" >> $f
45+
grep -qw "${LIMA_CIDATA_USER}" $f || echo "${LIMA_CIDATA_USER}:100000:65536" >> $f
4646
done
4747

4848
# Start systemd session
49-
loginctl enable-linger "{{.User}}"
49+
loginctl enable-linger "${LIMA_CIDATA_USER}"
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
#!/bin/bash
2-
set -eux -o pipefail
1+
#!/bin/sh
2+
set -eux
33

44
# Create mount points
5-
{{- range $val := .Mounts}}
6-
mkdir -p "{{$val}}"
7-
chown "{{$.User}}" "{{$val}}" || true
8-
{{- end}}
5+
# NOTE: Busybox sh does not support `for ((i=0;i<$N;i++))` form
6+
for f in $(seq 0 $((LIMA_CIDATA_MOUNTS - 1))); do
7+
mountpointvar="LIMA_CIDATA_MOUNTS_${f}_MOUNTPOINT"
8+
mountpoint="$(eval echo \$$mountpointvar)"
9+
mkdir -p "${mountpoint}"
10+
chown "${LIMA_CIDATA_USER}" "${mountpoint}"
11+
done
912

1013
# Install or update the guestagent binary
1114
install -m 755 "${LIMA_CIDATA_MNT}"/lima-guestagent /usr/local/bin/lima-guestagent
@@ -15,6 +18,6 @@ if [ -f /etc/alpine-release ]; then
1518
rc-update add lima-guestagent default
1619
rc-service lima-guestagent start
1720
else
18-
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
19-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" lima-guestagent install-systemd
21+
until [ -e "/run/user/${LIMA_CIDATA_UID}/systemd/private" ]; do sleep 3; done
22+
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" lima-guestagent install-systemd
2023
fi

pkg/cidata/cidata.TEMPLATE.d/boot/30-install-packages.sh

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,42 @@ set -eux -o pipefail
55
if command -v apt-get 2>&1 >/dev/null; then
66
export DEBIAN_FRONTEND=noninteractive
77
apt-get update
8-
{{- if .Mounts}}
9-
apt-get install -y sshfs
10-
{{- end }}
11-
{{- if or .Containerd.System .Containerd.User }}
12-
apt-get install -y iptables
13-
{{- end }}
14-
{{- if .Containerd.User}}
15-
apt-get install -y uidmap fuse3 dbus-user-session
16-
{{- end }}
8+
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
9+
apt-get install -y sshfs
10+
fi
11+
if [ "${LIMA_CIDATA_CONTAINERD_SYSTEM}" = 1 ] || [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
12+
apt-get install -y iptables
13+
fi
14+
if [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
15+
apt-get install -y uidmap fuse3 dbus-user-session
16+
fi
1717
elif command -v dnf 2>&1 >/dev/null; then
18-
: {{/* make sure the "elif" block is never empty */}}
19-
{{- if .Mounts}}
20-
dnf install -y fuse-sshfs
21-
{{- end}}
22-
{{- if or .Containerd.System .Containerd.User }}
23-
dnf install -y iptables
24-
{{- end }}
25-
{{- if .Containerd.User}}
26-
dnf install -y shadow-utils fuse3
27-
if [ ! -f /usr/bin/fusermount ]; then
28-
# Workaround for https://github.com/containerd/stargz-snapshotter/issues/340
29-
ln -s fusermount3 /usr/bin/fusermount
30-
fi
31-
{{- end}}
18+
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
19+
dnf install -y fuse-sshfs
20+
fi
21+
if [ "${LIMA_CIDATA_CONTAINERD_SYSTEM}" = 1 ] || [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
22+
dnf install -y iptables
23+
fi
24+
if [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
25+
dnf install -y shadow-utils fuse3
26+
if [ ! -f /usr/bin/fusermount ]; then
27+
# Workaround for https://github.com/containerd/stargz-snapshotter/issues/340
28+
ln -s fusermount3 /usr/bin/fusermount
29+
fi
30+
fi
3231
elif command -v apk 2>&1 >/dev/null; then
33-
: {{/* make sure the "elif" block is never empty */}}
34-
{{- if .Mounts}}
35-
if ! command -v sshfs 2>&1 >/dev/null; then
36-
apk update
37-
apk add sshfs
38-
fi
39-
modprobe fuse
40-
{{- end}}
32+
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
33+
if ! command -v sshfs 2>&1 >/dev/null; then
34+
apk update
35+
apk add sshfs
36+
fi
37+
modprobe fuse
38+
fi
4139
fi
4240
# Modify /etc/fuse.conf to allow "-o allow_root"
43-
{{- if .Mounts }}
44-
if ! grep -q "^user_allow_other" /etc/fuse.conf ; then
45-
echo "user_allow_other" >> /etc/fuse.conf
41+
42+
if [ "${LIMA_CIDATA_MOUNTS}" -gt 0 ]; then
43+
if ! grep -q "^user_allow_other" /etc/fuse.conf ; then
44+
echo "user_allow_other" >> /etc/fuse.conf
45+
fi
4646
fi
47-
{{- end}}

pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,66 @@
11
#!/bin/bash
22
set -eux -o pipefail
33

4+
5+
if [ "${LIMA_CIDATA_CONTAINERD_SYSTEM}" != 1 ] && [ "${LIMA_CIDATA_CONTAINERD_USER}" != 1 ]; then
6+
exit 0
7+
fi
8+
49
# This script does not work unless systemd is available
510
command -v systemctl 2>&1 >/dev/null || exit 0
611

712
if [ ! -x /usr/local/bin/nerdctl ]; then
813
tar Cxzf /usr/local "${LIMA_CIDATA_MNT}"/nerdctl-full.tgz
914
fi
10-
{{- if .Containerd.System}}
11-
mkdir -p /etc/containerd
12-
cat >"/etc/containerd/config.toml" <<EOF
15+
16+
if [ "${LIMA_CIDATA_CONTAINERD_SYSTEM}" = 1 ]; then
17+
mkdir -p /etc/containerd
18+
cat >"/etc/containerd/config.toml" <<EOF
1319
version = 2
1420
[proxy_plugins]
1521
[proxy_plugins."stargz"]
1622
type = "snapshot"
1723
address = "/run/containerd-stargz-grpc/containerd-stargz-grpc.sock"
1824
EOF
19-
systemctl enable --now containerd buildkit stargz-snapshotter
20-
{{- end}}
21-
{{- if .Containerd.User}}
22-
modprobe tap || true
23-
if [ ! -e "/home/{{.User}}.linux/.config/containerd/config.toml" ]; then
24-
mkdir -p "/home/{{.User}}.linux/.config/containerd"
25-
cat >"/home/{{.User}}.linux/.config/containerd/config.toml" <<EOF
25+
systemctl enable --now containerd buildkit stargz-snapshotter
26+
fi
27+
28+
if [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
29+
modprobe tap || true
30+
if [ ! -e "/home/${LIMA_CIDATA_USER}.linux/.config/containerd/config.toml" ]; then
31+
mkdir -p "/home/${LIMA_CIDATA_USER}.linux/.config/containerd"
32+
cat >"/home/${LIMA_CIDATA_USER}.linux/.config/containerd/config.toml" <<EOF
2633
version = 2
2734
[proxy_plugins]
2835
[proxy_plugins."fuse-overlayfs"]
2936
type = "snapshot"
30-
address = "/run/user/{{.UID}}/containerd-fuse-overlayfs.sock"
37+
address = "/run/user/${LIMA_CIDATA_UID}/containerd-fuse-overlayfs.sock"
3138
[proxy_plugins."stargz"]
3239
type = "snapshot"
33-
address = "/run/user/{{.UID}}/containerd-stargz-grpc/containerd-stargz-grpc.sock"
40+
address = "/run/user/${LIMA_CIDATA_UID}/containerd-stargz-grpc/containerd-stargz-grpc.sock"
3441
EOF
35-
chown -R "{{.User}}" "/home/{{.User}}.linux/.config"
36-
fi
37-
selinux=
38-
if command -v selinuxenabled 2>&1 >/dev/null && selinuxenabled; then
39-
selinux=1
40-
fi
41-
if [ ! -e "/home/{{.User}}}}.linux/.config/systemd/user/containerd.service" ]; then
42-
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
43-
if [ -n "$selinux" ]; then
44-
echo "Temporarily disabling SELinux, during installing containerd units"
45-
setenforce 0
42+
chown -R "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/.config"
4643
fi
47-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" systemctl --user enable --now dbus
48-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" containerd-rootless-setuptool.sh install
49-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" containerd-rootless-setuptool.sh install-buildkit
50-
sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" containerd-rootless-setuptool.sh install-fuse-overlayfs
51-
if ! sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" containerd-rootless-setuptool.sh install-stargz; then
52-
echo >&2 "WARNING: rootless stargz does not seem supported on this host (kernel older than 5.11?)"
44+
selinux=
45+
if command -v selinuxenabled 2>&1 >/dev/null && selinuxenabled; then
46+
selinux=1
5347
fi
54-
if [ -n "$selinux" ]; then
55-
echo "Restoring SELinux"
56-
setenforce 1
48+
if [ ! -e "/home/${LIMA_CIDATA_USER}}}.linux/.config/systemd/user/containerd.service" ]; then
49+
until [ -e "/run/user/${LIMA_CIDATA_UID}/systemd/private" ]; do sleep 3; done
50+
if [ -n "$selinux" ]; then
51+
echo "Temporarily disabling SELinux, during installing containerd units"
52+
setenforce 0
53+
fi
54+
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" systemctl --user enable --now dbus
55+
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" containerd-rootless-setuptool.sh install
56+
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" containerd-rootless-setuptool.sh install-buildkit
57+
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" containerd-rootless-setuptool.sh install-fuse-overlayfs
58+
if ! sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" containerd-rootless-setuptool.sh install-stargz; then
59+
echo >&2 "WARNING: rootless stargz does not seem supported on this host (kernel older than 5.11?)"
60+
fi
61+
if [ -n "$selinux" ]; then
62+
echo "Restoring SELinux"
63+
setenforce 1
64+
fi
5765
fi
5866
fi
59-
{{- end}}

pkg/cidata/cidata.TEMPLATE.d/lima.env

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
LIMA_CIDATA_USER={{ .User }}
2+
LIMA_CIDATA_UID={{ .UID }}
3+
LIMA_CIDATA_MOUNTS={{ len .Mounts }}
4+
{{- range $i, $val := .Mounts}}
5+
LIMA_CIDATA_MOUNTS_{{$i}}_MOUNTPOINT={{$val}}
6+
{{- end}}
7+
{{- if .Containerd.User}}
8+
LIMA_CIDATA_CONTAINERD_USER=1
9+
{{- else}}
10+
LIMA_CIDATA_CONTAINERD_USER=
11+
{{- end}}
12+
{{- if .Containerd.System}}
13+
LIMA_CIDATA_CONTAINERD_SYSTEM=1
14+
{{- else}}
15+
LIMA_CIDATA_CONTAINERD_SYSTEM=
16+
{{- end}}

pkg/cidata/cidata.TEMPLATE.d/user-data

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ users:
2020
write_files:
2121
- content: |
2222
#!/bin/sh
23-
set -eu
23+
set -eux
2424
LIMA_CIDATA_MNT="/mnt/lima-cidata"
2525
LIMA_CIDATA_DEV="/dev/disk/by-label/cidata"
2626
mkdir -p -m 700 "${LIMA_CIDATA_MNT}"
2727
mount -o ro,mode=0700,dmode=0700,overriderockperm,exec,uid=0 "${LIMA_CIDATA_DEV}" "${LIMA_CIDATA_MNT}"
2828
export LIMA_CIDATA_MNT
29+
while read line ; do export "$line"; done <"${LIMA_CIDATA_MNT}"/lima.env
2930
CODE=0
3031
for f in "${LIMA_CIDATA_MNT}"/boot/*; do
3132
echo "Executing $f"
@@ -44,11 +45,11 @@ write_files:
4445
done
4546
fi
4647
if [ -d "${LIMA_CIDATA_MNT}"/provision.user ]; then
47-
until [ -e "/run/user/{{.UID}}/systemd/private" ]; do sleep 3; done
48+
until [ -e "/run/user/${LIMA_CIDATA_UID}}/systemd/private" ]; do sleep 3; done
4849
for f in "${LIMA_CIDATA_MNT}"/provision.user/*; do
49-
echo "Executing $f (as user {{.User}})"
50-
if ! sudo -iu "{{.User}}" "XDG_RUNTIME_DIR=/run/user/{{.UID}}" "$f"; then
51-
echo "Failed to execute $f (as user {{.User}})"
50+
echo "Executing $f (as user ${LIMA_CIDATA_USER})"
51+
if ! sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" "$f"; then
52+
echo "Failed to execute $f (as user ${LIMA_CIDATA_USER})"
5253
CODE=1
5354
fi
5455
done

0 commit comments

Comments
 (0)