冷呆獸試驗場

Create customized AMI with AWS EBS

標題其實不太好下,基本上就是自行建立好整個作業系統,改完想要修改的設定與服務後,再上傳到AWS上製作出AMI。

Target OS: CentOS 5.5 x86_64

  1. Create an EC2 instance: Amazon Linux, and check its A/Z. (optional)
  2. Create an new EBS volume in the same A/Z with the Amazon Linux.
  3. Attach the new EBS volume to the Amazon Linux.
  4. Create partition on the EBS volume.

    /boot - 200MB
    swap - 1GB
    / - all
    
  5. Create filesystem and label.

    mkfs.ext3 /dev/xvdf1
    mkfs.ext3 /dev/xvdf3
    e2label /dev/xvdf1 /
    boote2label /dev/xvdf3 /
    mkswap -L swap /dev/xvdf2
    
  6. Mount these partitions.

    mkdir /ebs /ebsboot
    mount /dev/xvdf1 /ebsboot
    mount /dev/xvdf3 /ebs
    
  7. Allow root login using SSH

    sed -i '/disable_root/s/true/false/' /etc/cloud/cloud.cfg
    sed -i 's/PermitRootLogin force/#PermitRootLogin/; s/#PermitRootLogin yes/PermitRootLogin without-password/' /etc/ssh/sshd_config
    sed -i 's/^.*\(ssh-rsa .*\)$/\1/' /root/.ssh/authorized_keys
    service sshd reload
    
  8. Install a CentOS on XenServer using template (PV), and configure anything you want to modify.

    • Make sure the kernel information shows with "xen".
  9. Sync all files in the CentOS to the partitions on mounted EBS.

    rsync -avx --rsh "ssh -i key.pem" / root@[IP-of-AmazonLinux]:/ebs
    rsync -avx --rsh "ssh -i key.pem" /boot/ root@[IP-of-AmazonLinux]:/ebsboot 
    
  10. Modify files

    • /ebs/etc/sysconfig/network-scripts/ifcfg-eth0
      • Remove line "DHCPCLASS"、"HWADDR"、"IPADDR"、"NETMASK"
      • Modify "BOOTPROTO" = dhcp
    • /ebs/etc/sysconfig/network
      • Remove line "GATEWAY"
    • /ebs/etc/fstab
      • Modify the label name of swap "swap"
    • /ebsboot/grub/grub.conf
      • Remove line "splashimage"、"hiddenmenu"
  11. Unmount partitions.

    umount /ebs /ebsboot
    
  12. Take snapshot for this EBS volume, and remember snapshot id.

  13. In IAM, create an account with EC2 full permission, download Access Key and Secret Key.

  14. Use Amazon EC2 API Tools to create AMI from snapshot.

    • Find corresponding kernel id ec2-describe-images -o amazon --filter "name=pv-grub-hd00*x86_64.gz" -O [AccessKey] -W [SecretKey] --region [REGION] |head -n1|awk '{print $2}'
    • Register the snapshot as a new AMI ec2-register -n "CentOS-5.5-x86_64" -d "CentOS 5.5 x86_64 EBS backed" -a x86_64 --root-device-name /dev/sda1 -b /dev/sda=[SnapshotID]:30:true --kernel [KernelID] -O [AccessKey] -W [SecretKey] --region [REGION]
  15. Launch new EC2 instance using the new AMI.