Skip to content

Commit

Permalink
alpine: avoid wiping out writable host mounts under /home, etc.
Browse files Browse the repository at this point in the history
A host directory could be wiped out when all the following conditions are met:
- The directory is mounted to Lima via virtiofs
- The mount is writable
- The mount point in the guest is under one of: /etc /home /root /tmp /usr/local /var/lib
- The guest OS is Alpine Linux

Fix issue 2221
Fix rancher-sandbox/rancher-desktop issue 6582

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Mar 8, 2024
1 parent fe633d4 commit 16e0e77
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
for DIR in ${DATADIRS}; do
DEST="/mnt/data$(dirname "${DIR}")"
mkdir -p "${DIR}" "${DEST}"
# Unmount filesystems (virtiofs, 9p, etc.) below "${DIR}", otherwise host mounts will be wiped out
# https://github.com/rancher-sandbox/rancher-desktop/issues/6582
awk '$3 != "ext4" && $3 != "tmpfs" {print $2}' /proc/mounts | grep "^${DIR}" | while IFS= read -r line; do
# unmangle " \t\n\\#"
# https://github.com/torvalds/linux/blob/v6.6/fs/proc_namespace.c#L89
line="$(echo "$line" | sed -e 's/\\040/ /g; s/\\011/\t/g; s/\\012/\n/g; s/\\134/\\/g; s/\\043/#/g')"
[ -n "$line" ] && umount "$line"
done
mv "${DIR}" "${DEST}"
done
# Make sure all data moved to the persistent volume has been committed to disk
Expand Down

0 comments on commit 16e0e77

Please sign in to comment.