How-To: OpenVZ VPS configuration

First, you need an OS template. They are located in /var/lib/vz/template/cache/
Pre-created templates can be found here:
I will use debian-minimal template, but the same goes for others.

Download template and save it in /var/lib/vz/template/cache/

cd /var/lib/vz/template/cache/

Create the container (101 is VPS ID, it sould be integer number above 100) and assign it an ip address and hostname:

vzctl create 101 --ostemplate debian-5.0-i386-minimal
vzctl set 101 --ipadd --nameserver --hostname vps1 --save

VPS file system is located under /var/lib/vz/root/101/ (when VPS is running) and its private area under /var/lib/vz/private/101/

Start the VPS:

vzctl start 101

If you get this error:

vps-net_add WARNING: Function proxy_arp for eth0 is set to 0.

add following line to /etc/sysctl.conf (change eth0 with your interface name)

net.ipv4.conf.eth0.proxy_arp = 1

and run:

sysctl -p

Enter into VPS:

vzctl enter 101


Setting memory limit:

The unit of measure is memory pages (4KB on x86 and x86_64 processors).
The simplest way to convert MB into mem pages is to multiply number of MB with 256.
e.g. 128MB = 128*256 = 32768 pages

To set 128MB guaranteed and 256MB of burstable memory:

vzctl set 101 --vmguarpages 32768:2147483647 --save
vzctl set 101 --privvmpages 64000:65536 --save

512MB guaranteed and 1GB burstable:

vzctl set 101 --vmguarpages 131072:2147483647 --save
vzctl set 101 --privvmpages 256000:262144 --save

Disk qouta

To view current values for disk qouta:

vzquota stat 101

diskspace parameter is the number of 1K blocks ({GB} * 1048576).
To change disk space quota to 2GB:

vzctl set 101 --diskspace 2097152:2306048 --save

diskinodes parameter is roughly the total number of files and directories on system. Default value is 200000:220000.
To change it, run:

vzctl set 101 --diskinodes 400000:440000 --save

You can set/reset user password with:

vzctl set 101 --userpasswd user:password

Bind mounts

To make available a directory from HW node to container, you can either create a container script (/etc/vz/conf/101.mount), or global mount script for all containers (/etc/vz/conf/vps.mount)
Put this content into the file:

source /etc/vz/vz.conf
source ${VE_CONFFILE}
mkdir -p ${VE_ROOT}/data
mount -n --bind /data ${VE_ROOT}/data

Make script executable:

chmod +x /etc/vz/conf/vps.mount

Unmount script (/etc/vz/conf/101.umount or /etc/vz/conf/vps.umount) is not really necessary, because all mount points will be automatically unmounted on container stop. That's why we added "-n" switch to mount command (so it won't write mount points to /etc/mtab).

Useful tools / links