KVM Virsh Command Examples on CentOS & RHEL


Overview:

virsh is the interface or command for managing the virtual machines based on KVM hypervisor. On virsh interface virtual machines are identified by their domain names , so virsh is generally used to list current domains , to create , pause & shutdown domains. Virsh comes under GNU Lesser General Public Licenseand supports Xen, QEmu, KVM, LXC, OpenVZ, VirtualBox and VMware ESX.

In this tutorial we discuss the practical examples of virsh command :

Example:1 Get KVM version installed on the host machine

[root@localhost ~]# virsh version
Compiled against library: libvir 0.9.4
Using library: libvir 0.9.4
Using API: QEMU 0.9.4
Running hypervisor: QEMU 0.12.1

 Example:2 Get KVM Hypervisor(Host) Memory info

[root@localhost ~]# virsh nodememstats
total  :        65979984 kB
free   :        44783040 kB
buffers:        604388 kB
cached :        16473328 kB

Example:3 Get KVM Hypervisor CPU info

[root@localhost ~]# virsh nodecpustats
user:          122779270000000
system:        1304262720000000
idle:          470011564690000000
iowait:        110371800000000

Example:4 Get number of Guest Virtual machines irrespective of state such as running, save, shutdown etc.

[root@localhost ~]# virsh list --all
Id Name              State
----------------------------------
3 test               running

Example:5 Get all the networks available for KVM hypervisor

[root@localhost ~]# virsh net-list
Name                 State      Autostart
-----------------------------------------
default              active     yes

Example:6 Get Hardware information of a KVM guest machine

Synatx : virsh dominfo BaseMachine

[root@localhost ~]# virsh dominfo test
Id:             3
Name:           test
UUID:           9ae96029-6c3d-8bd1-6e19-926183f89074
OS Type:        hvm
State:          running
CPU(s):         4
CPU time:       26862.0s
Max memory:     4194304 kB
Used memory:    4194304 kB
Persistent:     yes
Autostart:      disable
Managed save:   no

Example:7 Shutdown the Virtual Machine

[root@localhost ~]# virsh shutdown machine_name

Example:8 Reboot the Virtual Machine

[root@localhost ~]# virsh reboot machine_name

Example:9 Force off or destory the machine

[root@localhost ~]# virsh destroy machine_name

Example:10 Start the Virtual Machine

[root@localhost ~]# virsh start machine_name

Example:11 Connect to specific virtual machine  using virt-viewer

syntax : virt-viewer -c qemu:///system  machine_name

[root@localhost ~]# virt-viewer -c qemu:///system  test

Example:12 Create New virtual Machine using virt-install

Suppose i want to install Centos 6.X on 10GB space , then first create a img file using below command :

[root@localhost ~]# dd if=/dev/zero of=/var/lib/libvirt/images/centos-linux.img bs=1M count=10240

then run virt-install command

[root@localhost ~]#  virt-install --virt-type kvm --name CentOS-Linux --ram 2048 --vcpus=2  --disk path=/var/lib/libvirt/images/centos-linux.img --network bridge=br0 --graphics vnc  --cdrom /root/CentOS-6.2-x86_64-bin-DVD1.iso  --os-variant=RHEL6

Below Screen will appear when we run above command

virt-viewer-centos

Example:13 Live Migration of Virtual Machine from One Hypervisor to Another using virsh command

Basic Requirements of Live Migration :

  • The guest image must be located on a shared storage and it must be accessible using iSCSI, NFS, GFS2 or Fibre Channel.
  • The shared storage must be mounted on the same path on both hosts.
  • Both guests must run the same version of KVM.
  • Both guests must have the same network configuration and bridging configuration (their IPs must be different)
# virsh migrate --live machine_name qemu+ssh://destination_server/system

One comment

  1. maytux says:

    nice written..

Leave a Reply

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