Total Pageviews

Friday, 23 December 2016

Virtualization Shell (virsh) Commands !!!

In this post I am going to show different commands of "virsh" CLI including output. To manage virtual machines of KVM through command line we need "virsh". To enable virsh CLI we install libvirt-bin package.

Now I am going to show basic commands and their sub-commands usage. Whenever we learn any new commands, it is always a good practice to remember a command as follows:

command: virsh.
sub-command: the part comes next to the command.
options or optional things: which changes as per sub-command.

Sorry if I messed it, but what I mean to explain is...

Every command starts with a name, sub-command and options. So if we need to attach a disk the sub-command will be attach-disk and options are like size of the disk. Similarly command for renaming a domain, can be virsh rename oldname newname. So sometimes instead of options there will be optional things.

Even if my explanation is still unclear, don't worry about it, just continue with the post. Below are the few commands and its usage with output.

root@ubuntu-kvm:~# virsh -v                                 --- for short description
2.1.0
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh --version
2.1.0
root@ubuntu-kvm:~#

root@ubuntu-kvm:~# virsh -V                                  --- for long description
Virsh command line tool of libvirt 2.1.0
See web site at http://libvirt.org/

Compiled with support for:
 Hypervisors: QEMU/KVM LXC UML Xen LibXL OpenVZ VMware VirtualBox ESX Test
 Networking: Remote Network Bridging Interface netcf Nwfilter VirtualPort
 Storage: Dir Disk Filesystem SCSI Multipath iSCSI LVM RBD Sheepdog ZFS
 Miscellaneous: Daemon Nodedev AppArmor Secrets Debug DTrace Readline Modular
root@ubuntu-kvm:~#

root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            running


root@ubuntu-kvm:~#

Host and Hypervisor related:

To view version of hypervisor,

root@ubuntu-kvm:~# virsh version                       
Compiled against library: libvirt 2.1.0
Using library: libvirt 2.1.0
Using API: QEMU 2.1.0
Running hypervisor: QEMU 2.6.1

root@ubuntu-kvm:~#

To view maximum cpus of host,

root@ubuntu-kvm:~# virsh maxvcpus                     
16

root@ubuntu-kvm:~#

To view memory statistics of host,

root@ubuntu-kvm:~# virsh nodememstats
total  :              2047852 KiB
free   :                81620 KiB
buffers:                 9600 KiB
cached :               148500 KiB

root@ubuntu-kvm:~#

To view cpu statistics of host,

root@ubuntu-kvm:~# virsh nodecpustats
user:                 27478810000000
system:                 736920000000
idle:                 14469860000000
iowait:                1864430000000

root@ubuntu-kvm:~#

To view info related to host,

root@ubuntu-kvm:~# virsh nodeinfo
CPU model:           x86_64
CPU(s):              1
CPU frequency:       2200 MHz
CPU socket(s):       1
Core(s) per socket:  1
Thread(s) per core:  1
NUMA cell(s):        1
Memory size:         2047852 KiB

root@ubuntu-kvm:~#

To view hostname of host machine,

root@ubuntu-kvm:~# virsh hostname
ubuntu-kvm

root@ubuntu-kvm:~#

To view URI (Uniform Resource Identifier),

root@ubuntu-kvm:~# virsh uri
qemu:///system

root@ubuntu-kvm:~#

To view complete system info of host,

root@ubuntu-kvm:~# virsh sysinfo
<sysinfo type='smbios'>
  <bios>
    <entry name='vendor'>innotek GmbH</entry>
    <entry name='version'>VirtualBox</entry>
    <entry name='date'>12/01/2006</entry>
  </bios>
  <system>
    <entry name='manufacturer'>innotek GmbH</entry>
    <entry name='product'>VirtualBox</entry>
    <entry name='version'>1.2</entry>
    <entry name='serial'>0</entry>
    <entry name='uuid'>6F5ED8B0-1976-4DB6-A716-93F90015C16A</entry>
    <entry name='sku'>Not Specified</entry>
    <entry name='family'>Virtual Machine</entry>
  </system>
  <baseBoard>
    <entry name='manufacturer'>Oracle Corporation</entry>
    <entry name='product'>VirtualBox</entry>
    <entry name='version'>1.2</entry>
    <entry name='serial'>0</entry>
    <entry name='asset'>Not Specified</entry>
    <entry name='location'>Not Specified</entry>
  </baseBoard>
</sysinfo>


root@ubuntu-kvm:~#

Domain Administration related:

To take console of our domain/VM,

root@ubuntu-kvm:~# virsh console VM1
Connected to domain VM1
Escape character is ^]
Killed

root@ubuntu-kvm:~#

To view description of our domain/VM,

root@ubuntu-kvm:~# virsh desc VM1
No description for domain: VM1

root@ubuntu-kvm:~#

To view cpustats of domain/VM,

root@ubuntu-kvm:~# virsh cpu-stats VM1
CPU0:
        cpu_time           451.542828932 seconds
Total:
        cpu_time           451.542828932 seconds
        user_time          360.800000000 seconds
        system_time         67.150000000 seconds

root@ubuntu-kvm:~#

To view memory stats of domain/VM,

root@ubuntu-kvm:~# virsh dommemstat VM1
actual 1048576
swap_in 0
last_update 1482452747
rss 1101100
root@ubuntu-kvm:~#

To display connection URI of domain/VM,

root@ubuntu-kvm:~# virsh domdisplay VM1
spice://127.0.0.1:5900
root@ubuntu-kvm:~#

To create dump of domain/VM,

root@ubuntu-kvm:~# virsh dumpxml VM1 >>vm1.xml
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# ls
vm1.xml
root@ubuntu-kvm:~#

To take snapshot of domain/VM,

root@ubuntu-kvm:~# virsh screenshot VM1 vmsnap
Screenshot saved to vmsnap, with type of image/x-portable-pixmap
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# ls
vm1.xml  vmsnap
root@ubuntu-kvm:~#

To suspend our domain/VM,

root@ubuntu-kvm:~# virsh suspend VM1
Domain VM1 suspended

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            paused

root@ubuntu-kvm:~#

To resume our domain/VM,

root@ubuntu-kvm:~# virsh resume VM1
Domain VM1 resumed

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            running

root@ubuntu-kvm:~#

To shutdown our domain/VM,

root@ubuntu-kvm:~# virsh shutdown VM1
Domain VM1 is being shutdown

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running

root@ubuntu-kvm:~#

To rename our domain/VM,

root@ubuntu-kvm:~# virsh domrename VM1 Ubuntu
Domain successfully renamed

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running

root@ubuntu-kvm:~#

To check state of our domain/VM,

root@ubuntu-kvm:~# virsh domstate Ubuntu
shut off
root@ubuntu-kvm:~#

To start our domain/VM,

root@ubuntu-kvm:~# virsh start VM1
error: failed to get domain 'VM1'                                 Error because VM1 no longer exists...
error: Domain not found: no domain with matching name 'VM1'
root@ubuntu-kvm:~#

As soon as we renamed our domain, the default xml file of our domain also gets renamed automatically...

root@ubuntu-kvm:/etc/libvirt/qemu# ls -lrth
total 16K
drwxr-xr-x 3 root root 4.0K Dec 21 18:48 networks
-rw------- 1 root root 4.3K Dec 22 18:48 win2003.xml
-rw------- 1 root root 3.9K Dec 23 17:58 Ubuntu.xml
root@ubuntu-kvm:/etc/libvirt/qemu#

root@ubuntu-kvm:~# virsh start Ubuntu
Domain Ubuntu started

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 9     Ubuntu                         running

root@ubuntu-kvm:~#

Similarly to restart/reset/destroy our domain/vm....

root@ubuntu-kvm:~# virsh restart/reset/destroy <domname>

Similarly to save/restore state of our domain/vm....

root@ubuntu-kvm:~#
 virsh save Ubuntu vm1state
Domain Ubuntu saved to vm1state
root@ubuntu-kvm:~#
root@ubuntu-kvm:~#
 virsh restore Ubuntu vm1state
root@ubuntu-kvm:~#

Adding/removing resources related:

To change vcpus of our domain/VM,

root@ubuntu-kvm:~# virsh setvcpus --count 1 Ubuntu

To change vcpus of our domain/VM,

root@ubuntu-kvm:~# virsh setmem --size 512 Ubuntu

To attach disk/device/interface to our domain/VM,

root@ubuntu-kvm:~# virsh 
attach-device 
<domain> <file> 
                                           attach-disk 
<domain> <source> <target> 
                                           attach-interface 
<domain> <type> <source>

To detach disk/device/interface to our domain/VM,

root@ubuntu-kvm:~# virsh de
tach-device 
<domain> <file> 
                                           detach-disk 
<domain> <source> <target> 
                                           detach-interface 
<domain> <type> <source> 

To view disks attached to our domain/VM,

root@ubuntu-kvm:~# virsh domblklist win2003
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/win2003.qcow2
hdb        /test/Windows 2003 Server.iso

root@ubuntu-kvm:~#

To view network properties of our domain/VM,

root@ubuntu-kvm:~# virsh domiflist win2003
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          network    default    rtl8139     52:54:00:20:1d:55

root@ubuntu-kvm:~#

######################################################################################

No comments:

Post a Comment