From 5bdd634354c2f3b162562413599c201290e42efa Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Fri, 4 Dec 2020 01:58:47 -0600
Subject: [PATCH] Work on automating GRUB install

---
 gentoostrap.sh | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/gentoostrap.sh b/gentoostrap.sh
index 150bd16..ca1dab0 100755
--- a/gentoostrap.sh
+++ b/gentoostrap.sh
@@ -79,6 +79,8 @@ Usage: $_name [OPTION]...
 
   -h			Print this help text
   -c			Perform the Gentoo half of installation. Must be invoked from a chroot
+			or a systemd-nspawn container
+  -d			Perform the second Gentoo half of installation from a chroot
   -v			Print more status messages. Stacks
 
 Copyright (c) 2020 rehashedsalt@cock.li
@@ -198,7 +200,13 @@ build-gentoo() {
 	sudo systemd-nspawn --directory="$_optdest" /gentoostrap.sh -c
 
 	# The nspawn container sadly cannot manipulate the environment entirely, so we need to now do an ACTUAL chroot to install GRUB
-
+	mount --types proc /proc "$_optdest/proc"
+	mount --rbind /sys "$_optdest/sys"
+	mount --make-rslave "$_optdest/sys"
+	mount --rbind /dev "$_optdest/dev"
+	mount --make-rslave "$_optdest/dev"
+	chroot "$_optdest" /gentoostrap.sh -d
+	log "Configuration complete! You should be good to reboot now."
 }
 build-gentoo-chroot() {
 	# Build Gentoo, but from within the chroot environment
@@ -235,6 +243,7 @@ build-gentoo-chroot() {
 	emerge --jobs --quiet-build y \
 		app-admin/sudo \
 		app-editors/vim \
+		sys-boot/grub:2 \
 		sys-fs/{e2fs,xfs}progs \
 		sys-process/cronie
 
@@ -252,7 +261,6 @@ build-gentoo-chroot() {
 	#emerge --jobs --quiet-build y @module-rebuild
 
 	# Install the bootloader
-	emerge --jobs --quiet-build y sys-boot/grub:2
 	mkdir -p /boot/efi
 	chmod 0700 /boot/efi
 
@@ -267,13 +275,21 @@ build-gentoo-chroot() {
 	cat <<-EOF
 	Initial system configuration is complete. There are now just a few tasks left:
 	* Mount the ESP under /boot/efi
-	* Install and configure grub
-	* Reboot and pray
+	* Make sure everything looks good before we install GRUB
 
 	Dumping to a shell now. Have fun!
 	EOF
 	exec bash
 }
+build-gentoo-chroot2() {
+	# Last few chroot steps, mostly involving GRUB
+	log "Configuring GRUB"
+	grub-mkconfig -o /boot/grub
+	log "Installing GRUB"
+	grub-install
+	cat <<-EOF
+	EOF
+}
 
 # Main
 main() {
@@ -282,6 +298,9 @@ main() {
 		# Parse out flags
 		while getopts ":chv" opt; do
 			case $opt in
+				d)
+					_optchroot2=1
+					;;
 				c)
 					_optchroot=1
 					;;
@@ -313,15 +332,17 @@ main() {
 	# TODO: That
 	# Validate core program dependencies
 	log "Validating dependencies" 2
-	if ! has basename curl sha512sum systemd-nspawn tar; then
+	if ! has basename chroot curl sha512sum systemd-nspawn tar; then
 		error "Failed to find program: $_return" 1
 	fi
 
 	# Do the do
-	if [ -z "$_optchroot" ]; then
-		build-gentoo
-	else
+	if [ -n "$_optchroot2" ]; then
+		build-gentoo-chroot2
+	elif [ -n "$_optchroot" ]; then
 		build-gentoo-chroot
+	else
+		build-gentoo
 	fi
 	exit 0
 }