QEMU/KVM Virtual machine Management

From WikiMLT

Virsh: Com­mand-line Man­age­ment User In­ter­face

The virsh pro­gram is the main in­ter­face for man­ag­ing virsh guest do­mains. The pro­gram can be used to cre­ate, pause, and shut­down do­mains… Lib­virt (which pro­vides virsh pro­gram) is a C toolk­it to in­ter­act with the vir­tu­al­iza­tion ca­pa­bil­i­ties of re­cent ver­sions of Lin­ux (and oth­er OS­es)… It cur­rent­ly sup­ports Xen, QE­MU, KVM, LXC, Open­VZ, Vir­tu­al­Box and VMware ESX.

Ba­sic op­er­a­tions

View the list of all avail­able vir­tu­al ma­chines:

virsh list --all

Start, re­boot, sus­pend, re­name, shut­down a VM:

virsh (start|reboot|suspend|rename|shutdown) {vm-name|vm-Id}

Dump a VM XML con­fig­u­ra­tion file:

virsh dumpxml {vm-name} > {vm-name}.xml

Cre­at­ing or defin­ing a VM based on XML dump con­fig­u­ra­tion file. Be­fore this step mod­i­fi {vm-name}.xml and up­date the val­ues of <name>, <uuid>, <ti­tle>, <de­scrip­tion>:

virsh create {vm-name}.xml
virsh define {vm-name}.xml
virsh start {vm-name}

Com­plete­ly re­move a VM:

sudo virsh undefine {vm-name|vm-Id}
sudo virsh destroy {vm-name|vm-Id}

View the list of the as­so­ci­at­ed vir­tu­al net­works:

virsh net-list --all

Dis­able an net­work and dis­able au­tostart:

virsh net-destroy {net-name}
virsh net-autostart --network {net-name} --disable

En­able an net­work and dis­able au­tostart:

virsh net-start {net-name}
virsh net-autostart --network {net-name} --enable

Con­fig­ur­ing Vir­tu­al ma­chine au­to­mat­ic start

1. First get in­fo about the tar­get in­stance.

virsh list --all                # list all available Virtual Machines
virsh dominfo {vm.name.org}     # check whether given virtual machine is configured to autostart
ls /etc/libvirt/qemu/autostart/ # list all virtual machines already configured to autostart

2. En­able Vir­tu­al Ma­chine au­to­mat­ic start.

virsh autostart {vm.name.org}
  • If virsh com­mand is not available/​​​installed, to con­fig­ure aus­tostart sim­ply cre­ate a new sym­bol­ic link with­in /​​​etc/​​​libvirt/​​​qemu/​​​autostart/​​​ di­rec­to­ry us­ing ln com­mand.
  • Re­load hy­per-vi­sor if nec­es­sary: sys­tem­ctl re­load lib­virtd

3. Dis­able Vir­tu­al Ma­chine au­tostart.

virsh autostart --disable {vm.name.org}
  • Or sim­ply use un­link com­mand to re­move vir­tu­al machine’s sym­bol­ic link from /​​​etc/​​​libvirt/​​​qemu/​​​autostart/​​​ di­rec­to­ry: un­link /etc­/­lib­virt­/­qemu­/­auto­start­/­{vm.­name.­org}.­xml

Ref­er­ences

Virt-man­ag­er: GUI Man­age­ment (lo­cal and via ssh)

For re­mote GUI man­age­ment you can use the tool virt-man­ag­er on the re­mote ma­chine. If there is a set SSH con­nec­tion (via ~/.ssh/config) you can use this op­tion where you won't have to wor­ry about ex­tra open ports in your fire­wall. If you are a Win­dows' user and WSL2 is en­abled, you can use virt-man­ag­er at least three ways: