100 lines
3 KiB
Bash
100 lines
3 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
# Path to the variables log file
|
|
VARIABLES_LOG="/rescue/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
|
|
mkdir -p /rescue/boot
|
|
mkdir -p /rescue/home
|
|
mkdir -p /rescue/proc
|
|
mkdir -p /rescue/sys
|
|
mkdir -p /rescue/dev/pts
|
|
mkdir -p /rescue/run
|
|
|
|
# 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"
|