Full rewrite

This commit is contained in:
Yorick Barbanneau 2017-01-27 00:34:33 +01:00
parent 971cafd935
commit 50d2b171fa

327
init.sh
View file

@ -6,42 +6,122 @@
# Dépend : syslinux,wget # Dépend : syslinux,wget
# #
OPTIND=1 OPTIND=1
DEBIAN_VERSION="8.7.1"
DEBIAN_ARCH="amd64" # Default Variables
BOOT_TYPE="efi64" debian_arch="amd64"
SYSLINUX_EXE=`which syslinux` 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_FILES="menu.c32 vesamenu.c32 libutil.c32 libcom32.c32"
SYSLINUX_BIOS_FILES="" SYSLINUX_BIOS_FILES=""
SYSLINUX_EFI_FILES="ldlinux.e64" SYSLINUX_EFI_FILES="ldlinux.e64"
TMP_DIR="tmp"
REPO_DIR="repo" process_args() {
MOUNT_DIR="/mnt/usbstick" # While getops doesn't handle long parameters
current_dir=`pwd` # 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() { usage() {
cat << EOF cat << EOF
USAGE: ${0} [-a architecture] [-b] USAGE: ${0} [-a architecture] [-b]
-a amd64 | i386 CPU architecture -a | --arch Select CPU Architecture amd64 | i386 CPU
-b bios boot type (default is efi64) -b | --bios Create a bios bott compatible key
-d device_block USB key to write -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. ${0} initialize an usb key for Debian automatic install with preseed and a partman recipe.
EOF EOF
} }
show_version () {
printf "%s %s 2016-2017 ephase@xieme-art.org\n" "$APP_NAME" "$VERSION"
}
download (){ download (){
#Download function #Download function
# $1 : filename # $1 : filename
# $2 : url # $2 : url
# $3 : 1 if abort download if file exist # $3 : 1 if abort download if file exist
echo -ne "Downloading $1 ... " printf "Downloading %s : " "$1"
if [[ -f $1 && $3 == "1" ]] if [[ -f $1 && $3 == "1" ]]
then then
echo file already downloaded. printf "\b\b\b\bfile already downloaded.\n"
return return
fi fi
wget no-clobber -4 $2/$1 &> /dev/null 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)}'
echo "done!" if [ $? -eq 0 ]
then
printf " done\n "
else
printf " error, exiting.\n"
exit 1
fi
} }
create_dir(){ create_dir(){
@ -58,66 +138,63 @@ create_dir(){
fi fi
} }
#Stop script if syslinux is not installed # unmount if device is mounted
if [[ $SYSLINUX_EXE == "" ]] umount_device (){
local mounted_part=$(mount | grep "${1}[0-9]" | awk '{print $3}')
if [ ! -z mounted_part ]
then then
echo "Syslinux not found, script can't continue…" for part in $mounted_part
exit 1 do
fi if [ -d $part ]
if [ -d /usr/lib/syslinux/efi64 ]
then then
syslinux_mod="/usr/lib/syslinux" printf "Unmount %s\n " "$part"
else umount $part --recursive
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
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 done
fi
}
bootfiles_Debian="http://ftp.debian.org/debian/dists/jessie/main/installer-${DEBIAN_ARCH}/current/images/hd-media" efi_create_key_structure () {
iso_url="http://cdimage.debian.org/debian-cd/${DEBIAN_VERSION}/${DEBIAN_ARCH}/iso-cd" parted -s $1 mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on
iso_file="debian-${DEBIAN_VERSION}-${DEBIAN_ARCH}-netinst.iso" parted -s $1 mkpart EFS fat32 128MiB 100%
repo_path="$REPO_DIR/$DEBIAN_ARCH/$DEBIAN_VERSION" 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 [ -z $SYSLINUX_EXE ]
then
printf "Syslinux not found, script can't continue.\n"
exit 1
fi
if [ -z $PARTED_EXE ]
then
printf "Parted not found, script can't continue.\n"
exit 1
fi
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"
# Download Debian files (kernel, initrd and iso) # 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 # Création du répertoire temporaire
create_dir $repo_path create_dir $repo_path
cd $repo_path cd $repo_path
@ -128,103 +205,85 @@ do
done done
cd $current_dir cd $current_dir
if [[ -z $dest ]] if [ -z $device ]
then 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 fi
create_dir $TMP_DIR 1 create_dir $tmp_dir 1
cp $repo_path/$iso_file $TMP_DIR cp $repo_path/$iso_file $tmp_dir
# Copy wanted syslinux file from archive # Copy wanted syslinux file from archive
case $BOOT_TYPE in case $boot_type in
efi64) efi64)
if [ -f /usr/lib/syslinux/efi64/syslinux.efi ] syslinux_folder=${tmp_dir}/efi/EFI/boot
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 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 for file in $SYSLINUX_EFI_FILES
do do
echo "copying $file ..." printf "copying %s \n" "$file"
cp $syslinux_mod/$BOOT_TYPE/$file $syslinux_folder cp $syslinux_modules_dir/$boot_type/$file $syslinux_folder
done done
for file in $SYSLINUX_FILES cp $repo_path/{vmlinuz,initrd.gz} $tmp_dir/efi
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 cp -R syslinux/syslinux.cfg $syslinux_folder &> /dev/null
;; ;;
bios) bios)
create_dir ${TMP_DIR}/syslinux 1 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 for file in $SYSLINUX_BIOS_FILES
do do
echo "copying $file ..." printf "copying %s\n" "$file"
cp ${syslinux_mod}/${BOOT_TYPE}/${file} ${TMP_DIR}/syslinux/ cp $syslinux_modules_dir/$boot_type/$file $tmp_dir/syslinux/
done done
cp -R syslinux $TMP_DIR &> /dev/null cp -R syslinux $tmp_dir &> /dev/null
;; ;;
esac 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 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]$ ]] if [[ ! $REPLY =~ ^[Yy]$ ]]
then then
exit 1 exit 1
fi 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 #dd if=/dev/zero of=${dest} bs=512 count=1 conv=notrunc &> /dev/null
echo -e "\nCreating partition" case $boot_type in
parted -s $dest mklabel gpt mkpart EFS fat32 1MiB 128MiB set 1 boot on "efi64")
parted -s $dest mkpart EFS fat32 128MiB 100% efi_create_key_structure $device $key_mountpoint
mkfs.vfat ${dest}1 -n efi &> /dev/null ;;
mkfs.vfat ${dest}2 -n debian &> /dev/null "bios")
bios_create_key_structure $device $key_mountpoint
# mount and copy ;;
create_dir $MOUNT_DIR esac
mount ${dest}2 $MOUNT_DIR printf "copying all file to USB drive\n"
create_dir $MOUNT_DIR/efi cp -R $tmp_dir/* $key_mountpoint
mount ${dest}1 $MOUNT_DIR/efi umount_device $device
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 sleep 5
# Make key bootable if bios. # Make key bootable if bios.
if [[ $BOOT_TYPE == "bios" ]] if [ $boot_type = "bios" ]
then then
# In this mode, we need to write syslinux MBR. # In this mode, we need to write syslinux MBR.
echo "Writing syslinux mbr.ini fo $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} dd bs=440 count=1 conv=notrunc if=${syslinux_mod}/${boot_type}/mbr.bin of=${dest}
sleep 2 sleep 2
syslinux --directory /syslinux --install ${dest}1 $SYSLINUX_EXE --directory /syslinux --install ${device}1
fi fi
rm -rf $TMP_DIR
printf "\nBootable USB key created\n"
exit 0 exit 0
#rm -rf $TMP_DIR
fi fi