OpenVZ Custom OStemplate for centos 5

Creating OpenVZ Custom OStemplate for centos 5

First create a test vps with precreated templates which you will get from openvz website

The templates need to be downloaded to the folder “vz/template/cache”

   vzctl create VEID --ostemplate centos-5-x86-64

Now start and enter the vps

  vzctl start VEID(I am taking it as 100)
  vzctl enter 100

Now install cpanel, configure apache,phpand mysql according to your needs, do basic hardening and also install other softwares like maldet,htscaner which we usually do while setting up a new server.

Now start creating the custom OStemplate from the intalled VPS.

For that we need to make sure that we are inside the VPS. Then execute the following steps from the VPS.

1. First, create a file called /tmp/exclude and add the following lines to it:

   .bash_history 
   lost+found 
   /dev/* 
   /mnt/* 
   /tmp/* 
   /proc/* 
   /sys/* 
   /usr/src/*

2,Now, tar the OS file up by typing:

      tar --numeric-owner –czvf /tmp/centos-5.0-<ARCH>-<DISTRO>-image.tar.gz –X /tmp/exclude / 

      Eg:tar --numeric-owner -czvf  /tmp/centos-5-x86-64-custom-image.tar.gz  -X /tmp/exclude / 

      where <ARCH> represents the system architecture (i386 or x86_64 ,x86 ) and <DISTRO> represents the distribution 
      (default, minimal, custom etc.)

Now the OS templates will get tared without the file /tmp/exclude

3, Then go to Node server.

4,Now transfer /tmp/centos-5–x86-64-custom-image.tar.gz file over to the OpenVZ server into /vz/template/cache folder.

(from the node you can find the VPS contents file in /vz/private/VEID. If VEID is 100 then you can find a folder named 100 in /vz/private/ directory)

     cp -p  /vz/private/100/tmp/centos-5--x86-64-custom-image.tar.gz /vz/template/cache

5, On the OpenVZ server create a “dummy” container by creating a folder called /vz/private/VEID. You can choose any unused VEID. I am taking 101 as the VEID

      mkdir /vz/private/101

6, Now copy the sample conf file to /etc/vz/conf/101.conf

       cp -p  /etc/vz/conf/ve-basic.conf-sample   /etc/vz/conf/101.conf 

7,Now create a new folder called /vz/template/centos/5/<ARCH>/config

        where  <ARCH> represents the system architecture( here x86_64 ).

8,Create a file in the folder called rpm and add the following line:

          43 

9,Create a file called yum.conf in the folder and add the following lines:

          [main] 
          cachedir=/vz/template/centos/5/<ARCH>/yum-cache/ 
          reposdir=/dev/null 
          installonlypkgs= 
          [centos5-base] 
          name=CentOS 5 - <ARCH> - Base 
          baseurl=http://mirror.centos.org/centos/5/os/<ARCH>/ 
          enabled=1 
          gpgcheck=1 
          [centos5-updates-released] 
          name=CentOS 5 - <ARCH> - Released Updates 
          baseurl=http://mirror.centos.org/centos/5/updates/<ARCH>/ 
          enabled=1 
          gpgcheck=1

10, cp -p /etc/vz/dists/centos.conf /etc/vz/dists/centos-5.conf

11, cp -p /vz/template/centos/4/i386/config/minimal.list /vz/template/centos/5/x86-64/config/minimal.list

12, Now enter the dummy vps folder

    cd /vz/private/101

13, Then run the command

          gunzip –dc /vz/template/cache/centos-5-<ARCH>-<DISTRO>-image.tar.gz | tar -xvf - 
      eg: gunzip -dc /vz/template/cache/centos-5.0-centos-5-x86-64-custom-image.tar.gz | tar -xvf -

14,Edit ‘etc/shadow’ and replace the root password with !! instead of the hashed value.

15,Edit the ‘etc/inittab’ file and comment out the lines that respawn /sbin/mingetty on tty1 through tty6. Just put a # at the beginning of the line.

16,Remove all of the lines from etc/fstab except for the line that mounts /dev/pts.

17,Remove the ‘ etc/mtab’ file and then create a symbolic link by typing ln -s /proc/mounts etc/mtab.

18,Remove all of the lines from etc/fstab except for the line that mounts /dev/pts.

19,Edit etc/rc.d/rc.sysinit and comment out the line that starts /sbin/start_udev by placing a # at the beginning of the line.

20,Now create device nodes by typing:

   mknod dev/ptmx c 5 2 
   mkdir dev/pts 
   /sbin/MAKEDEV –d /vz/private/50/dev ttyp ptyp 
   mknod dev/null c 1 3 
   mknod -m 644 dev/random c 1 8 
   mknod dev/urandom c 1 9

21,Create the var/lock/rpm folder.

22,If you wish to disable IPv6, do the following:

    Edit etc/sysconfig/network and set NETWORKING_IPV6 to no.

23,Now start the dummy vps. Type vzctl start 101 and wait for it to start.

24,Stop the container by typing vzctl stop 101.

25, Finally, package up the new template by typing

tar –czvf /vz/template/cache/centos-5-<ARCH>-<DISTRO>.tar.gz ./ 
     eg: tar -czvf /vz/template/cache/centos-5-x86-custom-image.tar.gz ./ 

The template is ready for use.

Now you can create new vps with the template

  vzctl create  --ostemplate centos-5.7-x86_64-default-image

please note that while creating new vps it will get create with the default parameters that is mentioned in /etc/vz/conf/ve-basic.conf-sample. You can specify according to your need in a file and use that config for creating new vps. I have created a new config file /etc/vz/conf/plan1

eg : vzctl create –ostemplate centos-5.7-x86_64-default-image –config plan1

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: