I would like to create a network with both persistent
and autostart
flags set using libvirt-java
:
Connect connect = new Connect("qemu:///system", false);
Network network = connect.networkCreateXML("""
<network>
<name>foo</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virfoo' stp='on' delay='0'/>
<ip address='192.168.99.1' netmask='255.255.255.0'/>
</network>
""");
network.setAutostart(true);
When executing network.setAutostart(true)
the following error is thrown:
libvirt: Network Driver error : Requested operation is not valid: cannot set autostart for transient network
virsh net-list --all
on command line shows:
Name State Autostart Persistent
--------------------------------------------
default active yes yes
foo active no no
How can I create or update the network as persistent using libvirt-java
?
I can see that there is a method network.isPersistent()
. But where can I set that flag?
The API follows the same conventions as the command line: create methods (like virsh net-create
) create transient resources; to create persistent resources, you need to use the define alternative (like virsh net-define
):
Network network = connect.networkDefineXML("""
<network>
<name>foo</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virfoo' stp='on' delay='0'/>
<ip address='192.168.99.1' netmask='255.255.255.0'/>
</network>
""");
network.create();
network.setAutostart(true);
Note that when you define
something, it will create the persistent resource but will not start it; that's why we need that extra call to network.create()
.
The result of running the above code is:
$ virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes
foo active yes yes