OpenVz installing and using
id : t4fiesjf3r
category : computer
blog : unixlinux
created : 03/13/11 - 16:18:20
  • Add openvz repository :
#cd /etc/yum.repos.d
#rpm --import

  • Install openvz kernel :
#yum search ovzkernel
#yum install ovzkernel

  • Modify /boot/grub/menu.lst to boot on openvz kernel :
#vi /boot/grub/menu.lst
title CentOS (2.6.18-194.26.1.el5.028stab081.1)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.26.1.el5.028stab081.1 ro root=/dev/rootvg/rootlv
        initrd /initrd-2.6.18-194.26.1.el5.028stab081.1.img
title CentOS (2.6.18-194.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/rootvg/rootlv
        initrd /initrd-2.6.18-194.el5.img

  • Install openvz userspace tools :
#yum install vzctl vzquota

  • Modify /etc/sysctl.conf, make sure 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
#sysctl -p

  • Change this setting if containers will be in a different subnet than the host system's IP address :
#vi /etc/vz/vz.conf

  • Don't forget to disable selinux (/etc/sysconfig/selinux)
  • Reboot your system :
#shutdown -r now

  • Check you have booted on the right kernel :
#uname -r2.6.18-194.26.1.el5.028stab081.1

  • Install vzdump :
#rpm -ivh ""
#rpm -ivh perl-LockFile-Simple-0.206-1.el5.rf.noarch.rpm
#rpm -ivh ""

  • Add to /etc/bashrc :
#vi /etc/bashrc
export PERL5LIB=/usr/share/perl5/

  • If you want to use and lv for your /vz do it :
#vi /etc/fstab
/dev/rootvg/vzlv        /vz             ext3    defaults        1 2

  • I suggest to add vzadm, and sudo for adminsitration :
#useradd vzadm
Cmnd_Alias OPENVZ = /usr/sbin/vzcalc,/usr/sbin/vzcfgvalidate,/usr/sbin/vzcpucheck,/usr/sbin/vzctl,/usr/sbin/vzdqcheck,/usr/sbin/vzdqdump,/usr/sbin/vzdqload,/usr/sbin/vzdump,/usr/sbin/vzeventd,/usr/sbin/vzifup-post,/usr/sbin/vzlist,/usr/sbin/vzmemcheck,/usr/sbin/vzmigrate,/usr/sbin/vznetaddbr,/usr/sbin/vznetcfg,/usr/sbin/vzpid,/usr/sbin/vzquota,/usr/sbin/vzrestore,/usr/sbin/vzsplit
#echo "export PATH=$PATH:/usr/sbin" >> /home/vzadm/.bashrc

Vz creation and usage
  • Download template :
#cd /vz/template/cache

  • Create a basic configuration file :
#cd /etc/vz/conf
#cp ve-basic.conf ve-vps.basic.conf-sample

  • Create your vz :
#vzctl create 101 --ostemplate centos-5-i386-default --config vps.basic
Creating container private area (centos-5-i386-default)
Performing postcreate actions
Container private area was created

  • Set IP address and hostname to your vz :
#vzctl set 101 --hostname powerslave --save
Saved parameters for CT 101
#vzctl set 101 --ipadd --save
Saved parameters for CT 101

  • Set nameservers to your vz :
#vzctl set 101 --nameserver <nameserver_ip> --nameserver <nameserver_ip2>
Saved parameters for CT 101

  • Start your vz :
#vzctl start 101
Starting container ...
Container is mounted
Adding IP address(es):
Setting CPU units: 1000
Set hostname: powerslave
File resolv.conf was modified
Container start in progress...

  • Set a root password on your vz :
#vzctl exec 101 passwd

  • You can now access your vz trough ssh :
# ssh -l root
[root@ovzhost conf]#ssh powerslave
The authenticity of host 'powerslave (' can't be established.RSA key fingerprint is fb:58:de:02:48:fe:9e:69:08:74:df:c9:9b:1a:19:35.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'powerslave,' (RSA) to the list of known hosts.
root@powerslave's password:
[root@powerslave ~]#

  • To stop your vz :
#vzctl stop 101

  • To restart your vz :
#vzctrl restart 101

  • To delete your vz (from harddrive)
#vzctl destroy 101

*To get a list of your vz and statues :
#vzlist -a
       101         17 running    powerslave

Vz dump and restore
  • Snapshot a vz (only available if /vz is lvm):
#vzdump --dumpdir /root/backup_vz --snapshot 101

  • Restore :
#vzctl stop 101
#vzctl detroy 101
#vzrestore /root/backup_vz/<tar_file_of_your_backup> 101
#vzctl start 101

  • Containers can be moved from one host to another using this commands.
Vz tunning