virsh is the interface or command for managing the virtual machines based on the 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 License and 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.
# 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.
# virsh nodememstats total : 65979984 kB free : 44783040 kB buffers: 604388 kB cached : 16473328 kB
Example 3: Get KVM Hypervisor CPU info.
# virsh nodecpustats user: 122779270000000 system: 1304262720000000 idle: 470011564690000000 iowait: 110371800000000
Example 4: Get a number of Guest Virtual machines irrespective of state such as running, save, shutdown, etc.
# virsh list --all Id Name State ---------------------------------- 3 test running
Example 5: Get all the networks available for the KVM hypervisor.
# virsh net-list Name State Autostart ----------------------------------------- default active yes
Example 6: Get Hardware information of a KVM guest machine.
Syntax:
# virsh dominfo BaseMachine
Example:
# 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.
# virsh shutdown machine_name
Example 8: Reboot the Virtual Machine.
# virsh reboot machine_name
Example 9: Force off or destory the machine.
# virsh destroy machine_name
Example 10: Start the Virtual Machine.
# virsh start machine_name
Example 11: Connect to specific virtual machine using virt-viewer.
syntax:
# virt-viewer -c qemu:///system machine_name
Example:
# 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:
# dd if=/dev/zero of=/var/lib/libvirt/images/centos-linux.img bs=1M count=10240
then run virt-install command:
# 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 the above command:
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