From f1d548310e25859159600afb52554bf627417726 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 14 Oct 2024 14:56:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=20=D0=B8=D0=BD?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D0=BB=D1=8F=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 install.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a39daad --- /dev/null +++ b/install.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +[ ${GFU_DEBUG} ] && set -x + +SCRIPT_NAME="${0}" +SCRIPT_PATH=$(realpath ${SCRIPT_NAME}) + +# [ ! "$(id -u)" == 0 ] && echo "Need to run as superuser" && exit 1 + +main () { + local GFU_KEY="" + local GFU_DEVICE="" + local GFU_LEGACY_MODE=false + local GFU_EFI_MODE=false + local GFU_MOUNT_PATH="" + + while [[ $# -gt 0 ]] ; do + GFU_KEY="$1" + case "${GFU_KEY}" in + -d|--device) + GFU_DEVICE="${2}" + shift + ;; + -l|--legacy) + GFU_LEGACY_MODE=true + ;; + -e|--efi) + GFU_EFI_MODE=true + ;; + -p|--path) + GFU_MOUNT_PATH="${2}" + shift + ;; + *) + break + ;; + esac + shift + done + + local GFU_GRUB_PATH="/usr/lib/grub" + local GFU_GRUB_EFI_X32="i386-efi" + local GFU_GRUB_EFI_X64="x86_64-efi" + local GFU_GRUB_LEGACY="i386-pc" + + GFU_DEVICE=${GFU_DEVICE:-$ENV_GFU_DEVICE} + [ -z "${GFU_DEVICE}" ] && echo "ENV_GFU_DEVICE: The device was not set" && exit 1 + [ ! -b ${GFU_DEVICE} ] && echo "ENV_GFU_DEVICE: Device not found: ${GFU_DEVICE}" && exit 1 + + ${GFU_LEGACY_MODE} && ${GFU_EFI_MODE} && echo "You only need to set one mode: legacy or efi" && exit 1 + ! ${GFU_LEGACY_MODE} && ! ${GFU_EFI_MODE} && echo "None of the modes are set: legacy or efi" && exit 1 + + GFU_MOUNT_PATH=${GFU_MOUNT_PATH:-$ENV_GFU_MOUNT_PATH} + [ -z "${GFU_MOUNT_PATH}" ] && echo "ENV_GFU_MOUNT_PATH: The path to the mount directory was not set" && exit 1 + GFU_MOUNT_PATH=$(realpath ${GFU_MOUNT_PATH} 2>/dev/null) + + if ${GFU_LEGACY_MODE} ; then + local GFU_GRUB_LEGACY_MODE_PATH="${GFU_GRUB_PATH}/${GFU_GRUB_LEGACY}" + local GFU_GRUB_LEGACY_MODE=false + + [ -d ${GFU_GRUB_LEGACY_MODE_PATH} ] && GFU_GRUB_LEGACY_MODE=true + + ! ${GFU_GRUB_LEGACY_MODE} && echo "No legacy bootloader was found for installation: ${GFU_GRUB_LEGACY}" && exit 1 + fi + + if ${GFU_EFI_MODE} ; then + local GFU_GRUB_EFI_MODE_X32_PATH="${GFU_GRUB_PATH}/${GFU_GRUB_EFI_X32}" + local GFU_GRUB_EFI_MODE_X64_PATH="${GFU_GRUB_PATH}/${GFU_GRUB_EFI_X64}" + local GFU_GRUB_EFI_MODE_X32=false + local GFU_GRUB_EFI_MODE_X64=false + + [ -d ${GFU_GRUB_EFI_MODE_X32_PATH} ] && GFU_GRUB_EFI_MODE_X32=true + [ -d ${GFU_GRUB_EFI_MODE_X64_PATH} ] && GFU_GRUB_EFI_MODE_X64=true + + ! ${GFU_GRUB_EFI_MODE_X32} && ! ${GFU_GRUB_EFI_MODE_X64} && echo "No EFI bootloader was found for installation: ${GFU_GRUB_EFI_X32} or ${GFU_GRUB_EFI_X64}" && exit 1 + fi + + mkdir -v -p ${GFU_MOUNT_PATH} 2>/dev/null + + if [ ! -d ${GFU_MOUNT_PATH} ] ; then + echo "Path is not a directory: ${GFU_MOUNT_PATH}" + exit 1 + fi + + if [ ! -z "$(ls -A ${GFU_MOUNT_PATH})" ]; then + echo "Path is not empty: ${GFU_MOUNT_PATH}" + exit 1 + fi + + while true; do + read -p "All data will be deleted from your device ${GFU_DEVICE}. Are you ready to continue? [Y/N]: " answer + case "$answer" in + [Yy]* ) break ;; + [Nn]* ) exit 0 ;; + * ) ;; + esac + done + + if ${GFU_LEGACY_MODE} ; then + parted ${GFU_DEVICE} -s mklabel msdos + else + parted ${GFU_DEVICE} -s mklabel gpt + fi + + parted ${GFU_DEVICE} -s -- mkpart primary 2048s 2099199s + parted ${GFU_DEVICE} -s set 1 boot on + + mkfs.fat -F32 ${GFU_DEVICE}1 + + if ! mount -v -o umask=000 ${GFU_DEVICE}1 ${GFU_MOUNT_PATH} ; then + echo "Failed to mount the device ${GFU_DEVICE}1 to the path ${GFU_MOUNT_PATH}. Further installation will be terminated" + exit 1 + fi + + if ${GFU_LEGACY_MODE} ; then + ${GFU_GRUB_LEGACY_MODE} && grub-install --no-floppy --boot-directory=${GFU_MOUNT_PATH}/boot --target=i386-pc ${GFU_DEVICE} + else + ${GFU_GRUB_EFI_MODE_X32} && grub-install --removable --boot-directory=${GFU_MOUNT_PATH}/boot --efi-directory=${GFU_MOUNT_PATH} --target=i386-efi ${GFU_DEVICE} + ${GFU_GRUB_EFI_MODE_X64} && grub-install --removable --boot-directory=${GFU_MOUNT_PATH}/boot --efi-directory=${GFU_MOUNT_PATH} --target=x86_64-efi ${GFU_DEVICE} + fi + + if [ -d ${SCRIPT_PATH}/grub ] ; then + cp -rv ${SCRIPT_PATH}/grub ${GFU_MOUNT_PATH}/boot + else + touch ${GFU_MOUNT_PATH}/boot/grub/grub.cfg + fi + + umount -v ${GFU_MOUNT_PATH}1 +} + +main "${@}"