#!/bin/bash # change-root.sh - Gentoo Chroot Entry Script # Purpose: Set up bind mounts and enter Gentoo chroot environment # Usage: Run after init.sh # --- Colors for Output --- RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # --- Configuration --- GENTOO_ROOT="/mnt/gentoo" set -e echo -e "${YELLOW}Gentoo Chroot Setup Script${NC}" echo -e "${YELLOW}=========================${NC}" # --- Verify Gentoo root is mounted --- if ! mountpoint -q "$GENTOO_ROOT" 2>/dev/null; then echo -e "${RED}Error: $GENTOO_ROOT is not mounted.${NC}" echo -e "${RED}Please run init.sh first to set up the Gentoo installation.${NC}" exit 1 fi # --- Verify essential files exist --- if [ ! -f "$GENTOO_ROOT/etc/portage/make.conf" ]; then echo -e "${YELLOW}Warning: $GENTOO_ROOT/etc/portage/make.conf not found.${NC}" echo -e "${YELLOW}init.sh should have copied this automatically.${NC}" read -r -p "Continue anyway? (y/n): " continue_confirm if [[ "$continue_confirm" != "y" ]]; then echo -e "${RED}Aborting. Please check init.sh output.${NC}" exit 1 fi fi if [ ! -f "$GENTOO_ROOT/etc/resolv.conf" ]; then echo -e "${YELLOW}Copying resolv.conf...${NC}" if ! cp --dereference /etc/resolv.conf "$GENTOO_ROOT/etc/"; then echo -e "${RED}Error: Could not copy resolv.conf${NC}" exit 1 fi echo -e "${GREEN}Copied resolv.conf${NC}" fi # --- Set up bind mounts --- echo -e "${YELLOW}Setting up bind mounts for chroot environment...${NC}" # Mount /proc if ! mountpoint -q "$GENTOO_ROOT/proc" 2>/dev/null; then echo -e "${YELLOW}Mounting /proc...${NC}" mount --rbind /proc "$GENTOO_ROOT/proc" mount --make-rslave "$GENTOO_ROOT/proc" echo -e "${GREEN}Mounted /proc${NC}" else echo -e "${YELLOW}/proc already mounted${NC}" fi # Mount /sys if ! mountpoint -q "$GENTOO_ROOT/sys" 2>/dev/null; then echo -e "${YELLOW}Mounting /sys...${NC}" mount --rbind /sys "$GENTOO_ROOT/sys" mount --make-rslave "$GENTOO_ROOT/sys" echo -e "${GREEN}Mounted /sys${NC}" else echo -e "${YELLOW}/sys already mounted${NC}" fi # Mount /dev if ! mountpoint -q "$GENTOO_ROOT/dev" 2>/dev/null; then echo -e "${YELLOW}Mounting /dev...${NC}" mount --rbind /dev "$GENTOO_ROOT/dev" mount --make-rslave "$GENTOO_ROOT/dev" echo -e "${GREEN}Mounted /dev${NC}" else echo -e "${YELLOW}/dev already mounted${NC}" fi # Mount /run if ! mountpoint -q "$GENTOO_ROOT/run" 2>/dev/null; then echo -e "${YELLOW}Mounting /run...${NC}" mount --rbind /run "$GENTOO_ROOT/run" mount --make-rslave "$GENTOO_ROOT/run" echo -e "${GREEN}Mounted /run${NC}" else echo -e "${YELLOW}/run already mounted${NC}" fi # --- Show mount status --- echo -e "${GREEN}Bind mounts ready:${NC}" echo " /proc: $(mountpoint -q "$GENTOO_ROOT/proc" && echo "mounted" || echo "not mounted")" echo " /sys: $(mountpoint -q "$GENTOO_ROOT/sys" && echo "mounted" || echo "not mounted")" echo " /dev: $(mountpoint -q "$GENTOO_ROOT/dev" && echo "mounted" || echo "not mounted")" echo " /run: $(mountpoint -q "$GENTOO_ROOT/run" && echo "mounted" || echo "not mounted")" # --- Enter chroot --- echo "" echo -e "${GREEN}Entering Gentoo chroot environment...${NC}" echo -e "${YELLOW}Type 'exit' when done to return to the live environment.${NC}" echo "" exec chroot "$GENTOO_ROOT" /bin/bash -c "source /etc/profile && export PS1='(chroot) \$PS1' && exec /bin/bash"