How to create virtual machines using OpenVZ

Learn how to create and manage virtual machines using OpenVZ, an open source, container-based alternative to hypervisor-based virtualization that runs on Linux.

In the first part of this series, I described how to install OpenVZ, which is an open source, container-based virtualization

product that is an alternative to hypervisor-based virtualization that runs on Linux.

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:
cd /vz/template/cache
wget http://download.openvz.org/template/precreated/contrib/
centos-5-i386-default.tar.gz

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 193.79.237.39 --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.
#

ONBOOT="yes"

# UBC parameters (in form of barrier:limit)
KMEMSIZE="14372700:14790164"
LOCKEDPAGES="256:256"
PRIVVMPAGES="65536:69632"
SHMPAGES="21504:21504"
NUMPROC="240:240"
PHYSPAGES="0:9223372036854775807"
VMGUARPAGES="33792:9223372036854775807"
OOMGUARPAGES="26112:9223372036854775807"
NUMTCPSOCK="360:360"
NUMFLOCK="188:206"
NUMPTY="16:16"
NUMSIGINFO="256:256"
TCPSNDBUF="1720320:2703360"
TCPRCVBUF="1720320:2703360"
OTHERSOCKBUF="1126080:2097152"
DGRAMRCVBUF="262144:262144"
NUMOTHERSOCK="360:360"
DCACHESIZE="3409920:3624960"
NUMFILE="9312:9312"
AVNUMPROC="180:180"
NUMIPTENT="128:128"

# Disk quota parameters (in form of softlimit:hardlimit)
DISKSPACE="1048576:1153024"
DISKINODES="200000:220000"
QUOTATIME="0"

# CPU fair sheduler parameter
CPUUNITS="1000"

VE_ROOT="/vz/root/$VEID"
VE_PRIVATE="/vz/private/$VEID"
OSTEMPLATE="centos-5-i386-default"
ORIGIN_SAMPLE="vps.basic"
HOSTNAME="nuuk.example.com"
IP_ADDRESS="192.168.1.150"
NAMESERVER="193.79.237.39"

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

Sander van Vugt, Contributor
Sander van Vugt is an independent trainer and consultant based in the Netherlands. Van Vugt is an expert in Linux high availability, virtualization and performance and has completed several projects that implement all three. He is also the writer of various Linux-related books, such as Beginning the Linux Command Line, Beginning Ubuntu Server Administration and Pro Ubuntu Server Administration.


This was first published in September 2009

Dig deeper on VMware management tools

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close