From 3b91eda6db03e373be4a1fae0af30553465fba03 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 12 Feb 2020 15:44:39 +0100 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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