Libvirt: Difference between revisions
→Bridge networking: Add default gateway in guest machine example |
→Configuration: Add default networking section and pci passthrough section |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 41: | Line 41: | ||
</nowiki>}} | </nowiki>}} | ||
=== Bridge networking === | === 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. | |||
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. | |||
==== 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 | ||
Line 149: | Line 164: | ||
}; | }; | ||
</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 188: | Line 238: | ||
=== 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 274: | Line 324: | ||
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]] |