Virsh Command Line Utility Manage KVM Virtual Machines Command Line

We have published an article way before to create Kernel-Based Virtual Machine and installing KVM in RHEL 7/Centos. In this article we learn to create new VM with ‘virt-install’ & managing KVM based Virtual machines in CLI using command line tool named ‘virsh’.  virsh 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 tool and the graphical guest Manager( virt-manager ).

Virsh Command Line Utility to Manage VM’s

Managing Kernel-Based Virtual Machines, Now that the machine has been created, we will learn managing KVM based VMs via CLI using ‘virsh’. Virsh supports a large number of commands, to view all of them use

$ virsh help

command output there options in text file

You will get all the commands that can be used with virsh. Some the important commands are listed here in tutorial as well.

List all Guests Virtual Machines To list all Virtual machines that are running under KVM, use the following command

$ virsh list –-all
 Id Name State
—————————————————-
4 arkitvm running

Start Virtual Machine using below command example

# virsh start arkitvm
Domain arkitvm started

Where VM name , in our case, can be arkitvm

Power off VM, For shutting down a VM run the following

 $ virsh shutdown arkitvm
virsh shutdown command

virsh shutdown command

This will shutdown VM in orderly manner. Which send an shutdown command to Virtual Machine you have to accept from VM

$ virsh destroy arkitvm

It will stop VM instantly and might cause damage to VM.

Reboot Virtual Machine using Virsh Command

Rebooting Virtual Machine using virsh command line utility

$ virsh reboot Arkitvm

Suspending a Domain, VM will be in suspended mode so you can’t do anything its just like freeze

 # virsh suspend arkitvm
Domain arkitvm suspended

Resuming a VM, Suspended Virtual Machine you can bring back to normal state

# virsh resume arkitvm
Domain arkitvm resumed

Note : If the host is rebooted when suspended state of the VM will be lost

Saving a system State. To save the current state of a VM, run below mentioned command

$ virsh save arkit /tmp/vm

Where, /tmp/vm is the destination where the state of the VM will be saved.

Restoring state of a VM from save state. A saved state of the VM can be restored using the following command

$ virsh restore /data/vm

A saved state can be restored even after a host system reboot

Connecting to VM console using command line

# virsh console arkitvm &
[1] 34894
[root@Server0 Desktop]# Connected to domain arkitvm
Escape character is ^]

Virsh Command  VM Information

Displaying VM information dominfo command output

[root@Server0 Desktop]# virsh dominfo arkitvm
Id: 5
Name: arkitvm
UUID: 72f49f6d-b086-4fc7-8aa6-9a7ef52a0cb2
OS Type: hvm
State: running
CPU(s): 1
CPU time: 277.0s
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c282,c635 (enforcing)

Check VM memory & cpu usage

# virsh cpu-stats arkitvm
CPU0:
 cpu_time 161.426776171 seconds
 vcpu_time 155.533521360 seconds
CPU1:
 cpu_time 158.594611520 seconds
 vcpu_time 152.607630417 seconds
Total:
 cpu_time 320.021469790 seconds
 user_time 0.510000000 seconds
 system_time 12.620000000 seconds

Print Node Memory stats using virsh command

# virsh nodememstats
total : 7997540 KiB
free : 877400 KiB
buffers: 1456 KiB
cached : 5372372 KiB

To Check Network Information you simple use below command

# virsh net-info <NETWORK>

Conclusion

You can do hell lot of things using virsh command which means you can simply control each and every guest using virsh. Attaching new devices and populate configuration using XML File.

what is RHEV

more command options

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

Leave a Reply

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