OpenVZ Container Administration

11 min read

In this article by Mark Furman, the author of OpenVZ Essentials, we will go over the various aspects of OpenVZ administration. Some of the things we are going to go over in this article are as follows:

  • Listing the containers that are running on the server
  • Starting, stopping, suspending, and resuming containers
  • Destroying, mounting, and unmounting containers
  • Setting quota on and off
  • Creating snapshots of the containers in order to back up and restore the container to another server

(For more resources related to this topic, see here.)

Using vzlist

The vzlist command is used to list the containers on a node. When you run vzlist on its own without any options, it will only list the containers that are currently running on the system:

vzlist

In the previous example, we used the vzlist command to list the containers that are currently running on the server.

Listing all the containers on the server

If you want to list all the containers on the server instead of just the containers that are currently running on the server, you will need to add -a after vzlist. This will tell vzlist to include all of the containers that are created on the node inside its output:

vzlist -a

In the previous example, we used the vzlist command with an -a flag to tell vzctl that we want to list all of the containers that have been created on the server.

The vzctl command

The next command that we are going to cover is the vzctl command. This is the primary command that you are going to use when you want to perform tasks with the containers on the node. The initial functions of the vzctl command that we will go over are how to start, stop, and restart the container.

Starting a container

We use vzctl to start a container on the node. To start a container, run the following command:

vzctl start 101

Starting Container ...
Setup slm memory limit
Setup slm subgroup (default)
Setting devperms 20002 dev 0x7d00
Adding IP address(es) to pool:
Adding IP address(es): 192.168.2.101
Hostname for Container set: gotham.example.com
Container start in progress...

In the previous example, we used the vzctl command with the start option to start the container 101.

Stopping a container

To stop a container, run the following command:

vzctl stop 101
Stopping container ...
Container was stopped
Container is unmounted

In the previous example, we used the vzctl command with the stop option to stop the container 101.

Restarting a container

To restart a container, run the following command:

vzctl restart 101
Stopping Container ...
Container was stopped
Container is unmounted
Starting Container...

In the previous example, we used the vzctl command with the restart option to restart the container 101.

Using vzctl to suspend and resume a container

The following set of commands will use vzctl to suspend and resume a container. When you use vzctl to suspend a container, it creates a save point of the container to a dump file. You can then use vzctl to resume the container to the saved point it was in before the container was suspended.

Suspending a container

To suspend a container, run the following command:

vzctl suspend 101

In the previous example, we used the vzctl command with the suspend option to suspend the container 101.

Resuming a container

To resume a container, run the following command:

vzctl resume 101

In the previous example, we used the vzctl command with the resume option to resume operations on the container 101.

In order to get resume or suspend to work, you may need to enable several kernel modules by 
running the following:
modprobe vzcpt
modprobe vzrst

Destroying a container

You can destroy a container that you created by using the destroy argument with vzctl. This will remove all the files including the configuration file and the directories created by the container. In order to destroy a container, you must first stop the container from running.

To destroy a container, run the following command:

vzctl destroy 101
Destroying container private area: /vz/private/101

Container private area was destroyed.

In the previous example, we used the vzctl command with the destroy option to destroy the container 101.

Using vzctl to mount and unmount a container

You are able to mount and unmount a container’s private area located at /vz/root/ctid, which provides the container with root filesystem that exists on the server. Mounting and unmounting containers come in handy when you have trouble accessing the filesystem for your container.

Mounting a container

To mount a container, run the following command:

vzctl mount 101

In the previous example, we used the vzctl command with the mount option to mount the private area for the container 101.

Unmounting a container

To unmount a container, run the following command:

vzctl umount 101

In the previous example, we used the vzctl command with the umount option to unmount the private area for the container 101.

Disk quotas

Disk quotas allow you to define special limits for your container, including the size of the filesystem or the number of inodes that are available for use.

Setting quotaon and quotaoff for a container

You can manually start and stop the containers disk quota by using the quotaon and quotaoff arguments with vzctl.

Turning on disk quota for a container

To turn on disk quota for a container, run the following command:

vzctl quotaon 101

In the previous example, we used the vzctl command with the quotaon option to turn disk quota on for the container 101.

Turning off disk quota for a container

To turn off disk quota for a container, run the following command:

vzctl quotaoff 101

In the previous example, we used the vzctl command with the quotaoff option to turn off disk quota for the container 101.

Setting disk quotas with vzctl set

You are able to set the disk quotas for your containers on your server using the vzctl set command. With this command, you can set the disk space, disk inodes, and the quota time.

To set the disk space for container 101 to 2 GB, use the following command:

vzctl set 101 --diskspace 2000000:2200000 --save

In the previous example, we used the vzctl set command to set the disk space quota to 2 GB with a 2.2 GB barrier. The two values that are separated with a : symbol and are the soft limit and the hard limit. The soft limit in the example is 2000000 and the hard limit is 2200000. The soft limit can be exceeded up to the value of the hard limit. The hard limit should never exceed its value. OpenVZ defines soft limits as barriers and hard limits as limits.

To set the inode disk for container 101 to 1 million inodes, use the following command:

vzctl set 101 --diskinodes 1000000:1100000 --save

In the previous example, we used the vzctl set command to set the disk inode limits to a soft limit or barrier of 1 million inodes and a hard limit or limit or 1.1 million inodes.

To set the quota time or the period of time in seconds that the container is allowed to exceed the soft limit values of disk quota and inode quota, use the following command:

vzctl set 101 --quotatime 900 --save

In the previous example, we used the vzctl command to set the quota time to 900 seconds or 15 minutes. This means that once the container soft limit is broken, you will be able to exceed the quota to the value of the hard limit for 15 minutes before the container reports that the value is over quota.

Further use of vzctl set

The vzctl set command allows you to make modifications to the container’s config file without the need to manually edit the file. We are going to go over a few of the options that are essential to administer the node.

–onboot

The –onboot flag allows you to set whether or not the container will be booted when the node is booted.

To set the onboot option, use the following command:

vzctl set 101 –onboot

In the previous example, we used the vzctl command with the set option and the –onboot flag to enable the container to boot automatically when the server is rebooted, and then saved to the container configuration file.

–bootorder

The –bootorder flag allows you to change the boot order priority of the container. The higher the value given, the sooner the container will start when the node is booted.

To set the bootorder option, use the following command:

vzctl set 101 --bootorder 9 --save

In the previous example, we used the vzctl command with the set option and the bootorder flag to tell that we would like to change the priority of the order that the container is booted in, and then we save the option to the container’s configuration file.

–userpasswd

The –userpasswd flag allows you to change a user’s password that belongs to the container. If the user does not exist, then the user will be created.

To set the userpasswd option, use the following command:

vzctl set 101 --userpasswd admin:changeme

In the previous example, we used the vzctl command with the set option and the –userpasswd flag and change the password for the admin user to the password changeme.

–name

The –name flag allows you to give the container a name that when assigned, can be used in place of the CTID value when using vzctl. This allows for an easier way to memorize your containers. Instead of focusing on the container ID, you will just need to remember the container name to access the container.

To set the name option, use the following command:

vzctl set 101 --name gotham --save

In the previous example, we used the vzctl command with the set option to set our container 101 to use the name gotham and then save the changes to containers configuration file.

–description

The –description flag allows you to add a description for the container to give an idea of what the container is for.

To use the description option, use the following command:

vzctl set 101 --description "Web Development Test Server" --save

In the previous example, we used the vzctl command with the set option and the –description flag to add a description of the container “Web Development Test Server”.

–ipadd

The –ipadd flag allows you to add an IP address to the specified container.

To set the ipadd option, use the following command:

vzctl set 101 --ipadd 192.168.2.103 --save

In the previous example, we used the vzctl command with the set option and the –ipadd flag to add the IP address 192.168.2.103 to container 101 and then save the changes to the containers configuration file.

–ipdel

The –ipdel flag allows you to remove an IP address from the specified container.

To use the ipdel option, use the following command:

vzctl set 101 --ipdel 192.168.2.103 --save

In the previous example, we used the vzctl command with the set option and the –ipdel flag to remove the IP address 192.168.2.193 from the container 101 and then save the changes to the containers configuration file.

–hostname

The –hostname flag allows you to set or change the hostname for your container.

To use the hostname option, use the following command:

vzctl set 101 --hostname gotham.example.com --save

In the previous example, we used the vzctl command with the set option and the –hostname flag to change the hostname of the container to gotham.example.com.

–disable

The –disable flag allows you to disable a containers startup. When this option is in place, you will not be able to start the container until this option is removed.

To use the disable option, use the following command:

vzctl set 101 --disable

In the preceding example, we used the vzctl command with the set option and the –disable flag to prevent the container 101 from starting and then save the changes to the container’s configuration file.

–ram

The –ram flag allows you to set the value for the physical page limit of the container and helps to regulate the amount of memory that is available to the container.

To use the ram option, use the following command:

vzctl set 101 --ram 2G --save

In the previous example, we set the physical page limit to 2 GB using the –ram flag.

–swap

The –swap flag allows you to set the value of the amount of swap memory that is available to the container.

To use the swap option, use the following command:

vzctl set 101 --swap 1G --save

In the preceding example, we set the swap memory limit for the container to 1 GB using the –swap flag.

Summary

In this article, we learned to administer the containers that are created on the node by using the vzctl command, and the vzlist command to list containers on the server. The vzctl command has a broad range of flags that can be given to it to allow you to perform many actions to a container.

It allows you to start, stop, and restart, create, and destroy a container. You can also suspend and unsuspend the current state of the container, mount and unmount a container, issue changes to the container’s config file by using vzctl set.

Resources for Article:


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Harnessing Tech for Good to Drive Environmental Impact

At Packt, we are always on the lookout for innovative startups that are not only…

2 months ago

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago