installer: remove unused functions, pointless case, recursive calls and faulty breaks

I suspect the recursive calls were to work around the error caused by break
being called in a case statement which itself is within a while loop - the break
causes the while loop to terminate, bringing the installer to a grinding halt... whoops.

Now, menu_main() is called only from the main process loop, which we don't exit accidentally.

I've removed an entire case statement from do_install_quick() as this function either
performs an installation, or it doesn't - the case statement is pointless.

Due to the removal of whitespace, add ?w=1 in github.com when reviewing.
This commit is contained in:
MilhouseVH 2018-05-16 00:17:48 +01:00
parent c1167e0bcb
commit d2ab6680a7

View File

@ -103,6 +103,7 @@ create_device_list() {
if [ "$DEVICES" = "" ]; then if [ "$DEVICES" = "" ]; then
msg_no_device msg_no_device
return 1
fi fi
for i in $DEVICES; do for i in $DEVICES; do
@ -111,6 +112,7 @@ create_device_list() {
DEVICE_NAME=$(echo $DEVICE_MODEL ${DEVICE_SIZE} | sed 's/ /_/g') DEVICE_NAME=$(echo $DEVICE_MODEL ${DEVICE_SIZE} | sed 's/ /_/g')
DEVICE_LIST="$DEVICE_LIST $i $DEVICE_NAME" DEVICE_LIST="$DEVICE_LIST $i $DEVICE_NAME"
done done
return 0
} }
do_install_quick() { do_install_quick() {
@ -119,137 +121,132 @@ do_install_quick() {
MSG_MENU="\nUse the up/down arrows to select the device you wish to install to.\n\nPlease select a device:" MSG_MENU="\nUse the up/down arrows to select the device you wish to install to.\n\nPlease select a device:"
MSG_CANCEL="Back" MSG_CANCEL="Back"
create_device_list create_device_list || return
whiptail --backtitle "$BACKTITLE" --cancel-button "$MSG_CANCEL" \ whiptail --backtitle "$BACKTITLE" --cancel-button "$MSG_CANCEL" \
$DIALOG_OPTIONS --title "$MSG_TITLE" --menu "$MSG_MENU" 20 50 5 \ $DIALOG_OPTIONS --title "$MSG_TITLE" --menu "$MSG_MENU" 20 50 5 \
$DEVICE_LIST 2> $TMPDIR/device_for_install $DEVICE_LIST 2> $TMPDIR/device_for_install
[ $? -ne 0 ] && return
# now we must do everything # now we must do everything
case $? in INSTALL_DEVICE=$(cat "$TMPDIR/device_for_install")
0) INSTALL_DEVICE_FULL=$(echo $DEVICE_LIST | sed "s|.*$INSTALL_DEVICE \([^ ]*\).*|$INSTALL_DEVICE \1|")
INSTALL_DEVICE=$(cat "$TMPDIR/device_for_install")
INSTALL_DEVICE_FULL=$(echo $DEVICE_LIST | sed "s|.*$INSTALL_DEVICE \([^ ]*\).*|$INSTALL_DEVICE \1|")
case $INSTALL_DEVICE in case $INSTALL_DEVICE in
"/dev/mmcblk"*|"/dev/nvme"*) "/dev/mmcblk"*|"/dev/nvme"*)
PART1="p1" PART1="p1"
PART2="p2" PART2="p2"
;; ;;
*) *)
PART1="1" PART1="1"
PART2="2" PART2="2"
;; ;;
esac esac
prompt_gpt prompt_gpt
prompt_backup_unpack prompt_backup_unpack
# check for confirmation (twice!) # check for confirmation (twice!)
MSG_TITLE="Confirmation before installing" MSG_TITLE="Confirmation before installing"
MSG_DETAIL="\nIf you continue the contents of the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n" MSG_DETAIL="\nIf you continue the contents of the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n"
DIALOG_OPTIONS="--defaultno" DIALOG_OPTIONS="--defaultno"
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" \ whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
$DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0 $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
if [ $? -ne 0 ]; then [ $? -ne 0 ] && return
menu_main
fi
MSG_TITLE="Confirmation before installing" MSG_TITLE="Confirmation before installing"
MSG_DETAIL="\nThis is last chance to abort the installation!\n\nIf you continue the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n\n" MSG_DETAIL="\nThis is last chance to abort the installation!\n\nIf you continue the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n\n"
DIALOG_OPTIONS="--defaultno" DIALOG_OPTIONS="--defaultno"
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" \ whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
$DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0 $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
if [ $? -ne 0 ]; then [ $? -ne 0 ] && return
menu_main
fi
# start the progress bar (whiptail --gauge) # start the progress bar (whiptail --gauge)
{ {
# remove all partitions # remove all partitions
msg_progress_install "1" "Get all partitions $INSTALL_DEVICE" msg_progress_install "1" "Get all partitions $INSTALL_DEVICE"
get_partition $INSTALL_DEVICE get_partition $INSTALL_DEVICE
msg_progress_install "5" "Wiping disk $INSTALL_DEVICE" msg_progress_install "5" "Wiping disk $INSTALL_DEVICE"
dd if=/dev/zero of=$INSTALL_DEVICE bs=4096 count=1024 2>>$LOGFILE dd if=/dev/zero of=$INSTALL_DEVICE bs=4096 count=1024 2>>$LOGFILE
# create 2 new partitions (first $PARTSIZE_SYSTEM, second rest) # create 2 new partitions (first $PARTSIZE_SYSTEM, second rest)
msg_progress_install "7" "Creating label on $INSTALL_DEVICE" msg_progress_install "7" "Creating label on $INSTALL_DEVICE"
if [ "$GPT" = "1" ]; then if [ "$GPT" = "1" ]; then
parted -s $INSTALL_DEVICE mklabel gpt >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE mklabel gpt >> $LOGFILE 2>&1
else else
parted -s $INSTALL_DEVICE mklabel msdos >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE mklabel msdos >> $LOGFILE 2>&1
fi fi
msg_progress_install "9" "Writing Master Boot Record on $INSTALL_DEVICE" msg_progress_install "9" "Writing Master Boot Record on $INSTALL_DEVICE"
if [ "$GPT" = "1" ]; then if [ "$GPT" = "1" ]; then
cat /usr/share/syslinux/gptmbr.bin > $INSTALL_DEVICE cat /usr/share/syslinux/gptmbr.bin > $INSTALL_DEVICE
else else
cat /usr/share/syslinux/mbr.bin > $INSTALL_DEVICE cat /usr/share/syslinux/mbr.bin > $INSTALL_DEVICE
fi fi
partsize_system_start=$PARTSIZE_SYSTEM_OFFSET partsize_system_start=$PARTSIZE_SYSTEM_OFFSET
partsize_system_end=$(((PARTSIZE_SYSTEM * 1024 * 1024 / 512) + partsize_system_start - 1)) partsize_system_end=$(((PARTSIZE_SYSTEM * 1024 * 1024 / 512) + partsize_system_start - 1))
partsize_storage_start=$((partsize_system_end + 1)) partsize_storage_start=$((partsize_system_end + 1))
partsize_storage_end=-1024 partsize_storage_end=-1024
msg_progress_install "10" "Creating partition on $INSTALL_DEVICE" msg_progress_install "10" "Creating partition on $INSTALL_DEVICE"
parted -s $INSTALL_DEVICE unit s mkpart primary fat32 -- $partsize_system_start $partsize_system_end >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE unit s mkpart primary fat32 -- $partsize_system_start $partsize_system_end >> $LOGFILE 2>&1
msg_progress_install "13" "Creating partition on $INSTALL_DEVICE" msg_progress_install "13" "Creating partition on $INSTALL_DEVICE"
parted -s $INSTALL_DEVICE unit s mkpart primary ext4 -- $partsize_storage_start $partsize_storage_end >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE unit s mkpart primary ext4 -- $partsize_storage_start $partsize_storage_end >> $LOGFILE 2>&1
msg_progress_install "16" "Setup bootflag on partition 1 of $INSTALL_DEVICE" msg_progress_install "16" "Setup bootflag on partition 1 of $INSTALL_DEVICE"
parted -s $INSTALL_DEVICE set 1 boot on >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE set 1 boot on >> $LOGFILE 2>&1
if [ "$GPT" = "1" ]; then if [ "$GPT" = "1" ]; then
parted -s $INSTALL_DEVICE set 1 legacy_boot on >> $LOGFILE 2>&1 parted -s $INSTALL_DEVICE set 1 legacy_boot on >> $LOGFILE 2>&1
fi fi
msg_progress_install "20" "Tell the kernel we have a new partition table on $INSTALL_DEVICE" msg_progress_install "20" "Tell the kernel we have a new partition table on $INSTALL_DEVICE"
partprobe $INSTALL_DEVICE >> $LOGFILE 2>&1 partprobe $INSTALL_DEVICE >> $LOGFILE 2>&1
# create filesystem # create filesystem
msg_progress_install "23" "Creating filesystem on ${INSTALL_DEVICE}1" msg_progress_install "23" "Creating filesystem on ${INSTALL_DEVICE}1"
mkfs.vfat ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1 mkfs.vfat ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
msg_progress_install "25" "Set uuid and disklabel $DISKLABEL_SYSTEM on ${INSTALL_DEVICE}${PART1}" msg_progress_install "25" "Set uuid and disklabel $DISKLABEL_SYSTEM on ${INSTALL_DEVICE}${PART1}"
dosfslabel ${INSTALL_DEVICE}${PART1} $DISKLABEL_SYSTEM >> $LOGFILE 2>&1 dosfslabel ${INSTALL_DEVICE}${PART1} $DISKLABEL_SYSTEM >> $LOGFILE 2>&1
msg_progress_install "28" "Creating filesystem on ${INSTALL_DEVICE}${PART2}" msg_progress_install "28" "Creating filesystem on ${INSTALL_DEVICE}${PART2}"
mke2fs -t ext4 -m 0 ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1 mke2fs -t ext4 -m 0 ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1
msg_progress_install "30" "Set uuid and disklabel $DISKLABEL_STORAGE on ${INSTALL_DEVICE}${PART2}" msg_progress_install "30" "Set uuid and disklabel $DISKLABEL_STORAGE on ${INSTALL_DEVICE}${PART2}"
tune2fs -U random -L $DISKLABEL_STORAGE ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1 tune2fs -U random -L $DISKLABEL_STORAGE ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1
UUID_SYSTEM="$(blkid --output udev ${INSTALL_DEVICE}${PART1} | grep ^ID_FS_UUID= | cut -d= -f2)" UUID_SYSTEM="$(blkid --output udev ${INSTALL_DEVICE}${PART1} | grep ^ID_FS_UUID= | cut -d= -f2)"
UUID_STORAGE="$(blkid --output udev ${INSTALL_DEVICE}${PART2} | grep ^ID_FS_UUID= | cut -d= -f2)" UUID_STORAGE="$(blkid --output udev ${INSTALL_DEVICE}${PART2} | grep ^ID_FS_UUID= | cut -d= -f2)"
echo "UUID_SYSTEM : ${UUID_SYSTEM}" >> $LOGFILE echo "UUID_SYSTEM : ${UUID_SYSTEM}" >> $LOGFILE
echo "UUID_STORAGE: ${UUID_STORAGE}" >> $LOGFILE echo "UUID_STORAGE: ${UUID_STORAGE}" >> $LOGFILE
# mount system partition # mount system partition
msg_progress_install "35" "Creating $TMPDIR/part1" msg_progress_install "35" "Creating $TMPDIR/part1"
mkdir -p $TMPDIR/part1 >> $LOGFILE 2>&1 mkdir -p $TMPDIR/part1 >> $LOGFILE 2>&1
msg_progress_install "40" "Mounting ${INSTALL_DEVICE}${PART1} to $TMPDIR/part1" msg_progress_install "40" "Mounting ${INSTALL_DEVICE}${PART1} to $TMPDIR/part1"
mount -t vfat ${INSTALL_DEVICE}${PART1} $TMPDIR/part1 >> $LOGFILE 2>&1 mount -t vfat ${INSTALL_DEVICE}${PART1} $TMPDIR/part1 >> $LOGFILE 2>&1
# installing syslinux # installing syslinux
msg_progress_install "50" "Installing syslinux to $TMPDIR/part1" msg_progress_install "50" "Installing syslinux to $TMPDIR/part1"
syslinux -i ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1 syslinux -i ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
# install system files # install system files
msg_progress_install "60" "Installing Kernel" msg_progress_install "60" "Installing Kernel"
cp /flash/KERNEL $TMPDIR/part1 >> $LOGFILE 2>&1 cp /flash/KERNEL $TMPDIR/part1 >> $LOGFILE 2>&1
msg_progress_install "65" "Installing System" msg_progress_install "65" "Installing System"
cp /flash/SYSTEM $TMPDIR/part1 >> $LOGFILE 2>&1 cp /flash/SYSTEM $TMPDIR/part1 >> $LOGFILE 2>&1
sync sync
# configuring bootloader # configuring bootloader
msg_progress_install "80" "Setup bootloader with boot label = $DISKLABEL_SYSTEM and disk label = $DISKLABEL_STORAGE" msg_progress_install "80" "Setup bootloader with boot label = $DISKLABEL_SYSTEM and disk label = $DISKLABEL_STORAGE"
mkdir -p $TMPDIR/part1/EFI/BOOT mkdir -p $TMPDIR/part1/EFI/BOOT
cat << EOF > $TMPDIR/part1/syslinux.cfg cat << EOF > $TMPDIR/part1/syslinux.cfg
DEFAULT linux DEFAULT linux
PROMPT 0 PROMPT 0
@ -257,7 +254,8 @@ LABEL linux
KERNEL /KERNEL KERNEL /KERNEL
APPEND boot=UUID=$UUID_SYSTEM disk=UUID=$UUID_STORAGE $SYSLINUX_PARAMETERS quiet APPEND boot=UUID=$UUID_SYSTEM disk=UUID=$UUID_STORAGE $SYSLINUX_PARAMETERS quiet
EOF EOF
cat << EOF > $TMPDIR/part1/EFI/BOOT/grub.cfg
cat << EOF > $TMPDIR/part1/EFI/BOOT/grub.cfg
set timeout="0" set timeout="0"
set default="LibreELEC" set default="LibreELEC"
@ -266,55 +264,46 @@ menuentry "LibreELEC" {
linux /KERNEL boot=UUID=$UUID_SYSTEM disk=UUID=$UUID_STORAGE quiet linux /KERNEL boot=UUID=$UUID_SYSTEM disk=UUID=$UUID_STORAGE quiet
} }
EOF EOF
# uefi boot / hybrid mode
cp /usr/share/syslinux/bootx64.efi $TMPDIR/part1/EFI/BOOT
cp /usr/share/syslinux/ldlinux.e64 $TMPDIR/part1/EFI/BOOT
cp /usr/share/grub/bootia32.efi $TMPDIR/part1/EFI/BOOT
sync
# umount system partition, remove mountpoint # uefi boot / hybrid mode
msg_progress_install "85" "Unmount $TMPDIR/part1" cp /usr/share/syslinux/bootx64.efi $TMPDIR/part1/EFI/BOOT
umount $TMPDIR/part1 >> $LOGFILE 2>&1 cp /usr/share/syslinux/ldlinux.e64 $TMPDIR/part1/EFI/BOOT
cp /usr/share/grub/bootia32.efi $TMPDIR/part1/EFI/BOOT
sync
msg_progress_install "87" "Remove $TMPDIR/part1" # umount system partition, remove mountpoint
rmdir $TMPDIR/part1 >> $LOGFILE 2>&1 msg_progress_install "85" "Unmount $TMPDIR/part1"
umount $TMPDIR/part1 >> $LOGFILE 2>&1
if [ "$BACKUP_UNPACK" = "1" ]; then msg_progress_install "87" "Remove $TMPDIR/part1"
# mount storage partition rmdir $TMPDIR/part1 >> $LOGFILE 2>&1
msg_progress_install "89" "Creating $TMPDIR/part2"
mkdir -p $TMPDIR/part2 >> $LOGFILE 2>&1
msg_progress_install "90" "Mounting ${INSTALL_DEVICE}${PART2} to $TMPDIR/part2" if [ "$BACKUP_UNPACK" = "1" ]; then
mount -t ext4 ${INSTALL_DEVICE}${PART2} $TMPDIR/part2 >> $LOGFILE 2>&1 # mount storage partition
msg_progress_install "89" "Creating $TMPDIR/part2"
mkdir -p $TMPDIR/part2 >> $LOGFILE 2>&1
msg_progress_install "92" "Restoring backup" msg_progress_install "90" "Mounting ${INSTALL_DEVICE}${PART2} to $TMPDIR/part2"
[ -f /flash/backup.tar.bz2 ] && tar -xjf /flash/backup.tar.bz2 -C $TMPDIR/part2 >> $LOGFILE 2>&1 mount -t ext4 ${INSTALL_DEVICE}${PART2} $TMPDIR/part2 >> $LOGFILE 2>&1
[ -f /flash/backup.zip ] && unzip -qq /flash/backup.zip -d $TMPDIR/part2 >> $LOGFILE 2>&1
sync
# umount system partition, remove mountpoint msg_progress_install "92" "Restoring backup"
msg_progress_install "97" "Unmount $TMPDIR/part2" [ -f /flash/backup.tar.bz2 ] && tar -xjf /flash/backup.tar.bz2 -C $TMPDIR/part2 >> $LOGFILE 2>&1
umount $TMPDIR/part2 >> $LOGFILE 2>&1 [ -f /flash/backup.zip ] && unzip -qq /flash/backup.zip -d $TMPDIR/part2 >> $LOGFILE 2>&1
sync
msg_progress_install "100" "Remove $TMPDIR/part2" # umount system partition, remove mountpoint
rmdir $TMPDIR/part2 >> $LOGFILE 2>&1 msg_progress_install "97" "Unmount $TMPDIR/part2"
fi umount $TMPDIR/part2 >> $LOGFILE 2>&1
} | whiptail --backtitle "$BACKTITLE" --gauge "Please wait while your system is being setup ..." 6 73 0
# install complete msg_progress_install "100" "Remove $TMPDIR/part2"
MSG_TITLE="@DISTRONAME@ Install Complete" rmdir $TMPDIR/part2 >> $LOGFILE 2>&1
MSG_DETAIL="You may now remove the install media and reboot.\n" fi
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_DETAIL" 7 52 } | whiptail --backtitle "$BACKTITLE" --gauge "Please wait while your system is being setup ..." 6 73 0
menu_main # install complete
;; MSG_TITLE="@DISTRONAME@ Install Complete"
1) MSG_DETAIL="You may now remove the install media and reboot.\n"
menu_main whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_DETAIL" 7 52
;;
255)
do_poweroff
;;
esac
} }
msg_no_device() { msg_no_device() {
@ -323,18 +312,6 @@ msg_no_device() {
MSG_INFOBOX=" No devices were found. " MSG_INFOBOX=" No devices were found. "
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_INFOBOX" 9 73 whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_INFOBOX" 9 73
menu_main
}
msg_target_space() {
# show an error dialog for missing space
MSG_TITLE="TARGET SPACE"
MSG_INFOBOX="Not enough target space!\nInstallation aborted.\n"
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_INFOBOX" 9 73
menu_main
} }
msg_progress_install() { msg_progress_install() {
@ -348,15 +325,6 @@ msg_progress_install() {
echo XXX echo XXX
} }
msg_install_ready() {
# show a dialog that we have installed
MSG_TITLE="INFORMATION"
whiptail --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox " $1" 7 73
menu_main
}
prompt_gpt() { prompt_gpt() {
GPT="0" GPT="0"
UEFI="0" UEFI="0"
@ -403,16 +371,16 @@ out during the installation. \
whiptail --backtitle "$BACKTITLE" --cancel-button "$MSG_CANCEL" \ whiptail --backtitle "$BACKTITLE" --cancel-button "$MSG_CANCEL" \
--title "$MSG_TITLE" --menu "$MSG_MENU" 18 73 3 \ --title "$MSG_TITLE" --menu "$MSG_MENU" 18 73 3 \
1 "Install @DISTRONAME@" \ 1 "Install @DISTRONAME@" \
2 "Installation log" \ 2 "View installation log" \
3 "Reboot" 2> $TMPDIR/mainmenu 3 "Reboot" 2> $TMPDIR/mainmenu
case $? in case $? in
0) 0)
ITEM_MAINMENU=$(cat "$TMPDIR/mainmenu") ITEM_MAINMENU=$(cat "$TMPDIR/mainmenu")
case $ITEM_MAINMENU in case $ITEM_MAINMENU in
1) do_install_quick; break;; 1) do_install_quick;;
2) logfile_show; break;; 2) logfile_show;;
3) do_reboot; 3) do_reboot;;
esac esac
;; ;;
1) 1)
@ -426,8 +394,6 @@ out during the installation. \
logfile_show() { logfile_show() {
whiptail --textbox "$LOGFILE" 20 73 --scrolltext --backtitle "$BACKTITLE" whiptail --textbox "$LOGFILE" 20 73 --scrolltext --backtitle "$BACKTITLE"
clear
menu_main
} }
do_reboot() { do_reboot() {