#!/bin/bash # # Script de creation d'un media bootable Debian # Pour l'ACAQB # # Dépend : syslinux,wget # OPTIND=1 DEBIAN_VERSION="8.6.0" DEBIAN_ARCH="amd64" BOOT_TYPE="efi64" SYSLINUX_EXE=`which syslinux` SYSLINUX_FILES="menu.c32 vesamenu.c32 libutil.c32 libcom32.c32" SYSLINUX_BIOS_FILES="" SYSLINUX_EFI_FILES="ldlinux.e64" TMP_DIR="tmp" REPO_DIR="repo" MOUNT_DIR="/mnt/usbstick" current_dir=`pwd` usage() { cat << EOF USAGE: ${0} [-a architecture] [-b] -a amd64 | i386 CPU architecture -b bios boot type (default is efi64) -d device_block USB key to write ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe. EOF } download (){ #Download function # $1 : filename # $2 : url # $3 : 1 if abort download if file exist echo -ne "Downloading $1 ... " if [[ -f $1 && $3 == "1" ]] then echo file already downloaded. return fi wget ‐‐no-clobber -4 $2/$1 &> /dev/null echo "done!" } create_dir(){ # $1 directory to create if [ ! -d $1 ] then mkdir -p $1 else if [[ $2 == 1 ]] then rm -rf $1 create_dir $1 fi fi } #Stop script if syslinux is not installed if [[ $SYSLINUX_EXE == "" ]] then echo "Syslinux not found, script can't continue…" exit 1 fi if [ -d /usr/lib/syslinux/efi64 ] then syslinux_mod="/usr/lib/syslinux" else if [ -d /usr/lib/syslinux/modules/efi64 ] then syslinux_mod="/usr/lib/syslinux/modules" else echo "Syslinux modules folder not found, exiting" exit 1 fi fi # Arguments ... while getopts "ha:bd:" opt; do case "$opt" in h) usage exit 0 ;; a) if [[ $OPTARG == "amd64" || $OPTARG == "i386" ]] then DEBIAN_ARCH=$OPTARG else echo "CPU architecture must be amd64 of i386, bye." exit 1 fi ;; b) BOOT_TYPE="bios" ;; d) if [ -b $OPTARG ] then dest=$OPTARG else echo "Destination is not a block device, bye." exit 1 fi ;; esac done bootfiles_Debian="http://ftp.debian.org/debian/dists/jessie/main/installer-${DEBIAN_ARCH}/current/images/hd-media" iso_url="http://cdimage.debian.org/debian-cd/${DEBIAN_VERSION}/${DEBIAN_ARCH}/iso-cd" iso_file="debian-${DEBIAN_VERSION}-${DEBIAN_ARCH}-netinst.iso" repo_path="$REPO_DIR/$DEBIAN_ARCH/$DEBIAN_VERSION" # Download Debian files (kernel, initrd and iso) echo -e "\nPrepare Debian files\n---" # Création du répertoire temporaire create_dir $repo_path cd $repo_path download $iso_file $iso_url 1 for file in "vmlinuz" "initrd.gz" do download $file $bootfiles_Debian 1 done cd $current_dir if [[ -z $dest ]] then echo "No destination device specified, USB key will not be created." fi create_dir $TMP_DIR 1 cp $repo_path/$iso_file $TMP_DIR # Copy wanted syslinux file from archive case $BOOT_TYPE in efi64) if [ -f /usr/lib/syslinux/efi64/syslinux.efi ] then syslinux_efi="/usr/lib/syslinux/efi64/syslinux.efi" else if [ -d /usr/lib/SYSLINUX.EFI/modules/efi64/syslinux.efi ] then syslinux_efi="/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi" else echo "Syslinux efi bootlader not found" exit 1 fi fi syslinux_folder=${TMP_DIR}/efi/EFI/boot create_dir $syslinux_folder cp -T ${syslinux_efi} $syslinux_folder/bootx64.efi &> /dev/null for file in $SYSLINUX_EFI_FILES do echo "copying $file ..." cp $syslinux_mod/$BOOT_TYPE/$file $syslinux_folder done for file in $SYSLINUX_FILES do echo "copie de $file" cp ${syslinux_mod}/${BOOT_TYPE}/${file} $syslinux_folder done cp $repo_path/{vmlinuz,initrd.gz} $TMP_DIR/efi cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null ;; bios) create_dir ${TMP_DIR}/syslinux 1 for file in $SYSLINUX_FILES do echo "copie de $file" cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/ done for file in $SYSLINUX_BIOS_FILES do echo "copying $file ..." cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/ done cp -R syslinux $TMP_DIR &> /dev/null ;; esac echo Copying configurations files create_dir $TMP_DIR/partman_recipes cp -R partman_recipes $TMP_DIR &> /dev/null create_dir $TMP_DIR/preseeds cp -R preseeds $TMP_DIR &> /dev/null if [[ -n $dest ]] then read -p "[WARNING] Disk $dest will be erased continue [Y/N]? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 1 fi #dd if=/dev/zero of=${dest} bs=512 count=1 conv=notrunc &> /dev/null echo -e "\nCreating partition" parted -s $dest mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on parted -s $dest mkpart EFS fat32 128MiB 100% mkfs.vfat ${dest}1 -n efi &> /dev/null mkfs.vfat ${dest}2 -n debian &> /dev/null # mount and copy create_dir $MOUNT_DIR mount ${dest}2 $MOUNT_DIR create_dir $MOUNT_DIR/efi mount ${dest}1 $MOUNT_DIR/efi echo "copying all file to USB drive" cp -R ${TMP_DIR}/* $MOUNT_DIR umount $MOUNT_DIR/efi umount $MOUNT_DIR echo Unmounting key, please wait ... sleep 5 # Make key bootable if bios. if [[ $BOOT_TYPE == "bios" ]] then # In this mode, we need to write syslinux MBR. echo "Writing syslinux mbr.ini fo $dest" dd bs=440 count=1 conv=notrunc if=${syslinux_mod}/${BOOT_TYPE}/mbr.bin of=${dest} sleep 2 syslinux --directory /syslinux --install ${dest}1 fi exit 0 #rm -rf $TMP_DIR fi