Pawan Sharma | August 6, 2013 | 3 Comments so far

KVM - Command Line Administration

In the previous post we have created KVM virtual machine using the Virtual Machine Manager GUI. In this post we will look at command line tools which we can use for administration of KVM virtual machines. We will see how to check installed KVM guests, start or shutdown KVM guest, installation of the guest. At many times we are in situations where we do not have the X11 (Graphical) environment. In such case command line KVM administration tools comes handy.
  • To install a virtual machine through command line use “virt-install”
virt-install \
--name TESTER1 \
--ram 500 \
--vcpus=2 \
--network network=default \
--os-variant RHEL6
  • To clone a virtual guest use “virt-clone”
# virt-clone --original TESTER1 --name TESTER2 --file /var/lib/libvirt/images/TESTER2.img --mac 08:00:27:B7:08:9F

Note: If you have problems while starting network of cloned virtual machine or getting error like “Device eth0 does not seem to be present, delaying initialization” please check this post.
  • To check currently installed KVM guests use “virsh list --all command”
# virsh list –all
 Id    Name                           State
----------------------------------------------------
 18    TESTER3                        running
 19    TESTER1                        running
 -     TESTER2                        shut off
  • Use virt-inspector to show the version of the OS, the kernel version, what drivers are installed, mount point details, etc.
# virt-inspector DOMAIN
  • To check cpu and memory utilization of KVM user “virt-top”
# virt-top
virt-top 14:49:49 - x86_64 2/2CPU 3400MHz 1751MB
3 domains, 2 active, 2 running, 0 sleeping, 0 paused, 1 inactive D:0 O:0 X:0
CPU: 1.5%  Mem: 1536 MB (1536 MB by guests)

   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
   19 R    0    0  12K    0  1.1 29.0   7:14.45 TESTER1
   18 R    0    0  184    0  0.3 58.0   2:39.18 TESTER3
    -                                           (TESTER2)
  • To check directory contents of a guest KVM machine use “virt-ls –l –a KVM_guest_image_file directory”
# virt-ls -l -a /Virtual/TESTER1.img /root
total 80
dr-xr-x---.  3 root root  4096 Jun 28 05:21 .
dr-xr-xr-x. 25 root root  4096 Jun 28 07:19 ..
-rw-------   1 root root   420 Jun 28 09:17 .bash_history
-rw-r--r--.  1 root root    18 May 20  2009 .bash_logout
-rw-r--r--.  1 root root   176 May 20  2009 .bash_profile
-rw-r--r--.  1 root root   176 Sep 23  2004 .bashrc
-rw-r--r--.  1 root root   100 Sep 23  2004 .cshrc
drwx------   2 root root  4096 Jun 24 05:22 .ssh
-rw-r--r--.  1 root root   129 Dec  4  2004 .tcshrc
-rw-------.  1 root root  1364 Jun 24 04:40 anaconda-ks.cfg
-rw-r--r--.  1 root root 26876 Jun 24 04:39 install.log
-rw-r--r--.  1 root root  7572 Jun 24 04:38 install.log.syslog

  • To cat a file of KVM guest use “virt-cat –d DOMAIN filename”
# virt-cat -d TESTER1 /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=TESTER1
  • To check the mount points utilization of a KVM guest from host use “virt-df –h –d DOMAIN”
# virt-df -h -d TESTER1
Filesystem                                Size       Used  Available  Use%
TESTER1:/dev/sda1                         193M        28M       155M   15%
TESTER1:/dev/OSVG/lvroot                  7.7G       1.7G       5.6G   23%
  • To find partitions, filesystem, LVM of KVM guest use “virt-filesystems -d DOMAIN
# virt-filesystems -d TEST-LINUX1 --long --all
Name                  Type       VFS  Label MBR Size        Parent
/dev/sda1             filesystem ext4 -     -   524288000   -
/dev/VolGroup/lv_root filesystem ext4 -     -   10242490368 -
/dev/VolGroup/lv_swap filesystem swap -     -   2113929216  -
/dev/VolGroup/lv_root lv         -    -     -   10242490368 /dev/VolGroup
/dev/VolGroup/lv_swap lv         -    -     -   2113929216  /dev/VolGroup
/dev/VolGroup         vg         -    -     -   12356419584 -
/dev/sda2             pv         -    -     -   12356419584 -
/dev/sda1             partition  -    -     83  524288000   /dev/sda
/dev/sda2             partition  -    -     8e  12359565312 /dev/sda
/dev/sda              device     -    -     -   12884901888 -
  • To copy file/directory from KVM guest to host use “virt-copy-out -d DOMAIN /file|dir localdir”
# virt-copy-out -d TESTER1 /root/testfile.txt /root
  • To copy file/directory from host to KVM guest use “virt-copy-in -d domain file|dir /destination”
# virt-copy-in -d TESTER1 /root/testerfile1.txt /root/

Note: Using "virt-copy-in" on live virtual machines can be dangerous, potentially causing disk corruption.  The virtual machine must be shutdown before you use this command.
  • To edit file of KVM guest use “virt-edit -d DOMAIN filename”
# virt-edit -b .orig -d TEST-LINUX1 /root/testfile.txt

Use –b option to make a backup of original file.

Note: Using "virt-edit" on live virtual machines can be dangerous, potentially causing disk corruption.  The virtual machine must be shutdown before you use this command.

There are lots of other command line tools to administer KVM virtual machines on RHEL6.

For any queries post comments.

3 comments:

  1. your articles are very useful! keep posting !!

    ReplyDelete
  2. Can you post an article on adding/assigning SAN Disk to KVMs and troubleshooting steps for KVM Servers.

    ReplyDelete
  3. Online Ethical Hacking Certification, CEH Training, EHCE Certification, Certified Ethical Hacking Training, Cyber Security Certification and Training, Ethical Hacking and Counter Measure Expert, etc. US Council Certification and Training offers them all to you at affordable prices. For more information visit http://www.us-council.com/

    ReplyDelete