Friday, November 29, 2013

Installing And Using OpenVZ On CentOS 5.7

In this HowTo I will describe how to prepare a CentOS 5.7 server for OpenVZ. With OpenVZ you can create multiple Virtual Private Servers (VPS) on the same hardware, similar to Xen and the Linux Vserver project. OpenVZ is the open-source branch of Virtuozzo, a commercial virtualization solution used by many providers that offer virtual servers. The OpenVZ kernel patch is licensed under the GPL license, and the user-level tools are under the QPL license.

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

1 Installing OpenVZ

In order to install OpenVZ, we need to add the OpenVZ repository to yum:
cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ
 
Now open openvz.repo...
vi openvz.repo
... and disable the [openvz-kernel-rhel6] repository (enabled=0) and enable the [openvz-kernel-rhel5] repository instead (enabled=1):


[...]
# Stable branches

[openvz-kernel-rhel6]
name=OpenVZ RHEL6-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel6-2.6.32/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel6-2.6.32
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ

[openvz-kernel-rhel5]
name=OpenVZ RHEL5-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel5-2.6.18/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel5-2.6.18
enabled=1
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ

[openvz-kernel-rhel4]
name=OpenVZ RHEL4-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel4-2.6.9/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel4-2.6.9
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[...] 
The repository contains a few different OpenVZ kernels (you can find more details about them here: http://wiki.openvz.org/Kernel_flavors). The command :
yum search ovzkernel

shows you the available kernels:
[root@server1 yum.repos.d]# yum search vzkernel
...
ovzkernel.i686 : Virtuozzo Linux kernel (the core of the Linux operating system)
ovzkernel.x86_64 : Virtuozzo Linux kernel (the core of the Linux operating system)
ovzkernel-PAE.i686 : The Linux kernel compiled for PAE capable machines.
ovzkernel-PAE-devel.i686 : Development package for building kernel modules to match the PAE kernel.
ovzkernel-devel.i686 : Development package for building kernel modules to match the kernel.
ovzkernel-devel.x86_64 : Development package for building kernel modules to match the kernel.
ovzkernel-ent.i686 : The Linux kernel compiled for huge mem capable machines.
ovzkernel-ent-devel.i686 : Development package for building kernel modules to match the ent kernel.
ovzkernel-xen.i686 : The Linux kernel compiled for Xen VM operations
ovzkernel-xen.x86_64 : The Linux kernel compiled for Xen VM operations
ovzkernel-xen-devel.i686 : Development package for building kernel modules to match the kernel.
ovzkernel-xen-devel.x86_64 : Development package for building kernel modules to match the kernel.
[root@server1 yum.repos.d]#

Pick one of them and install it as follows:
yum install ovzkernel

If the last command gives you errors like...
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol strcpy
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol add_memory
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol swiotlb
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol memory_add_physaddr_to_nid
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol remove_memory
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol strcmp
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/acpi/acpi_memhotplug.ko needs unknown symbol acpi_get_node
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/amd64_edac_mod.ko needs unknown symbol amd_unregister_ecc_decoder
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/amd64_edac_mod.ko needs unknown symbol amd_register_ecc_decoder
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/amd64_edac_mod.ko needs unknown symbol amd_report_gart_errors
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/amd64_edac_mod.ko needs unknown symbol amd_decode_nb_mce
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/cpufreq/freq_table.ko needs unknown symbol strcpy
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/cpufreq/freq_table.ko needs unknown symbol _cpu_pda
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/i7core_edac.ko needs unknown symbol edac_mce_register
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/i7core_edac.ko needs unknown symbol edac_mce_unregister
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/edac/i7core_edac.ko needs unknown symbol __memcpy
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol dma_ops
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol copy_from_user
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol bad_dma_address
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol find_first_bit
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol strcat
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol mem_section
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol copy_to_user
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol strncpy
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol __memcpy
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol strcmp
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/infiniband/hw/ipath/ib_ipath.ko needs unknown symbol alloc_pages_current
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/kernel/microcode.ko needs unknown symbol copy_from_user
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol cpu_to_node
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol copy_from_user
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol copy_user_generic
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol profile_pc
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol copy_to_user
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/oprofile/oprofile.ko needs unknown symbol strcmp
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/kernel/cpufreq/powernow-k8.ko needs unknown symbol strncmp
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/arch/x86_64/kernel/cpufreq/powernow-k8.ko needs unknown symbol _cpu_pda
WARNING: /lib/modules/2.6.18-274.el5.028stab093.2/kernel/drivers/pci/hotplug/shpchp.ko needs unknown symbol strcpy

... please remove ovzkernel...
yum remove ovzkernel 

... and install the ovzkernel package for your architecture:
On x86_64 systems:
yum install ovzkernel.x86_64 

On i686 systems:
yum install ovzkernel.i686 

This should automatically update the GRUB bootloader as well. Anyway, we should open /boot/grub/menu.lst; the first kernel stanza should now contain the new OpenVZ kernel. Make sure that the value of default is 0 so that the first kernel (the OpenVZ kernel) is booted automatically instead of the default CentOS kernel.
vi /boot/grub/menu.lst

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title OpenVZ (2.6.18-274.el5.028stab093.2)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-274.el5.028stab093.2 ro root=/dev/VolGroup00/LogVol00 selinux=0
        initrd /initrd-2.6.18-274.el5.028stab093.2.img
title CentOS (2.6.18-274.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-274.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-274.el5.img
Now we install some OpenVZ user tools:
yum install vzctl vzquota

Open /etc/sysctl.conf and make sure that you have the following settings in it:
vi /etc/sysctl.conf
[...]
net.ipv4.ip_forward = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
[...]
If you need to modify /etc/sysctl.conf, run
sysctl -p
afterwards.

The following step is important if the IP addresses of your virtual machines are from a different subnet than the host system's IP address. If you don't do this, networking will not work in the virtual machines!

Open /etc/vz/vz.conf and set NEIGHBOUR_DEVS to all:
vi /etc/vz/vz.conf
[...]
NEIGHBOUR_DEVS=all
[...]
SELinux needs to be disabled if you want to use OpenVZ. Open /etc/sysconfig/selinux and set the value of SELINUX to disabled:
vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted
Finally, reboot the system:
reboot

If your system reboots without problems, then everything is fine!
Run
uname -r

and your new OpenVZ kernel should show up:
[root@server1 ~]# uname -r
2.6.18-274.el5.028stab093.2
[root@server1 ~]#

2 Using OpenVZ

Before we can create virtual machines with OpenVZ, we need to have a template for the distribution that we want to use in the virtual machines in the /vz/template/cache directory. The virtual machines will be created from that template. You can find a list of precreated templates on http://wiki.openvz.org/Download/template/precreated.
I want to use CentOS 5 in my virtual machines, so I download a CentOS 5 template:
cd /vz/template/cache
wget http://download.openvz.org/template/precreated/centos-5-x86.tar.gz

I will now show you the basic commands for using OpenVZ.
To set up a VPS from the default CentOS 5 template, run:
vzctl create 101 --ostemplate centos-5-x86 --config basic

The 101 must be a uniqe ID - each virtual machine must have its own unique ID. You can use the last part of the virtual machine's IP address for it. For example, if the virtual machine's IP address is 192.168.0.101, you use 101 as the ID.
If you want to have the vm started at boot, run
vzctl set 101 --onboot yes --save

To set a hostname and IP address for the vm, run:
vzctl set 101 --hostname test.example.com --save
vzctl set 101 --ipadd 192.168.0.101 --save

Next we set the number of sockets to 120 and assign a few nameservers to the vm:
vzctl set 101 --numothersock 120 --save
vzctl set 101 --nameserver 8.8.8.8 --nameserver 8.8.4.4 --nameserver 145.253.2.75 --save

(Instead of using the vzctl set commands, you can as well directly edit the vm's configuration file which is stored in the /etc/vz/conf directory. If the ID of the vm is 101, then the configuration file is /etc/vz/conf/101.conf.)

To start the vm, run
vzctl start 101

To set a root password for the vm, execute
vzctl exec 101 passwd

You can now either connect to the vm via SSH (e.g. with PuTTY), or you enter it as follows:
vzctl enter 101

To leave the vm's console, type
exit

To stop a vm, run
vzctl stop 101

To restart a vm, run
vzctl restart 101

To delete a vm from the hard drive (it must be stopped before you can do this), run
vzctl destroy 101

To get a list of your vms and their statuses, run
vzlist -a


To find out about the resources allocated to a vm, run
vzctl exec 101 cat /proc/user_beancounters 

The failcnt column is very important, it should contain only zeros; if it doesn't, this means that the vm needs more resources than are currently allocated to the vm. Open the vm's configuration file in /etc/vz/conf and raise the appropriate resource, then restart the vm.
To find out more about the vzctl command, run
man vzctl 

Source : http://www.howtoforge.com/installing-and-using-openvz-on-centos-5.7-p2

No comments:

Post a Comment