Archive for noviembre, 2011

Script para crear isos de ESX con kickstart personalizado


#!/bin/bash
#
# This script creates an esx iso using a custom kickstart
# Usage:
# createiso [kickstart file] [iso file]
# Parameters:
# kickstart file = Kickstart to embed
# iso file = Iso to modify

# Save the program name into PROGNAME using basename command
PROGNAME=$(basename $0)

# Exit the script if something goes wrong, using a descriptive text and an error code
function error_exit() {

echo "Error $1"
exit $2
}

# Show the usage if parameters are incorrect
function usage() {
echo "Usage: ${PROGNAME} [kickstart file] [iso file]"
error_exit "checking parameters" 1
}

# Check if the parameters are ok
function check_parms() {
# Check if there are only 2 parameters
[ $# -ne 2 ] && usage

# Check if kickstart file is a text file
[ -f $1 ] || error_exit "$1 is not a valid file" "1"

# Check if the iso is really an iso (using file command)
file $2 | grep -q "CD-ROM"
RES=$?

[ $RES -eq 0 ] || error_exit "$2 is not a valid iso" "1"
}

# Clean up
function remove_temp() {
rm -Rf temp
rm -f $1
rm -f ks_`date +%Y%m%d`
umount mountpoint
rm -Rf newiso
rmdir mountpoint
}

# Main

check_parms $1 $2

POSTINSTALL=./resources/postinstall
BACK=./resources/back.jpg

# Check if mkisofs is available
[ -x /usr/bin/mkisofs ] || error_exit "mkisofs not found" 1

# Create a kickstart file copying the original
KICKSTART=ks_`date +%Y%m%d`

cp $1 $KICKSTART

# Create directory structure
mkdir -p {temp/init,mountpoint,newiso}

# Copy the iso to another temp, just in case
echo -n "Copying iso to temp..."
cp $2 $2.new || error_exit "copying $2" 1
echo "Done"

# Mount loopback temp iso file
echo -n "Mounting temp iso..."
mount -o loop $2.new mountpoint || error_exit "mounting $2" 1
echo "Done"

# Copy the initrd (the iso file is read-only)
echo -n "Copying initrd..."
cp mountpoint/isolinux/initrd.img temp/init || error_exit "copying initrd.img" 1
echo "Done"

# Copy the isolinux.cfg to a temp
echo -n "Copying isolinux.cfg..."
cp mountpoint/isolinux/isolinux.cfg temp/ || error_exit "copying isolinux.cfg" 1
echo "Done"

# Use cm as default boot option
echo -n "Setting cm to default boot option..."
sed -i 's/default esx/default cm/g' temp/isolinux.cfg || error_exit "setting cm to default boot option" 1
echo "Done"

# Initrd.img is a compressed file, so, uncompress it
echo -n "Uncompressing initrd..."
cd temp/init
`zcat initrd.img | cpio --extract` || error_exit "uncompressing initrd" 1
echo "Done"
cd -

# Remove the old initrd.img
echo -n "Removing old initrd..."
rm -f temp/init/initrd.img || error_exit "removing old initrd" 1
echo "Done"

# Embed the kickstart into the initrd
echo -n "Copying kickstart to initrd..."
cp $KICKSTART temp/init/ks.cfg || error_exit "copying kickstart to initrd" 1
echo "Done"

# Copy the postinstall script
echo -n "Copying postinstall to initrd..."
cp $POSTINSTALL temp/init/postinstall || error_exit "copying postinstall to initrd" 1
echo "Done"

# Recreate the initrd image
echo -n "Recreating initrd..."
cd temp/init/
find ./ | cpio -H newc -o > ../initrd || error_exit "recreating initrd" 1
gzip ../initrd --suffix .img || error_exit "recreating initrd" 1
cd -
echo "Done"

# Create the boot entry in isolinux.cfg
echo -n "Creating isolinux.cfg entry..."
echo "LABEL auto" >> temp/isolinux.cfg
echo "menu label ESX automatic installation" >> temp/isolinux.cfg
echo "kernel vmlinuz" >> temp/isolinux.cfg
echo "append initrd=initrd.img vmkopts=debugLogToSerial:1 mem=512M ks=file:///ks.cfg quiet" >> temp/isolinux.cfg
echo "Done"

# Copy the back image
echo -n "Copying back image to initrd..."
cp $BACK temp/back.jpg || error_exit "copying back.jpg to initrd" 1
echo "Done"

# Create the custom iso file
echo -n "Creating new iso file..."
# Copy all iso files
cp -pr mountpoint/* newiso/ || error_exit "copying files" 1
# Sync filesystem, just in case
sync
# Sleep for 5 secs., just in case too
sleep 5
# Overwrite the two modified files
cp -f temp/{isolinux.cfg,initrd.img,back.jpg} newiso/isolinux/
cd newiso
# Create the iso into esx-XXXXYYZZ.iso
mkisofs -l -J -R -r -T -o ../esx-vsphere-`date +%Y%m%d`.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./ || error_exit "creating iso" 1
cd -
echo "Done"

# Clean environment
echo -n "Cleaning..."
remove_temp $2.new
echo "Done"

# Finish!
echo "All done!"
echo "Now burn your new iso (esx-vsphere-`date +%Y%m%d`.iso) to a blank cd and boot the auto option"

# Exit with no errors :)
exit 0

Post to Twitter Post to Facebook Send Gmail Post to LinkedIn

No Comments »

minWi on noviembre 8th 2011 in bash, VMware

  • RSS
  • Facebook
  • Google+
  • LinkedIn
  • Twitter
  • Picasa
  • Flickr
  • YouTube