#!/bin/bash # shellcheck disable=SC2181 # # Author: jules randolph https://github.com/jsamr # License: MIT # Version 3.1.3 set -o pipefail set -E version="3.1.3" scriptName=$(basename "$0") bashVersion=$(echo "$BASH_VERSION" | cut -d. -f1) if [ -z "$BASH_VERSION" ] || [ "$bashVersion" -lt 4 ]; then echoerr "You need bash v4+ to run this script. Aborting..." exit 1 fi # program constrains definitions typeset -ar commandDependencies=('lsblk' 'sfdisk' 'mkfs' 'blkid' 'wipefs' 'grep' 'file' 'awk' 'mlabel' 'partprobe' 'tar') typeset -Ar commandPackages=( ['lsblk']='util-linux' ['sfdisk']='util-linux' ['mkfs']='util-linux' ['blkid']='util-linux' ['wipefs']='util-linux' ['grep']='grep' ['file']='file' ['awk']='gawk' ['mlabel']='mtools' ['syslinux']='syslinux' ['rsync']='rsync' ['partprobe']='parted' ['curl']='curl' ['tar']='tar' ) typeset shortOptions='bydJahlMftLp' typeset -ar supportedFS=('vfat' 'exfat' 'ntfs' 'ext2' 'ext3' 'ext4' 'f2fs') typeset -Ar userVarsCompatibilityMatrix=( ['iso-file']='install-auto install-mount-rsync install-dd inspect probe' ['device']='install-auto install-mount-rsync install-dd format' ['type']='install-mount-rsync format' ['label']='install-mount-rsync format' ['remote-bootloader']='install-auto install-mount-rsync' ) typeset -Ar userFlagsCompatibilityMatrix=( ['local-bootloader']='install-auto install-mount-rsync' ['assume-yes']='install-auto install-mount-rsync install-dd format' ['no-eject']='install-auto install-mount-rsync install-dd format' ['autoselect']='install-auto install-mount-rsync install-dd format' ['no-mime-check']='install-auto install-mount-rsync install-dd' ['no-usb-check']='install-auto install-mount-rsync install-dd format' ) # internal variables typeset ticketsurl="https://github.com/jsamr/bootiso/issues" typeset mountRoot=/mnt typeset tempRoot=/var/tmp/bootiso typeset cacheRoot=/var/cache/bootiso typeset selectedPartition typeset isoMountPoint typeset usbMountPoint typeset startTime typeset endTime typeset addSyslinuxBootloader=false typeset syslinuxVersion typeset -a devicesList typeset operationSuccess typeset expectingISOFile typeset foundSyslinuxMbrBinary typeset foundSyslinuxBiosFolder typeset -A syslinuxInstall typeset -a temporaryAssets=() typeset -A isoInspection=( ['isHybrid']='' ['syslinuxBin']='' ['syslinuxVer']='' ['syslinuxConf']='' ['supportsEFIBoot']='' ) typeset -A userFlags=( # Actions ['help']='' ['version']='' ['list-usb-drives']='' ['format']='' ['install-dd']='' ['install-mr']='' ['inspect']='' ['probe']='' # Options ['local-bootloader']='' ['assume-yes']='' ['device']='' ['no-eject']='' ['autoselect']='' ['no-mime-check']='' ['no-usb-check']='' ) typeset -A userVars=( ['iso-file']='' ['device']='' ['type']='' ['label']='' ['remote-bootloader']='' ) # user defined variables typeset selectedIsoFile # no default typeset selectedDevice # default to prompted to user typeset partitionLabel # default to inferred from ISO file label typeset partitionType # default to vfat typeset action='install-auto' typeset selectedBootloaderVersion # default to auto # options typeset disableMimeCheck typeset disableUSBCheck typeset disableConfirmation typeset autoselect typeset shouldMakePartition typeset noDeviceEjection typeset localBootloader # $1: The text to colorify. redify() { echo -e "\\033[0;31m$1\\033[0m" } # $1: The text to colorify. greenify() { echo -e "\\033[0;32m$1\\033[0m" } # $1: The text to colorify. yellowify() { echo -e "\\033[1;33m$1\\033[0m" } typeset openTicketMessage="This is not expected: please open a ticket at $ticketsurl." # 100 wide + no tabs typeset help_message="Create a bootable USB from any ISO securely. Usage: $scriptName [...] $scriptName [...] $scriptName [...] The default action [install-auto] as per first synopsis is to install an ISO file to a USB device in automatic mode. In such mode, $scriptName will analyze the ISO file and select the best course of actions to maximize the odds your USB stick be proven bootable. Option and action flags can be stacked in their POSIX form. ACTION FLAGS You can override the default [install-auto] action with bellow flags (cannonical names given in brackets): -h, --help, help [help] Display this help message and exit. -v, --version [version] Display version and exit. -l, --list-usb-drives [list-usb-drives] List available USB drives and exit. -i, --inspect [inspect] Inspect ISO file boot capabilities and how $scriptName can handle it, then exit. -p, --probe [probe] Equivalent to [inspect] followed by [list-usb-drives] actions. -f, --format [format] Format selected USB drive and exit. --dd [install-dd] Overrides \"automatic\" mode and install ISO in image-copy mode with \`dd' utility. It is recommended to run [inspect] action first. --mrsync [install-mount-rsync] Overrides \"automatic\" mode and install ISO in mount-rsync mode with \`rsync' utility. It is recommended to run [inspect] action first. OPTION FLAGS -d, --device Select block file as USB device. If is not connected through USB, $scriptName will fail and exit. Device block files are usually situated in /dev/sdX or /dev/hdX. You can omit \`/dev/' prefix. -y, --assume-yes $scriptName won't prompt the user for confirmation before erasing and partitioning USB device. $(yellowify 'Use at your own risks.') -a, --autoselect Enable autoselecting USB devices in combination with -y option. Autoselect will automatically select a USB drive device if there is exactly one connected to the system. Enabled by default when neither \`-d' nor \`--no-usb-check' options are given. -J, --no-eject Do not eject device after unmounting. -M, --no-mime-check $scriptName won't assert that selected ISO file has the right mime-type. -t, --type Format to \`' instead of default FAT32 (vfat). Supported types: ${supportedFS[*]}. Applicable to [install-mount-rsync] and [format] actions. -L, --label