Libvirt: Difference between revisions
→UEFI with OVMF: This is not necessary anymore. See https://github.com/NixOS/nixpkgs/blob/98ce61be576896913274454adf9506d15d1e63f7/nixos/modules/virtualisation/libvirtd.nix#L415 |
|||
| (12 intermediate revisions by 7 users not shown) | |||
| Line 5: | Line 5: | ||
Enable libvirt daemon | Enable libvirt daemon | ||
{{file| | {{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; | ||
}; | }; | ||
| 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| | {{file|||<nowiki> | ||
boot.extraModprobeConfig = | boot.extraModprobeConfig = '' | ||
</nowiki>}} | options kvm_intel nested=1 | ||
''; | |||
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}} | |||
=== 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: | |||
{{File|3=environment.systemPackages = with pkgs; [ | |||
dnsmasq | |||
];|name=/etc/nixos/configuration.nix|lang=nix}} | |||
Once the package is installed, enable and start the default network using the following commands: | |||
<syntaxhighlight lang="console"> | |||
# virsh net-autostart default | |||
# virsh net-start default | |||
</syntaxhighlight> | |||
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 53: | Line 76: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Add and enable bridge interface | Add and enable bridge interface. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 63: | Line 86: | ||
</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 69: | Line 92: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Add | Add: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 84: | Line 107: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Inside the guest configure networking for the interface <code>enp1s0</code> (name | 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> | ||
networking.interfaces.enp1s0 | networking.interfaces.enp1s0 = { | ||
ipv4.addresses = [{ | |||
address = "10.25.0.2"; | |||
}]; | prefixLength = 24; | ||
}]; | |||
defaultGateway = { | |||
address = "10.25.0.1"; | |||
interface = "ens1s0"; | |||
}; | |||
}; | |||
</nowiki>}} | </nowiki>}} | ||
| Line 97: | Line 126: | ||
=== File sharing === | === File sharing === | ||
In order to share files between host and guest, one recommended way | 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 115: | Line 144: | ||
</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 121: | Line 150: | ||
</nowiki>}} | </nowiki>}} | ||
List available shares for the guest | List available shares for the guest. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 127: | Line 156: | ||
</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> | ||
| Line 143: | Line 172: | ||
}; | }; | ||
</nowiki>}} | </nowiki>}} | ||
=== Hooks === | |||
Libvirt allows the use of hooks to run custom scripts during specific events, such as daemon lifecycle events, domain lifecycle events, and network events. On NixOS, you can configure hooks via the NixOS module to automate the placement of hook scripts in the appropriate directories. | |||
The following directories are used for placing hook scripts: | |||
* '''<code>/var/lib/libvirt/hooks/daemon.d/</code>''' Scripts here are triggered by daemon events like start, shutdown, and SIGHUP. | |||
* '''<code>/var/lib/libvirt/hooks/qemu.d/</code>''' Scripts for handling QEMU domain events such as begin, end, and migration. | |||
* '''<code>/var/lib/libvirt/hooks/lxc.d/</code>''' Scripts for LXC container events like begin and end. | |||
* '''<code>/var/lib/libvirt/hooks/libxl.d/</code>''' Scripts for Xen domains managed by <code>libxl</code> (begin/end events). | |||
* '''<code>/var/lib/libvirt/hooks/network.d/</code>''' Scripts triggered by network events such as begin and end. | |||
See the [https://libvirt.org/hooks.html libvirt documentation] for more information. | |||
An example config would be:<syntaxhighlight lang="nix"> | |||
{ | |||
virtualisation.libvirtd.hooks = { | |||
daemon = { | |||
"example" = ./scripts/daemon-example.sh; | |||
}; | |||
qemu = { | |||
"example" = ./scripts/qemu-example.sh; | |||
}; | |||
network = { | |||
"example" = ./scripts/network-example.sh; | |||
}; | |||
}; | |||
} | |||
</syntaxhighlight>Note that after you added the configuration and switch, you'll have the following command to setup the hooks.<syntaxhighlight lang="bash"> | |||
systemctl start libvirtd-config.service | |||
</syntaxhighlight> | |||
=== PCI Passthrough === | |||
For detailed instructions on configuring PCI passthrough with libvirt, refer to the [[PCI passthrough]] page. | |||
== Clients == | == Clients == | ||
| Line 182: | Line 246: | ||
=== NixVirt === | === NixVirt === | ||
[https:// | [https://github.com/AshleyYakeley/NixVirt NixVirt] is a flake that provides NixOS and Home Manager modules for setting up libvirt domains, networks and pools declaratively. | ||
=== Accessing QEMU VMs through Webbrowser === | === Accessing QEMU VMs through Webbrowser === | ||
| Line 225: | Line 289: | ||
==== Get EyeOS Spice Web Client ==== | ==== Get EyeOS Spice Web Client ==== | ||
As said, the experience with the EyeOS Spice Web Client has been the best so far. Another client would be the [https:// | As said, the experience with the EyeOS Spice Web Client has been the best so far. Another client would be the [https://gitlab.freedesktop.org/spice/spice-html5/ spice-html5] from freedesktop.org. | ||
1. Download the [https://github.com/eyeos/spice-web-client/ EyeOS Spice Web Client] and unpack it (if necessary) or , as example, just <code>git clone https://github.com/eyeos/spice-web-client/ /var/www/spice</code> | 1. Download the [https://github.com/eyeos/spice-web-client/ EyeOS Spice Web Client] and unpack it (if necessary) or , as example, just <code>git clone https://github.com/eyeos/spice-web-client/ /var/www/spice</code> | ||
| Line 268: | Line 332: | ||
And finally you can access the VMs GUI through <code>https://mydomain.tld:4500/spice/index.html?host=mydomain.tld&port=5959</code> | And finally you can access the VMs GUI through <code>https://mydomain.tld:4500/spice/index.html?host=mydomain.tld&port=5959</code> | ||
[[Category:Virtualization]] | [[Category:Virtualization]] | ||
[[Category:Applications]] | [[Category:Applications]] | ||