Skip to content

Commit

Permalink
python: sd_nspawn: Only add kvm.conf if the user has rw access to /de…
Browse files Browse the repository at this point in the history
…v/kvm

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
  • Loading branch information
nathanchance committed Dec 30, 2024
1 parent 39723a1 commit 136bb02
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions python/scripts/sd_nspawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
'x86_64': 'dev-arch',
}

DEV_KVM_ACCESS = os.access('/dev/kvm', os.R_OK | os.W_OK)


class NspawnConfig(UserDict):

Expand Down Expand Up @@ -223,8 +225,8 @@ def install_files(self):
# Allow containers started as services to access /dev/kvm to run
# accelerated VMs, which allows avoiding installing QEMU in the host
# environment.
if not (kvm_conf :=
Path('/etc/systemd/system/systemd-nspawn@.service.d/kvm.conf')).exists():
if DEV_KVM_ACCESS and not (kvm_conf := Path(
'/etc/systemd/system/systemd-nspawn@.service.d/kvm.conf')).exists():
kvm_conf_txt = ('[Service]\n'
'DeviceAllow=/dev/kvm rw\n')
if not kvm_conf.parent.exists():
Expand Down Expand Up @@ -315,8 +317,9 @@ def reset(self, mode):
setup_files = {
SYSTEMD_RUN_M,
Path('/etc/polkit-1/rules.d', f"50-permit-{USER}-machinectl-shell.rules"),
Path('/etc/systemd/system/systemd-nspawn@.service.d/kvm.conf'),
}
if DEV_KVM_ACCESS:
setup_files.add(Path('/etc/systemd/system/systemd-nspawn@.service.d/kvm.conf'))

if mode == 'machine':
items_to_remove = machine_files
Expand Down

0 comments on commit 136bb02

Please sign in to comment.