@@ -9,6 +9,36 @@ test -f /etc/alpine-release || exit 0
99# Data directories that should be persisted across reboots
1010DATADIRS=" /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
1444if [ " $( 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
95120fi
0 commit comments