Skip to content

Commit

Permalink
nixos/libvirtd: add option to run qemu as non-root
Browse files Browse the repository at this point in the history
New option virtualisation.libvirtd.qemuRunAsRoot (default: true).
If false, qemu is run as non-privileged user qemu-libvirtd,
reducing security risk
  • Loading branch information
xeji committed Aug 10, 2018
1 parent 9c33863 commit 980505d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nixos/modules/misc/ids.nix
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@
hydron = 298;
cfssl = 299;
cassandra = 300;
qemu-libvirtd = 301;

# When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399!

Expand Down Expand Up @@ -610,6 +611,7 @@
hydron = 298;
cfssl = 299;
cassandra = 300;
qemu-libvirtd = 301;

# When adding a gid, make sure it doesn't match an existing
# uid. Users and groups with the same name should have equal
Expand Down
24 changes: 24 additions & 0 deletions nixos/modules/virtualisation/libvirtd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ let
${optionalString cfg.qemuOvmf ''
nvram = ["/run/libvirt/nix-ovmf/OVMF_CODE.fd:/run/libvirt/nix-ovmf/OVMF_VARS.fd"]
''}
${optionalString (!cfg.qemuRunAsRoot) ''
user = "qemu-libvirtd"
group = "qemu-libvirtd"
''}
${cfg.qemuVerbatimConfig}
'';

Expand Down Expand Up @@ -56,6 +60,18 @@ in {
'';
};

virtualisation.libvirtd.qemuRunAsRoot = mkOption {
type = types.bool;
default = true;
description = ''
If true, libvirtd runs qemu as root.
If false, libvirtd runs qemu as unprivileged user qemu-libvirtd.
Changing this option to false may cause file permission issues
for existing guests. To fix these, manually change ownership
of affected files in /var/lib/libvirt/qemu to qemu-libvirtd.
'';
};

virtualisation.libvirtd.qemuVerbatimConfig = mkOption {
type = types.lines;
default = ''
Expand Down Expand Up @@ -110,6 +126,14 @@ in {

users.groups.libvirtd.gid = config.ids.gids.libvirtd;

# libvirtd runs qemu as this user and group by default
users.extraGroups.qemu-libvirtd.gid = config.ids.gids.qemu-libvirtd;
users.extraUsers.qemu-libvirtd = {
uid = config.ids.uids.qemu-libvirtd;
isNormalUser = false;
group = "qemu-libvirtd";
};

systemd.packages = [ pkgs.libvirt ];

systemd.services.libvirtd = {
Expand Down

0 comments on commit 980505d

Please sign in to comment.