git.iko.soy/typeable/nixpkgs
on master help sign in
libvirtd (nixos): support bridging for user sessions
commit28563ef5cb835898a6f2d69eea8d2c315dfb8258 authorPeter Hoeg <peter@hoeg.com> dateWed 2 Jan 2019, 21:35 (8 years ago) parent38798ee
1 file changed+64−40
nixos/modules/virtualisation/libvirtd.nix+64−40
nixos/modules/virtualisation/libvirtd.nix+64−40
@@ -23,14 +23,16 @@ let
2323 ''}
2424 ${cfg.qemuVerbatimConfig}
2525 '';
26+ dirName = "libvirt";
27+ subDirs = list: [ dirName ] ++ map (e: "${dirName}/${e}") list;
2628
2729in {
2830
2931 ###### interface
3032
31− options = {
33+ options.virtualisation.libvirtd = {
3234
33− virtualisation.libvirtd.enable = mkOption {
35+ enable = mkOption {
3436 type = types.bool;
3537 default = false;
3638 description = ''
@@ -41,7 +43,7 @@ in {
4143 '';
4244 };
4345
44− virtualisation.libvirtd.qemuPackage = mkOption {
46+ qemuPackage = mkOption {
4547 type = types.package;
4648 default = pkgs.qemu;
4749 description = ''
@@ -51,7 +53,7 @@ in {
5153 '';
5254 };
5355
54− virtualisation.libvirtd.extraConfig = mkOption {
56+ extraConfig = mkOption {
5557 type = types.lines;
5658 default = "";
5759 description = ''
@@ -60,7 +62,7 @@ in {
6062 '';
6163 };
6264
63− virtualisation.libvirtd.qemuRunAsRoot = mkOption {
65+ qemuRunAsRoot = mkOption {
6466 type = types.bool;
6567 default = true;
6668 description = ''
@@ -72,7 +74,7 @@ in {
7274 '';
7375 };
7476
75− virtualisation.libvirtd.qemuVerbatimConfig = mkOption {
77+ qemuVerbatimConfig = mkOption {
7678 type = types.lines;
7779 default = ''
7880 namespaces = []
@@ -84,7 +86,7 @@ in {
8486 '';
8587 };
8688
87− virtualisation.libvirtd.qemuOvmf = mkOption {
89+ qemuOvmf = mkOption {
8890 type = types.bool;
8991 default = true;
9092 description = ''
@@ -93,7 +95,7 @@ in {
9395 '';
9496 };
9597
96− virtualisation.libvirtd.extraOptions = mkOption {
98+ extraOptions = mkOption {
9799 type = types.listOf types.str;
98100 default = [ ];
99101 example = [ "--verbose" ];
@@ -102,7 +104,7 @@ in {
102104 '';
103105 };
104106
105− virtualisation.libvirtd.onShutdown = mkOption {
107+ onShutdown = mkOption {
106108 type = types.enum ["shutdown" "suspend" ];
107109 default = "suspend";
108110 description = ''
@@ -113,6 +115,14 @@ in {
113115 '';
114116 };
115117
118+ allowedBridges = mkOption {
119+ type = types.listOf types.str;
120+ default = [ "virbr0" ];
121+ description = ''
122+ List of bridge devices that can be used by qemu:///session
123+ '';
124+ };
125+
116126 };
117127
118128
@@ -120,7 +130,12 @@ in {
120130
121131 config = mkIf cfg.enable {
122132
123− environment.systemPackages = with pkgs; [ libvirt libressl.nc cfg.qemuPackage ];
133+ environment = {
134+ # this file is expected in /etc/qemu and not sysconfdir (/var/lib)
135+ etc."qemu/bridge.conf".text = lib.concatMapStringsSep "\n" (e:
136+ "allow ${e}") cfg.allowedBridges;
137+ systemPackages = with pkgs; [ libvirt libressl.nc cfg.qemuPackage ];
138+ };
124139
125140 boot.kernelModules = [ "tun" ];
126141
@@ -134,30 +149,15 @@ in {
134149 group = "qemu-libvirtd";
135150 };
136151
137− systemd.packages = [ pkgs.libvirt ];
138−
139− systemd.services.libvirtd = {
140− description = "Libvirt Virtual Machine Management Daemon";
141−
142− wantedBy = [ "multi-user.target" ];
143− after = [ "systemd-udev-settle.service" ]
144− ++ optional vswitch.enable "vswitchd.service";
145−
146− environment.LIBVIRTD_ARGS = ''--config "${configFile}" ${concatStringsSep " " cfg.extraOptions}'';
147−
148− path = [ cfg.qemuPackage ] # libvirtd requires qemu-img to manage disk images
149− ++ optional vswitch.enable vswitch.package;
150−
151− preStart = ''
152− mkdir -p /var/log/libvirt/qemu -m 755
153− rm -f /var/run/libvirtd.pid
154−
155− mkdir -p /var/lib/libvirt
156− mkdir -p /var/lib/libvirt/dnsmasq
152+ security.wrappers.qemu-bridge-helper = {
153+ source = "/run/${dirName}/nix-helpers/qemu-bridge-helper";
154+ };
157155
158− chmod 755 /var/lib/libvirt
159− chmod 755 /var/lib/libvirt/dnsmasq
156+ systemd.packages = [ pkgs.libvirt ];
160157
158+ systemd.services.libvirtd-config = {
159+ description = "Libvirt Virtual Machine Management Daemon - configuration";
160+ script = ''
161161 # Copy default libvirt network config .xml files to /var/lib
162162 # Files modified by the user will not be overwritten
163163 for i in $(cd ${pkgs.libvirt}/var/lib && echo \
@@ -169,21 +169,45 @@ in {
169169 done
170170
171171 # Copy generated qemu config to libvirt directory
172− cp -f ${qemuConfigFile} /var/lib/libvirt/qemu.conf
172+ cp -f ${qemuConfigFile} /var/lib/${dirName}/qemu.conf
173173
174174 # stable (not GC'able as in /nix/store) paths for using in <emulator> section of xml configs
175− mkdir -p /run/libvirt/nix-emulators
176175 for emulator in ${pkgs.libvirt}/libexec/libvirt_lxc ${cfg.qemuPackage}/bin/qemu-kvm ${cfg.qemuPackage}/bin/qemu-system-*; do
177− ln -s --force "$emulator" /run/libvirt/nix-emulators/
176+ ln -s --force "$emulator" /run/${dirName}/nix-emulators/
177+ done
178+
179+ for helper in libexec/qemu-bridge-helper bin/qemu-pr-helper; do
180+ ln -s --force ${cfg.qemuPackage}/$helper /run/${dirName}/nix-helpers/
178181 done
179182
180183 ${optionalString cfg.qemuOvmf ''
181− mkdir -p /run/libvirt/nix-ovmf
182− ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_CODE.fd /run/libvirt/nix-ovmf/
183− ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd /run/libvirt/nix-ovmf/
184+ ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_CODE.fd /run/${dirName}/nix-ovmf/
185+ ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd /run/${dirName}/nix-ovmf/
184186 ''}
185187 '';
186188
189+ serviceConfig = {
190+ Type = "oneshot";
191+ RuntimeDirectoryPreserve = "yes";
192+ LogsDirectory = subDirs [ "qemu" ];
193+ RuntimeDirectory = subDirs [ "nix-emulators" "nix-helpers" "nix-ovmf" ];
194+ StateDirectory = subDirs [ "dnsmasq" ];
195+ };
196+ };
197+
198+ systemd.services.libvirtd = {
199+ description = "Libvirt Virtual Machine Management Daemon";
200+
201+ wantedBy = [ "multi-user.target" ];
202+ requires = [ "libvirtd-config.service" ];
203+ after = [ "systemd-udev-settle.service" "libvirtd-config.service" ]
204+ ++ optional vswitch.enable "vswitchd.service";
205+
206+ environment.LIBVIRTD_ARGS = ''--config "${configFile}" ${concatStringsSep " " cfg.extraOptions}'';
207+
208+ path = [ cfg.qemuPackage ] # libvirtd requires qemu-img to manage disk images
209+ ++ optional vswitch.enable vswitch.package;
210+
187211 serviceConfig = {
188212 Type = "notify";
189213 KillMode = "process"; # when stopping, leave the VMs alone
@@ -203,7 +227,7 @@ in {
203227 systemd.sockets.virtlogd = {
204228 description = "Virtual machine log manager socket";
205229 wantedBy = [ "sockets.target" ];
206− listenStreams = [ "/run/libvirt/virtlogd-sock" ];
230+ listenStreams = [ "/run/${dirName}/virtlogd-sock" ];
207231 };
208232
209233 systemd.services.virtlogd = {
@@ -215,7 +239,7 @@ in {
215239 systemd.sockets.virtlockd = {
216240 description = "Virtual machine lock manager socket";
217241 wantedBy = [ "sockets.target" ];
218− listenStreams = [ "/run/libvirt/virtlockd-sock" ];
242+ listenStreams = [ "/run/${dirName}/virtlockd-sock" ];
219243 };
220244
221245 systemd.services.virtlockd = {