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 | |
---|---|
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. |
Create a File to Host the AMI
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.
To create a file to host the AMI
-
Enter the following command:
#
dd if=/dev/zero of=
<image_name>
bs=1M=<size>
The
<image_name>
is the name of the image file you are creating and<size>
is the size of the file in megabytes.
Example
The following command creates a one gigabyte file (1024*1MB).
#
dd if=/dev/zero of=
my-image.fs
bs=1M count=1024
1024+0 records in 1024+0 records out
Create a Root File System Inside the File
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.
To create an ext3
file system
-
Enter the following command:
#
mke2fs -F -j
<image_name>
The
<image_name>
is the name of the image file.
Example
The following command creates an ext3
file system.
#
mke2fs -F -j
my-image.fs
mke2fs 1.38 (30-Jun-2005) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 131072 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 24 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Mount the File through Loopback
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.
To mount the file through loopback
-
Create a mount point in the file system where the image will be attached:
#
mkdir
<image_mountpoint>
The
<image_mountpoint>
is the location where the image will be mounted. -
Mount the file system image:
#
mount -o loop
<image_name>
<image_mountpoint>
The
<image_name>
is the name of the image file and<image_mountpoint>
is the mount location.
Example
The following commands create and mount the my-image.fs image file.
#
mkdir
/mnt/ec2-fs
#
mount -o loop
my-image.fs
/mnt/ec2-fs
Prepare for the Installation
Before the operating system installation can proceed, you must create and prepare the newly created root file system.
To prepare for the installation
-
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 zeroThe
<image_mountpoint>
is the mount location. -
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
-
Create a temporary yum configuration file (e.g.,
yum-xen.conf
) and add the following content.[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.
-
Enter the following:
#
mkdir
<image_mountpoint>
/proc#
mount -t proc none <image_mountpoint>/proc
The
<image_mountpoint>
is the mount location. Agroupadd
utility bug in theshadow-utils
package (versions prior to 4.0.7-7) requires you to mount the newproc
file system manually with the preceding command.
Example
These commands create the /dev
directory and populate it with
a minimal set of devices:
#
mkdir /mnt/ec2-fs/dev
#
/sbin/MAKEDEV -d
/mnt/ec2-fs
/dev -x console MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists#
/sbin/MAKEDEV -d
/mnt/ec2-fs
/dev -x null MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists#
/sbin/MAKEDEV -d
/mnt/ec2-fs
/dev -x zero MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists MAKEDEV: mkdir: File exists
This example creates and mounts the /mnt/ec2-fs/proc
directory.
#
mkdir
/mnt/ec2-fs
/proc#
mount -t proc none
/mnt/ec2-fs
/proc
Install the Operating System
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.
To install the operating system
-
Enter the following command:
#
yum -c
<yum_configuration_file>
--installroot=<image_mountpoint>
-y groupinstall BaseThe
<yum_configuration_file>
is the name of the yum configuration file and<image_mountpoint>
is the mount location.You now have a base installation, which you can configure for operation inside Amazon EC2 and customize for your use.
Example
This example installs the operating system at the /mnt/ec2-fs
mount point using the yum-xen.conf
yum configuration file.
#
yum -c
yum-xen.conf
--installroot=/mnt/ec2-fs
-y groupinstall Base Setting up Group Process Setting up repositories base 100% |=========================| 1.1 kB 00:00 updates-released 100% |=========================| 1.1 kB 00:00 comps.xml 100% |=========================| 693 kB 00:00 comps.xml 100% |=========================| 693 kB 00:00 Setting up repositories Reading repository metadata in from local files primary.xml.gz 100% |=========================| 824 kB 00:00 base : ################################################## 2772/2772 Added 2772 new packages, deleted 0 old in 15.32 seconds primary.xml.gz 100% |=========================| 824 kB 00:00 updates-re: ################################################## 2772/2772 Added 2772 new packages, deleted 0 old in 10.74 seconds ... Complete!
Configure the Operating System
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
-
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 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. -
Ensure that networking starts by making sure the following line appears in the
/mnt/ec2-fs/etc/sysconfig/network
file:NETWORKING=yes
-
Ensure that local disk storage on
/dev/sda2
and swap space on/dev/sda3
are mounted at system startup by adding the following lines to/mnt/ec2-fs/etc/fstab
:/dev/sda2 /mnt ext3 defaults 0 0 /dev/sda3 swap swap defaults 0 0
Note The
/dev/sda2
and/dev/sda3
storage locations only apply to small instances. For more information on instance storage, see Instance Storage . -
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
Your new installation is successfully installed and configured to operate in the Amazon EC2 environment.
-
Umount the image by entering the following commands:
#
umount
<image_mountpoint>
/proc#
umount -d
<image_mountpoint>
The
<image_mountpoint>
is the mount location.
Example
The following example unmounts the installation from the
/mnt/ec2-fs
mount point.
#
umount
/mnt/ec2-fs
/proc#
umount -d
/mnt/ec2-fs