Join our Discussion Boards - Here

How To Create a Centos 5.2 EC2 Amazon Machine Image – AMI

Create the disk image
dd if=/dev/zero of=my-image.fs bs=1M count=1024
Create root file system inside file
mke2fs -F -j my-image.fs
Mount it
mkdir /mnt/ec2-fs
mount -o loop my-image.fs /mnt/ec2-fs
Create /dev
mkdir /mnt/ec2-fs/dev
Populate /dev with a minimal set of devices
/sbin/MAKEDEV -d /mnt/ec2-fs/dev/ -x console
/sbin/MAKEDEV -d /mnt/ec2-fs/dev/ -x null
/sbin/MAKEDEV -d /mnt/ec2-fs/dev/ -x zero
Create /etc
mkdir /mnt/ec2-fs/etc
Create yum-xen.conf in the local file system, not the mounted system.  Put the following in yum-xen.conf
[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
exclude=*-debuginfo
gpgcheck=0
obsoletes=1
pkgpolicy=newest
distroverpkg=redhat-release
tolerant=1
exactarch=1
reposdir=/dev/null
metadata_expire=1800
[base]
name=CentOS-5.2 – Base
baseurl=http://mirror.centos.org/centos/5.2/os/i386/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.2
priority=1
protect=1
#released updates
[update]
name=CentOS-5.2 – Updates
baseurl=http://mirror.centos.org/centos/5.2/updates/i386/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.2
priority=1
protect=1
#packages used/produced in the build but not released
[addons]
name=CentOS-5.2 – Addons
baseurl=http://mirror.centos.org/centos/5.2/addons/i386/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.2
priority=1
[extras]
name=CentOS 5.2 Extras $releasever – $basearch
baseurl=http://mirror.centos.org/centos/5.2/extras/i386/
enabled=1
Mount proc
mkdir /mnt/ec2-fs/proc
mount -t proc none /mnt/ec2-fs/proc
Install the Operating System
yum -c yum-xen.conf –installroot=/mnt/ec2-fs -y groupinstall Core
 Create the network settings
vi /mnt/ec2-fs/etc/sysconfig/network-scripts/ifcfg-eth0
Enter
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
Turn on networking
vi /mnt/ec2-fs/etc/sysconfig/network
Enter
NETWORKING=yes
Set up the hard drives
vi /mnt/ec2-fs/etc/fstab
Enter 
/dev/sda2  /mnt      ext3    defaults        1 2
/dev/sda3  swap      swap    defaults        0 0
Start some services on start
chroot /mnt/ec2-fs /bin/sh
chkconfig –level 345 sshd on
exit
Unmount the image
umount /mnt/ec2-fs/proc
umount -d /mnt/ec2-fs 
Now bundle the AMI
Install ruby
yum install ruby
Download the EC2 AMI Tools 
wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm
Install the EC2 AMI Tools
rpm -Uvh ec2-ami-tools.noarch.rpm
Put both your private key and cert in the same folder as my-image.fs
Use the AMI tools to bundle the your image
ec2-bundle-image -i my-image.fs -c my-cert.pem -k my-private-key.pem -u 12345678(amazon account id)  
nb. Selinux should be disabled while this is done.
This create the bundles the image to an AMI and puts it in /tmp
Now upload the  ec2-bundle to S3
ec2-upload-bundle -b my-bucket -m image.manifest -a my-aws-access-key-id -s my-secret-key-id 
This is a good resource on how to script the whole thing – http://www.centos.org/modules/newbb/viewtopic.php?topic_id=11788&forum=27&post_id=38389

6 comments to How To Create a Centos 5.2 EC2 Amazon Machine Image – AMI

  • weput

    I’ve been trying this method and it’s not working for me…
    I can’t ssh to the instance i launch using this fresh ami created with your method…

    when i do

    chroot /mnt/ec2-fs /bin/sh
    chkconfig –level 345 sshd on

    it works fine; but i can’t ssh into the instance…. then i mounted the filesystem and did chroot again…. and this is what’s going on.

    root@rawlinux # /sbin/chkconfig –list sshd
    sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    root@rawlinux # service sshd status
    sshd is stopped
    root@rawlinux # service sshd start
    Generating SSH1 RSA host key: [FAILED]

    I have no idea how to correct this problem.

  • weput

    Ok
    I’ve found some other interesting stuff…. this is the console output of the instance launched using the ami created with your method.

    Linux version 2.6.16-xenU (builder@xenbat.amazonsa) (gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)) #1 SMP Mon May 28 03:41:49 SAST 2007
    BIOS-provided physical RAM map:
    Xen: 0000000000000000 – 000000006a400000 (usable)
    980MB HIGHMEM available.
    727MB LOWMEM available.
    NX (Execute Disable) protection: active
    IRQ lockup detection disabled
    Built 1 zonelists
    Kernel command line: root=/dev/sda1 ro 4
    Enabling fast FPU save and restore… done.
    Enabling unmasked SIMD FPU exception support… done.
    Initializing CPU#0
    PID hash table entries: 4096 (order: 12, 65536 bytes)
    Xen reported: 2600.000 MHz processor.
    Dentry cache hash table entries: 131072 (order: 7, 524288 bytes)
    Inode-cache hash table entries: 65536 (order: 6, 262144 bytes)
    Software IO TLB disabled
    vmalloc area: ee000000-f53fe000, maxmem 2d7fe000
    Memory: 1718700k/1748992k available (1958k kernel code, 20948k reserved, 620k data, 144k init, 1003528k highmem)
    Checking if this processor honours the WP bit even in supervisor mode… Ok.
    Calibrating delay using timer specific routine.. 5200.43 BogoMIPS (lpj=26002197)
    Mount-cache hash table entries: 512
    CPU: L1 I Cache: 64K (64 bytes/line), D cache 64K (64 bytes/line)
    CPU: L2 Cache: 1024K (64 bytes/line)
    Checking ‘hlt’ instruction… OK.
    Brought up 1 CPUs
    migration_cost=0
    Grant table initialized
    NET: Registered protocol family 16
    Brought up 1 CPUs
    xen_mem: Initialising balloon driver.
    highmem bounce pool size: 64 pages
    VFS: Disk quotas dquot_6.5.1
    Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
    Initializing Cryptographic API
    io scheduler noop registered
    io scheduler anticipatory registered (default)
    io scheduler deadline registered
    io scheduler cfq registered
    i8042.c: No controller found.
    RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
    Xen virtual console successfully installed as tty1
    Event-channel device installed.
    netfront: Initialising virtual ethernet driver.
    mice: PS/2 mouse device common for all mice
    md: md driver 0.90.3 MAX_MD_DEVS=256, MD_SB_DISKS=27
    md: bitmap version 4.39
    NET: Registered protocol family 2
    Registering block device major 8
    IP route cache hash table entries: 65536 (order: 6, 262144 bytes)
    TCP established hash table entries: 262144 (order: 9, 2097152 bytes)
    TCP bind hash table entries: 65536 (order: 7, 524288 bytes)
    TCP: Hash tables configured (established 262144 bind 65536)
    TCP reno registered
    TCP bic registered
    NET: Registered protocol family 1
    NET: Registered protocol family 17
    NET: Registered protocol family 15
    Using IPI No-Shortcut mode
    md: Autodetecting RAID arrays.
    md: autorun …
    md: … autorun DONE.
    kjournald starting. Commit interval 5 seconds
    EXT3-fs: mounted filesystem with ordered data mode.
    VFS: Mounted root (ext3 filesystem) readonly.
    Freeing unused kernel memory: 144k freed
    Warning: unable to open an initial console.

    ***************************************************************
    ***************************************************************
    ** WARNING: Currently emulating unsupported memory accesses **
    ** in /lib/tls glibc libraries. The emulation is **
    ** slow. To ensure full performance you should **
    ** install a ‘xen-friendly’ (nosegneg) version of **
    ** the library, or disable tls support by executing **
    ** the following as root: **
    ** mv /lib/tls /lib/tls.disabled **
    ** Offending process: init (pid=1) **
    ***************************************************************
    ***************************************************************

    Pausing… 5Pausing… 4Pausing… 3Pausing… 2Pausing… 1Continuing…
    Restarting system.

  • [...] and is able to receive http requests on port 80. There are a lot of resources available (here, here, here, here, here, here,here and here) but none of them did exactly what I [...]

  • fortunato

    I get prompted for the root password when I try to SSH into the running instance. Any suggestions?
    Thanks.

  • tcasson

    I had the same issue due to missing /etc/shadow.

    run:

    chroot /mnt/ec2-fs /bin/sh
    pwconv
    passwd

    Should do the trick – details here: http://mohammednv.wordpress.com/2008/01/08/authentication-token-manipulation-error-when-changing-user-passwords-in-linux/

  • [...] using a centos instance that I built using this centos ami guide as such the on box storeage comes in two [...]

You must be logged in to post a comment.