Advertisement
Guest User

Custom-CentOS-5.5-iso_Create-Custom-iso.sh

a guest
Jan 22nd, 2011
2,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ------------------------------------------------------------------------
  4. # Copyright 2010 Lorin Soura <gr8can8dian@gmail.com>
  5. # This script is licensed under GNU GPL version 3.0 or above
  6. # -------------------------------------------------------------------------
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11.  
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16.  
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # -------------------------------------------------------------------------
  20.  
  21. ### Script to build a custom Centos 5.5 64-bit server edition iso image for: regular installation or kickstart deployment(s).
  22.  
  23. ##### SOURCES USED #####
  24.  
  25. # Source: http://smorgasbork.com/component/content/article/35-linux/58-building-a-custom-centos-5-kickstart-disc-part-1
  26. # Source: http://cinq.com/2008/04/14/rebuilding-the-dvd-iso-for-centos-5-or-rhel-5/
  27. # Source: http://people.redhat.com/rkeech/custom-distro.txt-el4
  28.  
  29. ### createPassword()
  30. # This function takes one parameter:
  31. # $1 is the message to display
  32. #Creates a password hash for the ks.cfg file.
  33. # See "man openssl" for info on changing hash algorithms.
  34. function createPassword(){
  35. echo "$1"
  36. passhash=$(openssl passwd -1 | sed 's/\//\\\//g')
  37. }
  38.  
  39. ### timer()
  40. # This function takes one parameter:
  41. # $1 is the text to display
  42. # Displays text in blue and delays for 3 seconds
  43. function timer() {
  44. echo -en "\033[34m$1\033[0m"
  45. for (( i=0; i<3; ++i )); do
  46. sleep 1
  47. prompt="."
  48. echo -en "\033[34m$prompt\033[0m"
  49. done
  50. echo
  51. }
  52.  
  53. ### usage()
  54. # This function takes no parameters:
  55. # Displays a usage message and a help file to the user.
  56. function usage(){
  57. echo "$(tput bold)USAGE:$(tput sgr0) $(basename "$0") -k | -r filename" >&2
  58. cat << EOF
  59. OPTIONS:
  60. -k Create a DVD that installs CentOS automatically using a customized kickstart file.
  61. -r Create a customized CentOS installation DVD that boots with user interaction.
  62.  
  63. You must enter either "-k" for a kickstart DVD or "-r" for a regular DVD.
  64. A valid (in Microsoft Windows) name for your new ISO file is also a required argument.
  65.  
  66. EXAMPLE: $0 -k My-Custom-DVD
  67. EOF
  68. }
  69.  
  70. ### validateWindowsFilename()
  71. # This function takes one parameter:
  72. # $1 proposed name that the file will be saved as.
  73. # Assures that the filename is valid in Microsoft Windows.
  74. #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.
  75. #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.
  76. function validateWindowsFilename(){
  77.  
  78. originalfilename="$1"
  79.  
  80. # Strip off all trailing whitespace
  81. validfilename=$(echo "$1" | sed 's/[ ]*$//')
  82.  
  83. # Strip off trailing periods (if any)
  84. validfilename=$(echo $validfilename | sed 's/[.]*$//')
  85.  
  86. # Strip off leading periods (if any)
  87. validfilename=$(echo $validfilename | sed 's/^[.]*//')
  88.  
  89. # Strip of the ".iso" extension (if present)
  90. validfilename=$(echo $validfilename | sed 's/.[iI][sS][oO]$//')
  91.  
  92. # Test for invalid MS Windows characters:
  93. # \ / : * ? " < > |
  94. if $(echo $validfilename | egrep -q "[\/?<>:”*|]+"); then
  95. echo -e "The following characters are not valid in Microsoft Windows filenames:\n\ / : * ? \" < > |"
  96. exit 2
  97. fi
  98. # Test for invalid MS Windows filenames:
  99. list="AUX CLOCK[$] COM[0-9] CON PRN NUL LPT[0-9]"
  100. for name in $list; do
  101. if $(echo $validfilename | egrep -iq "^$name$"); then
  102. echo -e "You cannot name your file \"$originalfilename\".\nThis is not a valid name in Microsoft Windows."
  103. exit 2
  104. fi
  105. done
  106. filename="$validfilename.iso"
  107. }
  108.  
  109. ##### CHECK OPTIONS #####
  110.  
  111. if [ $(echo "$#") -ne 2 ]; then
  112. usage
  113. exit 1
  114. fi
  115.  
  116. while getopts "kr:" arg
  117. do
  118. case $arg in
  119. k)
  120. burntype="kickstart"
  121. label="CentOS-5.5 Kickstart"
  122. ;;
  123.  
  124. r)
  125. burntype="regular"
  126.  
  127. label="CentOS-5.5 Installation"
  128. ;;
  129.  
  130. \?) ### the help function that displays a usage message
  131. usage
  132. exit 1
  133. ;;
  134. esac
  135. done
  136.  
  137. # Check to see if a valid filename has been supplied by the user.
  138. filename="$2"
  139. validateWindowsFilename $filename
  140.  
  141. ##### PREPARE FILES AND DIRECTIORIES #####
  142.  
  143. # Check that this script is running on a CentOS host machine.
  144. if [ ! $(cat /etc/issue | head -1 | cut -d" " -f1) == "CentOS" ]; then
  145. echo "This script must be run on a CentOS operating system to execute properly."
  146. exit 3
  147. fi
  148.  
  149. # Check for files needed by this script.
  150. timer "Verifying files"
  151.  
  152. if [ $burntype == "kickstart" ]; then
  153. 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"
  154. else
  155. neededfiles="CentOS-5.5-x86_64-bin-DVD-1of2.iso CentOS-5.5-x86_64-bin-DVD-2of2.iso comps.xml rpm.list"
  156. fi
  157.  
  158. for reqname in $neededfiles; do
  159. if [ ! -f $reqname ]; then
  160. 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."
  161. exit 3
  162. fi
  163. done
  164.  
  165. # Check for the presence of all required directories.
  166. timer "Verifying directories"
  167.  
  168. directorylist="./all_rpms ./custom_rpms /mnt"
  169. for reqfolder in $directorylist; do
  170. if [ ! -d $reqfolder ]; then
  171. mkdir $reqfolder
  172. fi
  173. done
  174.  
  175. # Count the number of RPMS in "all_rpms"
  176. # If the bare minimum number of RPM files from both DVDs are not there, re-copy them all.
  177. timer "Resolving RPM files"
  178.  
  179. filecount=$(dir ./all_rpms | awk 'END{print NR}')
  180. if [ $filecount -lt 3433 ]; then
  181. mount -o loop ./CentOS-5.5-x86_64-bin-DVD-1of2.iso /mnt
  182. cp /mnt/CentOS/* ./all_rpms/
  183. umount /mnt
  184.  
  185. mount -o loop ./CentOS-5.5-x86_64-bin-DVD-2of2.iso /mnt
  186. cp /mnt/CentOS/* ./all_rpms/
  187. umount /mnt
  188. fi
  189.  
  190. while read rpmitem; do
  191. cp ./all_rpms/$rpmitem ./custom_rpms
  192. done < ./rpm.list
  193.  
  194. # Remove all old build files
  195. if [ -d ./kickstart_build ]; then
  196. rm -rf ./kickstart_build
  197. fi
  198.  
  199. ##### BUILD THE DVD ######
  200.  
  201. # In the current directory, create an iso build directory:
  202. timer "Building iso directory tree"
  203.  
  204. if [ $burntype == "kickstart" ]; then
  205. mkdir -p ./kickstart_build/{isolinux,images,ks}
  206. else
  207. mkdir -p ./kickstart_build/{isolinux,images}
  208. fi
  209.  
  210. # Mount the CentOS DVD image, avoid read-only mount error message
  211. mount -o loop ./CentOS-5.5-x86_64-bin-DVD-1of2.iso /mnt > /dev/null 2>&1
  212.  
  213. ##### COPY FILES FROM THE CentOS DVD #####
  214. timer "Copying files to iso"
  215.  
  216. # Copy all the files from the isolinux directory on CentOS disc 1 into your kickstart_build/isolinux directory.
  217. cp -R /mnt/isolinux/* ./kickstart_build/isolinux
  218.  
  219. # Also copy .discinfo from the CentOS disc 1 into your kickstart_build/isolinux directory.
  220. cp /mnt/.discinfo ./kickstart_build
  221.  
  222. # Copy all the files from the images directory on CentOS disc 1 into your kickstart_build/isolinux/images directory.
  223. cp -R /mnt/images/* ./kickstart_build/images
  224.  
  225. # Copy your customized comps.xml to kickstart_build.
  226. cp -f ./comps.xml ./kickstart_build/comps.xml
  227. chmod 644 ./kickstart_build/comps.xml
  228.  
  229. # Your kickstart config file will go into the kickstart_build/ks directory.
  230. if [ $burntype == "kickstart" ]; then
  231. timer "Configuring kickstart files"
  232. cp ./ks.cfg ./kickstart_build/ks/ks.cfg
  233.  
  234. # Change permissions on 3 files:
  235. chmod 664 ./kickstart_build/isolinux/isolinux.bin
  236. chmod 664 ./kickstart_build/isolinux/isolinux.cfg
  237. chmod 664 ./kickstart_build/ks/ks.cfg
  238.  
  239. # Change the root password in the ks.cfg file
  240. createPassword "Please enter a root password."
  241. sed -i 's/^rootpw .*$/rootpw --iscrypted '$passhash'/' ./kickstart_build/ks/ks.cfg
  242.  
  243. # Change the user password in the ks.cfg file
  244. read -p "Please enter a username. " username
  245. createPassword "Please enter a user password."
  246. sed -i 's/^user .*$/user --name='$username' --password='$passhash' --iscrypted/' ./kickstart_build/ks/ks.cfg
  247.  
  248. # Modify the "isolinux.cfg" file, this is done so that the kickstart runs automatically.
  249. # Change the line: "default linux" to "default ks"
  250. sed -i 's/^default linux$/default ks/' ./kickstart_build/isolinux/isolinux.cfg
  251.  
  252. # Change the line: "append ks initrd=initrd.img" to "append ks=cdrom:/CentOS-VM-ks.cfg initrd=initrd.img"
  253. sed -i 's/^[ ]*append ks initrd=initrd.img$/ append ks=cdrom:\/ks\/ks.cfg initrd=initrd.img/' ./kickstart_build/isolinux/isolinux.cfg
  254. fi
  255.  
  256. # Install createrepo so that you can create a repository.
  257. if [ ! $(rpm -q createrepo) ]; then
  258. yum -y install createrepo
  259. fi
  260.  
  261. # Load RPMs on to new DVD.
  262. timer "Copying RPM files"
  263.  
  264. if [ ! -d ./kickstart_build/CentOS ]; then
  265. mkdir ./kickstart_build/CentOS
  266. fi
  267.  
  268. cp -R ./custom_rpms/* ./kickstart_build/CentOS
  269.  
  270. # Build repository on the new DVD
  271. timer "Building repository"
  272. cd ./kickstart_build
  273.  
  274. declare -x discinfo=$(head -1 .discinfo)
  275. createrepo -u "media://$discinfo" -g comps.xml .
  276.  
  277. cd ../
  278.  
  279. if [ $burntype == "kickstart" ]; then
  280. # Change permissions back to default for "isolinux.cfg" and "isolinux.bin"
  281. chmod 555 ./kickstart_build/isolinux/isolinux.cfg
  282. chmod 444 ./kickstart_build/isolinux/isolinux.bin
  283. fi
  284.  
  285. # Create the iso file.
  286. timer "Building image file"
  287. 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/
  288.  
  289. # Add checksum to iso (not working !!!):
  290. # implantisomd5 ./CentOS-VM.iso
  291.  
  292. # Record DVD automatically (not tested):
  293. #growisofs -Z /dev/cdrom=./CentOS-VM.iso
  294.  
  295. umount /mnt
  296.  
  297. #chown $REAL_USER $filename
  298.  
  299. # Finished!!!
  300. echo -e "\033[31mDone!!!\033[0m"
  301.  
  302. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement