Skip to content

Commit f10c3aa

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 f10c3aa

File tree

4 files changed

+114
-7
lines changed

4 files changed

+114
-7
lines changed

.github/workflows/test.yml

Lines changed: 13 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,12 @@ 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+
run: ./hack/test-templates.sh templates/${{ matrix.template }} ${{ matrix.name }}

hack/test-selinux.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
INFO "Testing secontext is set for rosetta mounts"
16+
expected="context=system_u:object_r:container_file_t:s0"
17+
got=$(limactl shell "$NAME" mount | grep "rosetta" | awk '{print $6}')
18+
INFO "secontext rosetta: expected=${expected}, got=${got}"
19+
if [[ $got != *$expected* ]]; then
20+
ERROR "secontext for rosetta mount is not set or Invalid"
21+
exit 1
22+
fi
23+
INFO "Testing secontext is set for bind mounts"
24+
INFO "Checking in mounts"
25+
got=$(limactl shell "$NAME" mount | grep "$HOME" | awk '{print $6}')
26+
INFO "secontext ${HOME}: expected=${expected}, got=${got}"
27+
if [[ $got != *$expected* ]]; then
28+
ERROR "secontext for \"$HOME\" dir is not set or Invalid"
29+
exit 1
30+
fi
31+
got=$(limactl shell "$NAME" mount | grep "/tmp/lima" | awk '{print $6}')
32+
INFO "secontext /tmp/lima: expected=${expected}, got=${got}"
33+
if [[ $got != *$expected* ]]; then
34+
ERROR 'secontext for "/tmp/lima" dir is not set or Invalid'
35+
exit 1
36+
fi
37+
INFO "Checking in fstab file"
38+
expected='context="system_u:object_r:container_file_t:s0"'
39+
got=$(limactl shell "$NAME" cat /etc/fstab | grep "$HOME" | awk '{print $4}')
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" cat /etc/fstab | grep "/tmp/lima" | awk '{print $4}')
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

hack/test-templates.sh

Lines changed: 26 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_SUBCMDS=""
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_SUBCMDS=(--vm-type vz --mount-type virtiofs --rosetta --network vzNAT)
81+
;;
7082
esac
7183

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

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

115132
INFO "Starting \"$NAME\""
@@ -223,7 +240,7 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
223240
if [ "${NAME}" = "debian" ]; then
224241
limactl shell "$NAME" sudo apt-get install -y netcat-openbsd
225242
fi
226-
if [ "${NAME}" = "fedora" ]; then
243+
if [[ ${NAME} == *"fedora"* ]]; then
227244
limactl shell "$NAME" sudo dnf install -y nc
228245
fi
229246
if [ "${NAME}" = "opensuse" ]; then
@@ -389,6 +406,10 @@ if [[ -n ${CHECKS["snapshot-offline"]} ]]; then
389406
limactl start "$NAME"
390407
fi
391408

409+
if [[ -n ${CHECKS["vz-selinux"]} ]]; then
410+
"${scriptdir}"/test-selinux.sh "$NAME"
411+
fi
412+
392413
INFO "Stopping \"$NAME\""
393414
limactl stop "$NAME"
394415
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)