Cosmetic changes and fix grub installation

Avoid a condition when user_drive variable could be empty so GRUB installation could fail
This commit is contained in:
Le0xFF 2022-07-17 22:54:09 +02:00
parent e915777031
commit 3614463fd9

129
chroot.sh
View File

@ -1,25 +1,41 @@
#! /bin/bash
function set_root {
clear
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### Setting root password #\n"
echo -e -n "#######################################\n"
echo -e -n "\nSetting root password:\n\n"
passwd root
echo -e -n "\nSetting root permissions...\n"
echo -e -n "\nSetting root permissions...\n\n"
chown root:root /
chmod 755 /
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
function edit_fstab {
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### fstab creation #\n"
echo -e -n "#######################################\n"
echo -e -n "\nExporting variables that will be used for fstab...\n"
export UEFI_UUID=$(blkid -s UUID -o value "${boot_partition}")
export LUKS_UUID=$(blkid -s UUID -o value "${encrypted_partition}")
export ROOT_UUID=$(blkid -s UUID -o value /dev/mapper/"${vg_name}"-"${lv_root_name}")
export HOME_UUID=$(blkid -s UUID -o value /dev/mapper/"${vg_name}"-"${lv_home_name}")
echo -e -n "\nWriting fstab...\n"
echo -e -n "\nWriting fstab...\n\n"
sed -i '/tmpfs/d' /etc/fstab
cat << EOF >> /etc/fstab
@ -30,28 +46,46 @@ UUID=$UEFI_UUID /boot/efi vfat defaults,noatime 0 2
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
EOF
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
function generate_random_key {
echo -e -n "\nGenerate random key to avoid typing password twice at boot...\n"
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### Random key generation #\n"
echo -e -n "#######################################\n"
echo -e -n "\nGenerate random key to avoid typing password twice at boot...\n\n"
dd bs=512 count=4 if=/dev/random of=/boot/volume.key
echo -e -n "\nRandom key generated, unlocking the encrypted partition...\n"
echo -e -n "\n\nRandom key generated, unlocking the encrypted partition...\n"
cryptsetup luksAddKey "${encrypted_partition}" /boot/volume.key
chmod 000 /boot/volume.key
chmod -R g-rwx,o-rwx /boot
echo -e -n "\nAdding random key to /etc/crypttab...\n"
echo -e -n "\nAdding random key to /etc/crypttab...\n\n"
cat << EOF >> /etc/crypttab
$encrypted_name UUID=$LUKS_UUID /boot/volume.key luks
EOF
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
function generate_dracut_conf {
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### Dracut configuration #\n"
echo -e -n "#######################################\n"
echo -e -n "\nAdding random key to dracut configuration...\n"
cat << EOF >> /etc/dracut.conf.d/10-crypt.conf
install_items+=" /boot/volume.key /etc/crypttab "
@ -62,38 +96,106 @@ EOF
echo -e "add_dracutmodules+=\" crypt btrfs lvm resume \"" >> /etc/dracut.conf.d/20-addmodules.conf
echo -e "tmpdir=/tmp" >> /etc/dracut.conf.d/30-tmpfs.conf
echo -e -n "\nGenerating new dracut initramfs...\n"
echo -e -n "\nGenerating new dracut initramfs...\n\n"
dracut --force --hostonly --kver $(ls /usr/lib/modules/)
echo
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
function install_grub {
echo -e -n "\nEnabling CRYPTODISK in GRUB...\n"
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### GRUB installation #\n"
echo -e -n "#######################################\n"
echo -e -n "\nEnabling CRYPTODISK in GRUB...\n\n"
cat << EOF >> /etc/default/grub
GRUB_ENABLE_CRYPTODISK=y
EOF
sed -i "/GRUB_CMDLINE_LINUX_DEFAULT=/s/\"$/ rd.auto=1 rd.luks.name=$LUKS_UUID=$encrypted_name rd.luks.allow-discards=$LUKS_UUID&/" /etc/default/grub
read -n 1 -r -p "[Press any key to continue...]" key
clear
if ! cat /proc/mounts | grep efivar &> /dev/null ; then
echo -e -n "\nMounting efivarfs...\n"
mount -t efivarfs efivarfs /sys/firmware/efi/efivars/
fi
if [[ -z "${user_drive}" ]] ; then
while true ; do
clear
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### GRUB installation #\n"
echo -e -n "#######################################\n"
lsblk -p
echo -e -n "\nOn Which drive do you want to install GRUB?\nPlease enter the full drive path (i.e. /dev/sda): "
read -r user_drive
if [[ ! -e "${user_drive}" ]] ; then
echo -e -n "\nPlease select a valid drive.\n\n"
read -n 1 -r -p "[Press any key to continue...]" key
else
while true; do
echo -e -n "\nYou selected: "${user_drive}".\n\n"
read -r -p "Are you sure you want to continue? (y/n and [ENTER]): " yn
if [[ "${yn}" == "n" ]] || [[ "${yn}" == "N" ]] ; then
echo -e -n "\nAborting, select another drive.\n\n"
read -n 1 -r -p "[Press any key to continue...]" key
break
elif [[ "${yn}" == "y" ]] || [[ "${yn}" == "Y" ]] ; then
echo -e -n "\nCorrect drive selected, continuing with grub installation...\n\n"
read -n 1 -r -p "[Press any key to continue...]" key
break 2
else
echo -e -n "\nPlease answer y or n.\n\n"
read -n 1 -r -p "[Press any key to continue...]" key
fi
done
fi
done
fi
clear
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### GRUB installation #\n"
echo -e -n "#######################################\n"
echo -e -n "\nInstalling GRUB on "${user_drive}" with \"VoidLinux\" as bootloader-id...\n\n"
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=VoidLinux --boot-directory=/boot --recheck
echo -e -n "\nEnabling SSD trim...\n"
echo -e -n "\nEnabling SSD trim...\n\n"
sed -i 's/issue_discards = 0/issue_discards = 1/' /etc/lvm/lvm.conf
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
function enabling_services {
echo -e -n "\nEnabling internet service at first boot...\n"
echo -e -n "#######################################\n"
echo -e -n "# VLI # Chroot #\n"
echo -e -n "#######################################\n"
echo -e -n "####### Enabling services #\n"
echo -e -n "#######################################\n"
echo -e -n "\nEnabling internet service at first boot...\n\n"
ln -s /etc/sv/dbus /etc/runit/runsvdir/default/
ln -s /etc/sv/NetworkManager /etc/runit/runsvdir/default/
read -n 1 -r -p "[Press any key to continue...]" key
clear
}
@ -104,7 +206,10 @@ generate_dracut_conf
install_grub
enabling_services
echo -e -n "\nReconfiguring every package...\n"
echo -e -n "\nReconfiguring every package...\n\n"
xbps-reconfigure -fa
echo -e -n "\nEverything's done, exiting chroot...\n"
echo -e -n "\nEverything's done, exiting chroot...\n\n"
read -n 1 -r -p "[Press any key to continue...]" key
clear