by JoKeru

a CentOS within a CentOS - virtualization with kvm and libvirt

This post will give you a quick intro into the basics of Kernel based Virtual Machine (KVM) in CentOS / RedHat. Bellow you have a step-by-step procedure on how to to get a CentOS VM running inside a CentOS host.

Install and start the services:

$ uname -a
Linux centos 2.6.32-504.12.2.el6.x86_64 #1 SMP Wed Mar 11 22:03:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ egrep -c '(vmx|svm)' /proc/cpuinfo

$ yum install kvm qemu-kvm # kvm kernel module and qemu hosted (type 2) hypervisor
$ yum install libvirt libvirt-python python-virtinst # kvm manager
# optional packages
$ yum install libguestfs-tools # tools for accessing and modifying virtual machine disk images

# start the libvirt virtualization management system server daemon
$ chkconfig libvirtd on
$ service libvirtd start # be sure your hostname resolves correctly to avoid any warnings
$ service libvirtd status
$ virsh -c qemu:///system list
Id Name State

The default virtual network configuration is known as Usermode Networking. Traffic is NATed through the host interface to the outside network.

Alternately, you can configure Bridged Networking to enable external hosts to directly access services on the guest operating system.

I'll use the second option for this demo:

# disable the Usermode Networking
$ virsh net-list
Name State Autostart Persistent
default active yes yes
$ virsh net-destroy default
Network default destroyed
$ virsh net-undefine default
Network default has been undefined
$ virsh net-list
Name State Autostart Persistent
$ service libvirtd restart # for safety

# enable Bridged Networking on eth1 (eth0 will remain for host traffic)
$ yum install bridge-utils
$ cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-eth1
$ cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-br1
$ /etc/init.d/network restart
$ brctl show
bridge name bridge id STP enabled interfaces
br1 8000.001a6496addf no eth1

Let's get the CentOS ISO image and mount it to be used by the install process:

$ cd /var/lib/libvirt/boot/
$ wget ""
$ mkdir /mnt/centos66/
$ mount -o loop /var/lib/libvirt/boot/CentOS-6.6-x86_64-netinstall.iso /mnt/centos66/

And create the VM:

$ virt-install \
--connect qemu:///system \
--name flash \
--description "Flash VM" \
--hvm \
--accelerate \
--ram 2048 \
--vcpus 1 \
--cpuset 3 \
--numatune 3 \
--cpu host \
--location /mnt/centos66 \
--os-type linux \
--os-variant rhel6 \
--disk path=/var/lib/libvirt/images/flash.img,bus=virtio,size=16,sparse=false \
--network bridge:br1,model=virtio \
--graphics none \
--extra-args="console=tty0 console=ttyS0,115200"

Some useful management commands:

virsh list
virsh list --all

virsh dominfo flash
virsh dumpxml flash
virsh edit flash

virsh console flash
virsh suspend flash
virsh resume flash
virsh shutdown flash # you need acpid inside the guest for this command to work
virsh start flash
virsh reboot flash # you need acpid inside the guest for this command to work
virsh destroy flash # ungraceful shutdown (can corrupt guest filesystems)
virsh autostart flash
virsh autostart --disable flash
virsh undefine flash # deletes all files associated with this guest

virt-clone --connect qemu:///system --original flash --name flash-copy --file /var/lib/libvirt/images/flash-copy.img