Libvirt: Difference between revisions
OVMF submodule has been removed |
Add section "File sharing via virtiofs mount" |
||
| (4 intermediate revisions by 4 users not shown) | |||
| Line 9: | Line 9: | ||
# 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 37: | Line 38: | ||
{{file|||<nowiki> | {{file|||<nowiki> | ||
boot.extraModprobeConfig = | boot.extraModprobeConfig = '' | ||
options kvm_intel nested=1 | |||
''; | |||
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}} | </nowiki>|name=/etc/nixos/configuration.nix|lang=nix}} | ||
| Line 44: | Line 47: | ||
==== Default networking ==== | ==== Default networking ==== | ||
Enable and start the default network using the following commands: | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 57: | 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 ==== | ||
| Line 119: | Line 118: | ||
The host should now be able to reach the guest via the bridge interface and vice versa. | The host should now be able to reach the guest via the bridge interface and vice versa. | ||
=== File sharing === | === File sharing via virtiofs mount === | ||
One of the best ways to share a host directory with the guest OS is with [https://virtio-fs.gitlab.io/ virtiofs]. On the host system, install the <code>virtiofsd</code> package:<syntaxhighlight lang="nix"> | |||
environment.systemPackages = with pkgs; [ | |||
guestfs-tools | |||
virtiofsd | |||
]; | |||
</syntaxhighlight>Next, a few sections of the XML must be edited, which can be done manually or via virt-manager in the guest configuration GUI. If using virt-manager, first navigate on the toolbar to Edit > Preferences > General, and click "Enable XML Editing". Next, open the virtual machine and under the hardware configuration, navigate to Memory and check the box "Enable shared memory". This will add an "access" block to the XML for you, similar to this:<syntaxhighlight lang="xml"> | |||
<memory unit="KiB">1638400</memory> | |||
<currentMemory unit="KiB">1638400</currentMemory> | |||
<memoryBacking> | |||
<source type="memfd"/> | |||
<access mode="shared"/> | |||
</memoryBacking> | |||
</syntaxhighlight>While still in the hardware configuration, click "Add Hardware" and select "Filesystem". For driver, select "virtiofs". For source path, input the folder on the host machine you wish to share, no trailing slash. For target path, don't put a path but instead a tag/label that is easily identifiable. It will be used in the mount options in the guest OS setup shortly. Once done, you should have a new Filesystem device configuration similar to this:<syntaxhighlight lang="xml"> | |||
<filesystem type="mount" accessmode="passthrough"> | |||
<driver type="virtiofs"/> | |||
<binary path="/run/current-system/sw/bin/virtiofsd"/> | |||
<source dir="/media"/> | |||
<target dir="my_host_media_share"/> | |||
<alias name="fs0"/> | |||
<address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/> | |||
</filesystem> | |||
</syntaxhighlight>If your guest system is using NixOS, you can boot the system and add the new filesystem entry to auto-mount on boot and you're done:<syntaxhighlight lang="nix"> | |||
fileSystems."/media" = { | |||
device = "my_host_media_share"; | |||
fsType = "virtiofs"; | |||
}; | |||
</syntaxhighlight>If the system fails to fully reboot after applying the changes, ensure the filesystem device matches the "Target path" in your XML exactly. | |||
==== Error starting domain: internal error: Child process (/run/current-system/sw/bin/virtiofsd --print-capabilities) unexpected exit status 127: libvirt: error : cannot execute binary /run/current-system/sw/bin/virtiofsd: No such file or directory ==== | |||
This error means virtiofsd was not installed on the host system. Ensure the system package was installed before making changes in virt-manager. | |||
==== Error starting domain: operation failed: Unable to find a satisfying virtiofsd ==== | |||
The virtiofsd binary path needs to be specified in the filesystem configuration. virt-manager doesn't add this by default and instead assumes a default path that doesn't exist under NixOS. Open the guest machine's hardware details page, click on the passthrough filesystem created earlier, open the XML tab and inside the `<filesystem>...</filesystem>` add the following element to tell virtio where to find the virtiofsd binary:<syntaxhighlight lang="xml"> | |||
<binary path="/run/current-system/sw/bin/virtiofsd"/> | |||
</syntaxhighlight> | |||
=== File sharing via WebDAV === | |||
Another recommended way to share files between host and guest 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. | ||