chroot-manager/manager.sh
hax 8b04312ee6 manager.sh aktualisiert
Signed-off-by: hax <hax@lainlounge.xyz>
2025-05-02 22:56:22 +00:00

151 lines
4.4 KiB
Bash

#!/bin/bash
set -euo pipefail
# Path to the variables log file
VARIABLES_LOG="/root/variables.log"
# Auto-elevate script if not running as root
if [ "$EUID" -ne 0 ]; then
echo "🔒 Root privileges required. Re-running with sudo..."
exec sudo "$0" "$@"
fi
# Check if variables log file exists and ask if user wants to reload them
if [ -f "$VARIABLES_LOG" ]; then
read -p "Found saved variables. Do you want to reload them? (y/n): " reload_choice
if [[ "$reload_choice" == "y" || "$reload_choice" == "Y" ]]; then
echo "Reloading saved variables from $VARIABLES_LOG..."
# Reload the variables from the file
source "$VARIABLES_LOG"
# Check if any variable is missing after loading
if [ -z "${partition:-}" ] || [ -z "${efi:-}" ] || [ -z "${home:-}" ]; then
echo "❌ One or more required variables are missing from the log file. Please re-enter them."
exit 1
fi
echo "Successfully reloaded variables:"
echo " OS Partition: $partition"
echo " EFI Partition: $efi"
echo " Home Partition: $home"
else
echo "Proceeding with fresh input..."
fi
fi
# Show block devices to assist user if variables aren't loaded
lsblk
# Prompt for partitions if variables weren't reloaded
if [ -z "${partition:-}" ]; then
read -p "Enter the defective OS partition (e.g., sda2): " partition
fi
if [ -z "${efi:-}" ]; then
read -p "Enter the EFI partition (e.g., sda1): " efi
fi
if [ -z "${home:-}" ]; then
read -p "Enter the Home partition (e.g., sda3): " home
fi
# Save the variables to a file for later use (in key=value format)
echo "partition=\"$partition\"" > "$VARIABLES_LOG"
echo "efi=\"$efi\"" >> "$VARIABLES_LOG"
echo "home=\"$home\"" >> "$VARIABLES_LOG"
echo "Variables saved to $VARIABLES_LOG."
# Check and create /rescue directory if not exists
if [ -d /rescue ]; then
echo "/rescue exists already, continuing..."
else
echo "Creating /rescue directory..."
mkdir -p /rescue
fi
# Ensure necessary mount points are available before mounting
if [ -d /rescue/boot ]; then
echo "/rescue/boot exists already, continuing..."
else
echo "Creating /rescue/boot directory..."
mkdir -p /rescue/boot
fi
if [ -d /rescue/home ]; then
echo "/rescue exists already, continuing..."
else
echo "Creating /rescue/home directory..."
mkdir -p /rescue/home
fi
if [ -d /rescue ]; then
echo "/rescue exists already, continuing..."
else
echo "Creating /rescue directory..."
mkdir -p /rescue
fi
# ----------------------------------------------------- #
if [ -d /rescue/proc ]; then
echo "/rescue/proc exists already, continuing..."
else
echo "Creating /rescue/proc directory..."
mkdir -p /rescue/proc
fi
if [ -d /rescue/sys ]; then
echo "/rescue/sys exists already, continuing..."
else
echo "Creating /rescue directory..."
mkdir -p /rescue/sys
fi
if [ -d /rescue/dev/pts ]; then
echo "/rescue/dev/pts exists already, continuing..."
else
echo "Creating /rescue/dev/pts directory..."
mkdir -p /rescue
fi
if [ -d /rescue/run ]; then
echo "/rescue exists already, continuing..."
else
echo "Creating /rescue/run directory..."
mkdir -p /rescue/run
fi
# ----------------------------------------------------- #
if [ -d /rescue/boot ] && [ -d /rescue/home ] && [ -d /rescue/proc ] && [ -d /rescue/sys ] && [ -d /rescue/dev/pts ] && [ -d /rescue/run ]; then
echo "All required directories exist. Proceeding with mounting procedure..."
else
echo "One or more required directories are missing. Please create them before chrooting."
exit 1
fi
# Mount the partitions
echo "Mounting OS partition..."
mount "/dev/$partition" /rescue
echo "Mounting EFI partition..."
mount "/dev/$efi" /rescue/boot
echo "Mounting Home partition..."
mount "/dev/$home" /rescue/home
# Mount virtual filesystems for chroot
echo "Mounting /proc..."
mount -t proc proc /rescue/proc
echo "Mounting /sys..."
mount -t sysfs sys /rescue/sys
echo "Mounting /dev..."
mount -o bind /dev /rescue/dev
echo "Mounting /dev/pts..."
mount -t devpts pts /rescue/dev/pts
echo "Mounting /run..."
mount -o bind /run /rescue/run
# Ensure /etc exists and link resolv.conf for DNS inside chroot
mkdir -p /rescue/etc
ln -sf /etc/resolv.conf /rescue/etc/resolv.conf
# Final instructions
echo
echo "✅ The chroot environment is ready."
echo "➡️ To start chroot, run: chroot /rescue"
echo "❌ When you're done, unmount everything with: umount -R /rescue"