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 /var/lib/vz/template/cache directory. The virtual machines will be created from that template. You can find a list of precreated templates on wiki.openvz.org
I want to use Ubuntu 8.04 in my virtual machines, so I download an Ubuntu 8.04 template in this case:
cd /var/lib/vz/template/cache wget http://download.openvz.org/template/precreated/ubuntu-8.04-x86.tar.gz
I will now show you the basic commands for using OpenVZ.
To set up a VPS from the Ubuntu 8.04 template
vzctl create 1101 --ostemplate ubuntu-8.04-x86 --config vps.basic
To have the vm started at boot
vzctl set 1101 --onboot yes --save
To set a hostname and IP address for the vm
vzctl set 1101 --hostname test.dw.lan --save vzctl set 1101 --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 1101 --numothersock 120 --save vzctl set 1101 --nameserver 192.168.0.31 --nameserver 151.8.12.158 --save
To start the vm
vzctl start 1101
To set a root password for the vm
vzctl exec 1101 passwd
To connect to the vm via SSH (e.g. with PuTTY), or you enter it as follows
vzctl enter 1101
To stop a vm
vzctl stop 1101
To restart a vm
vzctl restart 1101
To delete a vm from the hard drive (it must be stopped before you can do this)
vzctl destroy 1101
To get a list of your vms and their statuses
vzlist -a
To find out about the resources allocated to a vm
vzctl exec 1101 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