A tool to create Debian automatic installer USB key with preseed, partman recipe, syslinux and parted.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.sh 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/bash
  2. #
  3. # Script de creation d'un media bootable Debian
  4. # Pour l'ACAQB
  5. #
  6. # Dépend : syslinux,wget
  7. #
  8. OPTIND=1
  9. DEBIAN_VERSION="9.2.0"
  10. DEBIAN_ARCH="amd64"
  11. BOOT_TYPE="efi64"
  12. SYSLINUX_EXE=`which syslinux`
  13. SYSLINUX_FILES="menu.c32 vesamenu.c32 libutil.c32 libcom32.c32"
  14. SYSLINUX_BIOS_FILES=""
  15. SYSLINUX_EFI_FILES="ldlinux.e64"
  16. TMP_DIR="tmp"
  17. REPO_DIR="repo"
  18. MOUNT_DIR="/mnt/usbstick"
  19. current_dir=`pwd`
  20. usage() {
  21. cat << EOF
  22. USAGE: ${0} [-a architecture] [-b]
  23. -a amd64 | i386 CPU architecture
  24. -b bios boot type (default is efi64)
  25. -d device_block USB key to write
  26. ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe.
  27. EOF
  28. }
  29. download (){
  30. #Download function
  31. # $1 : filename
  32. # $2 : url
  33. # $3 : 1 if abort download if file exist
  34. echo -ne "Downloading $1 ... "
  35. if [[ -f $1 && $3 == "1" ]]
  36. then
  37. echo file already downloaded.
  38. return
  39. fi
  40. wget ‐‐no-clobber -4 $2/$1 &> /dev/null
  41. echo "done!"
  42. }
  43. create_dir(){
  44. # $1 directory to create
  45. if [ ! -d $1 ]
  46. then
  47. mkdir -p $1
  48. else
  49. if [[ $2 == 1 ]]
  50. then
  51. rm -rf $1
  52. create_dir $1
  53. fi
  54. fi
  55. }
  56. #Stop script if syslinux is not installed
  57. if [[ $SYSLINUX_EXE == "" ]]
  58. then
  59. echo "Syslinux not found, script can't continue…"
  60. exit 1
  61. fi
  62. if [ -d /usr/lib/syslinux/efi64 ]
  63. then
  64. syslinux_mod="/usr/lib/syslinux"
  65. else
  66. if [ -d /usr/lib/syslinux/modules/efi64 ]
  67. then
  68. syslinux_mod="/usr/lib/syslinux/modules"
  69. else
  70. echo "Syslinux modules folder not found, exiting"
  71. exit 1
  72. fi
  73. fi
  74. # Arguments ...
  75. while getopts "ha:bd:" opt; do
  76. case "$opt" in
  77. h)
  78. usage
  79. exit 0
  80. ;;
  81. a)
  82. if [[ $OPTARG == "amd64" || $OPTARG == "i386" ]]
  83. then
  84. DEBIAN_ARCH=$OPTARG
  85. else
  86. echo "CPU architecture must be amd64 of i386, bye."
  87. exit 1
  88. fi
  89. ;;
  90. b)
  91. BOOT_TYPE="bios"
  92. ;;
  93. d)
  94. if [ -b $OPTARG ]
  95. then
  96. dest=$OPTARG
  97. else
  98. echo "Destination is not a block device, bye."
  99. exit 1
  100. fi
  101. ;;
  102. esac
  103. done
  104. bootfiles_Debian="http://ftp.debian.org/debian/dists/jessie/main/installer-${DEBIAN_ARCH}/current/images/hd-media"
  105. iso_url="http://cdimage.debian.org/debian-cd/${DEBIAN_VERSION}/${DEBIAN_ARCH}/iso-cd"
  106. iso_file="debian-${DEBIAN_VERSION}-${DEBIAN_ARCH}-netinst.iso"
  107. repo_path="$REPO_DIR/$DEBIAN_ARCH/$DEBIAN_VERSION"
  108. # Download Debian files (kernel, initrd and iso)
  109. echo -e "\nPrepare Debian files\n---"
  110. # Création du répertoire temporaire
  111. create_dir $repo_path
  112. cd $repo_path
  113. download $iso_file $iso_url 1
  114. for file in "vmlinuz" "initrd.gz"
  115. do
  116. download $file $bootfiles_Debian 1
  117. done
  118. cd $current_dir
  119. if [[ -z $dest ]]
  120. then
  121. echo "No destination device specified, USB key will not be created."
  122. fi
  123. create_dir $TMP_DIR 1
  124. cp $repo_path/$iso_file $TMP_DIR
  125. # Copy wanted syslinux file from archive
  126. case $BOOT_TYPE in
  127. efi64)
  128. if [ -f /usr/lib/syslinux/efi64/syslinux.efi ]
  129. then
  130. syslinux_efi="/usr/lib/syslinux/efi64/syslinux.efi"
  131. else
  132. if [ -d /usr/lib/SYSLINUX.EFI/modules/efi64/syslinux.efi ]
  133. then
  134. syslinux_efi="/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi"
  135. else
  136. echo "Syslinux efi bootlader not found"
  137. exit 1
  138. fi
  139. fi
  140. syslinux_folder=${TMP_DIR}/efi/EFI/boot
  141. create_dir $syslinux_folder
  142. cp -T ${syslinux_efi} $syslinux_folder/bootx64.efi &> /dev/null
  143. for file in $SYSLINUX_EFI_FILES
  144. do
  145. echo "copying $file ..."
  146. cp $syslinux_mod/$BOOT_TYPE/$file $syslinux_folder
  147. done
  148. for file in $SYSLINUX_FILES
  149. do
  150. echo "copie de $file"
  151. cp ${syslinux_mod}/${BOOT_TYPE}/${file} $syslinux_folder
  152. done
  153. cp $repo_path/{vmlinuz,initrd.gz} $TMP_DIR/efi
  154. cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null
  155. ;;
  156. bios)
  157. create_dir ${TMP_DIR}/syslinux 1
  158. for file in $SYSLINUX_FILES
  159. do
  160. echo "copie de $file"
  161. cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/
  162. done
  163. for file in $SYSLINUX_BIOS_FILES
  164. do
  165. echo "copying $file ..."
  166. cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/
  167. done
  168. cp -R syslinux $TMP_DIR &> /dev/null
  169. ;;
  170. esac
  171. echo Copying configurations files
  172. create_dir $TMP_DIR/partman_recipes
  173. cp -R partman_recipes $TMP_DIR &> /dev/null
  174. create_dir $TMP_DIR/preseeds
  175. cp -R preseeds $TMP_DIR &> /dev/null
  176. if [[ -n $dest ]]
  177. then
  178. read -p "[WARNING] Disk $dest will be erased continue [Y/N]? " -n 1 -r
  179. if [[ ! $REPLY =~ ^[Yy]$ ]]
  180. then
  181. exit 1
  182. fi
  183. #dd if=/dev/zero of=${dest} bs=512 count=1 conv=notrunc &> /dev/null
  184. echo -e "\nCreating partition"
  185. parted -s $dest mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on
  186. parted -s $dest mkpart EFS fat32 128MiB 100%
  187. mkfs.vfat ${dest}1 -n efi &> /dev/null
  188. mkfs.vfat ${dest}2 -n debian &> /dev/null
  189. # mount and copy
  190. create_dir $MOUNT_DIR
  191. mount ${dest}2 $MOUNT_DIR
  192. create_dir $MOUNT_DIR/efi
  193. mount ${dest}1 $MOUNT_DIR/efi
  194. echo "copying all file to USB drive"
  195. cp -R ${TMP_DIR}/* $MOUNT_DIR
  196. umount $MOUNT_DIR/efi
  197. umount $MOUNT_DIR
  198. echo Unmounting key, please wait ...
  199. sleep 5
  200. # Make key bootable if bios.
  201. if [[ $BOOT_TYPE == "bios" ]]
  202. then
  203. # In this mode, we need to write syslinux MBR.
  204. echo "Writing syslinux mbr.ini fo $dest"
  205. dd bs=440 count=1 conv=notrunc if=${syslinux_mod}/${BOOT_TYPE}/mbr.bin of=${dest}
  206. sleep 2
  207. syslinux --directory /syslinux --install ${dest}1
  208. fi
  209. exit 0
  210. #rm -rf $TMP_DIR
  211. fi