CentOS OpenVZ Container Operations

When the host node is booted up, the containers should be started as well.

These are the basic commends for operating OpenVZ containers:

Create or destroy a container

Here is script that you might find useful:

 

#!/bin/bash

if [ ! $3 ] ; then
  echo Usage: $0 \<CTID\> \<container-ip\> \<ostemplate\>
  exit 0
fi

CTID=$1 # something like 101, 102, etc
CTIP=$2  # something like 192.168.1.101
OSTEMPLATE=$3 # something like centos-4-i386, centos-4-i386-minimal, etc

HOSTNAME="`hostname`-$CTID"

/usr/sbin/vzctl create $CTID --ostemplate $OSTEMPLATE
/usr/sbin/vzctl set $CTID --ipadd $CTIP --save
/usr/sbin/vzctl set $CTID --hostname $HOSTNAME --save
/usr/sbin/vzctl set $CTID --nameserver 192.168.1.254 --save
/usr/sbin/vzctl set $CTID --capability sys_time:on --save
/usr/sbin/vzctl start $CTID

# correct the timezone
# link to your own timezone if EDT/EST is not yours.
/usr/sbin/vzctl exec $CTID mv /etc/localtime /etc/localtime.old
/usr/sbin/vzctl exec $CTID ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
 

 To destroy a container, do this:

[host-node]# /usr/sbin/vzctl stop CTID
[host-node]# /usr/sbin/vzctl destroy CTID

Disk space

If you don't want to impose disk space limit to each VE, you can edit the file /etc/vz/vz.conf, and add or correct the line as below:

DISK_QUOTA=no

Otherwise, you can stop the VE, then run the command below from the HN:

[host-node]# /usr/sbin/vzctl set $CTID --diskspace 10G:11G --save

Then start the VE again.

 

Enter to and exit from the container

To enter container give the following command:

[host-node]# /usr/sbin/vzctl enter CTID
entered into container CTID
[container]#

To exit from container, just type exit and press Enter:

[container]# exit
exited from container VEID
[host-node]#

Check container processes

[host-node]# /usr/sbin/vzctl exec CTID ps ax

Install package into container

[host-node]# vzyum CTID install <package-name>

Most likely, your first package to be installed is yum:

[host-node]# vzyum CTID install yum

 

Container config

You can find the configuration file for a container in /etc/vz/conf/<CTID>.conf

Additional commands

To see the current containers:

# vzlist -a

Other commands: vzpkgadd    vzpkgcache  vzpkgls     vzpkgrm