I have a need to setup a virtual network on a VM-host server. Usually, I just use the virt-manager gui application to create this task. However, I’d like to do this via command line this time. Why? Well, why not? It’s always fun to learn new thing. :)
libvirt keeps network configuration in XML files under /etc/libvirt/qemu/networks/
. Here is what I have on one of my machines:
# ls -1 /etc/libvirt/qemu/networks/*xml
/etc/libvirt/qemu/networks/classroom.xml
/etc/libvirt/qemu/networks/default.xml
/etc/libvirt/qemu/networks/kubernetes.xml
/etc/libvirt/qemu/networks/minikube-net.xml
/etc/libvirt/qemu/networks/other.xml
/etc/libvirt/qemu/networks/storage.xml
/etc/libvirt/qemu/networks/student.xml
We can use also list all the available libvirt-related virtual networks using virsh
command:
# virsh net-list --all
Name State Autostart Persistent
---------------------------------------------------
classroom active yes yes
default active yes yes
kubernetes inactive no yes
minikube-net inactive no yes
other inactive no yes
storage active yes yes
student active yes yes
Okay, let’s get into creating a new virtual network called netpriv
. I have this XML file with the following content.
<network>
<name>netpriv</name>
<forward mode='nat'/>
<bridge name='virbr1' stp='on' delay='0' />
<ip address='192.168.2.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.2.201' end='192.168.2.254' />
</dhcp>
</ip>
</network>
To create a new virtual network, we’ll use virsh net-create --file FILENAME.xml
. (Note the name of FILENAME.xml doesn’t have to match with the name of the virtual network you want to create.)
# virsh net-create --file ./netpriv.xml
Network netpriv created from ./netpriv.xml
# virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes
netpriv active no no
We can verify our newly created network.
# virsh net-dumpxml netpriv
<network>
<name>netpriv</name>
<uuid>5cc26ad4-8af5-4538-b29f-c05975bdb3b2</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:c7:ee:34'/>
<ip address='192.168.2.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.2.201' end='192.168.2.254'/>
</dhcp>
</ip>
</network>
And one time, verify that a new virtual network interface has been created.
# ip a show virbr1
11: virbr1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c7:ee:34 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.1/24 brd 192.168.8.255 scope global virbr1
valid_lft forever preferred_lft forever
Update (2021-03-23):
I notice that the new virtual network is not started by default. Here’s how to make it autostart on the server’s reboot.
# virsh net-autostart netpriv
error: failed to mark network netpriv as autostarted
error: Requested operation is not valid: cannot set autostart for transient network
Great. What now?
According to this question on StackExchange, we can trick libvirt to convert the network configuration from transient to persistent with the following steps:
- Edit the network name
- Stop the network
- Start the network
- Finally, set the network to auto start
Let’s try it out. Remember the name of my virtual network is netpriv
. Run virsh net-edit network_name
. Add an empty line so that libvirt thinks there was a change in the file and will restart the network.
# virsh net-edit netpriv
Network netpriv XML configuration edited.
Next, stop the network by running virsh net-destroy network_name
. (Now, don’t let the name fool you. net-destroy
does not destroy your network. It only stops the network.)
# virsh net-destroy netpriv
Network netpriv destroyed
Then I start the network again.
# virsh net-start netpriv
Network netpriv started
Let’s try to enable autostart.
# virsh net-autostart netpriv
Network netpriv marked as autostarted
Finally we can verify the result.
# virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes
netpriv active yes yes
Reference: