OpenVZ, Xen, and KVM PDF
OpenVZ, Xen, and KVM PDF
KVM
KVM is full hardware virtualisation, you can run almost any operating system as a guest
BSD/Windows/Linux and with virtio driver you will get near native performance, some
experiments have shown only 3% loss on native hardware under ideal circumstances.
It does support installation from ISO and also template based installation, it comes with good
separation in terms of privacy, it can suffer I/O lag under heavy load which impacts the guest
operating systems and the host operating system.
Each guest (VM) runs as a process on the host node while this is great for discovering which
guest is causing issues when required it can also cause problems if the host is under heavy load
all guests suffer.
You can over allocate Ram with little effort however in most cases unless the host is full SSD this
is unlikely to happen due to the overhead it puts on the host node and subsequent and obvious
performance issues.
Due to KVM being native in most modern kernels it does lend a performance advantage over
others in some circumstances and is still fairly new and under very active development.
Most people select KVM for excellent performance and flexibility although perhaps not quite as
stable as Xen due to maturity.
XEN
Xen comes in 2 flavors but can run simultaneously on the same physical host, Xen PV
(paravirtualisation) and HVM (full hardware virtualisation)
Xen PV guests (in the hosting industry) tend to be template based for repid deployment and
snappy performance, you can run your own kernel in Xen PV and this is pretty much default these
days, you can only run Linux on Xen PV (BSD with additional configuration is possible but not
common).
Xen HVM runs much like KVM it has better drivers for Linux based distributions as PV has been
available by default since around 2006 in most kernels so you do not need to install virtio for a
performance boost however NetBSD and windows perform poorly on Xen HVM compared to
KVM, while you can over come this to some degree on Windows with Xen PV drivers for Windows
it does not run as well as KVM out of the box so to speak.
Xen is quite old now and very mature, most people select Xen for good performance with
exceptional stability.
Xen hosts will usually pre-allocate Ram and CPU cores to the xen hypervisor so it has its own
dedicated resources that guests cannot impact on to achieve stability.
OpenVZ
OpenVZ is hugely popular in the hosting industry due to its rapid deployment and very high
density, it achieves this as the host kernel is shared with the guests along with ram, cpu and disk,
with fairly basic separation between guest and host the I/O bottleneck is almost none existent.
In terms of disk access speed and disk latency OpenVZ is a clear winner when compared to KVM
and Xen however this comes at the cost of lack of separation in terms of privacy and also in terms
of how much impact 1 guest OS can have on both the host node and other guests, all individual
processes are visible to the host node and you cannot encrypt your data.
OpenVZ supports Linux only (unless using the commercial parallels which will support windows
in a fashion)
OpenVZ can also be nested inside Xen or KVM to achieve even greater density, due to the volume
of containers you can run on a single host node this keeps the price of OpenVZ much more
competitive than KVM and Xen.
Virsh Command
virsh is the interface or command for managing the virtual machines based on 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 Licenseand 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
[root@localhost ~]# 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
[root@localhost ~]# virsh nodememstats
total : 65979984 kB
free : 44783040 kB
buffers: 604388 kB
cached : 16473328 kB
Example:3 Get KVM Hypervisor CPU info
[root@localhost ~]# virsh nodecpustats
user: 122779270000000
system: 1304262720000000
idle: 470011564690000000
iowait: 110371800000000
Example:4 Get number of Guest Virtual machines irrespective of state such as running, save,
shutdown etc.
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------
3 test running
Example:5 Get all the networks available for KVM hypervisor
[root@localhost ~]# virsh net-list
Name State Autostart
-----------------------------------------
default active yes
Example:6 Get Hardware information of a KVM guest machine
Synatx : virsh dominfo BaseMachine
[root@localhost ~]# 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
[root@localhost ~]# virsh shutdown machine_name
Example:8 Reboot the Virtual Machine
[root@localhost ~]# virsh reboot machine_name
Example:9 Force off or destory the machine
[root@localhost ~]# virsh destroy machine_name
Example:10 Start the Virtual Machine
[root@localhost ~]# virsh start machine_name
Example:11 Connect to specific virtual machine using virt-viewer
syntax : virt-viewer -c qemu:///system machine_name
[root@localhost ~]# 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 :
[root@localhost ~]# dd if=/dev/zero of=/var/lib/libvirt/images/centos-linux.img bs=1M
count=10240
then run virt-install command
[root@localhost ~]# 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
OS installation Screen will appear when we run 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
OpenVZ commands
OpenVZ is a container based virtualization technique for Linux. OpenVZ creates multiple secure,
isolated Linux containers (otherwise known as VEs or VPSs) on a single physical server. Each
container performs and executes exactly like a stand-alone server.
A container can be rebooted independently and have root access, users, IP addresses, memory,
processes, files, applications, system libraries and configuration files. Here I am listing some
useful OpenVz commands with example.
Let’s start, here CTID representing the ID of a container (VPS – Virtual Private Server).
1, Command to list the running VPSs in a node
# vzlist
Example:
# vzlist
CTID NPROC STATUS IP_ADDR HOSTNAME
106 104 running xx.xx.xx.xx server1.test.com
107 46 running xx.xx.xx.xx server2.test.com
108 83 running xx.xx.xx.xx server3.test.com
109 86 running xx.xx.xx.xx server4.test.com
2, Command to list running and stopped VPSs in an node
This command lists all (running and stopped) the VPSs in a node.
# vzlist -a
Example:
# vzlist -a
CTID NPROC STATUS IP_ADDR HOSTNAME
106 104 running xx.xx.xx.xx server1.test1.com
107 46 running xx.xx.xx.xx server2.test2.com
108 83 running xx.xx.xx.xx server3.test3.com
109 86 running xx.xx.xx.xx server4.test4.com
110 - stopped xx.xx.xx.xx server5.test5.com
Other operations
10, Set hostname for a Server
# vzctl set CTID --hostname New_hostname --save
11, Add new IP to VPS
# vzctl set CTID --ipadd xx.xx.xx.xx --save
12, Delete IP from VPS
# vzctl set CTID --ipdel xx.xx.xx.xx --save
13, Reset root password of a VPS
# vzctl set CTID --userpasswd root:new_password --save
14, To add NameServer IPs to VPS
# vzctl set CTID --nameserver xx.xx.xx.xx --save
15, Executes commands for a container from node
# vzctl exec CTID command
Example:
# vzctl exec 110 df -h
# vzctl exec 101 df -h
Filesystem Size Used Avail Use% Mounted on
/dev/simfs 130G 89G 27G 78% /
none 8.0G 4.0K 8.0G 1% /dev
none 8.0G 0 8.0G 0% /dev/shm
16, To check the resource usages
# vzcalc -v CTID
Example:
# vzcalc -v 110
# vzcalc -v 101
Resource Current(%) Promised(%) Max(%)
Low Mem 1.53 182706895112.42 182706895112.42
Total RAM 33.79 n/a n/a
Mem + Swap 1.73 38695649865149.61 n/a
Alloc. Mem 2.95 38695649865149.61 38695649865149.61
Num. Proc 0.05 n/a 2395786836523892.00
--------------------------------------------
Memory 33.79 38695649865149.61 2395786836523892.00
Xen Commands
The most important command for managing pretty much anything in Linux is vi. So if you only
learn one command, learn that one. But if you want to learn another, the second most important
command for managing Xen is then xm (well, once you’ve apt-gotten or yummied up the
installation that is). The xm command has a number of easy verbs, each used for managing the
Xen environment.
● xm info – Shows information about the Xen host
● xm list – Shows information about doms (states include r for running, b for blocked, c for
crashed, p for paused and the worse, d for dying).
● xm network-list – Shows virtual interfaces for doms
● xm log – Shows information from the Xen logs
● xm reboot – Reboots a VM
● xm vcpu-list – Shows dom virtual processors
● xm top – Shows hosts and domains similar to how top works in *nix
● xm uptime – Shows uptime
● xm dmesg – Shows the send message buffer
● xm create krypted.com – Create a node called krypted.com
● xm console krypted.com – Switch to that new krypted.com node
● xm destroy krypted.com – Deletes that newly created krypted.com node
● xm shell – Invoke an interactive shell environment of your xend
● xm shutdown – Turn off a VM
● xm pause – Rather than shut the VM down, just pause it (starts back up much faster), but
if the host is rebooted then state is lost (otherwise use suspend)
● xm suspend – Suspends a VM, which writes the data to disk, so changes wouldn’t be lost
on restart.
● xm rename – Rename installed VMs
● xm resume – If a VM is paused, fire it up
● xm save – Similar to suspend except with user definable state file
● xm restore – Similar to resume except restoreable with exports that used the save verb
● xm dump-core – Dumps core per domain
● xm sysrq – Sends system requests per domain
● xm block-list – Lists block devices per domain
● xm mem-max – Configure the maximum memory for a domain
● xm mem-set – Configure the current memory allowance for a domain
● xm vcpu-set – Configure active processors for a domain
● xm migrate – Move a domain to another server (e.g. using the -l operator to do so live)
Virt-manager and virt-install can be used to manage and create virtual machines for use with Xen.
Virsh can also be of assistance:
● virsh nodeinfo – Shows information about each node
● virsh vcpuinfo – Shows information about virtual processors
● virsh dominfo – Shows information about domains
● virsh dumpxml – Dumps the same information just in parseable XML