Manage KVM guest virtual machines via virsh commands








Are you trying to manage KVM guest virtual machines using virsh commands?

This guide is for you.


virsh is a command line interface tool for managing guests and the hypervisor. The virsh tool is built on the libvirt management API and operates as an alternative to the xm command and the graphical guest Manager ( virt-manager ).

Virsh interacts with Libvirt which is a library aimed at providing a long-term stable API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox, and VMware ESX.

It can be used to create, pause, restart, and shutdown domains. In addition, it can be used to list current domains available in our Virtualization hypervisor platform.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to manage KVM guest virtual machines by means of virsh commands.

In this context, we shall look into how to use Virsh commands.


Different commands to Manage KVM guest virtual machines via Virsh 

The basic structure of most virsh usage is given below:

virsh [OPTION]... <command> <domain> [ARG]...

Display node information

To display the host node information and the machines that support the virtualization process, we can use the following command:

$ sudo virsh nodeinfo


List all domains

To list both inactive and active domains, we can use the following command:

$ sudo virsh list --all


List only active domains

For listing only active domains we can use the following command:

$ sudo virsh list


To start a VM

For starting a VM we can use the following command by replacing 'domain_name' with the required domain name:

$ sudo virsh start domain_name
$ sudo virsh list


Autostart VM

To set a VM to start automatically on system startup, we can use the following command:

$ sudo virsh autostart domain_name
$ sudo virsh dominfo domain_name


Autostart disable

To disable autostart feature for a VM, we can use the following command:

$ virsh autostart --disable domain_name
$ virsh dominfo domain_name


Stop/Shutdown VM

To shut down a running VM gracefully, we can use the following command:

$ sudo virsh shutdown domain_name
$ sudo virsh list


Force shutdown VM

To perform a forceful shutdown of the active domain we can use the following command:

$ sudo virsh destroy domain_name


Stop all running VMs

To shut down all running domains, we can use the following command:

$ for i in `sudo virsh list | grep running | awk '{print $2}'` do
sudo virsh shutdown $i
done


Reboot VM

To restart a VM we can use the following command:

$ sudo virsh reboot domain_name


Remove VM

To cleanly remove a VM including its storage columns, we can use the following commands:

$ sudo virsh destroy domain_name 2> /dev/null
$ sudo virsh undefine domain_name
$ sudo virsh pool-refresh default
$ sudo virsh vol-delete --pool default doamin_name.qcow2


Create a VM

To create a new virtual machine with virsh, we can use the following command:

$ sudo virt-install


Connect to VM console

To connect to the guest console, use the command:

$ sudo virsh console domain_name


Edit VM XML file

To edit a VM XML file, we can use the following command:

# To use vim text editor

$ sudo EDITOR=vim virsh edit domain_name

# To use nano text editor

$ sudo EDITOR=nano virsh edit domain_name


Suspend/Resume VM

To suspend a guest we can use the following command:

$ sudo virsh suspend domain_name


Resuming a guest VM

To restore a suspended guest VM, we can use the following command:

$ sudo virsh resume domain_name


Save VM

To save the current state of a VM to a file, we can use the following command:

$ sudo virsh save domain_name domain_name.save
$ ls -l domain_name.saved


Restoring a saved VM

To restore the saved VM from the file, we can use the following command:

$ virsh restore domain_name.save
$ sudo virsh list


Managing Volumes using virsh

Now let us see how to create a storage volume, attach it to a VM, detach it from a VM and how to delete a volume.


i.  Creating volume

To create a 2GB volume named domain_name_vol2 on the default storage pool, we can use the following command:

$ sudo virsh vol-create-as default domain_name_vol2.qcow2 2G
$ sudo du -sh /var/lib/libvirt/images/domain_name_vol2.qcow2


ii. Attach a volume to VM

To attach created volume above to VM domain_name, we can use the following command:

# virsh attach-disk --domain  domain_name \
--source /var/lib/libvirt/images/domain_name_vol2.qcow2 \
--persistent --target vdb


iii. Detach volume on VM

To detach the above volume domain_name_vol2 from the VM domain_name, we can use the following command:

$ sudo virsh detach-disk --domain domain_name --persistent --live --target vdb


iv. Delete volume

To delete volume, we can use the following commands:

$ sudo virsh vol-delete domain_name_vol2.qcow2 --pool default
$ sudo virsh pool-refresh default
$ sudo virsh vol-list default


[Need urgent assistance with Virsh commands? – We can help you. ]



Conclusion

This article will guide you on how to use to manage KVM guest virtual #machines using virsh #commands.

virsh is a command line utility for managing virsh guest domains/virtual machines and the #hypervisor.

Linux list a KVM vm guest using #virsh command.

The main command interface used to control both Solaris xVM and guest domains is the virsh command. virsh provides a generic and stable interface for controlling virtualized operating systems.

Many virsh commands act asynchronously. This means that the system prompt can return before the operation has completed.

#KVM lets you turn Linux into a hypervisor that allows a host machine to run multiple, isolated virtual environments called guests or virtual machines (VMs).

To log into VM with Virsh, simply:

1. Open a shell prompt or login using ssh. 

2. Login to a host server called server1. 

3. Use the virsh console command to log in to a running VM called 'centos7' type: virsh console centos7.

The virsh destroy #command initiates an immediate ungraceful shutdown and stops the specified guest virtual machine. 

Using virsh destroy can corrupt guest virtual machine file systems. 

Use the virsh destroy command only when the guest virtual machine is unresponsive.


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com