How to create virtual machines using OpenVZ

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

    Requires Free Membership to View

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/

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 --save
vzctl set 150 --nameserver --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
# 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
150 19 running 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 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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.