Quickly build your own CentOS 6 & 7 AMI

Following on from my previous post Roll your own CentOS 6.5 HVM AMI in less than 15 minutes here's the snippet to for booting into an automated kickstart install for building your new AMI.

This also works for other VM providers such as Azure.

Don't forget to replace the version and appropriate snippets with your own.

version=<%= @osver %>  
mirror=http://mirror.centos.org/centos/

# Detect primary root drive
if [ -e /dev/xvda ]; then  
  drive=xvda
elif [ -e /dev/vda ]; then  
  drive=vda
elif [ -e /dev/sda ]; then  
  drive=sda
fi

yum -y install wget  
mkdir /boot/centos  
cd /boot/centos  
wget ${mirror}/${version}/os/x86_64/isolinux/vmlinuz  
wget ${mirror}/${version}/os/x86_64/isolinux/initrd.img

cat > /boot/centos/kickstart.ks << EOL  
<%= snippet 'centos_kickstart' %>

%post
<%= snippet 'centos_post' %>  
<%= snippet 'centos_post_finish' %>  
%end
EOL

if [ ${version} == 6 ]; then  
  echo "
  default         0
  timeout         0
  hiddenmenu

  title CentOS 6 Installation
          root (hd0,0)
          kernel /boot/centos/vmlinuz ip=dhcp ksdevice=eth0 ks=hd:${drive}1:/boot/centos/kickstart.ks method=${mirror}/${version}/os/x86_64/ lang=en_US keymap=us
  initrd /boot/centos/initrd.img " > /boot/grub/grub.conf
else  
  echo "menuentry 'centosinstall' {
          set root='hd0,msdos1'
      linux /boot/centos/vmlinuz ip=dhcp ksdevice=eth0 ks=hd:${drive}1:/boot/centos/kickstart.ks method=${mirror}/${version}/os/x86_64/ lang=en_US keymap=us
          initrd /boot/centos/initrd.img
  }" >> /etc/grub.d/40_custom

  echo 'GRUB_DEFAULT=saved
  GRUB_HIDDEN_TIMEOUT=
  GRUB_TIMEOUT=2
  GRUB_RECORDFAIL_TIMEOUT=5
  GRUB_CMDLINE_LINUX_DEFAULT="quiet nosplash vga=771 nomodeset"
  GRUB_DISABLE_LINUX_UUID=true' > /etc/default/grub

  grub2-set-default 'centosinstall'
  grub2-mkconfig -o /boot/grub2/grub.cfg
fi  
reboot  
comments powered by Disqus