Building an AMI

From KlavoWiki
Jump to navigationJump to search

Create a Loopback File

A Loopback file is a file that will contain the contents of an Operating System. You can think of it like a VMDK file as located in a vmWare instance.

dd if=/dev/zero of=CentOS53_Vanilla.img bs=1M count=1024

The paramaters are as:

  • bs, block size. 1M = 1 MB, 1G = 1GB.
  • count, how many blocks.
  • of, the name of the image file

Example

======== ============ ======== ==================
bs count size caculation
======== ============ ======== ==================
bs=1M count=1024 1GB 1MB * 1024 = 1GB
bs=1M count=2048 2GB 1MB * 2048 = 2GB
bs=1M count=5120 5GB 1MB * 5120 = 5GB
bs=1G count=10 10GB 1GB * 10 = 10GB
======== ============ ======== ==================

Create root File System

mke2fs -F -j CentOS53_Vanilla.img

Mounting the Image File

mkdir /tmp/image_file
mount -o loop CentOS53_Vanilla.img /tmp/image_file

Prepare for Installation

Ignore the errors in the output

mkdir /tmp/image_file/dev
MAKEDEV -d /tmp/image_file/dev -x console
MAKEDEV -d /tmp/image_file/dev -x null
MAKEDEV -d /tmp/image_file/dev -x zero

Create the fstab File

Add the following to the file /etc/fstab
First create folder and file.

mkdir /tmp/image_file/etc
touch /tmp/image_file/etc/fstab

Now copy the contents below into the newly create file.

/dev/sda1    /         ext3   defaults       1 1
/dev/sda2    /mnt      ext3   defaults       0 0
/dev/sda3    swap      swap   defaults       0 0
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

proc

Create and then mount proc folder.

mkdir /tmp/image_file/proc
mount -t proc none /tmp/image_file/proc

Ignore any outpout messages.

MAKEDEV -d /tmp/image_file/dev -x console
MAKEDEV -d /tmp/image_file/dev -x null
MAKEDEV -d /tmp/image_file/dev -x zero

Install OS

yum -c /etc/yum.repos.d/CentOS-Base.repo --installroot=/tmp/image_file -y groupinstall Base

NOTE: You will need to edit or make a copy of the CentOS-Base.repo and make sure that you remove or remark out the mirrorlist and use baseurl. You will also need to change $releasever to the version that you would like to load.

Configure OS

Add the following contents to the file:
/tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
<pre>

= Un-Mount Image =
Now that the OS is installed and configured we can now unmount the image.
<pre>
umount /tmp/image_file/proc
umount -d /tmp/image_file
rmdir /tmp/image_file

Automated Script

#!/bin/bash

IMG="CentOS53_Vanilla.img"     # Image File Name
Size="2"                       # Image File Size in GB

dd if=/dev/zero of=$IMG bs=1G count=$Size
mke2fs -F -j $IMG

mkdir /tmp/image_file
mount -o loop $IMG /tmp/image_file

mkdir /tmp/image_file/dev
MAKEDEV -d /tmp/image_file/dev -x console
MAKEDEV -d /tmp/image_file/dev -x null
MAKEDEV -d /tmp/image_file/dev -x zero

mkdir /tmp/image_file/etc
echo "/dev/sda1    /        ext3   defaults                1 1" >  /tmp/image_file/etc/fstab
echo "/dev/sda2    /mnt     ext3   defaults                0 0" >> /tmp/image_file/etc/fstab
echo "/dev/sda3    swap     swap   defaults                0 0" >> /tmp/image_file/etc/fstab
echo "none         /dev/    pts    devpts   gid=5,mode=620 0 0" >> /tmp/image_file/etc/fstab
echo "none         /dev/    shm    tmpfs    defaults       0 0" >> /tmp/image_file/etc/fstab
echo "none         /proc    proc   defaults                0 0" >> /tmp/image_file/etc/fstab
echo "none         /sys     sysfs  defaults                0 0" >> /tmp/image_file/etc/fstab

mkdir /tmp/image_file/proc
mount -t proc none /tmp/image_file/proc
MAKEDEV -d /tmp/image_file/dev -x console
MAKEDEV -d /tmp/image_file/dev -x null
MAKEDEV -d /tmp/image_file/dev -x zero

yum -c /etc/yum.repos.d/CentOS-Base.repo --installroot=/tmp/image_file -y groupinstall Base

echo "DEVICE=eth0" > /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "BOOTPROTO=dhcp" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "ONBOOT=yes" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "TYPE=Ethernet" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "USERCTL=yes" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "PEERDNS=yes" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPV6INIT=no" >> /tmp/image_file/etc/sysconfig/network-scripts/ifcfg-eth0

umount /tmp/image_file/proc
umount -d /tmp/image_file
rmdir /tmp/image_file