From a5e84763528efd2b1f61aefd1cffa434328600f8 Mon Sep 17 00:00:00 2001 From: oddlama Date: Thu, 2 Jan 2020 23:28:16 +0100 Subject: [PATCH] Implemented script logging, export parallel compilation paramters in chroot --- scripts/functions.sh | 52 +++++++++++++++++------- scripts/install_gentoo_in_chroot | 1 + scripts/main.sh | 70 ++++++++++++++++++++++++-------- scripts/main_chroot.sh | 14 ++++++- scripts/utils.sh | 20 +++++++-- umount | 1 + 6 files changed, 120 insertions(+), 38 deletions(-) create mode 120000 scripts/install_gentoo_in_chroot create mode 120000 umount diff --git a/scripts/functions.sh b/scripts/functions.sh index c73081a..7132b68 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -143,27 +143,37 @@ format_partitions() { dev="$(get_device_by_partuuid "$PARTITION_UUID_LINUX")" \ || die "Could not resolve partition UUID '$PARTITION_UUID_LINUX'" einfo " $dev (linux)" - mkfs.ext4 -L "linux" "$dev" \ + mkfs.ext4 -q -L "linux" "$dev" \ || die "Could not create ext4 filesystem" } -mount_root() { - # Skip if root is already mounted - mountpoint -q -- "$ROOT_MOUNTPOINT" \ +mount_by_partuuid() { + local dev + local partuuid="$1" + local mountpoint="$2" + + # Skip if already mounted + mountpoint -q -- "$mountpoint" \ && return - # Mount root device - einfo "Mounting root device" - mkdir -p "$ROOT_MOUNTPOINT" \ - || die "Could not create mountpoint directory $ROOT_MOUNTPOINT" - local dev - dev="$(get_device_by_partuuid "$PARTITION_UUID_LINUX")" \ + # Mount device + einfo "Mounting device partuuid=$partuuid to '$mountpoint'" + mkdir -p "$mountpoint" \ + || die "Could not create mountpoint directory '$mountpoint'" + dev="$(get_device_by_partuuid "$partuuid")" \ || die "Could not resolve partition UUID '$PARTITION_UUID_LINUX'" - mount "$dev" "$ROOT_MOUNTPOINT" \ - || die "Could not mount root device '$dev'" + mount "$dev" "$mountpoint" \ + || die "Could not mount device '$dev'" +} + +mount_root() { + mount_by_partuuid "$PARTITION_UUID_LINUX" "$ROOT_MOUNTPOINT" } bind_bootstrap_dir() { + # Use new location by default + GENTOO_BOOTSTRAP_DIR="$GENTOO_BOOTSTRAP_BIND" + # Bind the bootstrap dir to a location in /tmp, # so it can be accessed from within the chroot mountpoint -q -- "$GENTOO_BOOTSTRAP_BIND" \ @@ -173,8 +183,8 @@ bind_bootstrap_dir() { einfo "Bind mounting bootstrap directory" mkdir -p "$GENTOO_BOOTSTRAP_BIND" \ || die "Could not create mountpoint directory '$GENTOO_BOOTSTRAP_BIND'" - mount --bind "$GENTOO_BOOTSTRAP_DIR" "$GENTOO_BOOTSTRAP_BIND" \ - || die "Could not bind mount '$GENTOO_BOOTSTRAP_DIR' to '$GENTOO_BOOTSTRAP_BIND'" + mount --bind "$GENTOO_BOOTSTRAP_DIR_ORIGINAL" "$GENTOO_BOOTSTRAP_BIND" \ + || die "Could not bind mount '$GENTOO_BOOTSTRAP_DIR_ORIGINAL' to '$GENTOO_BOOTSTRAP_BIND'" } download_stage3() { @@ -247,9 +257,21 @@ extract_stage3() { || die "Could not cd into '$TMP_DIR'" } +gentoo_umount() { + einfo "Unmounting root filesystem" + if mountpoint -q -- "$ROOT_MOUNTPOINT"; then + umount -R "$ROOT_MOUNTPOINT" \ + || die "Could not unmount filesystems" + fi +} + gentoo_chroot() { [[ $# -gt 0 ]] || die "Missing command argument" + [[ $EXECUTED_IN_CHROOT != true ]] \ + || die "Already in chroot" + + gentoo_umount mount_root bind_bootstrap_dir @@ -273,6 +295,6 @@ gentoo_chroot() { einfo "Chrooting..." EXECUTED_IN_CHROOT=true \ TMP_DIR=$TMP_DIR \ - exec chroot "$ROOT_MOUNTPOINT" "$GENTOO_BOOTSTRAP_BIND/scripts/main_chroot.sh" "$@" \ + exec chroot "$ROOT_MOUNTPOINT" "$GENTOO_BOOTSTRAP_DIR/scripts/main_chroot.sh" "$@" \ || die "Failed to chroot into '$ROOT_MOUNTPOINT'" } diff --git a/scripts/install_gentoo_in_chroot b/scripts/install_gentoo_in_chroot new file mode 120000 index 0000000..fe75b4e --- /dev/null +++ b/scripts/install_gentoo_in_chroot @@ -0,0 +1 @@ +main.sh \ No newline at end of file diff --git a/scripts/main.sh b/scripts/main.sh index c07d6e2..c93b490 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -14,9 +14,11 @@ get_source_dir() { echo -n "$(realpath "$(dirname "${source}")")" } -export GENTOO_BOOTSTRAP_DIR="$(dirname "$(get_source_dir)")" +export GENTOO_BOOTSTRAP_DIR_ORIGINAL="$(dirname "$(get_source_dir)")" +export GENTOO_BOOTSTRAP_DIR="$GENTOO_BOOTSTRAP_DIR_ORIGINAL" export GENTOO_BOOTSTRAP_SCRIPT_ACTIVE=true export GENTOO_BOOTSTRAP_SCRIPT_PID=$$ +LOGDATE="$(date +%Y%m%d-%H%M%S)" umask 0077 @@ -32,7 +34,7 @@ mkdir -p "$TMP_DIR" ################################################ # Functions -main_install_stage3() { +install_stage3() { [[ $# == 0 ]] || die "Too many arguments" prepare_installation_environment @@ -42,15 +44,27 @@ main_install_stage3() { extract_stage3 } -main_chroot() { - gentoo_chroot "$@" -} - -main_install_gentoo() { +main_install_gentoo_in_chroot() { [[ $# == 0 ]] || die "Too many arguments" - #remove root password - passwd -d root + # Lock the root password, making the account unaccessible for the + # period of installation, except by chrooting + einfo "Locking root account" + passwd -l root + + einfo "Selecting portage mirrors" + # TODO mirrorselect + # TODO gpg portage sync + # TODO additional binary repos + # TODO safe dns settings (claranet) + + einfo "Mounting efi" + mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi" + + einfo "Syncing portage tree" + emerge-webrsync + + einfo "Selecting portage profile '$'" #get kernel @@ -71,24 +85,46 @@ main_install_gentoo() { su } -main_install_full() { +main_install() { [[ $# == 0 ]] || die "Too many arguments" - "$GENTOO_BOOTSTRAP_DIR/install_stage3" \ + install_stage3 \ || die "Failed to install stage3" - "$GENTOO_BOOTSTRAP_DIR/chroot" "$GENTOO_BOOTSTRAP_DIR/install_gentoo" \ - || die "Failed to prepare gentoo in chroot" + + gentoo_chroot "$GENTOO_BOOTSTRAP_DIR/scripts/main.sh" install_gentoo_in_chroot \ + || die "Failed to install gentoo in chroot" +} + +main_chroot() { + gentoo_chroot "$@" \ + || die "Failed to execute script in chroot" +} + +main_umount() { + gentoo_umount } ################################################ # Main dispatch +# Redirect output to logfiles +exec 3>&1 +trap 'exec 1>&3' 0 1 2 3 +exec 1>"$GENTOO_BOOTSTRAP_DIR/log-$LOGDATE.out" +ln -sf "$GENTOO_BOOTSTRAP_DIR/log-$LOGDATE.out" "$GENTOO_BOOTSTRAP_DIR/log.out" +einfo "Verbose script output is logged to: '$GENTOO_BOOTSTRAP_DIR/log-$LOGDATE.out'" + SCRIPT_ALIAS="$(basename "$0")" +if [[ "$SCRIPT_ALIAS" == "main.sh" ]]; then + SCRIPT_ALIAS="$1" + shift +fi + case "$SCRIPT_ALIAS" in - "chroot") main_chroot "$@" ;; - "install") main_install_full "$@" ;; - "install_gentoo") main_install_gentoo "$@" ;; - "install_stage3") main_install_stage3 "$@" ;; + "chroot") main_chroot "$@" ;; + "install") main_install "$@" ;; + "install_gentoo_in_chroot") main_install_gentoo_in_chroot "$@" ;; + "umount") main_umount "$@" ;; *) die "Invalid alias '$SCRIPT_ALIAS' was used to execute this script" ;; esac diff --git a/scripts/main_chroot.sh b/scripts/main_chroot.sh index 95e98d1..a4deb4c 100755 --- a/scripts/main_chroot.sh +++ b/scripts/main_chroot.sh @@ -3,9 +3,19 @@ [[ "${EXECUTED_IN_CHROOT}" != true ]] \ && { echo "This script must not be executed directly!" >&2; exit 1; } +# Source the systems profile source /etc/profile -export NPROC="$(($(nproc || echo 2) + 1))" -hostname 'gentoo' +# Export nproc variables +export NPROC="$(nproc || echo 2)" +export NPROC_ONE="$(($NPROC + 1))" +# Set default makeflags and emerge flags for parallel emerges +export MAKEFLAGS="-j$NPROC" +export EMERGE_DEFAULT_OPTS="--jobs=$NPROC_ONE --load-average=$NPROC" + +# Set the PS1 to a recognizable value +export PS1="(chroot) $PS1" + +# Execute the requested command exec "$@" diff --git a/scripts/utils.sh b/scripts/utils.sh index b317dc7..e9a4280 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -2,20 +2,32 @@ source "$GENTOO_BOOTSTRAP_DIR/scripts/protection.sh" || exit 1 +log_stdout() { + echo "$*" + if { >&3; } 2<> /dev/null; then + echo "$*" >&3 + fi +} + +log_stderr() { + echo "$*" >&2 + echo "$*" +} + elog() { - echo " * $*" + log_stdout " * $*" } einfo() { - echo " * $*" + log_stdout " * $*" } ewarn() { - echo " * $*" >&2 + log_stderr " * $*" } eerror() { - echo " * ERROR: $*" >&2 + log_stderr " * ERROR: $*" } die() { diff --git a/umount b/umount new file mode 120000 index 0000000..e8e1bb5 --- /dev/null +++ b/umount @@ -0,0 +1 @@ +scripts/main.sh \ No newline at end of file