Over the past few weeks I have been working on a custom spin of the CentOS installation image that automatically installs cPanel.  This ISO also performs some pre-installation configuration and installs updated packages as well as installing cpanel.  I think this information is handy for anyone who wants to create a CentOS installation image, whether for cPanel or for something else.  To create the installation image, we will modify the CentOS installation DVD; adding in a kickstart file, removing unneeded packages, and updating the remaining packages.

What you need
For creating this image you will need a few things:

  1. A cPanel server running the target OS on the target architecture. I recommend using a fresh installation.
  2. The yum-utils and createrepo packages installed from yum:
    yum install -y yum-utils createrepo
  3. A copy of the CentOS installation DVD ISO
  4. 10+ gB of free disk space

Creating the package list
You will need to grab a list of packages from the cPanel server.  These are used at several points of the process to let the installer know what it needs to install,  while letting yumdownloader know what it needs to download.  Unfortunately, there isn’t an easy way to grab a list of packages that can be used directly.  Also, cPanel provides some packages that are not available within yum.  You should never install these manually.  We will need to filter these packages out.  You can accomplish this using the following (silly) shell command:
rpm -qa | egrep -v "(nsd|bandmin|proftpd|pure-ftpd|frontpage|gpg-pubkey|MySQL|exim|courier|dovecot)" | perl -lane '$F[0] =~ s/-[0-9].+$//; print $F[0];' | sort | uniq > ~/rpmlist

This command generates a file that contains the names of the packages on the system excluding the cPanel-installed packages and a couple of other packages.

Copying the ISO
Copying the installer ISO is a relatively simple task.  You simply need to copy it to the system in question, mount it, then copy it to your home directory.  There are a couple of dot files that need to be copied as well.
wget http://url/to/some/iso
mkdir /mnt/iso
mount -o loop some_iso /mnt/iso
cp -pr /mnt/iso ~
cp -pr /mnt/iso/.discinfo ~/iso/
cp -pr /mnt/iso/.treeinfo ~/iso/

Creating an populating the repository
Creating your repository is a very important step to this process.  Using the packages provided with the DVD will result in an out-of-date installation.  While this is fine for some uses, you will likely prefer that the installation process installs updated packages. This results in faster installation times.

The first step is to clear out the current repository:
chdir ~/iso/CentOS/
rm -f *

After the repo has been cleared, we need to repopulate the repo with the updated packages.  To do so, we will use the yumdownloader utility available within the yum-utils rpm.
cd ~/iso/CentOS/
yumdownloader --resolve `cat ~/rpmlist | tr ‘n’ ‘ ‘`

This will download the latest version of the packages in question from yum and place them in the current directory.

Note:  This process can take awhile depending on your Internet connection and the speed of the mirror in question.

Once this has been done, we have to populate the repodata files with the correct information.  This is handled via the createrepo utility:
cd ~/iso/
createrepo -g repodata/comps.xml .

Placing the kickstart file
You also need to create and place a kickstart file in the appropriate location.  You can find an example of this file at http://httpupdate.cpanel.net/cpanel-ks.cfg.  This file can be used for setting defaults, fully automating the installation process, and pre-configuring cPanel.  In a later post I’ll talk about what can be done with kickstart files for provisioning cPanel servers (documentation can be viewed at http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-kickstart2-options.html).  At this point, the only thing you need to know is that you should copy the contents of /root/rpmlist and paste them between %packages and %post inside of cpanel-ks.cfg.  Once this has been done, you should place it in the user’s home directory.
cd ~/iso/
wget http://httpupdate.cpanel.net/cpanel-ks.cfg

Configuring the Installer to use the kickstart file
The bootloader will have to be told to use the kickstart file.  You can accomplish this by editting ~/iso/isolinux/isolinux.cfg.

This file it should currently contain something similar to:
label linux
  kernel vmlinuz
  append initrd=initrd.img
label text
  kernel vmlinuz
  append initrd=initrd.img text

On both of these, ks=cpanel-ks.cfg should be added to the end of the append line.  This will tell the installer to automatically load the kickstart file.

Creating the ISO
Once this has been done the ‘mkisofs’ utility can be used to create the ISO:
mkisofs -o $output_file -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -T .

At this point you can burn the ISO to a disc and begin provisioning your server.