The Best VIRSH guide to manage KVM VMs

This post contain everything you need to know on virsh commands for VM management. Virsh uses libvrt open source API, and can be used to manage KVM, XEN, VMWare ESXI, QEMU and other virtualization technologies.

Node Information

Check node info like CPU , memory etc.

Create VM

Create a new VM using the command as below

$ sudo virt-install --name tux-linux --description "Tux NW VM" --ram=4096 --vcpus=2 --os-type=Linux --os-variant=debian --disk path=/var/lib/libvirt/images/tux-linux.qcow2,bus=virtio,size=20 --graphics none --location /share/DATA/iso/debian.iso --network bridge:virbr0 --console pty,target_type=serial -x 'console=ttyS0,115200n8 serial'

Delete VM

Delete a VM and clean removal of all other objects

$ sudo virsh destroy tux-linux
$ sudo virsh undefine tux-linux 
$ sudo virsh pool-refresh default 
$ sudo virsh vol-delete --pool default tux-linux.qcow2

Start VM

$ sudo virsh start tux-linux

Autostart VM

Use the following command to autostart VMs when server is started ( e.g. after reboot).

$ sudo virsh autostart tux-linux

To disable the same execute below

$ sudo virsh autostart --disable tux-linux

Reboot VM

$ sudo virsh reboot tux-linux

Stop VM (graceful)

$ sudo virsh shutdown tux-linux

Stop VM (Force Stop)

$ sudo virsh destroy tux-linux

Suspend / Resume VM

Use the following command to suspend a VM

$ sudo virsh suspend tux-linux

Use the following command to resume a VM

$ sudo virsh resume tux-linux

Virtual Machine Backup

Save VM

Use the following command to save current status of a VM to a file

$ sudo virsh save tux-linux tux-linux.<YYYYMMMDDHHMMSS>

Restore from a Saved VM File

Use the following command to restore a VM from a saved VM File

$ sudo virsh restore tux-linux.<YYYYMMDDHHMMSS>

Create a VM Snapshot

Use the following command to create a snapshot of a VM

$ sudo virsh snapshot-create-as --domain tux-linux --name "TuxLinux-Snap1" --description "Tux Linux - Golden Copy"
  Domain snapshot TuxLinux-Snap1 created

Restore a VM from a Snapshot

Use the following command to restore a VM from a snapshot

$ sudo virsh snapshot-revert --domain tux-linux --snapshotname TuxLinux-Snap1 --running

List All Snapshots

Use the following command to check all snapshots available for a VM

$ sudo virsh snapshot-list tux-linux
 Name             Creation Time             State  
---------------------------------------------------   TuxLinux-Snap1    2021-06-06 08:10:12 +0300 shutoff   

Delete a Snapshot

Use the following command to delete a snapshot

$ sudo virsh snapshot-delete --domain tux-linux --snapshotname TuxLinux-Snap1

Clone a VM

Use the following command to clone a VM ( before cloning a VM, the vm must be shutoff or in pause state)

$ sudo virsh clone --connect qemu:///system --original tux-linux --name tux-mailer --file /var/lib/libvirt/images/tux-mailer.qcow2

Allocating 'tux-mailer.qcow2'            | 20 GB  00:00:15
Clone 'tux-mailer' created successfully

Console Access

Use the following command to connect onto a guest console

$ sudo virsh autostart tux-linux

Virtual Machine Management

Manage VM memory

To modify the memory allocated to VMs, use following commands

$ sudo virsh setmaxmem tux-linux 4096 --config
$ sudo virsh setmem tux-linux 4096 --config
$ sudo virsh reboot tux-linux 

Check the memory using command

$ sudo virsh dominfo tux-linux | grep memory
Max memory:       4096 KiB
Used memory:      4096 KiB

Manage VM CPUs

To modify the CPUs allocated to VMs, use following commands

$ sudo virsh setvcpus --domain tux-linux --maximum 12 --config
$ sudo virsh setvcpus --domain tux-linux --count 12 --config
$ sudo virsh reboot tux-linux 

Check the memory using command

$ sudo virsh dominfo tux-linux | grep CPU
CPU(s):           12

Volume Management

Create Volume

Create a new 5GB volume “tux-linux-vol02” on the default storage spool, as

$ sudo virsh vol-create-as default tux-linux-vol02.qcow2 5G
  Vol tux-linux-vol02.qcow2 created
$ sudo du -sh /var/lib/libvirt/images/tux-linux-vol02.qcow2
 5.0G /var/lib/libvirt/images/tux-linux-vol02.qcow2

Attach a new Volume to VM

Use the following command to attach the new volume created on an existing VM

$ sudo virsh attach-disk --domain tux-linux --source /var/lib/libvirt/images/tux-linux-vol02.qcow2 --persistent --target vdb

Resize Volume

Use following command to resize the disk image

$ sudo qemu-img resize /var/lib/libvirt/images/tux-linux-vol02.qcow2 +5G

Detach Volume on VM

Use the following command to detach the volume

$ sudo virsh detach-disk --domain tux-linux --persistent --live --target vdb

Delete a Volume

Use the following command to delete a volume

$ sudo virsh vol-delete tux-linux-vol02.qcow2 --pool default
$ sudo virsh pool-refresh default
$ sudo virsh vol-list default

Conclusion

Hope this guide will be a great reference guide on virsh commands. Please share and comment if you see any issue.

0 Comments on The Best VIRSH guide to manage KVM VMs

Leave a Comment

Your email address will not be published. Required fields are marked *

You Might Be Interested In