2012-03-17

OpenVZ Centos/RedHat templates creation

I've recently found this article which is a good start on how to start to create a OpenVZ template from RPM based distribution such as RHEL, Fedora or Centos.

I'm working with Proxmox and there was some modification to do. 

So I decided to rewrite the tutorial, but it is highly inspired from the article. I recommend you to read the article before starting the following steps.

Installation

First I've installed on a VM Centos 6 using the netinstall.iso
I've chosen only the minimum installation.
The disk partitioning was done without LVM, only two partition 1GB swap, 9GB for / (formatted as ext4)

The only packages I've installed after the installation are openssh and openssh-server.
I've also ran an yum update and yum upgrade.

I've installed the Proxmox hypervisor (2.0RC), on a different VM using the ISO.

After the installation I've connected the hard disk of the Centos VM to the Hypervisor VM.

On the hypervisor, I suggest you to make a symbolic link to /vz so that you won't have to modify most of the scripts
ln -s /var/lib/vz /vz

Transferring to OpenVZ

Create a variable to identify the container ID
CTID=110
Make sure it is well assigned
echo $CTID

The following step will erase any existing container that use the $CTID number
rm -rf /vz/private/$CTID
mkdir /vz/private/$CTID 


For the rsync part, I was working on an hypervisor installed in a VM, I decided to connect the virtual hard drive of my newly installed minimal Centos 6 installation to the hypervisor instead of invoking rsync remotely 

I've mounted the Centos VM hard disk on the hypervisor on /media/centos6 and I've used this command to copy the file to the oz machine :
rsync -arvpz --numeric-ids --exclude=/dev --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/tmp  /media/centos6/ /vz/private/$CTID/

ftab modification
echo "none /dev/pts devpts rw 0 0" >  /vz/private/$CTID/etc/fstab
echo "proc /proc proc defaults 0 0" >> /vz/private/$CTID/etc/fstab

create directories
mkdir /vz/private/$CTID/dev
mkdir /vz/private/$CTID/mnt
mkdir /vz/private/$CTID/proc
mkdir /vz/private/$CTID/sys
mkdir /vz/private/$CTID/tmp
chmod 1777 /vz/private/$CTID/tmp 

I had a problem with the MAKEDEV command, the one provided by debian 6.0.4 (Used by Proxmox Hypervisor) does not work as the one described in the original HOWTO.
mkdir /vz/private/$CTID/dev/pts
mknod -m 600 /vz/private/$CTID/dev/console c 5 1
mknod -m 666 /vz/private/$CTID/dev/tty c 5 0
mknod -m 666 /vz/private/$CTID/dev/full c 1 7
mknod -m 600 /vz/private/$CTID/dev/initctl p
mknod -m 666 /vz/private/$CTID/dev/null c 1 3
mknod -m 666 /vz/private/$CTID/dev/ptmx c 5 2 
mknod -m 666 /vz/private/$CTID/dev/random c 1 8
mknod -m 444 /vz/private/$CTID/dev/urandom c 1 9 
mknod -m 666 /vz/private/$CTID/dev/zero c 1 5
mknod -m 660 /vz/private/$CTID/dev/pts/0 c 136 0
mknod -m 660 /vz/private/$CTID/dev/pts/1 c 136 1
mknod -m 000 /vz/private/$CTID/dev/pts/ptmx c 5 2
chown root.tty /vz/private/$CTID/dev/pts/0
chown root.tty /vz/private/$CTID/dev/pts/1

If you need to, set the interfaces to ONBOOT="no"
sed -i -e 's/ONBOOT="yes"/ONBOOT="no"/g'  /vz/private/$CTID/etc/sysconfig/network-scripts/ifcfg-eth*

remove HWADDR from the interfaces files
sed -i -e 's/^\(HWADDR=.*\)$//g' /vz/private/$CTID/etc/sysconfig/network-scripts/ifcfg-eth*

Change PROMPT=yes to PROMPT=no in sysconfig/init
sed -i -e 's/PROMPT=yes/PROMPT=no/g' /vz/private/$CTID/etc/sysconfig/init

Commenting out console from rc.conf
sed -i -e 's/^\(console.*\)$/#\1/g' /vz/private/$CTID/etc/init/rc.conf 

Making a default container configuration


(change centos-6-x86_64 for whatever distro you are creating a template for)
echo OSTEMPLATE=\"centos-6-x86_64\" > /etc/vz/conf/$CTID.conf
vzctl set $CTID --save --kmemsize 256M
vzctl set $CTID --save --lockedpages 256M
vzctl set $CTID --save --privvmpages unlimited
vzctl set $CTID --save --shmpages unlimited
vzctl set $CTID --save --numproc unlimited
vzctl set $CTID --save --physpages 512M
vzctl set $CTID --save --vmguarpages 0:unlimited
vzctl set $CTID --save --oomguarpages 0:unlimited
vzctl set $CTID --save --numtcpsock unlimited
vzctl set $CTID --save --numflock unlimited
vzctl set $CTID --save --numpty unlimited
vzctl set $CTID --save --numsiginfo unlimited
vzctl set $CTID --save --tcpsndbuf  unlimited
vzctl set $CTID --save --tcprcvbuf unlimited
vzctl set $CTID --save --othersockbuf unlimited
vzctl set $CTID --save --dgramrcvbuf unlimited
vzctl set $CTID --save --numothersock unlimited
vzctl set $CTID --save --numfile unlimited
vzctl set $CTID --save --dcachesize 116M:128M
vzctl set $CTID --save --numiptent unlimited
vzctl set $CTID --save --swappages 512M

vzctl set $CTID --save --quotatime 0
vzctl set $CTID --save --diskinodes 800000:880000
vzctl set $CTID --save --diskspace 1G

Enter in your OpenVZ container for cleanup

vzctl start $CTID
vzctl enter $CTID

Theses steps needs to be done from INSIDE your OpenVZ

Fix df 

rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab 

remove ssh host keys

rm -f /etc/ssh/ssh_host_*

remove boot and kernel

rm -rf /boot/*.*
for i in `rpm -q kernel`; do rpm -e --nodeps $i; done


Remove unwanted packages (tested on Centos 6.2)

Check
rpm -qa | egrep 'grub|fs|firmware|libdrm|b43-openfwwf|dracut|plymouth|qpid'
And proceed...
rpm -qa | egrep 'grub|fs|firmware|libdrm|b43-openfwwf|dracut|plymouth|qpid' | while read l; do rpm -e --nodeps $l; done

clean some other dependencies and unwanted stuff (might be centos specific)
yum remove matahari matahari-lib qpid-cpp-server

Clean rpm db :
yum clean all


Creating the template


Exit the OpenVZ container with "exit" and stop it
exit
vzctl stop $CTID 


Final cleanup


rm /vz/private/$CTID/etc/resolv.conf
touch /vz/private/$CTID/etc/resolv.conf
rm /vz/private/$CTID/etc/hostname

Go to the CTID directory

cd /vz/private/$CTID

Create the tar file

tar --numeric-owner -zcf /vz/template/cache/centos-6.2-standard-x86_64.tar.gz .

Test the template


create a sample file (or get it from elsewere)

cp /etc/pve/openvz/$CTID.conf /etc/pve/openvz/ve-testOZ.conf-sample
sed -i -e 's/^OSTEMPLATE.*//g'  /etc/pve/openvz/ve-testOZ.conf-sample

Test it!


Do it using the command line or the web based interface.
vzctl create 123456 --ostemplate  centos-6.2-standard-x86_64  --config testOZ


1 comment:

  1. Based on your article, I have just uploaded an article "Create OpenVZ RHEL 5.5 Template With Minimal Xwindows"
    https://awarmanf.wordpress.com/2014/12/12/create-openvz-rhel-5-5-template-with-minimal-xwindows/#more-1721

    Thanks for your great article.

    ReplyDelete