From bae6327c599bc251cd882e56a0a4414256daf169 Mon Sep 17 00:00:00 2001 From: oddlama Date: Tue, 21 Apr 2020 20:48:23 +0200 Subject: [PATCH] Added sylinux installation for bios systems --- README.md | 7 ++-- scripts/functions.sh | 33 ++++++++++------ scripts/internal_config.sh | 3 ++ scripts/main.sh | 78 ++++++++++++++++++++++++++++++-------- scripts/utils.sh | 3 +- 5 files changed, 91 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cc861fb..3ddeed8 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,11 @@ Here is a quick overview of what this script does: * Select best gentoo portage mirrors * Install git (so you can add your portage overlays later) * Install `sys-kernel/vanilla-kernel-bin` (temporarily, until you replace it) -* Copy kernel to efi partition -* Create boot entry using efibootmgr + - EFI: Copy kernel to efi partition + - EFI: Create boot entry using efibootmgr (or install syslinux for BIOS boot) + - BIOS: Install syslinux * Generate fstab -* Lets you set a root password +* Ask for a root password Also, optionally the following will be done: diff --git a/scripts/functions.sh b/scripts/functions.sh index 4cc3482..2139bdf 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -34,14 +34,20 @@ check_config() { [[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BOOT ]] \ || die "You must assign DISK_ID_EFI or DISK_ID_BOOT" - [[ -v "DISK_ID_BOOT" ]] && [[ -v "DISK_ID_TO_UUID[$DISK_ID_BOOT]" ]] \ - || die "Missing uuid for DISK_ID_BOOT, have you made sure it is used?" - [[ -v "DISK_ID_EFI" ]] && [[ -v "DISK_ID_TO_UUID[$DISK_ID_EFI]" ]] \ - || die "Missing uuid for DISK_ID_EFI, have you made sure it is used?" - [[ -v "DISK_ID_SWAP" ]] && [[ -v "DISK_ID_TO_UUID[$DISK_ID_SWAP]" ]] \ - || die "Missing uuid for DISK_ID_SWAP, have you made sure it is used?" - [[ -v "DISK_ID_ROOT" ]] && [[ -v "DISK_ID_TO_UUID[$DISK_ID_ROOT]" ]] \ - || die "Missing uuid for DISK_ID_ROOT, have you made sure it is used?" + [[ -v "DISK_ID_BOOT" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_BOOT]" ]] \ + && die "Missing uuid for DISK_ID_BOOT, have you made sure it is used?" + [[ -v "DISK_ID_EFI" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_EFI]" ]] \ + && die "Missing uuid for DISK_ID_EFI, have you made sure it is used?" + [[ -v "DISK_ID_SWAP" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_SWAP]" ]] \ + && die "Missing uuid for DISK_ID_SWAP, have you made sure it is used?" + [[ -v "DISK_ID_ROOT" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_ROOT]" ]] \ + && die "Missing uuid for DISK_ID_ROOT, have you made sure it is used?" + + if [[ -v "DISK_ID_EFI" ]]; then + IS_EFI=true + else + IS_EFI=false + fi if [[ $INSTALL_ANSIBLE == true ]]; then [[ $INSTALL_SSHD == true ]] \ @@ -154,10 +160,11 @@ disk_create_gpt() { device_desc="$device" fi - disk_id_to_resolvable[$new_id]="raw:$device" + local uuid="${DISK_ID_TO_UUID[$new_id]}" + disk_id_to_resolvable[$new_id]="uuid:$uuid" einfo "Creating new gpt partition table ($new_id) on $device_desc" - sgdisk -Z "$device" >/dev/null \ + sgdisk -Z -U "$uuid" "$device" >/dev/null \ || die "Could not create new gpt partition table ($new_id) on '$device'" partprobe "$device" } @@ -180,8 +187,9 @@ disk_create_partition() { local device="$(resolve_id_to_device "$id")" local partuuid="${DISK_ID_TO_UUID[$new_id]}" + local extra_args="" case "$type" in - 'boot') type='ef02' ;; + 'boot') type='ef02' extra_args='--attributes=0:set:2';; 'efi') type='ef00' ;; 'swap') type='8200' ;; 'raid') type='fd00' ;; @@ -193,7 +201,8 @@ disk_create_partition() { disk_id_to_resolvable[$new_id]="partuuid:$partuuid" einfo "Creating partition ($new_id) with type=$type, size=$size on $device" - sgdisk -n "0:0:$arg_size" -t "0:$type" -u 0:"$partuuid" "$device" >/dev/null \ + # shellcheck disable=SC2086 + sgdisk -n "0:0:$arg_size" -t "0:$type" -u 0:"$partuuid" $extra_args "$device" >/dev/null \ || die "Could not create new gpt partition ($new_id) on '$device' ($id)" partprobe "$device" } diff --git a/scripts/internal_config.sh b/scripts/internal_config.sh index e2892e4..99cde49 100644 --- a/scripts/internal_config.sh +++ b/scripts/internal_config.sh @@ -24,6 +24,8 @@ USED_LUKS=false # An array of disk related actions to perform DISK_ACTIONS=() +# An associative array from partuuid to disk uuid +declare -A DISK_PARTUUID_TO_GPT_UUID # An associative array to check for existing ids (maps to uuids) declare -A DISK_ID_TO_UUID # An associative set to check for correct usage of size=remaining in gpt tables @@ -126,6 +128,7 @@ create_partition() { [[ ${arguments[size]} == "remaining" ]] \ && DISK_GPT_HAD_SIZE_REMAINING[${arguments[id]}]=true + DISK_PARTUUID_TO_GPT_UUID[${DISK_ID_TO_UUID[$new_id]}]="${DISK_ID_TO_UUID[$id]}" DISK_ACTIONS+=("action=create_partition" "$@" ";") } diff --git a/scripts/main.sh b/scripts/main.sh index 40d888a..188b3d9 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -89,14 +89,12 @@ install_sshd() { || die "Could not create group 'sshusers'" } -install_kernel() { - # Install vanilla kernel and efibootmgr, to be able to boot the system. - einfo "Installing binary vanilla kernel" - try emerge --verbose sys-kernel/vanilla-kernel-bin sys-boot/efibootmgr +install_kernel_efi() { + try emerge --verbose sys-boot/efibootmgr # Copy kernel to EFI local kernel_version - kernel_version="$(find "/boot" -name "vmlinuz-*" -printf '%f\n' | sort -V | tail -1)" \ + kernel_version="$(find "/boot" -name "vmlinuz-*" -printf '%f\n' | sort -V | tail -n 1)" \ || die "Could not list newest kernel file" kernel_version="${kernel_version#vmlinuz-}" \ || die "Could not find kernel version" @@ -112,11 +110,46 @@ install_kernel() { local linuxdev linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_ROOT")" \ || die "Could not resolve partition UUID '$PARTITION_UUID_ROOT'" - local efidev - efidev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \ + local efipartdev + efipartdev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \ || die "Could not resolve partition UUID '$PARTITION_UUID_EFI'" - local efipartnum="${efidev: -1}" - try efibootmgr --verbose --create --disk "$PARTITION_DEVICE" --part "$efipartnum" --label "gentoo" --loader '\EFI\vmlinuz.efi' --unicode "root=$linuxdev initrd=\\EFI\\initramfs.img" + local efipartnum="${efipartdev: -1}" + local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_EFI]}" + local gptdev + gptdev="$(get_device_by_uuid "$gptuuid")" \ + || die "Could not resolve GPT UUID '$gptuuid'" + try efibootmgr --verbose --create --disk "$gptdev" --part "$efipartnum" --label "gentoo" --loader '\EFI\vmlinuz.efi' --unicode "root=$linuxdev initrd=\\EFI\\initramfs.img" +} + +install_kernel_bios() { + try emerge --verbose sys-boot/syslinux + + # Install syslinux MBR record + einfo "Copying syslinux MBR record" + local bootdev + bootdev="$(get_device_by_partuuid "$PARTITION_UUID_BOOT")" \ + || die "Could not resolve partition UUID '$PARTITION_UUID_BOOT'" + local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_BOOT]}" + local gptdev + gptdev="$(get_device_by_uuid "$gptuuid")" \ + || die "Could not resolve GPT UUID '$gptuuid'" + try dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of="$gptdev" + + # Install syslinux + einfo "Installing syslinux" + syslinux --install "$bootdev" +} + +install_kernel() { + # Install vanilla kernel + einfo "Installing binary vanilla kernel" + try emerge --verbose sys-kernel/vanilla-kernel-bin + + if [[ $IS_EFI == "true" ]]; then + install_kernel_efi + else + install_kernel_bios + fi } install_ansible() { @@ -153,10 +186,16 @@ main_install_gentoo_in_chroot() { passwd -l root \ || die "Could not change root password" - # Mount efi partition - mount_efivars - einfo "Mounting efi partition" - mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi" + if [[ $IS_EFI == "true" ]]; then + # Mount efi partition + mount_efivars + einfo "Mounting efi partition" + mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi" + else + # Mount boot partition + einfo "Mounting boot partition" + mount_by_partuuid "$PARTITION_UUID_BOOT" "/boot" + fi # Sync portage einfo "Syncing portage tree" @@ -193,8 +232,13 @@ main_install_gentoo_in_chroot() { || die "Could not overwrite /etc/fstab" echo "PARTUUID=$PARTITION_UUID_ROOT / ext4 defaults,noatime,errors=remount-ro,discard 0 1" >> /etc/fstab \ || die "Could not append entry to fstab" - echo "PARTUUID=$PARTITION_UUID_EFI /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ - || die "Could not append entry to fstab" + if [[ $IS_EFI == "true" ]]; then + echo "PARTUUID=$PARTITION_UUID_EFI /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ + || die "Could not append entry to fstab" + else + echo "PARTUUID=$PARTITION_UUID_BOOT /boot vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ + || die "Could not append entry to fstab" + fi if [[ -v "PARTITION_UUID_SWAP" ]]; then echo "PARTUUID=$PARTITION_UUID_SWAP none swap defaults,discard 0 0" >> /etc/fstab \ || die "Could not append entry to fstab" @@ -245,7 +289,9 @@ main_install() { gentoo_umount install_stage3 - mount_efivars + + [[ $IS_EFI == "true" ]] \ + && mount_efivars gentoo_chroot "$GENTOO_INSTALL_REPO_BIND/scripts/main.sh" install_gentoo_in_chroot gentoo_umount } diff --git a/scripts/utils.sh b/scripts/utils.sh index 9f6a8bf..45a20fa 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -18,8 +18,7 @@ eerror() { die() { eerror "$*" - echo "Aborted." >&2 - kill "$GENTOO_INSTALL_REPO_SCRIPT_PID" >/dev/null + kill "$GENTOO_INSTALL_REPO_SCRIPT_PID" exit 1 }