#!/bin/bash set -e DISTROLIST=() ISOLIST=() ROOTPATH= DEVNAME= KERNELDIR= DATADIR= INSTALL_GRUB=1 GRUBCFG_ONLY=0 DOWNLOAD_ONLY=0 . functions.sh msg() { echo -e "$1" >&2 } fatalerror() { msg "\x1b[1;31m$1\x1b[0m" exit 1 } usage() { >&2 cat << EOF $0 [--root=] [options] [distro 1] [distro 2] ... use $0 -L to list available distros options: --no-grub: do not install GRUB loader (still generate grub.cfg) --grubcfg: do not do real install, only generate grub.cfg file --downloadonly: only download the ISO files --dev=: write boot sector to (default is autodetected from ) EOF } unset ISOPATH CFGFILE="$HOME/.liveusb-builder" test -f "$CFGFILE" && source "$CFGFILE" || true ISOPATH=${ISOPATH:-"$HOME/isofiles"} while [[ -n "$1" ]] do case "$1" in -L) exec ./listisos.sh ;; --root=*) ROOTPATH=${1/--root=} UUID="$(findmnt -o UUID --raw --noheadings $ROOTPATH \ || fatalerror "UUID of $ROOTPATH not found, not a mountpoint?")" DEVNAME="/dev/$(getdiskbypart "/dev/disk/by-uuid/$UUID")" KERNELDIR="$ROOTPATH/liveusb-kernel" DATADIR="$ROOTPATH/liveusb-data" GRUBCFG="$ROOTPATH/grub/grub.cfg" install -d "$KERNELDIR" "$DATADIR" msg "Files will be copy to $ROOTPATH" ;; --dev=*) DEVNAME=${1/--dev=} ;; --no-grub) INSTALL_GRUB=0 ;; --grubcfg) GRUBCFG_ONLY=1 ;; --downloadonly) DOWNLOAD_ONLY=1 ;; *=*|-*) usage exit 1 ;; *) if [ -f "distro/$1/isoinfo" ] then DISTROLIST=(${DISTROLIST[@]} "$1") else fatalerror "directory distro/$1/isoinfo not found" fi ;; esac shift done if [[ -z "$ROOTPATH" && "$DOWNLOAD_ONLY" == 0 ]]; then usage exit 1 fi if [[ "${#DISTROLIST[@]}" == 0 ]]; then usage exit 1 fi if [ ! -z "$DEVNAME" ]; then msg "Boot sector will be written to $DEVNAME" fi for i in ${DISTROLIST[@]} do process_isoinfo "$i" ISOLIST=("${ISOLIST[@]}" "$ISOFILE") ISONAMELIST=("${ISONAMELIST[@]}" "$ISONAME") if [ -f "$ISOPATH/$ISOFILE" ] && checksum_verify; then true else download_iso fi done if [[ "$DOWNLOAD_ONLY" == 1 ]]; then exit 0 fi install -d "$ROOTPATH/grub" echo '# The live USB grub.cfg file' > "$GRUBCFG" if [ -z "$TXTMODE" ]; then cat >> "$GRUBCFG" << 'EOF' if [ ${grub_platform} == efi ]; then insmod all_video insmod font if loadfont /grub/fonts/unicode.pf2; then insmod gfxterm set gfxmode=auto set gfxpayload=keep terminal_output gfxterm fi fi EOF fi for i in `seq 1 ${#DISTROLIST[@]}` do ISOFILE="${ISOLIST[$i-1]}" ISONAME="${ISONAMELIST[$i-1]}" DISTRO="${DISTROLIST[$i-1]}" process_distro "$DISTRO" if [ "$GRUBCFG_ONLY" == 0 ]; then install_live fi gen_grubcfg "$DISTRO" >> "$GRUBCFG" done if [ "$GRUBCFG_ONLY" == 1 ]; then msg 'Only generate grub.cfg, will not install GRUB.' elif [ "$INSTALL_GRUB" == 1 ]; then read -p 'Answer Y to install GRUB.[Y/n] ' if [[ "$REPLY" =~ ^[Yy]|^$ ]]; then as-root grub-install --boot-directory="$ROOTPATH" --target=i386-pc "$DEVNAME" as-root grub-install --boot-directory="$ROOTPATH" --efi-directory="$ROOTPATH" --bootloader-id=grub --target=x86_64-efi --removable --no-nvram "$DEVNAME" fi fi