浏览文件
05-lima-mounts.sh
78 行 · 3.7 KB历史下载| 1 | |
| 2 | |
| 3 | # SPDX-FileCopyrightText: Copyright The Lima Authors |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | set -eux -o pipefail |
| 7 | |
| 8 | # Check if mount type is virtiofs and vm type as vz |
| 9 | if ! [[ ${LIMA_CIDATA_VMTYPE} == "vz" && ${LIMA_CIDATA_MOUNTTYPE} == "virtiofs" ]]; then |
| 10 | exit 0 |
| 11 | fi |
| 12 | |
| 13 | # cloud-init's cc_mounts writes the mount point into /etc/fstab without octal-escaping it, so a |
| 14 | # space/tab in the path makes an unparsable line that mount(8) silently skips via nofail. |
| 15 | # cc_mounts already created the directory from the unescaped value, so just repair the fstab |
| 16 | # syntax (see util/escape_fstab.sh) and (re)mount. |
| 17 | # https://github.com/lima-vm/lima/issues/5136 |
| 18 | # https://github.com/abiosoft/colima/issues/1471 |
| 19 | if grep -q virtiofs /etc/fstab; then |
| 20 | escape_fstab.sh </etc/fstab >/etc/fstab.lima.tmp && |
| 21 | cp /etc/fstab.lima.tmp /etc/fstab && rm -f /etc/fstab.lima.tmp |
| 22 | # Mount entries cc_mounts skipped due to the previously broken line (already-mounted ones |
| 23 | # are a no-op). On Oracle Linux the virtiofs module is installed later in |
| 24 | # 30-install-packages.sh (REMOUNT_VIRTIOFS=1 remounts then), so tolerate failure here. |
| 25 | mount -t virtiofs -a || true |
| 26 | fi |
| 27 | |
| 28 | # Update fstab entries and unmount/remount the volumes with secontext options |
| 29 | # when selinux is enabled in kernel |
| 30 | if [ -d /sys/fs/selinux ]; then |
| 31 | LABEL_BIN="system_u:object_r:bin_t:s0" |
| 32 | LABEL_NFS="system_u:object_r:nfs_t:s0" |
| 33 | # shellcheck disable=SC2013 |
| 34 | for line in $(grep -n virtiofs </etc/fstab | cut -d':' -f1); do |
| 35 | OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab) |
| 36 | TAG=$(awk -v line="$line" 'NR==line {print $1}' /etc/fstab) |
| 37 | MOUNT_OPTIONS=$(mount | grep "${TAG}" | awk '{print $6}') |
| 38 | if [[ ${OPTIONS} != *"context"* ]]; then |
| 39 | ########################################################################################## |
| 40 | ## When using vz & virtiofs, initially container_file_t selinux label |
| 41 | ## was considered which works perfectly for container work loads |
| 42 | ## but it might break for other work loads if the process is running with |
| 43 | ## different label. Also these are the remote mounts from the host machine, |
| 44 | ## so keeping the label as nfs_t fits right. Package container-selinux by |
| 45 | ## default adds rules for nfs_t context which allows container workloads to work as well. |
| 46 | ## https://github.com/lima-vm/lima/pull/1965 |
| 47 | ## |
| 48 | ## With integration[https://github.com/lima-vm/lima/pull/2474] with systemd-binfmt, |
| 49 | ## the existing "nfs_t" selinux label for Rosetta is causing issues while registering it. |
| 50 | ## This behaviour needs to be fixed by setting the label as "bin_t" |
| 51 | ## https://github.com/lima-vm/lima/pull/2630 |
| 52 | ########################################################################################## |
| 53 | if [[ ${TAG} == *"rosetta"* ]]; then |
| 54 | label=${LABEL_BIN} |
| 55 | else |
| 56 | label=${LABEL_NFS} |
| 57 | fi |
| 58 | sed -i -e "$line""s/comment=cloudconfig/comment=cloudconfig,context=\"$label\"/g" /etc/fstab |
| 59 | if [[ ${MOUNT_OPTIONS} != *"$label"* ]]; then |
| 60 | # fstab stores the mount point octal-escaped (e.g. space = "\040"); decode |
| 61 | # it before passing the path to mount(8). |
| 62 | MOUNT_POINT=$(awk -v line="$line" 'NR==line {print $2}' /etc/fstab | unescape_fstab.sh) |
| 63 | OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab) |
| 64 | |
| 65 | ######################################################### |
| 66 | ## We need to migrate existing users of Fedora having |
| 67 | ## Rosetta mounted from nfs_t to bin_t by unregistering |
| 68 | ## it from binfmt before remounting |
| 69 | ######################################################### |
| 70 | if [[ ${TAG} == *"rosetta"* && ${MOUNT_OPTIONS} == *"${LABEL_NFS}"* ]]; then |
| 71 | [ ! -f "/proc/sys/fs/binfmt_misc/rosetta" ] || echo -1 >/proc/sys/fs/binfmt_misc/rosetta |
| 72 | fi |
| 73 | umount "${TAG}" |
| 74 | mount -t virtiofs "${TAG}" "${MOUNT_POINT}" -o "${OPTIONS}" |
| 75 | fi |
| 76 | fi |
| 77 | done |
| 78 | fi |

