跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

04-persistent-data-volume.sh

155 行 · 6.4 KB历史下载
1#!/bin/bash
2
3# SPDX-FileCopyrightText: Copyright The Lima Authors
4# SPDX-License-Identifier: Apache-2.0
5
6# bash is used for enabling `set -o pipefail`.
7# NOTE: On Alpine, /bin/bash is ash with ASH_BASH_COMPAT, not GNU bash
8set -eux -o pipefail
9
10# Restrict the rest of this script to Alpine until it has been tested with other distros
11test -f /etc/alpine-release || exit 0
12
13# Nothing to do unless we are running from a ramdisk
14[ "$(awk '$2 == "/" {print $3}' /proc/mounts)" != "tmpfs" ] && exit 0
15
16# Data directories that should be persisted across reboots
17DATADIRS="/etc /home /root /tmp /usr/local /var/lib"
18
19# Prepare mnt.sh (used for restoring mounts later)
20echo "#!/bin/sh" >/mnt.sh
21echo "set -eux" >>/mnt.sh
22for DIR in ${DATADIRS}; do
23 while IFS= read -r LINE; do
24 [ -z "$LINE" ] && continue
25 MNTDEV="$(echo "${LINE}" | awk '{print $1}')"
26 # unmangle " \t\n\\#"
27 # https://github.com/torvalds/linux/blob/v6.6/fs/proc_namespace.c#L89
28 MNTPNT="$(echo "${LINE}" | awk '{print $2}' | unescape_fstab.sh)"
29 # Ignore if MNTPNT is neither DIR nor a parent directory of DIR.
30 # It is not a parent if MNTPNT doesn't start with DIR, or the first
31 # character after DIR isn't a slash.
32 WITHOUT_DIR="${MNTPNT#"$DIR"}"
33 # shellcheck disable=SC2166
34 [ "$MNTPNT" != "$DIR" ] && [ "$MNTPNT" == "$WITHOUT_DIR" -o "${WITHOUT_DIR::1}" != "/" ] && continue
35 MNTTYPE="$(echo "${LINE}" | awk '{print $3}')"
36 [ "${MNTTYPE}" = "ext4" ] && continue
37 [ "${MNTTYPE}" = "tmpfs" ] && continue
38 MNTOPTS="$(echo "${LINE}" | awk '{print $4}')"
39 if [ "${MNTTYPE}" = "9p" ]; then
40 # https://github.com/torvalds/linux/blob/v6.6/fs/9p/v9fs.h#L61
41 MNTOPTS="$(echo "${MNTOPTS}" | sed -e 's/cache=8f,/cache=fscache,/; s/cache=f,/cache=loose,/; s/cache=5,/cache=mmap,/; s/cache=1,/cache=readahead,/; s/cache=0,/cache=none,/')"
42 fi
43 # Before mv, unmount filesystems (virtiofs, 9p, etc.) below "${DIR}", otherwise host mounts will be wiped out
44 # https://github.com/rancher-sandbox/rancher-desktop/issues/6582
45 umount "${MNTPNT}" || exit 1
46 MNTPNT=${MNTPNT//\\/\\\\}
47 MNTPNT=${MNTPNT//\"/\\\"}
48 echo "mount -t \"${MNTTYPE}\" -o \"${MNTOPTS}\" \"${MNTDEV}\" \"${MNTPNT}\"" >>/mnt.sh
49 done </proc/mounts
50done
51chmod +x /mnt.sh
52
53mkdir -p /mnt/data
54
55# Resolve the data volume device via blkid, not the udev symlink.
56# The /dev/disk/by-label/ symlink depends on udev having probed the device.
57# A race between growpart (which triggers a partition table re-read and thus
58# a udev re-probe) and e2fsck (which modifies the superblock) can cause the
59# re-probe to see an inconsistent ext4 checksum and delete the symlink.
60# BusyBox blkid doesn't support --label, so we parse the output instead.
61# The leading whitespace anchor on LABEL= avoids matching PARTLABEL= on
62# util-linux blkid output; quitting after the first match prevents a
63# multi-match from producing a newline-separated device list.
64DATA_VOLUME=$(blkid | sed -n '/[[:space:]]LABEL="data-volume"/{s/^\([^:]*\):.*/\1/p;q;}')
65
66if [ -n "${DATA_VOLUME}" ]; then
67 DATA_DISK="/dev/$(lsblk --noheadings --output pkname "${DATA_VOLUME}")"
68 # growpart command may be missing in older VMs
69 if command -v growpart >/dev/null 2>&1 && command -v resize2fs >/dev/null 2>&1; then
70 # Automatically expand the data volume filesystem
71 growpart "$DATA_DISK" 1 || true
72 # growpart re-reads the partition table; settle before e2fsck so
73 # the resulting udev re-probe doesn't race with fsck. Tolerate
74 # failure: the blkid above already bypasses udev, so this is
75 # defense-in-depth, and a missing udevadm must not kill boot
76 # under set -e.
77 udevadm settle || true
78 # Only resize when filesystem is in a healthy state
79 if e2fsck -f -p "${DATA_VOLUME}"; then
80 resize2fs "${DATA_VOLUME}" || true
81 fi
82 fi
83 # Mount data volume
84 mount -t ext4 "${DATA_VOLUME}" /mnt/data
85 # Update /etc files that might have changed during this boot
86 cp /etc/network/interfaces /mnt/data/etc/network/
87 cp /etc/resolv.conf /mnt/data/etc/
88 if [ -f /etc/localtime ]; then
89 # Preserve symlink
90 cp -d /etc/localtime /mnt/data/etc/
91 # setup-timezone copies the single zoneinfo file into /etc/zoneinfo and targets the symlink there
92 if [ -d /etc/zoneinfo ]; then
93 rm -rf /mnt/data/etc/zoneinfo
94 cp -r /etc/zoneinfo /mnt/data/etc
95 fi
96 fi
97 if [ -f /etc/timezone ]; then
98 cp /etc/timezone /mnt/data/etc/
99 fi
100 # TODO there are probably others that should be updated as well
101else
102 # Find an unpartitioned disk and create data-volume
103 DISKS=$(lsblk --list --noheadings --output name,type | awk '$2 == "disk" {print $1}')
104 for DISK in ${DISKS}; do
105 # A disk is in use if it has any partitions, carries a filesystem
106 # signature directly, or is mounted. Check lsblk for partitions
107 # and filesystem types, not just /proc/mounts; an unmounted but
108 # partitioned or raw-formatted disk (e.g. the data volume after
109 # a failed boot) must not be reformatted.
110 if lsblk --list --noheadings --output fstype /dev/"${DISK}" | grep -q "[^[:space:]]"; then
111 continue
112 fi
113 if lsblk --list --noheadings --output type /dev/"${DISK}" | grep -q "part"; then
114 continue
115 fi
116 if awk '/^\/dev\// {sub("^/dev/", "", $1); print $1}' /proc/mounts | grep -q "^${DISK}$"; then
117 continue
118 fi
119 echo 'type=83' | sfdisk --label dos /dev/"${DISK}"
120 # sfdisk's BLKRRPART emits a uevent for the new partition; settle
121 # before lsblk so its libudev query sees that partition rather
122 # than stale pre-sfdisk state. Tolerate failure as above.
123 udevadm settle || true
124 PART=$(lsblk --list /dev/"${DISK}" --noheadings --output name,type | awk '$2 == "part" {print $1}')
125 mkfs.ext4 -L data-volume /dev/"${PART}"
126 # Let udev create /dev/disk/by-label/data-volume; mount uses the
127 # device path directly, but later scripts may depend on the
128 # symlink. Tolerate failure as above.
129 udevadm settle || true
130 mount -t ext4 /dev/"${PART}" /mnt/data
131 # setup apk package cache
132 mkdir -p /mnt/data/apk/cache
133 mkdir -p /etc/apk
134 ln -s /mnt/data/apk/cache /etc/apk/cache
135 # Move all persisted directories to the data volume
136 for DIR in ${DATADIRS}; do
137 DEST="/mnt/data$(dirname "${DIR}")"
138 mkdir -p "${DIR}" "${DEST}"
139 mv "${DIR}" "${DEST}"
140 done
141 # Make sure all data moved to the persistent volume has been committed to disk
142 sync
143 break
144 done
145fi
146for DIR in ${DATADIRS}; do
147 if [ -d /mnt/data"${DIR}" ]; then
148 mkdir -p "${DIR}"
149 mount --bind /mnt/data"${DIR}" "${DIR}"
150 fi
151done
152# Remount submounts on top of the new ${DIR}
153/mnt.sh
154# Reinstall packages from /mnt/data/apk/cache into the RAM disk
155apk fix --no-network