From 50d2b171fa87faaba5fbf40764b9d06b5abbc40b Mon Sep 17 00:00:00 2001 From: Yorick Date: Fri, 27 Jan 2017 00:34:33 +0100 Subject: [PATCH 01/26] Full rewrite --- init.sh | 321 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 190 insertions(+), 131 deletions(-) diff --git a/init.sh b/init.sh index f6d5bde..755f68d 100755 --- a/init.sh +++ b/init.sh @@ -6,42 +6,122 @@ # Dépend : syslinux,wget # OPTIND=1 -DEBIAN_VERSION="8.7.1" -DEBIAN_ARCH="amd64" -BOOT_TYPE="efi64" -SYSLINUX_EXE=`which syslinux` + +# Default Variables +debian_arch="amd64" +debian_version="8.7.1" +boot_type="efi64" +repo_dir="repo" +tmp_dir="tmp" +current_dir=`pwd` +# In Archlinux, this is default files for syslinux... +syslinux_modules_dir="/usr/lib/syslinux" +syslinux_efi_bootloader="/usr/lib/syslinux" +key_mountpoint="/mnt/usbstick" +device="" +# constant +APP_NAME="Debian USB Creator" +VERSION="1.99.0" +SYSLINUX_EXE=$(which syslinux) +PARTED_EXE=$(which parted) 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` + +process_args() { + # While getops doesn't handle long parameters + # I need an personnal function + # Inspired by http://mywiki.wooledge.org/BashFAQ/035 + while :; do + case $1 in + -h|-\?|--help) + usage + exit 0 + ;; + -a|--arch) + if [[ $2 == "amd64" || $2 == "i386" ]] + then + debian_arch=$2 + shift + else + printf "Error : CPU architecture must be amd64 or i386" + exit 1 + fi + ;; + -b|--bios) + boot_type="bios" + ;; + -d|--device) + if [ -b $2 ] + then + device=$2 + shift + else + printf "%s doesn't a block device, exiting" "$2" + exit 1 + fi + ;; + --debian-version) + debian_version=$2 + shift + ;; + --repo) + repo_dir=$2 + shift + ;; + --tmp) + tmp_dir=$2 + shift + ;; + -v|--version) + show_version + exit 0 + ;; + *) + break + esac + shift + done +} 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 + -a | --arch Select CPU Architecture amd64 | i386 CPU + -b | --bios Create a bios bott compatible key + -d | --device Device_block USB key to write + --debian-ver Choose Debian Version to download + --repo Repo directory (defaukt is ./repo) + --temp Temp directory (default is ./tmp) + -v | --version Show version ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe. EOF } +show_version () { + printf "%s %s 2016-2017 ephase@xieme-art.org\n" "$APP_NAME" "$VERSION" +} + download (){ #Download function # $1 : filename # $2 : url # $3 : 1 if abort download if file exist - echo -ne "Downloading $1 ... " + printf "Downloading %s : " "$1" if [[ -f $1 && $3 == "1" ]] then - echo file already downloaded. + printf "\b\b\b\bfile already downloaded.\n" return fi - wget ‐‐no-clobber -4 $2/$1 &> /dev/null - echo "done!" + wget --no-clobber --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' + if [ $? -eq 0 ] + then + printf " done\n " + else + printf " error, exiting.\n" + exit 1 + fi } create_dir(){ @@ -58,66 +138,63 @@ create_dir(){ fi } +# unmount if device is mounted +umount_device (){ + local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') + if [ ! -z mounted_part ] + then + for part in $mounted_part + do + if [ -d $part ] + then + printf "Unmount %s\n " "$part" + umount $part --recursive + fi + done + fi +} + +efi_create_key_structure () { + parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on + parted -s $1 mkpart EFS fat32 128MiB 100% + mkfs.vfat ${1}1 -n efi &> /dev/null + mkfs.vfat ${1}2 -n debian &> /dev/null + mount ${1}2 $2 + create_dir $2/efi + mount ${1}1 $2/efi +} + +bios_create_key_structure() { + parted -s $1 mklabel gpt mkpart debian fat32 1MiB 100% set 1 boot on + mkfs.vfat ${1}2 -n debian &> /dev/null + mount ${1}1 $2 +} + #Stop script if syslinux is not installed -if [[ $SYSLINUX_EXE == "" ]] +if [ -z $SYSLINUX_EXE ] then - echo "Syslinux not found, script can't continue…" + printf "Syslinux not found, script can't continue.\n" 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 +if [ -z $PARTED_EXE ] +then + printf "Parted not found, script can't continue.\n" + exit 1 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 +process_args $@ -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" +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---" +printf "Downloading Debian files\n---\n" + # Création du répertoire temporaire create_dir $repo_path cd $repo_path @@ -128,103 +205,85 @@ do done cd $current_dir -if [[ -z $dest ]] +if [ -z $device ] then - echo "No destination device specified, USB key will not be created." + printf "No destination device specified, USB key will not be created.\n" fi -create_dir $TMP_DIR 1 -cp $repo_path/$iso_file $TMP_DIR +create_dir $tmp_dir 1 +cp $repo_path/$iso_file $tmp_dir # Copy wanted syslinux file from archive -case $BOOT_TYPE in +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 + syslinux_folder=${tmp_dir}/efi/EFI/boot create_dir $syslinux_folder - cp -T ${syslinux_efi} $syslinux_folder/bootx64.efi &> /dev/null + cp -T $syslinux_efi_bootloader/$boot_type/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 + printf "copying %s \n" "$file" + cp $syslinux_modules_dir/$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 $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 - + create_dir $tmp_dir/syslinux 1 for file in $SYSLINUX_BIOS_FILES do - echo "copying $file ..." - cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/ + printf "copying %s\n" "$file" + cp $syslinux_modules_dir/$boot_type/$file $tmp_dir/syslinux/ done - cp -R syslinux $TMP_DIR &> /dev/null + 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 ]] +for file in $SYSLINUX_FILES +do + printf "copying %s\n" "$file" + cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder +done + +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 [ ! -z $device ] then - read -p "[WARNING] Disk $dest will be erased continue [Y/N]? " -n 1 -r + printf "Partitionning and formating %s\n" "$device" + read -p "[WARNING] Disk $device will be erased continue [Y/N]? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 1 fi + printf "\n\n" + umount_device $device + create_dir $key_mountpoint #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 ... + case $boot_type in + "efi64") + efi_create_key_structure $device $key_mountpoint + ;; + "bios") + bios_create_key_structure $device $key_mountpoint + ;; + esac + printf "copying all file to USB drive\n" + cp -R $tmp_dir/* $key_mountpoint + umount_device $device sleep 5 # Make key bootable if bios. - if [[ $BOOT_TYPE == "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} + printf "Writing syslinux mbr.ini to %s\n" "$device" + dd bs=440 count=1 conv=notrunc if=${syslinux_mod}/${boot_type}/mbr.bin of=${dest} sleep 2 - syslinux --directory /syslinux --install ${dest}1 + $SYSLINUX_EXE --directory /syslinux --install ${device}1 fi - exit 0 - #rm -rf $TMP_DIR + rm -rf $TMP_DIR + printf "\nBootable USB key created\n" + exit 0 fi From 1c2829b34f0bcc4a998ae62056dbb899ea955675 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 30 Jan 2017 11:50:21 +0100 Subject: [PATCH 02/26] Change disk label for msdos in bios_vreate_key_structure --- init.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.sh b/init.sh index 755f68d..fa04da6 100755 --- a/init.sh +++ b/init.sh @@ -165,8 +165,8 @@ efi_create_key_structure () { } bios_create_key_structure() { - parted -s $1 mklabel gpt mkpart debian fat32 1MiB 100% set 1 boot on - mkfs.vfat ${1}2 -n debian &> /dev/null + parted -s $1 mklabel msdos mkpart debian fat32 1MiB 100% set 1 boot on + mkfs.vfat ${1}1 -n debian &> /dev/null mount ${1}1 $2 } From b45d5bfbede7ebd0abe3436c7b56a9d8b7da4d85 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 30 Jan 2017 12:16:20 +0100 Subject: [PATCH 03/26] Various fixes for bios key creation --- init.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/init.sh b/init.sh index fa04da6..6861243 100755 --- a/init.sh +++ b/init.sh @@ -165,7 +165,7 @@ efi_create_key_structure () { } bios_create_key_structure() { - parted -s $1 mklabel msdos mkpart debian fat32 1MiB 100% set 1 boot on + parted -s $1 mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on mkfs.vfat ${1}1 -n debian &> /dev/null mount ${1}1 $2 } @@ -228,12 +228,14 @@ case $boot_type in ;; bios) - create_dir $tmp_dir/syslinux 1 + syslinux_folder=${tmp_dir}/syslinux + create_dir $syslinux_folder for file in $SYSLINUX_BIOS_FILES do printf "copying %s\n" "$file" - cp $syslinux_modules_dir/$boot_type/$file $tmp_dir/syslinux/ + cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder done + cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir cp -R syslinux $tmp_dir &> /dev/null ;; esac @@ -279,7 +281,7 @@ then then # In this mode, we need to write syslinux MBR. printf "Writing syslinux mbr.ini to %s\n" "$device" - dd bs=440 count=1 conv=notrunc if=${syslinux_mod}/${boot_type}/mbr.bin of=${dest} + dd bs=440 count=1 conv=notrunc if=$syslinux_modules_dir/$boot_type/mbr.bin of=$device sleep 2 $SYSLINUX_EXE --directory /syslinux --install ${device}1 fi From 8cb126af3bf81a8f8fea6d284b84eded3649c864 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 30 Jan 2017 12:23:01 +0100 Subject: [PATCH 04/26] Wget use -c flag in download for resuming --- init.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/init.sh b/init.sh index 6861243..cd8d3d2 100755 --- a/init.sh +++ b/init.sh @@ -109,15 +109,10 @@ download (){ # $2 : url # $3 : 1 if abort download if file exist printf "Downloading %s : " "$1" - if [[ -f $1 && $3 == "1" ]] - then - printf "\b\b\b\bfile already downloaded.\n" - return - fi - wget --no-clobber --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' + wget -c --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' if [ $? -eq 0 ] then - printf " done\n " + printf " \b\b\b\bdone\n " else printf " error, exiting.\n" exit 1 From cc4a1f043fc6109e13fa9b60b6de70b6d4e4778b Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 30 Jan 2017 12:23:47 +0100 Subject: [PATCH 05/26] Updated for new version --- .gitignore | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 12c8f5f..20f9ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,2 @@ tmp/ -syslinux/*.c32 -syslinux/ldlinux.* -EFI -debian-8.5.0-amd64-netinst.iso -vmlinuz -initrd.gz +repo/ From cc5efbcc2157c5935576146baaf2705eefba9235 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 30 Jan 2017 21:40:38 +0100 Subject: [PATCH 06/26] Partition 2 is now primary in efi_create_key_structure() --- init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.sh b/init.sh index cd8d3d2..960f7ce 100755 --- a/init.sh +++ b/init.sh @@ -151,7 +151,7 @@ umount_device (){ efi_create_key_structure () { parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on - parted -s $1 mkpart EFS fat32 128MiB 100% + parted -s $1 mkpart primary fat32 128MiB 100% mkfs.vfat ${1}1 -n efi &> /dev/null mkfs.vfat ${1}2 -n debian &> /dev/null mount ${1}2 $2 From a4a384f25ff502c929672d72c9bcefa80ce5133b Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 31 Jan 2017 01:02:37 +0100 Subject: [PATCH 07/26] Update documentation --- README.md | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e31002c..6ded0dd 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,64 @@ Debian Auto Installer ==================== -A set of tools to create automated Debian Installation Key. I created this tools -for my work. You can create bios, efi64 key but prepare file in the folder - whitout creating key. +A script to create automated Debian Installation Key. I created it for my work. + You can create bios, efi64 key or prepare files into a folder whitout creating +key. * init.sh : script to create USB key * preseed : textfile that contain all instructions for Debian Installer * partman : partition recipe for preseed * syslinux/syslinux.cfg : Syslinux configuration file + * conf/*.conf : "folder configuration" files -# Howto create usb key - - 1. Plug your key - 2. Make sure yout key is not mounted - 3. Execute ``./init.sh -d /dev/sdb`` (replace /dev/sdb with your device) - 4. Done! # Init.sh command line options * ``-a [amd64|i386]`` choose architecture for debian files (default amd64) * ``-b`` create key for bios boot architecture (default efi64) + * ``-c --conf`` set folder configuration files (default conf/archlinux.conf) * ``-d device`` USB device like /dev/sdb + * ``--debian-version`` debian iso version to download (default 8.7.1) + * ``--repo`` repository folder (default ./repo) + * ``--tmp`` Temporary folder used to create USB key to create directory tree Be careful : if ``-d`` parameter is define then the relative peripheral will be erased -For now, this script doesn't verify the state of your device (is mounted or not) - but this is planned. +Since syslinux files are placed differently depending on the distribution, + configuration file (``-c``) contain directory to syslinux files : + + * syslinux_mbr_bl_folder : folder containing mbr.bin + * syslinux_efi_bl_folder : folder containing efi64 folder + * syslinux_modules_folder : folder containing syslinux modules folders -## Exemples +## Examples -Prepare file in directory for efi system without writing an usb key for AMD64 arch. +Prepare file in directory for efi system without writing an usb key for AMD64. ``` ./init.sh ``` -Prepare an usb key (/dev/sdb) for bios / i386 architecture +Prepare an USB key (/dev/sdb) for bios / i386 architecture ``` ./init.sh -a i386 -b -d /dev/sdb ``` + +Prepare an USB key on Debian + +``` +./init.sh -d /dev/sdb -c conf/debian.conf +``` + +# How it work + + - Create a repo folder then a subfolder named with debian version to + download then download debian iso, vmlinuz and initrt.gz. + - Create a temporary folder and copy all necessary files into it (pressed, + partman recipes, syslinux modules...) + - If ``-d /dev/sdx``, unmount usb key( if mounted) and create partition scheme, + format and mount partitions + - Copy all content of temporary folder into mounted usb key. + - Enjoy! From 0a06b0f08ec85b02dc7a64a5310c15ab5a475000 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 31 Jan 2017 01:03:47 +0100 Subject: [PATCH 08/26] Add external syslinux folder configuration files --- conf/archlinux.conf | 4 ++++ conf/debian.conf | 4 ++++ init.sh | 52 +++++++++++++++++++++++++++++++++------------ 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 conf/archlinux.conf create mode 100644 conf/debian.conf diff --git a/conf/archlinux.conf b/conf/archlinux.conf new file mode 100644 index 0000000..5df685a --- /dev/null +++ b/conf/archlinux.conf @@ -0,0 +1,4 @@ +# Archlinux syslinux folder configuration +syslinux_mbr_bl_folder="/usr/lib/syslinux/bios" +syslinux_efi_bl_folder="/usr/lib/syslinux" +syslinux_modules_folder="/usr/lib/syslinux" diff --git a/conf/debian.conf b/conf/debian.conf new file mode 100644 index 0000000..b9fee3b --- /dev/null +++ b/conf/debian.conf @@ -0,0 +1,4 @@ +# Debian syslinux folder configuration +syslinux_mbr_bl_folder="/usr/lib/SYSLINUX" +syslinux_efi_bl_folder="/usr/lib/SYSLINUX.EFI" +syslinux_modules_folder="/usr/lib/syslinux" diff --git a/init.sh b/init.sh index 960f7ce..55716fb 100755 --- a/init.sh +++ b/init.sh @@ -15,8 +15,7 @@ repo_dir="repo" tmp_dir="tmp" current_dir=`pwd` # In Archlinux, this is default files for syslinux... -syslinux_modules_dir="/usr/lib/syslinux" -syslinux_efi_bootloader="/usr/lib/syslinux" +conf_file="conf/archlinux.conf" key_mountpoint="/mnt/usbstick" device="" # constant @@ -51,6 +50,16 @@ process_args() { -b|--bios) boot_type="bios" ;; + -c|--conf) + if [ -f $2 ] + then + conf_file=$2 + else + printf "%s file not found" $2 + exit 1 + fi + shift + ;; -d|--device) if [ -b $2 ] then @@ -119,6 +128,15 @@ download (){ fi } +process_conf_file (){ + for p in `cat $1 | grep '^[^#].*'` + do + var=`echo "$p" | awk -F"=" '{print $1}'` + param=`echo "$p" | awk -F"=" '{print $2}'` + eval $var=$param + done +} + create_dir(){ # $1 directory to create if [ ! -d $1 ] @@ -180,6 +198,12 @@ fi process_args $@ +process_conf_file $conf_file +if [[ ! -d $syslinux_mbr_bl_folder || ! -d $syslinux_efi_bl_folder || ! -d $syslinux_modules_folder ]] +then + printf "Bad folder configuration in %s\n" "$conf_file" + exit 1 +fi 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" @@ -212,23 +236,23 @@ case $boot_type in efi64) syslinux_folder=${tmp_dir}/efi/EFI/boot create_dir $syslinux_folder - cp -T $syslinux_efi_bootloader/$boot_type/syslinux.efi $syslinux_folder/bootx64.efi &> /dev/null + cp -T $syslinux_efi_bl_folder/$boot_type/syslinux.efi $syslinux_folder/bootx64.efi &> /dev/null for file in $SYSLINUX_EFI_FILES do printf "copying %s \n" "$file" - cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder + cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder done cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir/efi - cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null + cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null - ;; + ;; bios) syslinux_folder=${tmp_dir}/syslinux create_dir $syslinux_folder for file in $SYSLINUX_BIOS_FILES do - printf "copying %s\n" "$file" - cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder + prin tf "copying %s\n" "$file" + cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder done cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir cp -R syslinux $tmp_dir &> /dev/null @@ -237,8 +261,8 @@ esac for file in $SYSLINUX_FILES do - printf "copying %s\n" "$file" - cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder + printf "copying %s\n" "$file" + cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder done echo Copying configurations files @@ -260,11 +284,11 @@ then create_dir $key_mountpoint #dd if=/dev/zero of=${dest} bs=512 count=1 conv=notrunc &> /dev/null case $boot_type in - "efi64") + "efi64") efi_create_key_structure $device $key_mountpoint - ;; + ;; "bios") - bios_create_key_structure $device $key_mountpoint + bio s_create_key_structure $device $key_mountpoint ;; esac printf "copying all file to USB drive\n" @@ -276,7 +300,7 @@ then then # In this mode, we need to write syslinux MBR. printf "Writing syslinux mbr.ini to %s\n" "$device" - dd bs=440 count=1 conv=notrunc if=$syslinux_modules_dir/$boot_type/mbr.bin of=$device + dd bs=440 count=1 conv=notrunc if=$syslinux_mbr_bl_folder/mbr.bin of=$device sleep 2 $SYSLINUX_EXE --directory /syslinux --install ${device}1 fi From 9431ab929e50d0e9bc6d57946c500a37265b684b Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 31 Jan 2017 01:08:58 +0100 Subject: [PATCH 09/26] Update documentation --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6ded0dd..e4a7b7d 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ be erased Since syslinux files are placed differently depending on the distribution, configuration file (``-c``) contain directory to syslinux files : - * syslinux_mbr_bl_folder : folder containing mbr.bin - * syslinux_efi_bl_folder : folder containing efi64 folder - * syslinux_modules_folder : folder containing syslinux modules folders + * __syslinux_mbr_bl_folder__ : folder containing mbr.bin + * __syslinux_efi_bl_folder__ : folder containing efi64 folder + * __syslinux_modules_folder__ : folder containing syslinux modules folders ## Examples @@ -54,11 +54,11 @@ Prepare an USB key on Debian # How it work - - Create a repo folder then a subfolder named with debian version to - download then download debian iso, vmlinuz and initrt.gz. - - Create a temporary folder and copy all necessary files into it (pressed, - partman recipes, syslinux modules...) - - If ``-d /dev/sdx``, unmount usb key( if mounted) and create partition scheme, - format and mount partitions - - Copy all content of temporary folder into mounted usb key. - - Enjoy! + 1. Create a repo folder then a subfolder named with debian version to + download then download debian iso, vmlinuz and initrt.gz. + 2. Create a temporary folder and copy all necessary files into it (pressed, + partman recipes, syslinux modules...) + 4. If ``-d /dev/sdx``, unmount usb key( if mounted) and create partition scheme, + format and mount partitions + 5. Copy all content of temporary folder into mounted usb key. + 6. Enjoy! From ad4ff0de3ff05df82fc1a8b8629e0cb5b6cc7fda Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 31 Jan 2017 01:14:49 +0100 Subject: [PATCH 10/26] Update documentation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e4a7b7d..770d8d9 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ A script to create automated Debian Installation Key. I created it for my work. key. * init.sh : script to create USB key - * preseed : textfile that contain all instructions for Debian Installer - * partman : partition recipe for preseed + * preseeds : folder containing all preseeds files + * partman_recipes : folder containinf partman recipes * syslinux/syslinux.cfg : Syslinux configuration file * conf/*.conf : "folder configuration" files From aa5705cde42d8d42feb7d202508ddc19bcb86c05 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 31 Jan 2017 22:37:04 +0100 Subject: [PATCH 11/26] Add 2sec. sleep after parted in efi_create_key_structure() and bios_create_key structure() Because a bug my Debian Setup : kernel doesn't have time to refresh partitions scheme then mkfs doesn't format partitions --- init.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/init.sh b/init.sh index 55716fb..e1e14a8 100755 --- a/init.sh +++ b/init.sh @@ -168,8 +168,8 @@ umount_device (){ } efi_create_key_structure () { - parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on - parted -s $1 mkpart primary fat32 128MiB 100% + parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% + sleep 2 mkfs.vfat ${1}1 -n efi &> /dev/null mkfs.vfat ${1}2 -n debian &> /dev/null mount ${1}2 $2 @@ -179,6 +179,7 @@ efi_create_key_structure () { bios_create_key_structure() { parted -s $1 mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on + sleep 2 mkfs.vfat ${1}1 -n debian &> /dev/null mount ${1}1 $2 } From 056031901b5d795a20819454229d7e3768d663f3 Mon Sep 17 00:00:00 2001 From: Yorick Date: Thu, 23 Feb 2017 12:01:10 +0100 Subject: [PATCH 12/26] Syntax error in bios_create_key_structure() --- init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.sh b/init.sh index e1e14a8..4ac6a47 100755 --- a/init.sh +++ b/init.sh @@ -289,7 +289,7 @@ then efi_create_key_structure $device $key_mountpoint ;; "bios") - bio s_create_key_structure $device $key_mountpoint + bios_create_key_structure $device $key_mountpoint ;; esac printf "copying all file to USB drive\n" From aa939b2be097fc32f0d4c1d455e5bd49c2d1ec1f Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 3 Oct 2017 11:12:35 +0200 Subject: [PATCH 13/26] update help message --- init.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/init.sh b/init.sh index 4ac6a47..ef6f709 100755 --- a/init.sh +++ b/init.sh @@ -96,13 +96,13 @@ process_args() { usage() { cat << EOF USAGE: ${0} [-a architecture] [-b] - -a | --arch Select CPU Architecture amd64 | i386 CPU - -b | --bios Create a bios bott compatible key - -d | --device Device_block USB key to write - --debian-ver Choose Debian Version to download - --repo Repo directory (defaukt is ./repo) - --temp Temp directory (default is ./tmp) - -v | --version Show version + -a | --arch Select CPU Architecture amd64 | i386 CPU + -b | --bios Create a bios bott compatible key + -d | --device Device_block USB key to write + --debian-version Choose Debian Version to download + --repo Repo directory (defaukt is ./repo) + --temp Temp directory (default is ./tmp) + -v | --version Show version ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe. EOF From 9ef4d922f58654d26dd22d3b256a8ed66fce26f8 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 3 Oct 2017 11:16:06 +0200 Subject: [PATCH 14/26] done is correctly displayed in download() --- init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.sh b/init.sh index ef6f709..8460d93 100755 --- a/init.sh +++ b/init.sh @@ -121,7 +121,7 @@ download (){ wget -c --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' if [ $? -eq 0 ] then - printf " \b\b\b\bdone\n " + printf " \b\b\b\b\b done\n " else printf " error, exiting.\n" exit 1 From 8a62ce69dfc244e2269b81ebd289f2dc63781a50 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 3 Oct 2017 13:50:06 +0200 Subject: [PATCH 15/26] download() stop script if 404 --- init.sh | 109 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 51 deletions(-) diff --git a/init.sh b/init.sh index 8460d93..e562959 100755 --- a/init.sh +++ b/init.sh @@ -94,87 +94,94 @@ process_args() { } usage() { - cat << EOF +cat << EOF USAGE: ${0} [-a architecture] [-b] - -a | --arch Select CPU Architecture amd64 | i386 CPU - -b | --bios Create a bios bott compatible key - -d | --device Device_block USB key to write - --debian-version Choose Debian Version to download - --repo Repo directory (defaukt is ./repo) - --temp Temp directory (default is ./tmp) - -v | --version Show version - +-a | --arch Select CPU Architecture amd64 | i386 CPU +-b | --bios Create a bios bott compatible key +-d | --device Device_block USB key to write +--debian-version Choose Debian Version to download +--repo Repo directory (defaukt is ./repo) +--temp Temp directory (default is ./tmp) +-v | --version Show version + ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe. EOF } show_version () { - printf "%s %s 2016-2017 ephase@xieme-art.org\n" "$APP_NAME" "$VERSION" +printf "%s %s 2016-2017 ephase@xieme-art.org\n" "$APP_NAME" "$VERSION" } download (){ - #Download function - # $1 : filename - # $2 : url - # $3 : 1 if abort download if file exist - printf "Downloading %s : " "$1" +#Download function +# $1 : filename +# $2 : url +# $3 : 1 if abort download if file exist +printf "Downloading %s : " "$1" +http_response=$(wget --spider --server-response $2/$1 2>&1 | grep HTTP/ | tail -1 | awk ' { printf $2 }') +if [ $http_response -eq 200 ] +then wget -c --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' - if [ $? -eq 0 ] + if [ $? -eq 0 ] then printf " \b\b\b\b\b done\n " else printf " error, exiting.\n" exit 1 fi +else + printf "Error 404 : file not found, exiting\n" + exit 1 +fi } process_conf_file (){ - for p in `cat $1 | grep '^[^#].*'` - do - var=`echo "$p" | awk -F"=" '{print $1}'` - param=`echo "$p" | awk -F"=" '{print $2}'` - eval $var=$param - done +for p in `cat $1 | grep '^[^#].*'` +do + var=`echo "$p" | awk -F"=" '{print $1}'` + param=`echo "$p" | awk -F"=" '{print $2}'` + eval $var=$param +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 +# $1 directory to create +if [ ! -d $1 ] +then + mkdir -p $1 +else + if [[ $2 == 1 ]] + then + rm -rf $1 + create_dir $1 fi +fi } # unmount if device is mounted umount_device (){ - local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') - if [ ! -z mounted_part ] - then - for part in $mounted_part - do - if [ -d $part ] - then - printf "Unmount %s\n " "$part" - umount $part --recursive - fi - done - fi +local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') +if [ ! -z mounted_part ] +then + for part in $mounted_part + do + if [ -d $part ] + then + printf "Unmount %s\n " "$part" + umount $part --recursive + fi + done +fi } efi_create_key_structure () { - parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% - sleep 2 - mkfs.vfat ${1}1 -n efi &> /dev/null - mkfs.vfat ${1}2 -n debian &> /dev/null - mount ${1}2 $2 - create_dir $2/efi - mount ${1}1 $2/efi +parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% +sleep 2 +mkfs.vfat ${1}1 -n efi &> /dev/null +mkfs.vfat ${1}2 -n debian &> /dev/null +mount ${1}2 $2 +create_dir $2/efi +mount ${1}1 $2/efi } bios_create_key_structure() { From bc433889b4148d4925a06b87f01c71e437ba23f9 Mon Sep 17 00:00:00 2001 From: Yorick Date: Thu, 12 Oct 2017 12:04:30 +0200 Subject: [PATCH 16/26] Retrieve automaticaly lastest debian version --- init.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/init.sh b/init.sh index e562959..7011534 100755 --- a/init.sh +++ b/init.sh @@ -9,7 +9,9 @@ OPTIND=1 # Default Variables debian_arch="amd64" -debian_version="8.7.1" +debian_version=$(wget -q --output-document - https://cdimage.debian.org/debian-cd/ | \ + grep -o '[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0.9]\{1,2\}' | head -1) +archive=0 boot_type="efi64" repo_dir="repo" tmp_dir="tmp" @@ -71,7 +73,11 @@ process_args() { fi ;; --debian-version) - debian_version=$2 + if [[ $debian_version != $2 ]] + then + archive=1 + debian_version=$2 + fi shift ;; --repo) @@ -214,7 +220,12 @@ then fi 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" +if [ $archive ] +then + iso_url="http://cdimage.debian.org/cdimage/archive/${debian_version}/${debian_arch}/iso-cd" +else + iso_url="http://cdimage.debian.org/debian-cd/${debian_version}/${debian_arch}/iso-cd" +fi iso_file="debian-${debian_version}-${debian_arch}-netinst.iso" repo_path="$repo_dir/$debian_arch/$debian_version" From 0701bf4323ce2f46cf5e56f2e67a4188ac1fadc1 Mon Sep 17 00:00:00 2001 From: Yorick Date: Thu, 12 Oct 2017 12:28:42 +0200 Subject: [PATCH 17/26] Update documentation --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 770d8d9..201e804 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ Debian Auto Installer -==================== +===================== A script to create automated Debian Installation Key. I created it for my work. You can create bios, efi64 key or prepare files into a folder whitout creating @@ -18,12 +18,12 @@ key. * ``-b`` create key for bios boot architecture (default efi64) * ``-c --conf`` set folder configuration files (default conf/archlinux.conf) * ``-d device`` USB device like /dev/sdb - * ``--debian-version`` debian iso version to download (default 8.7.1) + * ``--debian-version`` debian iso version to download (default lastest stable) * ``--repo`` repository folder (default ./repo) * ``--tmp`` Temporary folder used to create USB key to create directory tree -Be careful : if ``-d`` parameter is define then the relative peripheral will -be erased +Be careful : if ``-d`` parameter is define then the usb key / external drive +will be erased. Since syslinux files are placed differently depending on the distribution, configuration file (``-c``) contain directory to syslinux files : @@ -54,11 +54,11 @@ Prepare an USB key on Debian # How it work - 1. Create a repo folder then a subfolder named with debian version to - download then download debian iso, vmlinuz and initrt.gz. + 1. Create a repo folder then a subfolder named with debian version then + download debian iso, vmlinuz and initrt.gz. 2. Create a temporary folder and copy all necessary files into it (pressed, - partman recipes, syslinux modules...) + partman recipes, syslinux modules...). 4. If ``-d /dev/sdx``, unmount usb key( if mounted) and create partition scheme, - format and mount partitions + format and mount partitions. 5. Copy all content of temporary folder into mounted usb key. - 6. Enjoy! + 6. Unmount usb key. From a4319d238e2076dd494cfcabecc82833420aa5a8 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Thu, 22 Feb 2018 16:21:00 +0100 Subject: [PATCH 18/26] URL for current version was incorrect --- init.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/init.sh b/init.sh index 7011534..daac3bc 100755 --- a/init.sh +++ b/init.sh @@ -220,7 +220,7 @@ then fi bootfiles_Debian="http://ftp.debian.org/debian/dists/jessie/main/installer-${debian_arch}/current/images/hd-media" -if [ $archive ] +if [ $archive -eq 1 ] then iso_url="http://cdimage.debian.org/cdimage/archive/${debian_version}/${debian_arch}/iso-cd" else @@ -236,6 +236,7 @@ printf "Downloading Debian files\n---\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 From f950d5716791bec982ff50767801fe418e84033e Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 6 Mar 2018 10:45:36 +0100 Subject: [PATCH 19/26] Change kernel and init download URL --- init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.sh b/init.sh index daac3bc..4103121 100755 --- a/init.sh +++ b/init.sh @@ -219,7 +219,7 @@ then exit 1 fi -bootfiles_Debian="http://ftp.debian.org/debian/dists/jessie/main/installer-${debian_arch}/current/images/hd-media" +bootfiles_Debian="http://ftp.debian.org/debian/dists/stable/main/installer-${debian_arch}/current/images/hd-media" if [ $archive -eq 1 ] then iso_url="http://cdimage.debian.org/cdimage/archive/${debian_version}/${debian_arch}/iso-cd" From 7345b86390939f2feca1ecad1d9104e5696f1420 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 6 Mar 2018 11:00:19 +0100 Subject: [PATCH 20/26] syntax error in umount_device() --- init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.sh b/init.sh index 4103121..1e7c46f 100755 --- a/init.sh +++ b/init.sh @@ -167,7 +167,7 @@ fi # unmount if device is mounted umount_device (){ local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') -if [ ! -z mounted_part ] +if [ ! -z $mounted_part ] then for part in $mounted_part do From e2f41eb862b714a80d789a163d22fb7e9e1ae0fa Mon Sep 17 00:00:00 2001 From: yorick Barbanneau Date: Mon, 25 Feb 2019 12:36:55 +0100 Subject: [PATCH 21/26] Add sync statement for efi_create_syructure() + indetation --- init.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/init.sh b/init.sh index 1e7c46f..c12304b 100755 --- a/init.sh +++ b/init.sh @@ -167,7 +167,7 @@ fi # unmount if device is mounted umount_device (){ local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') -if [ ! -z $mounted_part ] +if [ ! "$mounted_part" == "" ] then for part in $mounted_part do @@ -181,13 +181,14 @@ fi } efi_create_key_structure () { -parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% -sleep 2 -mkfs.vfat ${1}1 -n efi &> /dev/null -mkfs.vfat ${1}2 -n debian &> /dev/null -mount ${1}2 $2 -create_dir $2/efi -mount ${1}1 $2/efi + parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% + sleep 2 + mkfs.vfat ${1}1 -n efi &> /dev/null + mkfs.vfat ${1}2 -n debian &> /dev/null + sync + mount ${1}2 $2 + create_dir $2/efi + mount ${1}1 $2/efi } bios_create_key_structure() { From 3b91eda6db03e373be4a1fae0af30553465fba03 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 15:44:39 +0100 Subject: [PATCH 22/26] Error in download() bug resolved --- init.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/init.sh b/init.sh index c12304b..bcdcfab 100755 --- a/init.sh +++ b/init.sh @@ -124,11 +124,14 @@ download (){ # $2 : url # $3 : 1 if abort download if file exist printf "Downloading %s : " "$1" -http_response=$(wget --spider --server-response $2/$1 2>&1 | grep HTTP/ | tail -1 | awk ' { printf $2 }') -if [ $http_response -eq 200 ] +local url +local http_response +url="$2/$1" +http_response=$(wget --spider --server-response "$url" 2>&1 | grep HTTP/ | tail -1 | awk ' { printf $2 }') +if [ "$http_response" -eq 200 ] then - wget -c --progress=dot $2/$1 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' - if [ $? -eq 0 ] + wget -c --progress=dot "$url" 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' + if [ "$?" -eq 0 ] then printf " \b\b\b\b\b done\n " else From a22a055fe3fb4ce4e4d129fa4b692c66f0f4313b Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 15:57:16 +0100 Subject: [PATCH 23/26] Use `while read <` in process_conf_file() --- init.sh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/init.sh b/init.sh index bcdcfab..6640f91 100755 --- a/init.sh +++ b/init.sh @@ -131,7 +131,7 @@ http_response=$(wget --spider --server-response "$url" 2>&1 | grep HTTP/ | tail if [ "$http_response" -eq 200 ] then wget -c --progress=dot "$url" 2>&1 | grep --line-buffered "[0-9]\{1,3\}%" -o | awk '{printf ("\b\b\b\b%4s", $1)}' - if [ "$?" -eq 0 ] + if [ $? -eq 0 ] then printf " \b\b\b\b\b done\n " else @@ -145,12 +145,13 @@ fi } process_conf_file (){ -for p in `cat $1 | grep '^[^#].*'` -do - var=`echo "$p" | awk -F"=" '{print $1}'` - param=`echo "$p" | awk -F"=" '{print $2}'` - eval $var=$param -done + while read -r p + do + var=$(echo "$p" | awk -F"=" '{print $1}') + param=$(echo "$p" | awk -F"=" '{print $2}') + eval "$var"="$param" + done < <( cat "$1" | grep '^[^#].*') + } create_dir(){ From 5ed96fcd1343288986950625b9b826862a184109 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 16:00:08 +0100 Subject: [PATCH 24/26] create_dir() use double quotes toprevent gobbing --- init.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/init.sh b/init.sh index 6640f91..96bbb6b 100755 --- a/init.sh +++ b/init.sh @@ -156,16 +156,16 @@ process_conf_file (){ create_dir(){ # $1 directory to create -if [ ! -d $1 ] -then - mkdir -p $1 -else - if [[ $2 == 1 ]] - then - rm -rf $1 - create_dir $1 + if [ ! -d "$1" ] + then + mkdir -p "$1" + else + if [ "$2" -eq 1 ] + then + rm -rf "$1" + create_dir "$1" + fi fi -fi } # unmount if device is mounted From 99aeeb677edf0bccdb37c3594f6f9feccd11153b Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 16:03:18 +0100 Subject: [PATCH 25/26] unmount_device() use double quote when needed --- init.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/init.sh b/init.sh index 96bbb6b..5b37f29 100755 --- a/init.sh +++ b/init.sh @@ -170,18 +170,19 @@ create_dir(){ # unmount if device is mounted umount_device (){ -local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') -if [ ! "$mounted_part" == "" ] -then - for part in $mounted_part - do - if [ -d $part ] - then - printf "Unmount %s\n " "$part" - umount $part --recursive - fi - done -fi + local mounted_part + mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') + if [ ! "$mounted_part" == "" ] + then + for part in $mounted_part + do + if [ -d "$part" ] + then + printf "Unmount %s\n " "$part" + umount "$part" --recursive + fi + done + fi } efi_create_key_structure () { From 91956a3c776edec7499dbde3c88a8763cd77c263 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 17:20:26 +0100 Subject: [PATCH 26/26] Make spellcheck a little bit happy --- init.sh | 102 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/init.sh b/init.sh index 5b37f29..9214a3a 100755 --- a/init.sh +++ b/init.sh @@ -160,7 +160,7 @@ create_dir(){ then mkdir -p "$1" else - if [ "$2" -eq 1 ] + if [ -n "$2" ] && [ "$2" -eq 1 ] then rm -rf "$1" create_dir "$1" @@ -170,7 +170,7 @@ create_dir(){ # unmount if device is mounted umount_device (){ - local mounted_part + local mounted_part="" mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}') if [ ! "$mounted_part" == "" ] then @@ -186,40 +186,40 @@ umount_device (){ } efi_create_key_structure () { - parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% + parted -s "$1" mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on mkpart debian fat32 128MiB 100% sleep 2 - mkfs.vfat ${1}1 -n efi &> /dev/null - mkfs.vfat ${1}2 -n debian &> /dev/null + mkfs.vfat "${1}1" -n efi &> /dev/null + mkfs.vfat "${1}2" -n debian &> /dev/null sync - mount ${1}2 $2 - create_dir $2/efi - mount ${1}1 $2/efi + mount "${1}2" "$2" + create_dir "$2/efi" + mount "${1}1" "$2/efi" } bios_create_key_structure() { - parted -s $1 mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on + parted -s "$1" mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on sleep 2 - mkfs.vfat ${1}1 -n debian &> /dev/null - mount ${1}1 $2 + mkfs.vfat "${1}1" -n debian &> /dev/null + mount "${1}1" "$2" } #Stop script if syslinux is not installed -if [ -z $SYSLINUX_EXE ] +if [ -z "$SYSLINUX_EXE" ] then printf "Syslinux not found, script can't continue.\n" exit 1 fi -if [ -z $PARTED_EXE ] +if [ -z "$PARTED_EXE" ] then printf "Parted not found, script can't continue.\n" exit 1 fi -process_args $@ -process_conf_file $conf_file -if [[ ! -d $syslinux_mbr_bl_folder || ! -d $syslinux_efi_bl_folder || ! -d $syslinux_modules_folder ]] +process_args "$@" +process_conf_file "$conf_file" +if [[ ! -d "$syslinux_mbr_bl_folder" || ! -d "$syslinux_efi_bl_folder" || ! -d "$syslinux_modules_folder" ]] then printf "Bad folder configuration in %s\n" "$conf_file" exit 1 @@ -240,64 +240,64 @@ repo_path="$repo_dir/$debian_arch/$debian_version" printf "Downloading Debian files\n---\n" # Création du répertoire temporaire -create_dir $repo_path -cd $repo_path +create_dir "$repo_path" +cd "$repo_path" -download $iso_file $iso_url 1 +download "$iso_file" "$iso_url" 1 for file in "vmlinuz" "initrd.gz" do - download $file $bootfiles_Debian 1 + download "$file" "$bootfiles_Debian" 1 done -cd $current_dir +cd "$current_dir" -if [ -z $device ] +if [ -z "$device" ] then printf "No destination device specified, USB key will not be created.\n" fi -create_dir $tmp_dir 1 -cp $repo_path/$iso_file $tmp_dir +create_dir "$tmp_dir" 1 +cp "$repo_path/$iso_file" "$tmp_dir" # Copy wanted syslinux file from archive case $boot_type in efi64) syslinux_folder=${tmp_dir}/efi/EFI/boot - create_dir $syslinux_folder - cp -T $syslinux_efi_bl_folder/$boot_type/syslinux.efi $syslinux_folder/bootx64.efi &> /dev/null + create_dir "$syslinux_folder" + cp -T "$syslinux_efi_bl_folder/$boot_type/syslinux.efi" "$syslinux_folder/bootx64.efi" &> /dev/null for file in $SYSLINUX_EFI_FILES do printf "copying %s \n" "$file" - cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder + cp "$syslinux_modules_folder/$boot_type/$file" "$syslinux_folder" done - cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir/efi - cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null + cp "$repo_path"/{vmlinuz,initrd.gz} "$tmp_dir/efi" + cp -R syslinux/syslinux.cfg "$syslinux_folder" &> /dev/null ;; bios) syslinux_folder=${tmp_dir}/syslinux - create_dir $syslinux_folder + create_dir "$syslinux_folder" for file in $SYSLINUX_BIOS_FILES do - prin tf "copying %s\n" "$file" - cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder + printf "copying %s\n" "$file" + cp "$syslinux_modules_folder/$boot_type/$file" "$syslinux_folder" done - cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir - cp -R syslinux $tmp_dir &> /dev/null + cp "$repo_path"/{vmlinuz,initrd.gz} "$tmp_dir" + cp -R syslinux "$tmp_dir" &> /dev/null ;; esac for file in $SYSLINUX_FILES do printf "copying %s\n" "$file" - cp $syslinux_modules_folder/$boot_type/$file $syslinux_folder + cp "$syslinux_modules_folder/$boot_type/$file" "$syslinux_folder" done 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 +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 [ ! -z $device ] +if [ -n "$device" ] then printf "Partitionning and formating %s\n" "$device" read -p "[WARNING] Disk $device will be erased continue [Y/N]? " -n 1 -r @@ -306,31 +306,31 @@ then exit 1 fi printf "\n\n" - umount_device $device - create_dir $key_mountpoint + umount_device "$device" + create_dir "$key_mountpoint" #dd if=/dev/zero of=${dest} bs=512 count=1 conv=notrunc &> /dev/null - case $boot_type in + case "$boot_type" in "efi64") - efi_create_key_structure $device $key_mountpoint + efi_create_key_structure "$device" "$key_mountpoint" ;; "bios") - bios_create_key_structure $device $key_mountpoint + bios_create_key_structure "$device" "$key_mountpoint" ;; esac printf "copying all file to USB drive\n" - cp -R $tmp_dir/* $key_mountpoint - umount_device $device + cp -R "$tmp_dir"/* "$key_mountpoint" + umount_device "$device" sleep 5 # Make key bootable if bios. - if [ $boot_type = "bios" ] + if [ "$boot_type" = "bios" ] then # In this mode, we need to write syslinux MBR. printf "Writing syslinux mbr.ini to %s\n" "$device" - dd bs=440 count=1 conv=notrunc if=$syslinux_mbr_bl_folder/mbr.bin of=$device + dd bs=440 count=1 conv=notrunc if="$syslinux_mbr_bl_folder"/mbr.bin of="$device" sleep 2 - $SYSLINUX_EXE --directory /syslinux --install ${device}1 + "$SYSLINUX_EXE" --directory /syslinux --install "${device}1" fi - rm -rf $TMP_DIR + rm -rf "$tmp_dir" printf "\nBootable USB key created\n" exit 0 fi