Modifying VM configuration with libvirtd

virt

The other day, I had to update a VM configuration managed via libvirt from the command line. There are different ways to do this. The easiest probably is to use the virt-manager application and use the GUI to modify things.

virt-manager details

... virt-manager screenshot ...

Alternatively you can use virsh with dumpxml+ undefine + define or edit to modify the XML definition directly. While straight forward this is more diffiecult.

A good in-between is to use the virt-xml command, which lets you add/remove devices directly. See man page.

Examples

Adding a network interface:

virt-xml vmname --add-device --network bridge=br-ens13f3,model=virtio

Removing sda definition:

virt-xml vmname --remove-device --disk target=sda

Adding sda definition as an iscsi:

virt-xml vmname --add-device --disk target=sda,driver.type=raw,source.protocol=iscsi,source.name=iqn.2023-10.world.srv:dlp.target01/1,source.host.name=192.168.39.184,source.host.port=3260,target.bus=sata

Note that this can not specify iSCSI initiator IQN

Adding a serial console:

virt-xml vmname --add-device --serial pty,target.port=0

This functionality can be access programmatically from python directly (not via a command) using:

import os
import sys
sys.path.insert(0, '/usr/share/virt-manager')
from virtinst import virtxml

Unfortunately there is no API documentation for this so this needs to be read from the source.