Skip to content

Commit b14a40c

Browse files
authored
Merge pull request #2244 from AkihiroSuda/prepare-0.20.2
[release/0.20] alpine: avoid wiping out writable host mounts under /home, etc
2 parents 4ea0a83 + c02b0f7 commit b14a40c

File tree

7 files changed

+85
-13
lines changed

7 files changed

+85
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ jobs:
219219
- opensuse.yaml
220220
- experimental/net-user-v2.yaml
221221
- docker.yaml
222+
- ../hack/test-templates/alpine-9p-writable.yaml
222223
steps:
223224
- uses: actions/checkout@v4
224225
with:

examples/alpine.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# Using the Alpine 3.19 aarch64 image with vmType=vz requires macOS Ventura 13.3 or later.
33

44
images:
5-
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.35/alpine-lima-std-3.19.0-x86_64.iso"
5+
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.37/alpine-lima-std-3.19.0-x86_64.iso"
66
arch: "x86_64"
7-
digest: "sha512:e02599dc7fc4dc279d66d800f6edc68f6f112c4b370d4c74f43040214c53b23ae4c903ce56c7083fd56d5027ec33711d30d1c2e71836c60dc3bf639f76d4fa0e"
8-
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.35/alpine-lima-std-3.19.0-aarch64.iso"
7+
digest: "sha512:568852df405e6b9858e678171a9894c058f483df0b0570c22cf33fc75f349ba6cc5bb3d50188180d8c31faaf53400fe884ca3e5f949961b03b2bf53e65de88d7"
8+
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.37/alpine-lima-std-3.19.0-aarch64.iso"
99
arch: "aarch64"
10-
digest: "sha512:13e50601ee65af5d7a6dfd30bb41fd89f8bf806ecdb516c61fe238c3cf3b57cf67469418a99f329bb4c343e3387e6e0fd4fe20501cfd501f031f7244adc67215"
10+
digest: "sha512:3a4bd5ad0201f503e9bb9f3b812aa0df292e2e099148c0323d23244046ad199a2946ef9e0619fec28726bfdcc528233f43c3b4b036c9e06e92ac730d579f0ca3"
1111

1212
mounts:
1313
- location: "~"

hack/test-port-forwarding.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
foreach my $id (0..@test-1) {
127127
my $test = $test[$id];
128128
my $nc = "nc -l $test->{guest_ip} $test->{guest_port}";
129-
if ($instance eq "alpine") {
129+
if ($instance =~ /^alpine/) {
130130
$nc = "nc -l -s $test->{guest_ip} -p $test->{guest_port}";
131131
}
132132

hack/test-templates.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ declare -A CHECKS=(
3434
["vmnet"]=""
3535
["disk"]=""
3636
["user-v2"]=""
37+
["mount-path-with-spaces"]=""
3738
)
3839

3940
case "$NAME" in
40-
"alpine")
41+
"alpine"*)
4142
WARNING "Alpine does not support systemd"
4243
CHECKS["systemd"]=
4344
CHECKS["container-engine"]=
45+
[ "$NAME" = "alpine-9p-writable" ] && CHECKS["mount-path-with-spaces"]="1"
4446
;;
4547
"k3s")
4648
ERROR "File \"$FILE\" is not testable with this script"
@@ -59,6 +61,7 @@ case "$NAME" in
5961
CHECKS["disk"]=1
6062
CHECKS["snapshot-online"]="1"
6163
CHECKS["snapshot-offline"]="1"
64+
CHECKS["mount-path-with-spaces"]="1"
6265
;;
6366
"net-user-v2")
6467
CHECKS["port-forwards"]=""
@@ -113,6 +116,11 @@ set -x
113116
"${LIMACTL_CREATE[@]}" ${LIMACTL_CREATE_ARGS} "$FILE"
114117
set +x
115118

119+
if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
120+
mkdir -p "/tmp/lima test dir with spaces"
121+
echo "test file content" >"/tmp/lima test dir with spaces/test file"
122+
fi
123+
116124
INFO "Starting \"$NAME\""
117125
set -x
118126
if ! limactl start "$NAME"; then
@@ -126,6 +134,15 @@ limactl shell "$NAME" uname -a
126134
limactl shell "$NAME" cat /etc/os-release
127135
set +x
128136

137+
INFO "Testing that host home is not wiped out"
138+
[ -e "$HOME/.lima" ]
139+
140+
if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
141+
INFO 'Testing that "/tmp/lima test dir with spaces" is not wiped out'
142+
[ "$(cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
143+
[ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
144+
fi
145+
129146
INFO "Testing proxy settings are imported"
130147
got=$(limactl shell "$NAME" env | grep FTP_PROXY)
131148
# Expected: FTP_PROXY is set in addition to ftp_proxy, localhost is replaced
@@ -409,3 +426,7 @@ sleep 3
409426

410427
INFO "Deleting \"$NAME\""
411428
limactl delete "$NAME"
429+
430+
if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
431+
rm -rf "/tmp/lima test dir with spaces"
432+
fi
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Background: https://github.com/lima-vm/lima/pull/2234
2+
# Should be tested on a Linux host
3+
images:
4+
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.37/alpine-lima-std-3.19.0-x86_64.iso"
5+
arch: "x86_64"
6+
digest: "sha512:568852df405e6b9858e678171a9894c058f483df0b0570c22cf33fc75f349ba6cc5bb3d50188180d8c31faaf53400fe884ca3e5f949961b03b2bf53e65de88d7"
7+
- location: "https://github.com/lima-vm/alpine-lima/releases/download/v0.2.37/alpine-lima-std-3.19.0-aarch64.iso"
8+
arch: "aarch64"
9+
digest: "sha512:3a4bd5ad0201f503e9bb9f3b812aa0df292e2e099148c0323d23244046ad199a2946ef9e0619fec28726bfdcc528233f43c3b4b036c9e06e92ac730d579f0ca3"
10+
11+
mountType: "9p"
12+
mounts:
13+
- location: "~"
14+
writable: true
15+
- location: "/tmp/lima test dir with spaces"
16+
writable: true
17+
- location: "/tmp/lima"
18+
writable: true
19+
20+
containerd:
21+
system: false
22+
user: false

hack/test-templates/test-misc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ images:
2020

2121
mounts:
2222
- location: "~"
23+
writable: true
24+
- location: "/tmp/lima test dir with spaces"
25+
writable: true
2326
- location: "/tmp/lima"
2427
writable: true
2528

pkg/cidata/cidata.TEMPLATE.d/boot/04-persistent-data-volume.sh

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ test -f /etc/alpine-release || exit 0
99
# Data directories that should be persisted across reboots
1010
DATADIRS="/etc /home /root /tmp /usr/local /var/lib"
1111

12+
# Prepare mnt.sh (used for restoring mounts later)
13+
echo "#!/bin/sh" >/mnt.sh
14+
echo "set -eux" >>/mnt.sh
15+
for DIR in ${DATADIRS}; do
16+
while IFS= read -r LINE; do
17+
[ -z "$LINE" ] && continue
18+
MNTDEV="$(echo "${LINE}" | awk '{print $1}')"
19+
# unmangle " \t\n\\#"
20+
# https://github.com/torvalds/linux/blob/v6.6/fs/proc_namespace.c#L89
21+
MNTPNT="$(echo "${LINE}" | awk '{print $2}' | sed -e 's/\\040/ /g; s/\\011/\t/g; s/\\012/\n/g; s/\\134/\\/g; s/\\043/#/g')"
22+
# Ignore if MNTPNT is neither DIR nor a parent directory of DIR.
23+
# It is not a parent if MNTPNT doesn't start with DIR, or the first
24+
# character after DIR isn't a slash.
25+
WITHOUT_DIR="${MNTPNT#"$DIR"}"
26+
# shellcheck disable=SC2166
27+
[ "$MNTPNT" != "$DIR" ] && [ "$MNTPNT" == "$WITHOUT_DIR" -o "${WITHOUT_DIR::1}" != "/" ] && continue
28+
MNTTYPE="$(echo "${LINE}" | awk '{print $3}')"
29+
[ "${MNTTYPE}" = "ext4" ] && continue
30+
[ "${MNTTYPE}" = "tmpfs" ] && continue
31+
MNTOPTS="$(echo "${LINE}" | awk '{print $4}')"
32+
# Before mv, unmount filesystems (virtiofs, 9p, etc.) below "${DIR}", otherwise host mounts will be wiped out
33+
# https://github.com/rancher-sandbox/rancher-desktop/issues/6582
34+
umount "${MNTPNT}" || exit 1
35+
MNTPNT=${MNTPNT//\\/\\\\}
36+
MNTPNT=${MNTPNT//\"/\\\"}
37+
echo "mount -t \"${MNTTYPE}\" -o \"${MNTOPTS}\" \"${MNTDEV}\" \"${MNTPNT}\"" >>/mnt.sh
38+
done </proc/mounts
39+
done
40+
chmod +x /mnt.sh
41+
1242
# When running from RAM try to move persistent data to data-volume
1343
# FIXME: the test for tmpfs mounts is probably Alpine-specific
1444
if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
@@ -61,11 +91,6 @@ if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
6191
PART=$(lsblk --list /dev/"${DISK}" --noheadings --output name,type | awk '$2 == "part" {print $1}')
6292
mkfs.ext4 -L data-volume /dev/"${PART}"
6393
mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
64-
# Unmount all mount points under /tmp so we can move it to the data volume:
65-
# "mount1 on /tmp/lima type 9p (rw,dirsync,relatime,mmap,access=client,trans=virtio)"
66-
for MP in $(mount | awk '$3 ~ /^\/tmp\// {print $3}'); do
67-
umount "${MP}"
68-
done
6994
# setup apk package cache
7095
mkdir -p /mnt/data/apk/cache
7196
mkdir -p /etc/apk
@@ -88,8 +113,8 @@ if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
88113
mount --bind /mnt/data"${DIR}" "${DIR}"
89114
fi
90115
done
91-
# Make sure to re-mount any mount points under /tmp
92-
mount -a
116+
# Remount submounts on top of the new ${DIR}
117+
/mnt.sh
93118
# Reinstall packages from /mnt/data/apk/cache into the RAM disk
94119
apk fix --no-network
95120
fi

0 commit comments

Comments
 (0)