Basic operations in OpenVZ environment

This article assumes you have already installed OpenVZ. If not, follow the link to perform the steps needed.

Create and start a container

To create and start a container, run the following commands:

[host-node]# vzctl create CTID --ostemplate osname
[host-node]# vzctl set CTID --ipadd a.b.c.d --save
[host-node]# vzctl set CTID --nameserver a.b.c.d --save
[host-node]# vzctl start CTID

Here CTID is the numeric ID for the container; osname is the name of the OS template for the container, and a.b.c.d is the IP address to be assigned to the container.

Example:

[host-node]# vzctl create 101 --ostemplate fedora-core-5-minimal
[host-node]# vzctl set 101 --ipadd 10.1.2.3 --save
[host-node]# vzctl set 101 --nameserver 10.0.2.1 --save
[host-node]# vzctl start 101

Your freshly-created container should be up and running now; you can see its processes:

[host-node]# vzctl exec CTID ps ax

Enter to and exit from the container

To enter container give the following command:

[host-node]# vzctl enter CTID
entered into container CTID
[container]#

To exit from container, just type exit and press Enter:

[container]# exit
exited from container VEID
[host-node]#

Stop and destroy the container

To stop container:

[host-node]# vzctl stop CTID
Stopping container ...
Container was stopped
Container is unmounted

And to destroy container:

[host-node]# vzctl destroy CTID
Destroying container private area: /vz/private/CTID
Container private area was destroyed

OpenVZ installation

Requirements

This guide assumes you are running RHEL (CentOS, Scientific Linux) 6 on your system. Currently, this is a recommended platform to run OpenVZ on.

/vz file system

It is recommended to use a separate partition for containers (by default /vz) and format it to ext4.

yum pre-setup

Download openvz.repo file and put it to your /etc/yum.repos.d/ repository:

wget -P /etc/yum.repos.d/ http://ftp.openvz.org/openvz.repo

Import OpenVZ GPG key used for signing RPM packages:

rpm --import http://ftp.openvz.org/RPM-GPG-Key-OpenVZ

Kernel installation

Limited OpenVZ functionality is supported when you run a recent 3.x kernel (check vzctl for upstream kernel, so OpenVZ kernel installation is optional but still recommended.

# yum install vzkernel

System configuration

Please make sure the following steps are performed before rebooting into OpenVZ kernel.

sysctl

There are a number of kernel parameters that should be set for OpenVZ to work correctly. These parameters are stored in /etc/sysctl.conf file. Here are the relevant portions of the file; please edit accordingly.

# On Hardware Node we generally need
# packet forwarding enabled and proxy arp disabled
net.ipv4.ip_forward = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.all.forwarding = 1
net.ipv4.conf.default.proxy_arp = 0

# Enables source route verification
net.ipv4.conf.all.rp_filter = 1

# Enables the magic-sysrq key
kernel.sysrq = 1

# We do not want all our interfaces to send redirects
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0

SELinux

SELinux should be disabled. Put SELINUX=disabled to /etc/sysconfig/selinux:

echo "SELINUX=disabled" > /etc/sysconfig/selinux

Tools installation

Before installing tools, please read about vzstats and opt-out if you don’t want to help the project.

OpenVZ needs some user-level tools installed:

# yum install vzctl vzquota ploop

Reboot into OpenVZ

Now reboot the machine and choose “OpenVZ” on the boot loader menu (it should be default choice).

Download OS templates

An OS template is a Linux distribution installed into a container and then packed into a gzipped tarball. Using such a cache, a new container can be created in a minute.

Download precreated template directly from download.openvz.org/template/precreated, or from one of the mirrors. Put those tarballs as-is (no unpacking needed) to the /vz/template/cache/ directory.

How to Install Linux KVM and Create Guest VM with Examples

KVM stands for Kernel-based Virtual Machine.

As the name suggests, this is kernel based virtualization technology for Linux OS on hardware that supports virtualization.

The guest operating systems can be fully virtualized or para virtualized.

Para-virtualization enables several operating systems to run on one set of hardware by effectively using resources such as processors and memory. In Para virtualization, the operating system is modified to work with a virtual machine, which will minimize the execution time required to perform the operations that are otherwise difficult to run in a virtual environment.

This is the 1st article in an on-going series of articles on KVM.

1. Tools to Manage VM

The KVM package provides qemu-kvm, a hypervisor specific tool to manage virtual machines.

Instead of this tool, you can use the software package known as libvirt which is more convenient and recommended way to manage virtual machines. This software includes an API library, a daemon (libvirtd), and a command line utility (virsh).

You can use this libvirt tool to manage any existing virtual machines running on KVM, Xen, VMWARE ESX, etc. Full list of supported hypervisors, is listed on libvirt.

There are few other tools available that you can use to manage your guest VM.

  • virt-manager (Virtual Machine Manager): A graphical management tool for VM Guests.
  • vm-install: A script based menu driven utility that define a VM Guest and install its operating system.
  • virt-viewer: An X viewer client for VM Guests which supports TLS/SSL encryption of x509 certificate authentication and SASL authentication.

2. Check Virtualization Support on your Hardware

To check whether your CPU supports the hardware virtualization, execute the following command.

# egrep '(vmx|svm)' /proc/cpuinfo

If you see vmx or svm in the output under flags section, then your hardware (CPU) supports the virtualization.

flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov 
pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm 
constant_tsc archrfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni 
dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 sse4_2 
popcnt lahf_lm tpr_shadow vnmi flexprioritpt vpid
.

3. Install KVM Packages

Install KVM packages using your distro specific package management tool (for example, yum on RedHat and CentOS).

# yum install kvm

Once you install the main kvm package, install the following KVM related packages that will help you to manage the VMs on your system.

# yum install qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools

The following are the KVM related packages that are installed on my machine. The version number on your system might vary slightly.

# rpm -qa | egrep "virt|kvm|qemu"
python-virtinst-0.600.0-18.el6.noarch
qemu-img-0.12.1.2-2.415.el6.x86_64
libvirt-0.10.2-29.el6.x86_64
virt-viewer-0.5.6-8.el6.x86_64
qemu-kvm-0.12.1.2-2.415.el6.x86_64
libvirt-python-0.10.2-29.el6.x86_64
virt-manager-0.9.0-19.el6.x86_64
virt-top-1.0.4-3.15.el6.x86_64
libvirt-client-0.10.2-29.el6.x86_64
gpxe-roms-qemu-0.9.7-6.10.el6.noarch
virt-what-1.11-1.2.el6.x86_64

After the packages are installed, it is recommended to reboot your machine to load all the KVM and libvirt modules, even though you can reload it using modprobe command.

You can install the Guest OS using Virtul machine manager which vm-install (for SUSE Linux) and virt-install (for RedHat).

When your display is set (by exceed, reflectionX or some other display managers), it provides a graphical interface to create virtual machine Wizard that guides you through the installation process. If the display is not set, i.e when connecting the machine using ssh with no X11-forwarding, vm-install offers a command line wizard to interactively setup a VM guest for installation.

4. Network Pre-req to Create Guest VM

By default, VMs will only have network access to other VMs on the same server (and to the host itself). If you want the VMs to have access to your VLAN, then you must create a network bridge on the host as explained here.

Edit /etc/sysconfig/network-scripts/ifcfg-eth0 and add the line “BRIDGE=br0″ (make sure to remove any static IPs).

Create the file /etc/sysconfig/network-scripts/ifcfg-br0 and add the entries as shown below. You can use static or dhcp. I used static IP in this case.

DEVICE="br0"
BOOTPROTO="static"
IPADDR="xxx.xxx.xxx.xxx"
NETMASK="255.255.255.0"
ONBOOT="yes"
TYPE="Bridge"
NM_CONTROLLED="no"

If you are running a firewall, (like iptables), create a rule in to allow bridged traffic.

5. Additional Pre-reqs to Create Guest VM

The default location for the VM image files are under /var/lib/libvirt/images. Please ensure adequate space is available under this directory. If not, you can point the image file to different directory during the creation of VM.

Enable IP forwarding in /etc/sysctl.conf by adding the following line:

inet.ipv3.ip_forward=1

During the testing phase, if you have SELinux running, disable it by setting SELinux to permissive mode.

Reboot the system after the above changes are done.

6. Creating a new Guest VM using virt-install

virt-install tool is used to create the VM. This tool can be used in both interactive or non-interactive mode.

In the following example, I passed all the required values to create an VM as command line parameters to the virt-install command.

# virt-install \
 -n myRHELVM1 \
 --description "Test VM with RHEL 6" \
 --os-type=Linux \
 --os-variant=rhel6 \
 --ram=2048 \
 --vcpus=2 \
 --disk path=/var/lib/libvirt/images/myRHELVM1.img,bus=virtio,size=10 \
 --graphics none \
 --cdrom /var/rhel-server-6.5-x86_64-dvd.iso \
 --network bridge:br0

In the above virt-install command:

  • n Name of your virtual machine
  • description Some valid description about your VM. For example: Application server, database server, web server, etc.
  • os-type OS type can be Linux, Solaris, Unix or Windows.
  • os-variant Distribution type for the above os-type. For example, for linux, it can be rhel6, centos6, ubuntu14, suse11, fedora6 , etc. For windows, this can be win2k, win2k8, win8, win7
  • ram Memory for the VM in MB
  • vcpu Total number of virtual CPUs for the VM.
  • disk path=/var/lib/libvirt/images/myRHELVM1.img,bus=virtio,size=10 Path where the VM image files is stored. Size in GB. In this example, this VM image file is 10GB.
  • graphics none This instructs virt-install to use a text console on VM serial port instead of graphical VNC window. If you have the xmanager set up, then you can ignore this parameter.
  • cdrom Indicates the location of installation image. You can specify the NFS or http installation location (instaed of –cdrom). For example, –location=http://.com/pub/rhel6/x86_64/
  • network bridge:br0 This example uses bridged adapter br0. It is also possible to create your own network on any specific port instead of bridged adapter. If you want to use the NAT then use something like below for the network parameter with the virtual network name known as VMnetwork1. All the network configuration files are located under /etc/libvirt/qemu/networks/ for the virtual machines. For example: –network network=VMnetwork1

7. List Virtual Machines

You can edit the grub menu with “console=tty0 console=ttyS0,115200″ to display the console for your screen during installation process on your SSH window. This will let you to install the OS through the guided installation. Else you can use X server for the display.

The following command will display all the virtual machines that are installed on your OS. In this example, we have two VMs installed on this system.

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     dev-dev-vm                     running
 2     myRHELVM1                      running

8. Edit VM Config File

After the VM is created, you’ll see the VM configuration file in the following location. The name of the configuration file will be the name of the VM with .xml extension.

# ls -l /etc/libvirt/qemu/myRHELVM1.xml
-rw-------. 1 root root 2109 Oct 15 12:30 /etc/libvirt/qemu/myRHELVM1.xml

You should avoid editing this file manually and instead use the command virsh edit. To edit the domain (virtual machine) do the following:

virsh edit myRHELVM1

9. Connect to VM Console

To connect to the console of the virtual machine use the following command. You can use “ctrl + ]” to exit out of the VM console.

virsh console myRHELVM1

If you do not have an X server running on your host, connecting to a VMs serial console might be the only way to login to a VM if networking is not available.

Setting up access to a VM’s console is no different than a physical server, where you simply add the proper kernel boot parameters to the VM.

For example, for a RHEL VM (or CentOS VM), append the following parameters to the kernel boot line in /etc/grub.conf and then reboot the VM.

console=tty0 console=ttyS0,115200

Alternatively, you can also add the following entries to the /etc/grub.conf file.

serial --unit=0 --speed=115200
terminal --timeout=5 serial console

10. Display VM Information

To display the VM information, use the following command:

# virsh dominfo myRHELVM1
Id:             5
Name:           myRHELVM1
UUID:           58083ae7-51db-50c3-64d8-bc4c49f642d0
OS Type:        Linux
State:          running
CPU(s):         2
CPU time:       207.6s
Max memory:     2097152 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c698,c788 (permissive)

11. Display VM CPU and Memory Uasge

To display the virtual machine CPU and memory usage, use the virt-top command as shown below.

# virt-top
virt-top 07:14:44 - x86_64 8/8CPU 1600MHz 32094MB
3 domains, 2 active, 2 running, 0 sleeping, 0 paused, 1 inactive D:0 O:0 X:0
CPU: 0.1%  Mem: 4096 MB (4096 MB by guests)
   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
    5 R    0    0 1546    0  0.0  6.0   3:29.55 dev-dev-vm
    8 R    0    0 1546    0  0.0  6.0   1:42.17 myRHELVM2
    -

12. Shutdown, Reboot, or Start VM using virsh

To shutdown the VM, do the following:

# virsh shutdown myRHELVM1
Domain myRHELVM1 is being shutdown

To reboot the VM, do the following:

# virsh reboot myRHELVM1

To start or power on the virtual machine, do the following. Once the VM is started, execute “virsh list –all” to view all the running VMs.

# virsh start myRHELVM1
Domain myRHELVM1 started

In the next article of the KVM series, we’ll cover more details on how to edit and manipulate configurations on an individual VM.

Proxmox Virtual Environment

Proxmox VE is a complete open source virtualization management solution for servers. It is based on KVM virtualization and container-based virtualization and manages virtual machines, storage, virtualized networks, and HA Clustering.

The enterprise-class features and the intuitive web interface are designed to help you increase the use of your existing resources and reduce hardware cost and administrating time – in business as well as home use. You can easily virtualize even the most demanding Linux and Windows application workloads.

The following video tutorial will explain how the installation is performed;