Monday, January 16, 2012

CentOS 6.2 64bit AWS EC2 AMI creation step-by-step

This guy couldn't find any instructions on how to create a CentOS 6 AMI but then fails to provide necessary details in his howto.
""I used PV-Grub, which boots into a mini Xen OS first, using one of the Amazon supplied kernels. It then boots the kernel inside the AMI, which happens to be the native kernel supplied with CentOS 6.""
So here are my steps - in as much detail as I recorded - maybe I need to add a bit more explanation to some of the steps so one does not follow them blindly:
  1. Start an EC2 instance, preferably a Redhat/CentOS 6, as it already has the tools needed.
    • used CentOS ami-697bae00
    • used defaults for kernel (aki-8e5ea7e7), ram disk etc
  2. Create a 5GB EBS and attach it to the running instance
    • vol-xxxxxxx (note volume id)
    • /dev/sdf
  3. Format (labeling it) and mount the partition, creating a few skeleton directories
    • cat /proc/partitions (to verify what dev our volume was attached to .. newer/XEN kernels rename them)
    • parted /dev/xvdj
      • mklabel msdos
      • mkpart primary ext4 1 -1
      • set 1 boot on
      • quit
    • mkfs.ext4 /dev/xvdj1
    • mkdir /mnt/ami
    • mount /dev/xvdj1 /mnt/ami
    • mkdir -p /mnt/ami/{dev,etc,proc,sys}
  4. Create base devices for the new install
    • /sbin/MAKEDEV -v -d /mnt/ami/dev -x console
    • /sbin/MAKEDEV -v -d /mnt/ami/dev -x null
    • /sbin/MAKEDEV -v -d /mnt/ami/dev -x zero
  5. Create fstab for the new install
    • cp /etc/fstab /mnt/ami/etc/fstab
    • mount -t proc proc /mnt/ami/proc
    • mount -t sysfs sysfs /mnt/ami/sys
  6. Install YUM
    • mkdir -p /mnt/ami/var/lib/rpm
    • rpm --rebuilddb --root=/mnt/ami
    • rpm --import --root=/mnt/ami http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
    • wget http://mirror.centos.org/centos/6.2/os/x86_64/Packages/centos-release-6-2.el6.centos.7.x86_64.rpm
    • rpm -i --root=/mnt/ami --nodeps centos-release-6-2.el6.centos.7.x86_64.rpm
    • yum --installroot=/mnt/ami install -y rpm-build yum
  7. mkdir /mnt/ami/root
  8. cp -v /mnt/ami/etc/skel/.??* /mnt/ami/root
  9. mount --bind /proc /mnt/ami/proc
  10. mount --bind /dev /mnt/ami/dev
  11. cp /etc/sysconfig/network /mnt/ami/etc/sysconfig/network
  12. cp -v /etc/resolv.conf /mnt/ami/etc/resolv.conf
  13. chroot /mnt/ami/ su -
  14. cp /etc/fstab /etc/mtab
  15. yum clean all
  16. yum groupinstall base
  17. yum groupinstall core
  18. exit
  19. cp -v /etc/sysconfig/network-scripts/ifcfg-eth0 /mnt/ami/etc/sysconfig/network-scripts/ifcfg-eth0
  20. Enable fetching of assigned ssh keypair from instance user data at boot time
    • cp /etc/rc.local /mnt/ami/etc/rc.local
  21. Disable DNS checks and allow root to log into SSH (as in original AMI we started from)
    • cp /etc/ssh/sshd_config /mnt/ami/etc/ssh/sshd_config
  22. [?? STILL TRUE ??] Disable (as it screws up with Xen) selinux in /mnt/ami/etc/selinux/config
    • cp /etc/selinux/config /mnt/ami/etc/selinux/config
  23. Configure GRUB "ghost" to satisfy PV-GRUB (menu.lst link to grub.conf pointing to kernel & initramfs)
    • \rm -r /mnt/ami/boot/grub
    • cp -r /boot/grub /mnt/ami/boot/grub/
  24. Update initramfs
    • chroot /mnt/ami/ su -
    • cd /boot
    • mv initramfs-2.6.32-220.2.1.el6.x86_64.img orig_initramfs-2.6.32-220.2.1.el6.x86_64.img
    • mkinitrd --force initramfs-2.6.32-220.2.1.el6.x86_64.img 2.6.32-220.2.1.el6.x86_64
  25. With the AMI completed, time to sync and unmount the drive (verify unmounts via "cat /etc/mtab")
    • sync
    • umount /mnt/ami/dev
    • umount /mnt/ami/proc
    • umount /mnt/ami/sys
    • umount /mnt/ami
  26. Create a snapshot of the EBS volume
  27. Create an AMI from snapshot (root/block device MUST be /dev/sda NOT default of /dev/sda1)
    • kernel: aki-8e5ea7e7
    • arch: x86_64
    • root: /dev/sda

2 comments:

  1. Great. Thanks for this info. Just one mistake in the the Step 6, on the 5, it should be centos-release-6-2.el6.centos.7.x86_64.rpm instead of centos-release-6-2.el6.centos.7.i686.rpm.

    ReplyDelete
  2. Step 24 you need to exit out at the end.

    ReplyDelete