Skip to content

Commit 3936c02

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 1a72344 commit 3936c02

File tree

4 files changed

+120
-7
lines changed

4 files changed

+120
-7
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ jobs:
405405
name: "vz"
406406
runs-on: macos-13
407407
timeout-minutes: 120
408+
strategy:
409+
fail-fast: false
410+
matrix:
411+
template:
412+
- experimental/vz.yaml
413+
- fedora.yaml
414+
include:
415+
- template: experimental/vz.yaml
416+
name: default
417+
- template: fedora.yaml
418+
name: vz-fedora
408419
steps:
409420
- uses: actions/checkout@v4
410421
with:
@@ -417,12 +428,14 @@ jobs:
417428
with:
418429
path: ~/Library/Caches/lima/download
419430
# hashFiles do not seem to support symlinks
420-
key: ${{ runner.os }}-${{ hashFiles('examples/experimental/vz.yaml') }}
431+
key: ${{ runner.os }}-${{ hashFiles('examples/*.yaml') }}
421432
- name: Make
422433
run: make
423434
- name: Install
424435
run: make install
425436
- name: Install test dependencies
426437
run: brew install qemu bash coreutils
427438
- name: Test
428-
run: ./hack/test-templates.sh templates/experimental/vz.yaml
439+
env:
440+
ARGS: "--vm-type vz --mount-type virtiofs --rosetta --network vzNAT"
441+
run: ./hack/test-templates.sh templates/${{ matrix.template }} ${{ matrix.name }}

hack/test-selinux.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
# shellcheck source=common.inc.sh
7+
source "${scriptdir}/common.inc.sh"
8+
9+
if [ "$#" -ne 1 ]; then
10+
ERROR "Usage: $0 NAME"
11+
exit 1
12+
fi
13+
14+
NAME="$1"
15+
expected="context=system_u:object_r:container_file_t:s0"
16+
#Skip Rosetta checks for x86 GHA mac runners
17+
if [ "$(arch)" == "arm64" ]; then
18+
INFO "Testing secontext is set for rosetta mounts"
19+
got=$(limactl shell "$NAME" mount | grep "rosetta" | awk '{print $6}')
20+
INFO "secontext rosetta: expected=${expected}, got=${got}"
21+
if [[ $got != *$expected* ]]; then
22+
ERROR "secontext for rosetta mount is not set or Invalid"
23+
exit 1
24+
fi
25+
fi
26+
INFO "Testing secontext is set for bind mounts"
27+
INFO "Checking in mounts"
28+
got=$(limactl shell "$NAME" mount | grep "$HOME" | awk '{print $6}')
29+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
30+
if [[ $got != *$expected* ]]; then
31+
ERROR "secontext for \"$HOME\" dir is not set or Invalid"
32+
exit 1
33+
fi
34+
got=$(limactl shell "$NAME" mount | grep "/tmp/lima" | awk '{print $6}')
35+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
36+
if [[ $got != *$expected* ]]; then
37+
ERROR 'secontext for "/tmp/lima" dir is not set or Invalid'
38+
exit 1
39+
fi
40+
INFO "Checking in fstab file"
41+
expected='context="system_u:object_r:container_file_t:s0"'
42+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "$HOME" | awk '{print $4}')
43+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
44+
if [[ $got != *$expected* ]]; then
45+
ERROR "secontext for \"$HOME\" dir is not set or Invalid"
46+
exit 1
47+
fi
48+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "/tmp/lima" | awk '{print $4}')
49+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
50+
if [[ $got != *$expected* ]]; then
51+
ERROR 'secontext for "/tmp/lima" dir is not set or Invalid'
52+
exit 1
53+
fi

hack/test-templates.sh

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
# shellcheck source=common.inc.sh
66
source "${scriptdir}/common.inc.sh"
77

8-
if [ "$#" -ne 1 ]; then
9-
ERROR "Usage: $0 FILE.yaml"
8+
if [ "$#" -eq 0 ]; then
9+
ERROR "Usage: $0 FILE.yaml VM_NAME"
1010
exit 1
1111
fi
1212

1313
FILE="$1"
14-
NAME="$(basename -s .yaml "$FILE")"
14+
if [[ $# -eq 2 && $2 != "default" ]]; then
15+
NAME="$2"
16+
else
17+
NAME="$(basename -s .yaml "$FILE")"
18+
fi
1519

1620
INFO "Validating \"$FILE\""
1721
limactl validate "$FILE"
1822

1923
# --cpus=1 is needed for running vz on GHA: https://github.com/lima-vm/lima/pull/1511#issuecomment-1574937888
2024
LIMACTL_CREATE=(limactl --tty=false create --cpus=1 --memory=1)
25+
LIMACTL_ARGS=""
2126

2227
CONTAINER_ENGINE="nerdctl"
2328

@@ -34,6 +39,7 @@ declare -A CHECKS=(
3439
["vmnet"]=""
3540
["disk"]=""
3641
["user-v2"]=""
42+
["vz-selinux"]=""
3743
)
3844

3945
case "$NAME" in
@@ -67,6 +73,12 @@ case "$NAME" in
6773
"docker")
6874
CONTAINER_ENGINE="docker"
6975
;;
76+
"vz-fedora")
77+
WARNING "Relaxing systemd tests for vz-fedora (For avoiding CI failure)"
78+
CHECKS["systemd-strict"]=
79+
CHECKS["vz-selinux"]=1
80+
LIMACTL_ARGS="${ARGS}"
81+
;;
7082
esac
7183

7284
if limactl ls -q | grep -q "$NAME"; then
@@ -109,7 +121,13 @@ if [[ -n ${CHECKS["disk"]} ]]; then
109121
fi
110122

111123
set -x
112-
"${LIMACTL_CREATE[@]}" "$FILE"
124+
# shellcheck disable=SC2128
125+
if [ "${LIMACTL_ARGS}" == "" ]; then
126+
"${LIMACTL_CREATE[@]}" "$FILE"
127+
else
128+
# shellcheck disable=SC2086
129+
"${LIMACTL_CREATE[@]}" ${LIMACTL_ARGS} --name "$NAME" "$FILE"
130+
fi
113131
set +x
114132

115133
INFO "Starting \"$NAME\""
@@ -223,7 +241,7 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
223241
if [ "${NAME}" = "debian" ]; then
224242
limactl shell "$NAME" sudo apt-get install -y netcat-openbsd
225243
fi
226-
if [ "${NAME}" = "fedora" ]; then
244+
if [[ ${NAME} == *"fedora"* ]]; then
227245
limactl shell "$NAME" sudo dnf install -y nc
228246
fi
229247
if [ "${NAME}" = "opensuse" ]; then
@@ -389,6 +407,10 @@ if [[ -n ${CHECKS["snapshot-offline"]} ]]; then
389407
limactl start "$NAME"
390408
fi
391409

410+
if [[ -n ${CHECKS["vz-selinux"]} ]]; then
411+
"${scriptdir}"/test-selinux.sh "$NAME"
412+
fi
413+
392414
INFO "Stopping \"$NAME\""
393415
limactl stop "$NAME"
394416
sleep 3
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)