Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ------------------------------------------------------------------------
- # Copyright 2010 Lorin Soura <gr8can8dian@gmail.com>
- # This script is licensed under GNU GPL version 3.0 or above
- # -------------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- # -------------------------------------------------------------------------
- ### Script to build a custom Centos 5.5 64-bit server edition iso image for: regular installation or kickstart deployment(s).
- ##### SOURCES USED #####
- # Source: http://smorgasbork.com/component/content/article/35-linux/58-building-a-custom-centos-5-kickstart-disc-part-1
- # Source: http://cinq.com/2008/04/14/rebuilding-the-dvd-iso-for-centos-5-or-rhel-5/
- # Source: http://people.redhat.com/rkeech/custom-distro.txt-el4
- ### createPassword()
- # This function takes one parameter:
- # $1 is the message to display
- #Creates a password hash for the ks.cfg file.
- # See "man openssl" for info on changing hash algorithms.
- function createPassword(){
- echo "$1"
- passhash=$(openssl passwd -1 | sed 's/\//\\\//g')
- }
- ### timer()
- # This function takes one parameter:
- # $1 is the text to display
- # Displays text in blue and delays for 3 seconds
- function timer() {
- echo -en "\033[34m$1\033[0m"
- for (( i=0; i<3; ++i )); do
- sleep 1
- prompt="."
- echo -en "\033[34m$prompt\033[0m"
- done
- echo
- }
- ### usage()
- # This function takes no parameters:
- # Displays a usage message and a help file to the user.
- function usage(){
- echo "$(tput bold)USAGE:$(tput sgr0) $(basename "$0") -k | -r filename" >&2
- cat << EOF
- OPTIONS:
- -k Create a DVD that installs CentOS automatically using a customized kickstart file.
- -r Create a customized CentOS installation DVD that boots with user interaction.
- You must enter either "-k" for a kickstart DVD or "-r" for a regular DVD.
- A valid (in Microsoft Windows) name for your new ISO file is also a required argument.
- EXAMPLE: $0 -k My-Custom-DVD
- EOF
- }
- ### validateWindowsFilename()
- # This function takes one parameter:
- # $1 proposed name that the file will be saved as.
- # Assures that the filename is valid in Microsoft Windows.
- #Windows is fussy. As well as slashes, it doesn't allow colons,backslashes, asterisks, question marks, whitespace other than the space character itself, less-than and greater-than signs, double quotes and the pipe symbol.
- #There are also a small number of reserved file names that cannot be created, such as "CON", "PRN", "CLOCK$" COM[0-9]and "LPT[0-9]" -- these special filenames were used to represent various hardware devices in DOS.
- function validateWindowsFilename(){
- originalfilename="$1"
- # Strip off all trailing whitespace
- validfilename=$(echo "$1" | sed 's/[ ]*$//')
- # Strip off trailing periods (if any)
- validfilename=$(echo $validfilename | sed 's/[.]*$//')
- # Strip off leading periods (if any)
- validfilename=$(echo $validfilename | sed 's/^[.]*//')
- # Strip of the ".iso" extension (if present)
- validfilename=$(echo $validfilename | sed 's/.[iI][sS][oO]$//')
- # Test for invalid MS Windows characters:
- # \ / : * ? " < > |
- if $(echo $validfilename | egrep -q "[\/?<>:”*|]+"); then
- echo -e "The following characters are not valid in Microsoft Windows filenames:\n\ / : * ? \" < > |"
- exit 2
- fi
- # Test for invalid MS Windows filenames:
- list="AUX CLOCK[$] COM[0-9] CON PRN NUL LPT[0-9]"
- for name in $list; do
- if $(echo $validfilename | egrep -iq "^$name$"); then
- echo -e "You cannot name your file \"$originalfilename\".\nThis is not a valid name in Microsoft Windows."
- exit 2
- fi
- done
- filename="$validfilename.iso"
- }
- ##### CHECK OPTIONS #####
- if [ $(echo "$#") -ne 2 ]; then
- usage
- exit 1
- fi
- while getopts "kr:" arg
- do
- case $arg in
- k)
- burntype="kickstart"
- label="CentOS-5.5 Kickstart"
- ;;
- r)
- burntype="regular"
- label="CentOS-5.5 Installation"
- ;;
- \?) ### the help function that displays a usage message
- usage
- exit 1
- ;;
- esac
- done
- # Check to see if a valid filename has been supplied by the user.
- filename="$2"
- validateWindowsFilename $filename
- ##### PREPARE FILES AND DIRECTIORIES #####
- # Check that this script is running on a CentOS host machine.
- if [ ! $(cat /etc/issue | head -1 | cut -d" " -f1) == "CentOS" ]; then
- echo "This script must be run on a CentOS operating system to execute properly."
- exit 3
- fi
- # Check for files needed by this script.
- timer "Verifying files"
- if [ $burntype == "kickstart" ]; then
- neededfiles="CentOS-5.5-x86_64-bin-DVD-1of2.iso CentOS-5.5-x86_64-bin-DVD-2of2.iso comps.xml ks.cfg rpm.list"
- else
- neededfiles="CentOS-5.5-x86_64-bin-DVD-1of2.iso CentOS-5.5-x86_64-bin-DVD-2of2.iso comps.xml rpm.list"
- fi
- for reqname in $neededfiles; do
- if [ ! -f $reqname ]; then
- echo -e "The following files are required to be present in the same directory as this script for it to run properly: \"CentOS-5.5-x86_64-bin-DVD-1of2.iso\" \"CentOS-5.5-x86_64-bin-DVD-2of2.iso\", \"comps.xml\", \"ks.cfg\", and \"rpm.list\".\nThe file $filename is not present in this directory."
- exit 3
- fi
- done
- # Check for the presence of all required directories.
- timer "Verifying directories"
- directorylist="./all_rpms ./custom_rpms /mnt"
- for reqfolder in $directorylist; do
- if [ ! -d $reqfolder ]; then
- mkdir $reqfolder
- fi
- done
- # Count the number of RPMS in "all_rpms"
- # If the bare minimum number of RPM files from both DVDs are not there, re-copy them all.
- timer "Resolving RPM files"
- filecount=$(dir ./all_rpms | awk 'END{print NR}')
- if [ $filecount -lt 3433 ]; then
- mount -o loop ./CentOS-5.5-x86_64-bin-DVD-1of2.iso /mnt
- cp /mnt/CentOS/* ./all_rpms/
- umount /mnt
- mount -o loop ./CentOS-5.5-x86_64-bin-DVD-2of2.iso /mnt
- cp /mnt/CentOS/* ./all_rpms/
- umount /mnt
- fi
- while read rpmitem; do
- cp ./all_rpms/$rpmitem ./custom_rpms
- done < ./rpm.list
- # Remove all old build files
- if [ -d ./kickstart_build ]; then
- rm -rf ./kickstart_build
- fi
- ##### BUILD THE DVD ######
- # In the current directory, create an iso build directory:
- timer "Building iso directory tree"
- if [ $burntype == "kickstart" ]; then
- mkdir -p ./kickstart_build/{isolinux,images,ks}
- else
- mkdir -p ./kickstart_build/{isolinux,images}
- fi
- # Mount the CentOS DVD image, avoid read-only mount error message
- mount -o loop ./CentOS-5.5-x86_64-bin-DVD-1of2.iso /mnt > /dev/null 2>&1
- ##### COPY FILES FROM THE CentOS DVD #####
- timer "Copying files to iso"
- # Copy all the files from the isolinux directory on CentOS disc 1 into your kickstart_build/isolinux directory.
- cp -R /mnt/isolinux/* ./kickstart_build/isolinux
- # Also copy .discinfo from the CentOS disc 1 into your kickstart_build/isolinux directory.
- cp /mnt/.discinfo ./kickstart_build
- # Copy all the files from the images directory on CentOS disc 1 into your kickstart_build/isolinux/images directory.
- cp -R /mnt/images/* ./kickstart_build/images
- # Copy your customized comps.xml to kickstart_build.
- cp -f ./comps.xml ./kickstart_build/comps.xml
- chmod 644 ./kickstart_build/comps.xml
- # Your kickstart config file will go into the kickstart_build/ks directory.
- if [ $burntype == "kickstart" ]; then
- timer "Configuring kickstart files"
- cp ./ks.cfg ./kickstart_build/ks/ks.cfg
- # Change permissions on 3 files:
- chmod 664 ./kickstart_build/isolinux/isolinux.bin
- chmod 664 ./kickstart_build/isolinux/isolinux.cfg
- chmod 664 ./kickstart_build/ks/ks.cfg
- # Change the root password in the ks.cfg file
- createPassword "Please enter a root password."
- sed -i 's/^rootpw .*$/rootpw --iscrypted '$passhash'/' ./kickstart_build/ks/ks.cfg
- # Change the user password in the ks.cfg file
- read -p "Please enter a username. " username
- createPassword "Please enter a user password."
- sed -i 's/^user .*$/user --name='$username' --password='$passhash' --iscrypted/' ./kickstart_build/ks/ks.cfg
- # Modify the "isolinux.cfg" file, this is done so that the kickstart runs automatically.
- # Change the line: "default linux" to "default ks"
- sed -i 's/^default linux$/default ks/' ./kickstart_build/isolinux/isolinux.cfg
- # Change the line: "append ks initrd=initrd.img" to "append ks=cdrom:/CentOS-VM-ks.cfg initrd=initrd.img"
- sed -i 's/^[ ]*append ks initrd=initrd.img$/ append ks=cdrom:\/ks\/ks.cfg initrd=initrd.img/' ./kickstart_build/isolinux/isolinux.cfg
- fi
- # Install createrepo so that you can create a repository.
- if [ ! $(rpm -q createrepo) ]; then
- yum -y install createrepo
- fi
- # Load RPMs on to new DVD.
- timer "Copying RPM files"
- if [ ! -d ./kickstart_build/CentOS ]; then
- mkdir ./kickstart_build/CentOS
- fi
- cp -R ./custom_rpms/* ./kickstart_build/CentOS
- # Build repository on the new DVD
- timer "Building repository"
- cd ./kickstart_build
- declare -x discinfo=$(head -1 .discinfo)
- createrepo -u "media://$discinfo" -g comps.xml .
- cd ../
- if [ $burntype == "kickstart" ]; then
- # Change permissions back to default for "isolinux.cfg" and "isolinux.bin"
- chmod 555 ./kickstart_build/isolinux/isolinux.cfg
- chmod 444 ./kickstart_build/isolinux/isolinux.bin
- fi
- # Create the iso file.
- timer "Building image file"
- mkisofs -r -N -L -d -J -T -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -V "$label" -boot-load-size 4 -boot-info-table -o "$filename" kickstart_build/
- # Add checksum to iso (not working !!!):
- # implantisomd5 ./CentOS-VM.iso
- # Record DVD automatically (not tested):
- #growisofs -Z /dev/cdrom=./CentOS-VM.iso
- umount /mnt
- #chown $REAL_USER $filename
- # Finished!!!
- echo -e "\033[31mDone!!!\033[0m"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement