Creating an AMI through a Loopback File

This method involves doing a full operating system installation on a clean root file system, but avoids having to create a new root disk partition and file system on a physical disk. Once you have installed your operating system, the resulting image can be bundled as an AMI with the ec2-bundle-image utility.

[Note]Note

Before selecting an AMI, determine whether the instance types you plan to launch are 32-bit or 64-bit. For more information, see Instance Types

Make sure you are using GNU Tar 1.15 or later.

These examples use Fedora Core 4. Please make any adjustments for your distribution.

AMI Creation Process


The dd utility can create files of arbitrary sizes. Make sure to create a file large enough to host the operating system, tools, and applications that you will install. For example, a baseline Linux installation requires about 700MB, so your file should be at least 1 GB.


There are several variations on the mkfs utility that can create a file system inside the image file you are creating. Typical Linux installations default to ext2 or ext3 file systems.


The loopback module allows you to use a normal file as if it were a raw device, which gives you a file system within a file. Mounting a file system image file through loopback presents it as part of the normal file system. You can then modify it using your favorite file management tools and utilities.


Before the operating system installation can proceed, you must create and prepare the newly created root file system.

To prepare for the installation

  1. Create a /dev directory and populate it with a minimal set of devices (you can ignore the errors in the output):

    # mkdir /mnt/ec2-fs/dev
    # /sbin/MAKEDEV -d <image_mountpoint>/dev -x console
    # /sbin/MAKEDEV -d <image_mountpoint>/dev -x null
    # /sbin/MAKEDEV -d <image_mountpoint>/dev -x zero

    where <image_mountpoint> is the mount location.

  2. Create the fstab file within the /etc directory and add the following:

    /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
  3. Create a temporary yum configuration file (e.g., yum-xen.conf) and add the following components:

    [main]
    cachedir=/var/cache/yum
    debuglevel=2
    logfile=/var/log/yum.log
    exclude=*-debuginfo
    gpgcheck=0
    obsoletes=1
    reposdir=/dev/null
    
    [base]
    name=Fedora Core 4 - $basearch - Base
    mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever
    enabled=1
    
    [updates-released]
    name=Fedora Core 4 - $basearch - Released Updates
    mirrorlist=http://fedora.redhat.com/download/mirrors/updates-released-fc$releasever
    enabled=1

    This ensures all the required basic packages and utilities are installed. This file can be located anywhere on your main file system (not on your loopback file system) and is only used during installation.

  4. Enter the following:

    # mkdir <image_mountpoint>/proc
    # mount -t proc none <image_mountpoint>/proc

    where <image_mountpoint> is the mount location. A groupadd utility bug in the shadow-utils package (versions prior to 4.0.7-7) requires you to mount the new proc file system manually with the above command.


At this stage, the basic directories and files are created and you are ready to install the operating system. Depending on the speed of the host and network link to the repository, this process might take a while.


After successfully installing the base operating system, you must configure the networking and hard drives to work in the Amazon EC2 environment.

To configure the operating system

  1. Edit (or create) /mnt/ec2-fs/etc/sysconfig/network-scripts/ifcfg-eth0 and make sure it contains at least the following information:

    DEVICE=eth0
    BOOTPROTO=dhcp
    ONBOOT=yes
    TYPE=Ethernet
    USERCTL=yes
    PEERDNS=yes
    IPV6INIT=no
    [Note]Note

    The Amazon EC2 DHCP server ignores hostname requests. If you set DHCP_HOSTNAME, the local hostname will be set on the instance but not externally. Additionally, the local hostname will be the same for all instances of the AMI, which might be confusing.

  2. Ensure that networking starts by making sure the following line appears in the /mnt/ec2-fs/etc/sysconfig/network file:

    NETWORKING=yes
  3. Amazon EC2 provides the instance with additional local disk storage on /dev/sda2 and swap space on /dev/sda3. To ensure both these are mounted at system startup, add the following lines to /mnt/ec2-fs/etc/fstab:

    /dev/sda2  /mnt      ext3    defaults        0 0
    /dev/sda3  swap      swap    defaults        0 0
    [Note]Note

    The /dev/sda2 and /dev/sda3 storage locations only apply to small instances. For more information on instance storage, see the section called “Instance Storage” .

  4. Make sure all of your required services start at system startup by allocating them appropriate system run levels. For example, to enable the service my-service on multi-user and networked run levels, enter the following commands:

    # chroot /mnt/ec2-fs /bin/sh
    # chkconfig --level 345 my-service on
    # exit 
  5. After your new installation is successfully installed and configured to operate in the Amazon EC2 environment, you can unmount the image:

    # umount <image_mountpoint>/proc
    # umount -d <image_mountpoint> 

    where <image_mountpoint> is the mount location.