Create custom centos 5.5 AMI


This article will gives an idea about building a Cent OS 5.5 Amazon Machine Image. This method maybe used to build Fedora or Redhat based latest AMI. There are so many docs and methods available to build custom AMI. Below is the method which I followed. A step by step guide.

Pre-Requirements

  • Amazon AWS S3 Account

  • Amazon AWS EC2 Account

  • AWS Account Number

  • AWS or S3 Access Key

  • AWS or S3 Secret Key

  • EC2 Private key file

  • EC2 Certificate file

I'm not going to explain you how to meet the pre-requirements or to explain you about each commands which I used here. This is a step by step guide to create centos5.5 ami very quickly.

Step 1:

Launch a Fedora 8 or Centos or Redhat OS. Any version is enough. I have used Fedora 8 32bit provided by amazon. ( ami-48aa4921 )

Step 2:

Login to the server and create an image file of 10 GB

[root@nixhat]$ dd if=/dev/zero of=/mnt/ami-centos5.5-32bit.img bs=1M count=10240

Step 3:

Create a file system in newly created image file

[root@nixhat]$/sbin/mke2fs -F -j /mnt/ami-centos5.5-32bit.img

Step 4:

Mount the image file to a directory

[root@nixhat]$mkdir /mnt/centos5.5

[root@nixhat]$mount -o loop /mnt/ami-centos5.5-32bit.img /mnt/centos5.5

Step 5:

Lets create folders to hold system files and devices

[root@nixhat]$mkdir -p mnt/centos5.5/{dev,etc,proc,sys}

[root@nixhat]$/sbin/MAKEDEV -d /mnt/centos5.5/dev -x console

[root@nixhat]$/sbin/MAKEDEV -d /mnt/centos5.5/dev -x null

[root@nixhat]$/sbin/MAKEDEV -d /mnt/centos5.5/dev -x zero

Step 6:

Create fstab file

[root@nixhat]$ vi /mnt/centos5.5/etc/fstab

If you are using 64BIT use these fstab entries below

/dev/sda1 / ext3 defaults 1 1

/dev/sdb /mnt ext3 defaults 0 0

none /proc proc defaults 0 0

none /sys sysfs defaults 0 0

If you are using 32BIT use these fstab entries below

/dev/sda1 / ext3 defaults 1 1

none /dev/pts devpts gid=5,mode=620 0 0

none /dev/shm tmpfs defaults 0 0

none /proc proc defaults 0 0

none /sys sysfs defaults 0 0

/dev/sda2 /mnt ext3 defaults 0 0

/dev/sda3 swap swap defaults 0 0

Step 7:

Mount proc and sys in your new root

[root@nixhat]$mount -t proc proc /mnt/centos5.5/proc

[root@nixhat]$mount -t sysfs sysfs /mnt/centos5.5/sys

Step 8:

Create a yum conf file for your required OS. In our case its the repo details of centos 5.5. Also, rename the original /etc/yum.conf file to avoid conflicts.

[root@nixhat]$vi /mnt/yum.conf

#Enter the following conf

[main]

cachedir=/var/cache/yum

keepcache=1

debuglevel=2

logfile=/var/log/yum.log

pkgpolicy=newest

distroverpkg=redhat-release

tolerant=1

exactarch=1

obsoletes=1

gpgcheck=0

plugins=1

metadata_expire=1800

[base]

name=CentOS-5 – Base

baseurl=http://mirror.centos.org/centos/5.5/os/i386/

#released updates

[updates]

name=CentOS-5 – Updates

baseurl=http://mirror.centos.org/centos/5.5/updates/i386/

Step 9:

Now its time to install the OS in our image file. Lets install base and core packages and xen kernel image

[root@nixhat]$ yum -c /mnt/yum.conf –installroot /mnt/centos5.5 -y groupinstall base

[root@nixhat]$ yum -c /mnt/yum.conf –installroot /mnt/centos5.5 -y groupinstall core

[root@nixhat]$ yum -c /mnt/yum.conf –installroot /mnt/centos5.5 -y install kernel-xen

[root@nixhat]$ yum -c /mnt/yum.conf –installroot /mnt/centos5.5 -y remove kernel

Step 10:

Configure network settings.

Create /mnt/centos5.5/etc/sysconfig/network-scripts/ifcfg-eth0 with following entries.

DEVICE=eth0

BOOTPROTO=dhcp

ONBOOT=yes

TYPE=Ethernet

USERCTL=yes

PEERDNS=yes

IPV6INIT=no

 

Create /mnt/centos5.5/etc/sysconfig/network with the following entry

NETWORKING=yes

Create /mnt/centos5.5/etc/hosts with the following entry

127.0.0.1 localhost.localdomain localhost

Step 11:

Disable selinux in /mnt/centos5.5/etc/selinux/config

SELINUX=disabled

Step 12:

Now its time to install grub to boot the OS. Amazon has published AKIs (amazon kernel images) that uses PVGRUB. PVGRUB selects the kernel to boot by reading /mnt/centos5.5/boot/grub/menu.lst from your image. Create a file /mnt/centos5.5/boot/grub/menu.lst with the following contents.

default 0

timeout 5

title Centos

root (hd0)

kernel /boot/vmlinuz-2.6.18-194.26.1.el5xen ro root=/dev/sda1

initrd /boot/initrd-2.6.18-194.26.1.el5xen.img

Make sure you use the correct versions of vmlinuz and initrd in grub configuration.

Step13:

As you know EC2 uses ssh keys to login and is automatically created at the time of booting. The easiest way achieve this with your ami is to copy the contents from /etc/rc.local to your new image file. Also, copy get-credentials.sh proivided by amazon.

[root@nixhat]$ cp /etc/rc.local /mnt/centos5.5/etc/rc.local

[root@nixhat]$ cp /usr/local/sbin/get-credentials.sh /mnt/centos5.5/usr/local/sbin/get-credentials.sh

[root@nixhat]$ chmod 755 /mnt/centos5.5/usr/local/sbin/get-credentials.sh

Step14:

Disable some of the default services in new image.

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig firstboot off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig gpm off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig iptables off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig ip6tables off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig kudzu off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig rpcgssd off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig rpcidmapd off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig rpcsvcgssd off

[root@nixhat]$ chroot /mnt/centos5.5/ chkconfig yum-updatesd off

Configuring the custom image is compeleted. Now, its time to bundle the custom image, upload it to S3 and register it as amazon ami. CLICK HERE TO CONTINUE TO PAGE2