## Creating New Container ##

### choose the new Container ID; 
- Every Container has a numeric ID, also known as Container ID
- ID 0 = Host, Hardware Node itself
- ID 1-100 = reserved
- An approach is to follow some pattern of Container IP addresses

### choose the OS template to use for the Container; 
- # ls /vz/template/cache/  <- This is Sample template directory
   centos-4-x86.tar.gz       fedora-7-x86.tar.gz     suse-10.3-x86.tar.gz

- You have to remove the .tar.gz suffix from the name to make it a valid OS template name.
   For example, centos-4-x86 is a valid OS template name.

### create the Container itself. 
- The private area is
 directory containing the actual files of the given Container; it is usually residing in /vz/private/CTID/.
 mounted to the /vz/root/CTID directory on the Hardware Node
 provides Container users with a complete Linux file system tree.

- Sample configuration files are residing in the /etc/vz/conf directory and have names with the following mask: ve-configname.conf-sample.

- Create a new Container by typing the following string: 
# vzctl create 101 --ostemplate centos-5-x86 --config basic
 -> Create a Container with ID 101, the private area based on the centos-5-x86 OS template, and configuration parameters taken from the ve-basic.conf-sample sample configuration file.

- vzctl will take default values from the global OpenVZ configuration file (/etc/vz/vz.conf) if no configure or template. 
   DEF_OSTEMPLATE="centos-5-x86"
   CONFIGFILE="basic"
# vzctl create 101

### Configuring Container

- Setting Container startup parameters; 
# vzctl set 101 --onboot yes --save

- Setting Container network parameters; 
# vzctl set 101 --hostname server101.mydomain.com --save
# vzctl set 101 --ipadd 10.0.186.1 --save
# vzctl set 101 --nameserver 192.168.1.165 --save
 Can issue the above commands when the Container is running. Without --save option, it is not persist

- SSH daemon shall be running inside the Container

To check whether SSH is running inside the Container, use vzctl exec, which allows executing any commands in the Container context. 
# vzctl start 101
[This command starts Container 101, if it is not started yet]
# vzctl exec 101 service sshd status
sshd is stopped
# vzctl exec 101 service sshd start
Starting sshd: [ OK ]
# vzctl exec 101 service sshd status
sshd (pid 3801) is running...

- Setting Container user passwords; 
By default, the root account is locked in a newly created Container, and you cannot log in. In order to log in to the Container, it is necessary to create a user account inside the Container and set a password for this account, or unlock the root account.
# vzctl set 101 --userpasswd root:test

- Setting Name for Container and Extended Information about Container 
# vzctl set 101 --name  --save
# vzlist -o name 101    <- find out what name is assigned
# vzctl set 101 --description "Container 101
> owner - Mr. Johnson
> purpose - hosting the MySQL server" --save
# vzlist -o description 101

## Starting, Stopping, Restarting, and Querying Status of Container

# vzctl start   101
# vzctl stop    101
# vzctl stop    101 --fast
# vzctl restart 101
# vzctl status 101
# vzlist 101
# cat /proc/vz/veinfo

# vzlist -a
By default, only running Containers are shown.

### Migrating Container (offline)

For the command to be successful, a direct SSH connection (on port 22) should be allowed between the Source and Destination Nodes.
# vzmigrate  

### Disable & Suspending Container

# vzctl set 101 --disabled yes --save
# vzctl set 101 --disabled no  --save
After the Container disabled, the Container user will not be able to start it again until you enable it 
# vzctl start 103 --force

# vzctl chkpnt 101
The /vz/dump/Dump.101 file containing the entire state of Container 101 is created and the Container itself is stopped. 
# vzctl restore 101

### Delete Container
 Delete a Container by removes the Container private area completely and renames the Container configuration file and action scripts by appending the .destroyed suffix to them. 
 If you do not need the backup copy of the Container configuration files (with the .destroyed suffix), you may delete them manually.

# vzctl destroy 101
Container is currently running. Stop it first.
# vzctl stop 101
Stopping container ...
Container was stopped
Container is unmounted
# vzctl destroy 101
Destroying container private area: /vz/private/101
Container private area was destroyed
# ls /etc/vz/conf/101.*
/etc/vz/conf/101.conf.destroyed
# vzctl status 101
CTID 102 deleted unmounted down

### Clone Container
If you need to create an exact copy of an existing VE, these are the steps to make. 
# OLDVE=222 NEWVE=333 # Just an example
# vzctl stop $OLDVE
# mkdir /vz/root/$NEWVE
# cp /etc/vz/conf/$OLDVE.conf /etc/vz/conf/$NEWVE.conf
# mkdir /vz/private/$NEWVE
# pushd /vz/private/$OLDVE; tar c --numeric-owner * | tar x --numeric-owner -C /vz/private/$NEWVE; popd
# vi /etc/vz/conf/$NEWVE.conf # Change the IP_ADDRESS
# vzctl start $NEWVE; vzctl start $OLDVE
In case you are making bridge don't forget, before start the VEs,
 to change host_ifname=veth$OLDVE by host_ifname=veth$NEWVE in the /etc/vz/conf/$NEWVE.conf file

### Running Commands in Container 
Use the vzctl exec command for running a command inside the Container with the given ID.
# vzctl exec 103 /etc/init.d/sshd status
openssh-daemon is stopped
# vzctl exec 103 /etc/init.d/sshd start
Starting sshd: [  OK  ]
# vzctl exec 103 /etc/init.d/sshd status
openssh-daemon (pid  9899) is running...

### Running Commands in ALL Container

If you wish to execute a command in all running Containers, you can use the following script: 
# for CT in $(vzlist -H -o ctid); do echo "== CT $CT =="; vzctl exec $CT command; done

where command is the command to be executed in all the running Containers. For example: 
# for CT in $(vzlist -H -o ctid); do echo "== CT $CT =="; vzctl exec $CT uptime; done
== CT 103 ==
 15:17:19 up 13 min,  0 users,  load average: 0.00, 0.00, 0.00
== CT 123123123 ==
 15:17:19 up 22:00,  0 users,  load average: 0.00, 0.00, 0.00

Labels: my.