Vagrant can't use default network of libvirt. Vagrant + libvirt + Linux. no network with matching name 'default' (Libvirt::Error)

To avoid mess my host pc, I have created a f39 vm in Gnome Boxes, that will be my host.

➜  rocky9 pwd     
/home/chrisvm/projects/rocky9
# I was trying to create a custom network for regular user.
➜  rocky9 ll              
total 8.0K
-rw-r--r--. 1 chrisvm chrisvm  246 Mar  1 21:26 custom-network.xml
-rw-r--r--. 1 chrisvm chrisvm 3.0K Mar  1 18:16 Vagrantfile
➜  rocky9 

I have installed vagrant following this tutorial

I have installed virt-manager too.

With respect a installing plugins, there is two ways. By the way, what way do you recommend?

  • from the fedora repo
  • with the vagrant plugin install tool

The default vagrant-libvirt from fedora repo was installed automatically.

➜  rocky9 dnf list vagrant-libvirt
Last metadata expiration check: 4:12:49 ago on Fri 01 Mar 2024 06:03:18 PM -05.
Installed Packages
vagrant-libvirt.noarch                                                                 0.11.2-2.fc39                                                                 @fedora
➜  rocky9 
vagrant plugin list

vagrant-libvirt (0.12.2, global)

The network interfaces

➜  rocky9 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:47:44:9b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.16/24 brd 10.0.2.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 fec0::3f52:a148:3a05:2e87/64 scope site dynamic noprefixroute 
       valid_lft 86229sec preferred_lft 14229sec
    inet6 fe80::26f7:c015:a854:df49/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:a6:45:51 brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
       valid_lft forever preferred_lft forever
➜  rocky9 


Virtual network using virsh, supersuser & regular user

# regular user mode
➜  rocky9 virsh net-list --all                                                 
 Name   State   Autostart   Persistent
----------------------------------------

➜  rocky9 

# superuser mode
➜  rocky9 sudo !!
➜  rocky9 sudo virsh net-list --all
[sudo] password for chrisvm: 
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

➜  rocky9 

➜  rocky9 nmcli connection show -a
NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  10ee4d8d-ef14-3349-82cb-23cad0e71d74  ethernet  enp1s0 
lo                  d8344f06-de67-4403-b9c1-7ce93fdcb413  loopback  lo     
virbr0              edb8af9d-ea6b-4a4c-8c43-ff9aa8928f61  bridge    virbr0 
➜  rocky9 

About the problem or problems:

There is no problems creating rocky linux 9 vm with the default Vagrantfile. I means with the following commands.

# in the rocky9 folder
vagrant init
vagrant up
# login to the rocky server
vagrant ssh

The problems is that the default synced folder in the vm isn’t being created, There is no /vagrant folder in the vm.

The another problems is that can’t to setup a private (static) ip.

If edit the Vagrantfile like this.

Vagrant.configure("2") do |config|
  config.vm.box = "generic/rocky9"
  config.vm.network "private_network", ip: "192.168.124.10"
end

This is the error.

==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Removing domain...
==> default: Deleting the machine folder
/usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/requests/compute/vm_action.rb:7:in `create': Call to virDomainCreateWithFlags failed: Network not found: no network with matching name 'default' (Libvirt::Error)
        from /usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/requests/compute/vm_action.rb:7:in `vm_action'
        from /usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/models/compute/server.rb:76:in `start'
        from /usr/share/vagrant/gems/gems/vagrant-libvirt-0.11.2/lib/vagrant-libvirt/action/start_domain.rb:546:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.4/lib/vagrant/action/warden.rb:48:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-libvirt-0.11.2/lib/vagrant-libvirt/action/set_boot_order.rb:22:in `call'
   

Think that have to create a new network for the regular user mode. That’s why a have this file custom-network.xml.

➜  rocky9 ll
total 8.0K
-rw-r--r--. 1 chrisvm chrisvm 246 Mar  1 21:26 custom-network.xml
-rw-r--r--. 1 chrisvm chrisvm 134 Mar  1 21:57 Vagrantfile
➜  rocky9 

➜  rocky9 vim custom-network.xml

<network>
  <name>custom-net</name>
  <bridge name="custom-br"/>
  <forward mode="nat"/>
  <ip address="192.168.200.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.200.2" end="192.168.200.254"/>
    </dhcp>
  </ip>
</network>

With the help of chatGPT that’s have understood, but I am not sure. Can you help me how create the new network and how edit the Vagrantfile that it use the new network for regular user. Because vagrant is executed as regular uiser vagrant up.

It works for me like this:

sudo dnf install vagrant vagrant-libvirt
sudo usermod -a -G libvirt ${USER}
sudo systemctl --now enable virtnetworkd.service
sudo firewall-cmd --permanent --zone=libvirt --add-service=nfs
sudo firewall-cmd --reload
vagrant box add rockylinux/9 --provider=libvirt
tee Vagrantfile << EOF > /dev/null
Vagrant.configure("2") do |config|
  config.vm.box = "rockylinux/9"
  config.vm.synced_folder ".", "/vagrant", nfs_version: 4
  config.vm.network "private_network", ip: "192.168.121.10"
  config.vm.provider :libvirt do |libvirt|
    libvirt.qemu_use_session = false
  end
end
EOF
vagrant up
vagrant ssh

Resulting runtime firewall and virtual network configs on the host:

> sudo firewall-cmd --get-active-zones 
FedoraWorkstation (default)
  interfaces: enp6s0
libvirt
  interfaces: virbr0 virbr1

> sudo firewall-cmd --info-zone=libvirt
libvirt (active)
  target: ACCEPT
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces: virbr0 virbr1
  sources: 
  services: dhcp dhcpv6 dns nfs ssh tftp
  ports: 
  protocols: icmp ipv6-icmp
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule priority="32767" reject

> virsh -c qemu:///system net-list 
 Name              State    Autostart   Persistent
----------------------------------------------------
 default           active   yes         yes
 vagrant-libvirt   active   no          yes

> virsh -c qemu:///system net-dumpxml vagrant-libvirt
<network connections='2' ipv6='yes'>
  <name>vagrant-libvirt</name>
  <uuid>c25f5f74-06ec-4205-9371-50d31ae09619</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr1' stp='on' delay='0'/>
  <mac address='52:54:00:59:7a:08'/>
  <ip address='192.168.121.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.121.1' end='192.168.121.254'/>
    </dhcp>
  </ip>
</network>

The shared directory mounted on the guest:

> grep -e /vagrant /etc/mtab
192.168.121.1:/home/vgaetera /vagrant nfs4 rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.121.178,local_lock=none,addr=192.168.121.1 0 0

This is redundant when using Vagrant.

It works for me like that.

You need to use NFSv4 and configure the port opening as explained above.

Check my config as you are missing the part about session mode.

No need for that since Vagrant automatically creates the network.

I have followed your notes about firewall-cmd and NFS. By the way, it using generic/rocky9.

Think because of this line: libvirt.qemu_use_session = false in the Vagrantfile, the following Error is showed there after run vagrant up

Cannot access storage file '/root/.local/share/libvirt/images/rocky9_default.img' (as uid:107, gid:107): Permission denied (Libvirt::Error).

==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Removing domain...
==> default: Deleting the machine folder
/usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/requests/compute/vm_action.rb:7:in `create': Call to virDomainCreateWithFlags failed: Cannot access storage file '/root/.local/share/libvirt/images/rocky9_default.img' (as uid:107, gid:107): Permission denied (Libvirt::Error)
        from /usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/requests/compute/vm_action.rb:7:in `vm_action'
        from /usr/share/gems/gems/fog-libvirt-0.9.0/lib/fog/libvirt/models/compute/server.rb:76:in `start'
        from /usr/share/vagrant/gems/gems/vagrant-libvirt-0.11.2/lib/vagrant-libvirt/action/start_domain.rb:546:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.4/lib/vagrant/action/warden.rb:48:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-libvirt-0.11.2/lib/vagrant-libvirt/action/set_boot_order.rb:22:in `call'

More information

➜  rocky9 virsh uri                       
qemu:///session

➜  rocky9 sudo !!                                 
➜  rocky9 sudo virsh uri
[sudo] password for chrisvm: 
qemu:///system

➜  rocky9 groups                    
chrisvm wheel libvirt
➜  rocky9 
➜  rocky9 virsh -c qemu:///system net-list 
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

➜  rocky9 

If it is run with session true in the Vagrantfile. The following error appears.

==> default:  -- TPM Backend:       passthrough
==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Removing domain...
==> default: Deleting the machine folder
Network 192.168.121.10 is not available. Specify available network
name, or an ip address if you want to create a new network.
➜  rocky9 

image inside the /root/.local/share/libvirt/images folder with superuser mode.

[root@fedora]/home/chrisvm/projects/rocky9# ll /root/.local/share/libvirt/images 
total 1097596
-rwxr--r--. 1 root root 1123937280 Mar  1 17:27 generic-VAGRANTSLASH-rocky9_vagrant_box_image_4.3.12_box.img

I tested that as well and it also works for me.
However, I prefer using images from the official Rocky Linux account.

You cannot use arbitrary locations due to permission and SELinux restrictions.
Remove your storage pool and Vagrant should automatically create the proper one:

virsh -c qemu:///system pool-destroy default
virsh -c qemu:///system pool-undefine default

The resulting storage pool should look like this:

> virsh -c qemu:///system pool-dumpxml default
<pool type='dir'>
  <name>default</name>
  <uuid>0718efb8-97e6-4709-a970-f4d837b1c0b6</uuid>
  <capacity unit='bytes'>19769851904</capacity>
  <allocation unit='bytes'>14360838144</allocation>
  <available unit='bytes'>5409013760</available>
  <source>
  </source>
  <target>
    <path>/var/lib/libvirt/images</path>
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
      <label>system_u:object_r:virt_image_t:s0</label>
    </permissions>
  </target>
</pool>

The problems appears when you install vagrant and the virt-manager related tools, both at the same time, because both use libvirt.

In the tutorial of how to install virt-manager and libvirt and KVM/qemu in fedora you edit the libvirtd configuration file and enable libvirt group and rw permissions and add your regular user to the libvirt group. That installation allow virt-manager can run in supersuser mode means libvirt use the system mode to manage virtual machines. And the default storage /root/.local/share/libvirt/images to store hard disk of the virtual machines is created.

sudo vi /etc/libvirt/libvirtd.conf

unix_sock_group = "libvirt"

unix_sock_rw_perms = "0770"

sudo usermod -a -G libvirt $(whoami)

virt-manager

Then have a questions.

How install both tools virt-manager with all other packages, and vagrant with all other packages in the way that vagrant tool do not be blocked, because default Storage does is necessary when you use virt-manager which run in superuser mode.

When following documentation, especially outdated ones, you should not blindly trust it, as it may contain inaccurate or even detrimental instructions.
In this case, steps 2 and 3 seem no longer necessary.

Never run it as superuser, instead run as a normal user that is a member of the libvirt group and create a system mode connection:

virt-manager -c qemu:///system

That’s the cause of your problem, so recreate this storage pool using the following directory:

/var/lib/libvirt/images
1 Like

Well, ChatGPT gave me the info, whit that info wrote the above reply.

Prompt: tell me about this storage, /root/.local/share/libvirt/images, in the context of install virt-manager, libvirt, qemu/KVM

Response: In the context of installing and using virt-manager, libvirt, and QEMU/KVM, the directory /root/.local/share/libvirt/images typically serves as the default location where disk images for virtual machines are stored. Here’s what each component does:

  1. QEMU/KVM: QEMU is an open-source emulator and virtualizer that provides hardware virtualization capabilities. KVM (Kernel-based Virtual Machine) is a Linux kernel module that allows the Linux kernel to act as a hypervisor. Together, QEMU and KVM form the foundation for running virtual machines on Linux systems.
  2. Libvirt: Libvirt is a toolkit for managing virtualization platforms, including QEMU/KVM. It provides a stable and extensive API for interacting with various virtualization technologies, enabling the management of virtual machines, storage, networks, and more.
  3. virt-manager: Virt-manager is a desktop application that provides a graphical user interface (GUI) for managing virtual machines on Linux systems. It uses libvirt as its backend to interact with virtualization technologies such as QEMU/KVM.

When you create a new virtual machine using virt-manager or any other libvirt-based tool, a disk image file is typically created to serve as the virtual machine’s hard drive. By default, libvirt stores these disk image files in the /var/lib/libvirt/images directory. However, in some cases, particularly when running as the root user, libvirt may use a different directory for storing disk images.

The directory /root/.local/share/libvirt/images is one such alternative location. It’s within the home directory of the root user (/root), under the .local/share/libvirt/images subdirectory. This directory structure follows the convention of storing user-specific application data under the user’s home directory.

If you encounter issues related to permissions or access when working with virtual machines and disk images in libvirt, it’s essential to ensure that the appropriate user (typically the user running the libvirt service) has the necessary permissions to read from and write to the disk image files and their parent directories. Additionally, check the configuration settings of libvirt and virt-manager to ensure that they are correctly configured to use the desired storage locations for disk images.

Here’s a workaround if you insist on using the current path:

sudo setfacl -m u:qemu:r-X ~root/{,.local/{,share/{,libvirt/{,images}}}}