To create an OpenVZ virtual machine, you need a template for the virtual operating system that you want to create. Templates are available for all major Linux distributions, and you can download a list of templates. For instance, if you want to use a CentOS 5 virtual machine, you would download the CentOS 5 template with the following commands:
Once the template is downloaded, you can use it to start one or more virtual machines. To start a virtual machine (VM) based on the template you have just downloaded, use the following command:
vzctl create 150 --ostemplate centos-5-i386-default --config vps.basic
Here "vzctl" is the master command that allows you to create and manage virtual machines. Like the IP command, vzctl is used with subcommands to create a virtual machine. Each VM gets its own unique ID. It might be a good idea to use the last part of the IP address for this unique ID, hence 150 in this example. Next, specify which template to use. The template contains a bare basic file that you need to fill with its own configuration. The "--config" option makes sure that a configuration file is created for this virtual machine. The configuration file will be in the directory "/etc/vz/conf." Every virtual machine gets its own configuration file. To manage the virtual machine, you can edit this file directly, but you can also pass different parameters on the command line to change the VM's properties.
When you create a virtual machine in this way, it will not restart automatically when you reboot your computer. To make sure that also happens, use the following command:
vzctl set 150 --onboot yes --save
Next, start entering the other parameters that you want to use in the virtual machine. This includes at least the IP configuration, which you can set with the following commands. Change the parameters used in these commands to match your current configuration:
vzctl set 150 --hostname nuuk.example.com --save
vzctl set 150 --ipadd 192.168.1.150 --save
vzctl set 150 --nameserver 184.108.40.206 --save
At this point, you have created a fairly decent basic configuration, which is stored in /etc/vz/conf/150.conf (given that this example used 150 as the ID of the virtual machine). Listing 2 shows what this file looks like at this point.
Listing 2: Virtual Machine Configuration Written to a Configuration File
[root@centos conf]# cat 150.conf
# Copyright (C) 2000-2008, Parallels Inc. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the license or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 U.S.A.
# UBC parameters (in form of barrier:limit)
# Disk quota parameters (in form of softlimit:hardlimit)
# CPU fair sheduler parameter
Now that you have created the virtual machine configuration, it's time to start the machine using the following command: vzctl start 150
Next, make sure to set the root password:
vzctl exec 150 passwd
The command "vzctl passwd" will prompt you to set the root password. At this point, the virtual container is ready for usage. You can now contact it by using Secure Shell (SSH) or directly from the console of the host operating system by entering the following:
vzctl enter 150
If you have accessed the virtual machine using "vzctl enter," you can quit it by typing "exit."
Basic OpenVZ virtual machine management
Now that the virtual machine is up and running, it's time to look at some of the commands used to manage it. As you'll see, those commands look a lot like the commands that you would use to manage a Xen virtual machine. First, you can see a list of all the virtual machines that are currently available by using the command "vzlist -a." [root@centos conf]# vzlist -a
CTID NPROC STATUS IP_ADDR HOSTNAME
150 19 running 192.168.1.150 nuuk.example.com
As you can see, this command displays the current status of each VM (just one machine is running in this example). Based on this information, you can stop a virtual machine (vzctl stop 150), restart it (vzctl restart 150) or completely delete it from the host machine's hard drive (vzctl destroy 150). The last command works only if you have first stopped the virtual machine.
This article has explained how to set up virtualization using OpenVZ container-based virtualization. With this information, you should be able to determine if this virtualization approach can benefits the way virtualization is handled in your organization.
About the expert
This was first published in September 2009