Skip to content

Commit 83ca1f3

Browse files
committed
Set secontext for bind volumes in selinux enabled distros
Fixes #1882 Signed-off-by: T K Chandra Hasan <t.k.chandra.hasan@ibm.com>
1 parent b5aa0dd commit 83ca1f3

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ jobs:
405405
name: "vz"
406406
runs-on: macos-13
407407
timeout-minutes: 120
408+
strategy:
409+
fail-fast: false
410+
matrix:
411+
template:
412+
- templates/experimental/vz.yaml
413+
- hack/test-templates/test-vz-fedora.yaml
408414
steps:
409415
- uses: actions/checkout@v4
410416
with:
@@ -425,4 +431,4 @@ jobs:
425431
- name: Install test dependencies
426432
run: brew install qemu bash coreutils
427433
- name: Test
428-
run: ./hack/test-templates.sh templates/experimental/vz.yaml
434+
run: ./hack/test-templates.sh ${{ matrix.template }}

hack/test-mount-home.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,42 @@ if [ "$got" != "$expected" ]; then
2424
ERROR "Home directory is not shared?"
2525
exit 1
2626
fi
27+
28+
if [ "${NAME}" == "test-vz-fedora" ]; then
29+
INFO "Testing secontext is set for rosetta mounts"
30+
expected="context=system_u:object_r:container_file_t:s0"
31+
got=$(limactl shell "$NAME" mount | grep "rosetta" | awk '{print $6}')
32+
INFO "secontext rosetta: expected=${expected}, got=${got}"
33+
if [[ $got != *$expected* ]]; then
34+
ERROR "Secontext for rosetta mount is not set or Invalid"
35+
exit 1
36+
fi
37+
INFO "Testing secontext is set for bind mounts"
38+
INFO "Checking in mounts"
39+
got=$(limactl shell "$NAME" mount | grep "$HOME" | awk '{print $6}')
40+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
41+
if [[ $got != *$expected* ]]; then
42+
ERROR "Secontext for \"$HOME\" dir is not set or Invalid"
43+
exit 1
44+
fi
45+
got=$(limactl shell "$NAME" mount | grep "/tmp/lima" | awk '{print $6}')
46+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
47+
if [[ $got != *$expected* ]]; then
48+
ERROR 'Secontext for "/tmp/lima" dir is not set or Invalid'
49+
exit 1
50+
fi
51+
INFO "Checking in fstab file"
52+
expected='context="system_u:object_r:container_file_t:s0"'
53+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "$HOME" | awk '{print $4}')
54+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
55+
if [[ $got != *$expected* ]]; then
56+
ERROR "Secontext for \"$HOME\" dir is not set or Invalid"
57+
exit 1
58+
fi
59+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "/tmp/lima" | awk '{print $4}')
60+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
61+
if [[ $got != *$expected* ]]; then
62+
ERROR 'Secontext for "/tmp/lima" dir is not set or Invalid'
63+
exit 1
64+
fi
65+
fi

hack/test-templates.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ case "$NAME" in
6767
"docker")
6868
CONTAINER_ENGINE="docker"
6969
;;
70+
"test-vz-fedora")
71+
WARNING "Relaxing systemd tests for fedora (For avoiding CI failure)"
72+
CHECKS["systemd-strict"]=
73+
;;
7074
esac
7175

7276
if limactl ls -q | grep -q "$NAME"; then
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# A template to run ubuntu using vmType: vz instead of qemu (Default)
2+
# This template requires Lima v0.14.0 or later and macOS 13.
3+
vmType: "vz"
4+
rosetta:
5+
# Enable Rosetta for Linux.
6+
# Hint: try `softwareupdate --install-rosetta` if Lima gets stuck at `Installing rosetta...`
7+
enabled: true
8+
# Register rosetta to /proc/sys/fs/binfmt_misc
9+
binfmt: true
10+
11+
# Note: On Intel Mac, macOS >= 13.5 is required to boot kernel v6.2 (used by Ubuntu 23.04, Fedora 38, etc.) with vz.
12+
# https://github.com/lima-vm/lima/issues/1577
13+
images:
14+
- location: "https://download.fedoraproject.org/pub/fedora/linux/releases/38/Cloud/x86_64/images/Fedora-Cloud-Base-38-1.6.x86_64.qcow2"
15+
arch: "x86_64"
16+
digest: "sha256:d334670401ff3d5b4129fcc662cf64f5a6e568228af59076cc449a4945318482"
17+
- location: "https://download.fedoraproject.org/pub/fedora/linux/releases/38/Cloud/aarch64/images/Fedora-Cloud-Base-38-1.6.aarch64.qcow2"
18+
arch: "aarch64"
19+
digest: "sha256:ad71d22104a16e4f9efa93e61e8c7bce28de693f59c802586abbe85e9db55a65"
20+
21+
mounts:
22+
- location: "~"
23+
- location: "/tmp/lima"
24+
writable: true
25+
mountType: "virtiofs"
26+
27+
networks:
28+
# The "vzNAT" IP address is accessible from the host, but not from other guests.
29+
- vzNAT: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
# Check if mount type is virtiofs and vm type as vz
6+
if ! [[ ${LIMA_CIDATA_VMTYPE} == "vz" && ${LIMA_CIDATA_MOUNTTYPE} == "virtiofs" ]]; then
7+
exit 0
8+
fi
9+
10+
# Update fstab entries and unmount/remount the volumes with secontext options
11+
# when selinux is enabled in kernel
12+
if [ -d /sys/fs/selinux ]; then
13+
# shellcheck disable=SC2013
14+
for line in $(grep -n virtiofs </etc/fstab | cut -d':' -f1); do
15+
OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab)
16+
if [[ ${OPTIONS} != *"context"* ]]; then
17+
sed -i -e "$line""s/comment=cloudconfig/comment=cloudconfig,context=\"system_u:object_r:container_file_t:s0\"/g" /etc/fstab
18+
TAG=$(awk -v line="$line" 'NR==line {print $1}' /etc/fstab)
19+
MOUNT_POINT=$(awk -v line="$line" 'NR==line {print $2}' /etc/fstab)
20+
OPTIONS=$(awk -v line="$line" 'NR==line {print $4}' /etc/fstab)
21+
umount "${TAG}"
22+
mount -t virtiofs "${TAG}" "${MOUNT_POINT}" -o "${OPTIONS}"
23+
fi
24+
done
25+
fi

0 commit comments

Comments
 (0)