I am trying to automate the process of creating a Guest VM. I am using uvt-kvm to create the VM. I am also taking a network type as a parameter. This can be either nat
or macvtap
.
Now in the script I create the macvtap interface using these steps:
MACVTAP_INTERFACE="macvtap$VM_NAME"
ip link add link eno1 name $MACVTAP_INTERFACE type macvtap mode bridge
ip link set $MACVTAP_INTERFACE up
ip addr add 10.0.0.1/24 dev $MACVTAP_INTERFACE
The MACVTAP interface with the same name is created without issues. Then I try to attach it to the guest VM which is already created and shutdown in the script before this step. I tried two ways of attaching it:
Way 1:
virsh attach-interface $VM_NAME --type direct --source eno1 --mode bridge --model virtio --target $MACVTAP_INTERFACE --config
This does not actually attach the interface I created before but rather creates a new interface like macvtap11
and attaches it to the VM. Although source, mode and model are set, the target is still not set.
Way 2:
cat > interface-{$VM_NAME}.xml << EOF
<interface type='direct'>
<source dev="eno1" mode="bridge"/>
<model type='virtio'/>
<target dev="$MACVTAP_INTERFACE"/>
</interface>
EOF
virsh attach-device $VM_NAME interface-{$VM_NAME}.xml --persistent --config
rm interface-{$VM_NAME}.xml
This also results in the same thing. Creates a new interface and attaches it to the VM and doesn't take <target dev="$MACVTAP_INTERFACE"/>
into consideration.
This is the XML from a VM which I created using virt-manager UI:
<interface type="direct">
<mac address="52:54:00:58:03:46"/>
<source dev="eno1" mode="bridge"/>
<target dev="macvtap9"/>
<model type="virtio"/>
<alias name="net0"/>
<address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>
I made sure all parameters remain same when I attach my interface to the new VM.
Please help me out with how I can define a custom interface and attach it to the VM. The same solution should also work for NAT as well.
Also I am assigning a public IP address subnet while creating the MACVTAP interface. However, my guest VM is not getting a public IP when I use ip addr show
. When I create the VM with a MACVTAP interface in virt-manager
, it does assign the public IP. I think it is not a DHCP issue or else virt-manager should not have been able to assign the IP either.