Libvirt: Difference between revisions

Pigs (talk | contribs)
Configuration: Add default networking section and pci passthrough section
Marie (talk | contribs)
dnsmasq is provided in the package by default and does not need installation.
 
(5 intermediate revisions by 3 users not shown)
Line 5: Line 5:
Enable libvirt daemon
Enable libvirt daemon


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|||<nowiki>
virtualisation.libvirtd.enable = true;
virtualisation.libvirtd.enable = true;


# Enable TPM emulation (optional)
# Enable TPM emulation (optional)
# install pkgs.swtpm system-wide for use in virt-manager (optional)
virtualisation.libvirtd.qemu = {
virtualisation.libvirtd.qemu = {
   swtpm.enable = true;
   swtpm.enable = true;
  ovmf.packages = [ pkgs.OVMFFull.fd ];
};
};


Line 17: Line 17:
virtualisation.spiceUSBRedirection.enable = true;
virtualisation.spiceUSBRedirection.enable = true;


</nowiki>}}
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}


To enable local user access to libvirt, for example by using <code>virt-manager</code> or <code>gnome-boxes</code>, add yourself to the <code>libvirtd</code> group
To enable local user access to libvirt, for example by using <code>virt-manager</code> or <code>gnome-boxes</code>, add yourself to the <code>libvirtd</code> group
Line 37: Line 37:
If you would like to enable nested virtualization for your guests to run KVM hypervisors inside them, you should enable it as follows:  {{nixos:option|boot.extraModprobeConfig}}, for example:
If you would like to enable nested virtualization for your guests to run KVM hypervisors inside them, you should enable it as follows:  {{nixos:option|boot.extraModprobeConfig}}, for example:


{{file|/etc/nixos/configuration.nix|xml|<nowiki>
{{file|||<nowiki>
boot.extraModprobeConfig = "options kvm_intel nested=1";
boot.extraModprobeConfig = ''
</nowiki>}}
  options kvm_intel nested=1
'';
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}


=== Networking ===
=== Networking ===
Line 45: Line 47:
==== Default networking ====
==== Default networking ====


To utilize the default libvirt network, you will need to install the {{nixos:package|dnsmasq}} package. This is required for DNS and DCHP functionality within the network.
Enable and start the default network using the following commands:
 
Once the package is installed, enable and start the default network using the following commands:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 54: Line 54:
</syntaxhighlight>
</syntaxhighlight>


This will configure the default network to start automatically on boot and immediately activate it.
This will configure the default network to start automatically on boot and immediately activate it. You may need to whitelist the interface for the firewall like so:
 
{{File|3=networking.firewall.trustedInterfaces = [ "virbr0" ];|name=/etc/nixos/configuration.nix|lang=nix}}


==== Bridge networking ====
==== Bridge networking ====


Create a XML file called <code>virbr0.xml</code> with the definition of the bridge interface
Create a XML file called <code>virbr0.xml</code> with the definition of the bridge interface.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 68: Line 70:
</syntaxhighlight>
</syntaxhighlight>


Add and enable bridge interface
Add and enable bridge interface.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 78: Line 80:
</syntaxhighlight>
</syntaxhighlight>


Edit the libvirt guest <code>my_guest</code> XML file and add the bridge interface to it
Edit the libvirt guest <code>my_guest</code> XML file and add the bridge interface to it.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 84: Line 86:
</syntaxhighlight>
</syntaxhighlight>


Add
Add:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 99: Line 101:
</syntaxhighlight>
</syntaxhighlight>


Inside the guest configure networking for the interface <code>enp1s0</code> (name might differ)
Inside the guest configure networking for the interface <code>enp1s0</code> (name may differ).


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 118: Line 120:
=== File sharing ===
=== File sharing ===


In order to share files between host and guest, one recommended way of doing this is to use <code>spice-webdavd</code>.
In order to share files between host and guest, one recommended way is to use <code>spice-webdavd</code>.


Shutdown the client, in this example named <code>my_guest</code>, and edit the libvirt XML file.
Shutdown the client, in this example named <code>my_guest</code>, and edit the libvirt XML file.
Line 136: Line 138:
</syntaxhighlight>
</syntaxhighlight>


Start the guest machine. Inside the guest, add following part to your system configuration and apply it
Start the guest machine. Inside the guest, add following part to your system configuration and apply it.


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 142: Line 144:
</nowiki>}}
</nowiki>}}


List available shares for the guest
List available shares for the guest.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 148: Line 150:
</syntaxhighlight>
</syntaxhighlight>


Mount an example share called <code>myshare</code> to the mountpoint <code>myshare</code>
Mount an example share called <code>myshare</code> to the mountpoint <code>myshare.</code>


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>