Libvirt: Difference between revisions
m Move hooks section to config |
m Updated Formating |
||
| (3 intermediate revisions by 2 users not shown) | |||
| 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 = "options kvm_intel nested=1"; | boot.extraModprobeConfig = "options kvm_intel nested=1"; | ||
</nowiki>}} | </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. | |||
=== 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 72: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Add and enable bridge interface | Add and enable bridge interface. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 63: | Line 82: | ||
</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 88: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Add | Add: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 84: | Line 103: | ||
</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> | ||
| Line 103: | Line 122: | ||
=== 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 121: | Line 140: | ||
</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 127: | Line 146: | ||
</nowiki>}} | </nowiki>}} | ||
List available shares for the guest | List available shares for the guest. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 133: | Line 152: | ||
</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 150: | Line 169: | ||
</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. | 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. | ||
| Line 177: | Line 196: | ||
}; | }; | ||
} | } | ||
</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> | </syntaxhighlight> | ||
=== PCI Passthrough === | |||
For detailed instructions on configuring PCI passthrough with libvirt, refer to the [[PCI passthrough]] page. | |||
== Clients == | == Clients == | ||