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.

Pawan Sharma | July 18, 2013 | Be the first to comment!

Creating KVM Virtual Machines in RHEL6.

In this post we will discuss about packages required for KVM (Virtualization), creation of KVM virtual machine and managing those virtual machines. KVM can run multiple windows and Linux guest operating system. KVM virtualization is only available for 64 bits machines. You can’t use KVM virtualization on 34 bit architecture machines.

You can install KVM virtualization environment at the time of RHEL6 installation by selecting virtual host packages. Or on currently installed RHEL6 server, you can install packages using yum. For full KVM virtualization host you will require virt-manager, libvirt libvirt-python, python-virtinst, libvirt-client.


# yum install –y virt-manager libvirt libvirt-python python-virtinst libvirt-client

If you don’t have yum server, you can configure it by following easy steps mentioned in this post.

After installing KVM packages you can now create new virtual machines using virt-install command or using virt-manager GUI.

1. Start libvirtd daemon to start KVM virtualization.

# /etc/init.d/libvirtd start
# chkconfig libvirtd on

2. Open Applications --> System Tools --> Virtual Machine Manager. Or run virt-manager command as root user. Click on create new virtual machine button.

 

3. Give name to virtual machine and select installation media option.


4. In this example we have selected ISO image as our installation media.


5. Define RAM and CPU for the KVM virtual machine.


6. Define disk size.


7. Select network options (Bridge or NAT) as per your requirements and click finish.


8. Install RHEL on the created KVM virtual guest.


For RHEL installation steps follow this post.
In next post we will discuss about command line tools for administering KVM virtual machines.
Please comment if you have any questions.

Pawan Sharma | July 9, 2013 | Be the first to comment!

Network Device not working after cloning RHEL6 VM (Device eth0 does not seem to be present, delaying initialization)

In this post I will give solution to one problem I faced while cloning RHEL6 virtual machine.

ISSUE: I have installed RHEL6.2 on KVM/VMware workstation/Oracle VirtualBox. After installation I have cloned the virtual machine. And when starting cloned virtual machine I get below error while restarting the network service.

Device eth0 does not seem to be present, delaying initialization


I have also removed HWADDR entry from “/etc/sysconfig/network-scripts/ifcfg-eth0”, but still I am getting the same error.

SOLUTION: To resolve this problem follow below mentioned steps:

1. As mentioned above, remove HWADDR entry form “/etc/sysconfig/network-scripts/ifcfg-eth0”.

2. Take backup of file “/etc/udev/rules.d/70-persistent-net.rules” and remove all reference to eth0.

Ex.
[root@squid2 ~]# cp /etc/udev/rules.d/70-persistent-net.rules /root/
[root@squid2 ~]# cat /etc/udev/rules.d/70-persistent-net.rules

# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:08:fa:85", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b7:08:97", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

Entry for PCI device marked with green color is the entry for Ethernet card.

3. Remove the marked entry for both PCI devices from the file “/etc/udev/rules.d/70-persistent-net.rules”.

4. Restart the server.

After restart, you will be able to get IP information for the Ethernet card.

If you have any questions please comment.

Pawan Sharma | March 22, 2013 | Be the first to comment!

Configure YUM server to install/update packages on remote server

In the previous post we learn how to create a YUM repository or YUM server on Redhat Enterprise Linux 6. Now if you want to use this YUM server to manage packages on other servers follow below mentioned steps.

1. Create the YUM server as described in previous post.
2. Install vsftpd using yum command.

# yum install vsftpd

3. Make changes in “/etc/vsftpd/vsftpd.conf” as below.

anonymous_enable=YES
anon_root=/mnt

4. Login to the client machine on which you want to install packages.
5. Create a .repo file in “/etc/yum.repos.d/” directory.

# vi /etc/yum.repos.d/RHEL6.repo

[RHEL6]
name= RHEL 6 remote repository
baseurl=file:///yum_server_ip/
enabled=1
gpgcheck=0

6. Run command yum clean all.

# yum clean all

You can now use yum commands to install, remove and query packages.

# yum info httpd

Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, refresh-packagekit, security, subscription-manager, tmprepo, verify,
               : versionlock
Updating certificate-based repositories.
Loading support for Red Hat kernel ABI
Installed Packages
Name           : httpd
Arch           : x86_64
Version        : 2.2.15
Release        : 15.el6
Size           : 2.9 M
Repo           : installed
From repo      : anaconda-RedHatEnterpriseLinux-201105101844.x86_64
Summary        : Apache HTTP Server
URL            : http://httpd.apache.org/
License        : ASL 2.0
Description    : The Apache HTTP Server is a powerful, efficient, and extensible
               : web server.

Please comment if you have any questions.

Pawan Sharma | March 20, 2013 | Be the first to comment!

Configure YUM server in Redhat Enterprise Linux 6 (RHEL6)

Package management is one of the main objectives of RHCSA certification exam. Also as a system administrator you have to install, remove and update packages. In this post we will learn how to configure YUM server for package management on local machine as well as on remote machines. Yum server gives ease to package management in Linux. Using yum server you can query about installed packages, install or remove packages and upgrade or degrade packages as per your requirements.

The main configuration file for yum server is “/etc/yum.conf”. YUM server gets you out of which we know as “Dependency Hell”. It resolves and installs all dependencies related to a package. YUM server creates a database of packages. In this database it stores information like: all installed packages, all available packages, updates, etc. In this post we will see:

  • How to configure local YUM server for RHEL6 DVD iso.
  • How to create YUM server to update packages through FTP.
  • How to configure YUM clients.

STEPS TO CONFIGURE YUM SERVER.

In this exercise we will use RHEL6 DVD.iso. You can mount and copy contents of DVD on local server or if you don’t have enough space you can mount and directly use it as a repository. For now we will mount RHEL6 DVD.iso and use it as our repository for packages.

1. Mount the RHEL6 DVD.iso on /mnt (you can mount it on any directory of your choice).

# mount -o loop rhel-server-6.2-x86_64-dvd.iso /mnt

2. Create a new file in directory “/etc/yum.repos.d/”.

Note: “/etc/yum.repos.d/” directory is default configuration directory for yum. You have to create a file with extension “.repo” and make below mentioned entries in the file.

# vi /etc/yum.repos.d/RHEL6.repo

[RHEL6]
name= RHEL 6 local repository
baseurl=file:///mnt/
enabled=1
gpgcheck=0

3. Run command yum clean all.

# yum clean all

4. You can now use yum commands to install, remove and query packages.

# yum info httpd

Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, refresh-packagekit, security, subscription-manager, tmprepo, verify,
             : versionlock
Updating certificate-based repositories.
Loading support for Red Hat kernel ABI
Installed Packages

Name          : httpd
Arch          : x86_64
Version       : 2.2.15
Release       : 15.el6
Size          : 2.9 M
Repo          : installed
From repo     : anaconda-RedHatEnterpriseLinux-201105101844.x86_64
Summary       : Apache HTTP Server
URL           : http://httpd.apache.org/
License       : ASL 2.0
Description   : The Apache HTTP Server is a powerful, efficient, and extensible
              : web server.


In the next post we will learn how to install/update packages on a remote server form yum server.