2012-12-29

Unattended Install of CentOS 6.3 with a USB Key

Here are the steps to create a bootable USB KEY that performs unattended installation of CentOS 6.3

Prerequisites


  • I expect that you know what you are doing. Some of the commands provided can destroy your system if they are not used wisely!
  • A USB Key with at least 512MB capacity. It will be formatted during the process. Don’t forget to set USBKEY variable correctly. This is very important that you make sure you don’t execute the commands on another disk since it can destroy the data on  your system.
  • Download CentOS-6.3-x86_64-minimal.iso (I assume you have downloaded it in ~/Downloads)
  • Make sure you have the following commands installed on your system :
    • sfdisk
    • mkfs.vfat
    • grub-install
    • sudo (or root login)
  • If you want to add driver disk to the initrd.img you will also need :
    • cpio
    • xz
    • sudo
  • This procedure have been tested on Ubuntu 12.04 LTS

Initialize the USB key

In order to be able to boot from the USB key you need to make a bootable partition of type 0xB (FAT 32 C/H/S), other types might work as well, I haven’t tried.

The quickest way to do it is using sfdisk like this :
# modify the variable to suit your situation. BEWARE this is very important, 
# if you put the wrong device it can make your system unusable.
USBKEY=/dev/sdc
printf ";;b;*\n" | sudo sfdisk  ${USBKEY}

You can use -uM with sfdisk to specify the size in megabytes. It’s not relevant here since we are creating only one partition that fills the USB key.

After repartitioning your USB key, you will format it :
sudo mkfs.vfat -F 32 -n INSTALL ${USBKEY}1

Install Grub to your USB Key

Create mount point
sudo mkdir /tmp/INSTALL

Mount your USB key
sudo mount ${USBKEY}1 /tmp/INSTALL

Install grub on your USB key
sudo grub-install --force --root-directory=/tmp/INSTALL ${USBKEY}

Create a grub.cfg file

With your favorite editor create /tmp/INSTALL/boot/grub/grub.cfg
set timeout=5
set default=0

menuentry "Install CentOS 6.3" {
 linux /vmlinuz ksdevice=eth0 ks=hd:sda1:/ks.cfg --
 initrd /initrd.img
}


Create a Kickstart file



Create the file /tmp/INSTALL/ks.cfg
This is an example. You should customize your ks.cfg to suit your needs.
Keep in mind that the first harddisk will be sdb, since sda is the usbkey when you are booting.
lang en_US
keyboard us
timezone America/Montreal

rootpw --iscrypted  $1$bZB2mB8q$msVgTjbsA.RSvwiQjmr9R0
auth  --useshadow  --enablemd5
selinux --disabled
network --bootproto=dhcp --device=eth0
firewall --disabled

install
harddrive --dir=/ --partition=sda1

# Add some repo if you want (uncomment if needed)
#repo --name="EPEL6-x86_64" --baseurl=http://fedora.mirror.nexicom.net/epel/6/x86_64/
#repo --name="PuppetLabs" --baseurl=http://yum.puppetlabs.com/el/6/products/x86_64/

url --url http://less.cogeco.net/CentOS/6.3/os/x86_64/

zerombr yes
clearpart --all --initlabel
bootloader --location=mbr

# this is VERY IMPORTANT otherwise your USBKEY will be wiped during install process!
ignoredisk --drives=sda

# I’ve added fsoptions because I’m installing CentOS on SSD drives.
part swap --size 2048 --ondisk=sdb
part / --fstype ext4 --size 4096 --ondisk=sdb --fsoptions=discard,noatime,nodiratime
part /boot --fstype ext4 --size 512 --ondisk=sdb --fsoptions=discard,noatime,nodiratime
part /var --fstype ext4 --size 2048 --grow  --ondisk=sdb --fsoptions=discard,noatime,nodiratime
text
skipx
reboot

#customize the packages you want
%packages
@base
git
wget
ntp
openssh-server
openssh-clients
screen
vim


You can change the password hash used in ks.cfg using this command :
openssl passwd -1

Copy files from the ISO

Create mount point

sudo mkdir /tmp/ISO

Mount the iso file
sudo mount -o loop ~/Downloads/CentOS-6.3-x86_64-minimal.iso  /tmp/ISO
Copy the kernel and initrd from the iso
sudo cp /tmp/ISO/isolinux/initrd.img /tmp/INSTALL/
sudo cp /tmp/ISO/isolinux/vmlinuz /tmp/INSTALL/

Copy the images directory from the iso
sudo cp -r /tmp/ISO/images/ /tmp/INSTALL/

Unmount the iso and the usb key
sudo umount /tmp/INSTALL/
sudo umount /tmp/ISO/


Your USB Key is now ready to boot and install CentOS 6.3!

OPTIONAL - Add a driver disk

If  you have specialized hardware, you probably need to add a driver disk during the installation process. The best way to add a driver disk to make an unattended installation is to add it to the root of initrd image with the name dd.img.

Anaconda will load automatically the driver disk during the installation process. Here are the steps recreate the initrd.img with the dd.img



sudo mount -o loop ~/Downloads/CentOS-6.3-x86_64-minimal.iso  /tmp/ISO
cd  /tmp/INSTALL/
# the directory should be empty if you followed the instructions. If it's not empty, make sure # you unmounted the /tmp/INSTALL directory.
ls
# decompress initrd.img
xzcat --force /tmp/ISO/isolinux/initrd.img | cpio -ivdum
cp WHERE_IS_MY_DRIVER_DISK.img dd.img
# recompress initrd.img in /tmp
find . -print |cpio -o -H newc | xz --format=lzma > /tmp/initrd.img
# clean the directory rm -rf /tmp/INSTALL/*
cd /tmp
# mount the USBKEY and copy the new initrd.img sudo mount ${USBKEY}1 /tmp/INSTALL
sudo cp  /tmp/initrd.img  /tmp/INSTALL
# unmount the CD and the USBKEY
sudo umount /tmp/INSTALL
sudo umount /tmp/ISO