Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Exit on error
- set -e
- # Function to clean up mounts on exit
- cleanup() {
- echo "Cleaning up mounts..."
- # Define mount points in reverse order of mounting
- MOUNT_POINTS=(
- "/mnt/chroot/dev/pts"
- "/mnt/chroot/dev"
- "/mnt/chroot/sys"
- "/mnt/chroot/proc"
- "/mnt/chroot/var/log/audit"
- "/mnt/chroot/var/log"
- "/mnt/chroot/var/tmp"
- "/mnt/chroot/var"
- "/mnt/chroot/home"
- "/mnt/chroot/boot"
- "/mnt/chroot/boot/efi"
- "/mnt/chroot"
- )
- # Unmount each point if it's mounted
- for mount_point in "${MOUNT_POINTS[@]}"; do
- if mountpoint -q "$mount_point"; then
- echo "Unmounting $mount_point"
- umount -f "$mount_point" || echo "Warning: Failed to unmount $mount_point"
- fi
- done
- }
- # Set cleanup to run on script exit
- trap cleanup EXIT
- # Check if running as root
- if [ "$(id -u)" -ne 0 ]; then
- echo "Error: This script must be run as root"
- exit 1
- fi
- # Check for required tools and install if missing
- echo "Checking and installing required tools..."
- if ! rpm -q nvme-cli &>/dev/null; then
- dnf install -y nvme-cli
- fi
- for tool in lsblk parted mkfs.xfs mkfs.fat lvcreate pvcreate vgcreate; do
- if ! command -v $tool >/dev/null 2>&1; then
- echo "Error: Required tool $tool is not installed"
- exit 1
- fi
- done
- # Function to detect NVMe device
- detect_nvme_device() {
- echo "DEBUG: Starting device detection..."
- # Get the root device with more detailed debug
- root_mount=$(mount | grep ' / ')
- echo "DEBUG: Root mount line: $root_mount"
- root_device=$(echo "$root_mount" | cut -d' ' -f1)
- echo "DEBUG: Root device is: $root_device"
- # For AWS NVMe, we need to get the base device without any partition number
- root_disk=$(echo "$root_device" | sed -E 's/p?[0-9]+$//')
- echo "DEBUG: Root disk is: $root_disk"
- # List all block devices
- echo "DEBUG: All block devices:"
- lsblk -p
- # Find all NVMe devices
- mapfile -t nvme_devices < <(lsblk -d -n -o NAME,TYPE | grep " disk" | grep -E '^nvme[0-9]+n1' | cut -d' ' -f1)
- echo "DEBUG: Found NVMe devices: ${nvme_devices[*]}"
- # Look for the non-root device
- for device in "${nvme_devices[@]}"; do
- device_path="/dev/$device"
- echo "DEBUG: Checking device: $device_path against root: $root_disk"
- if [ "$device_path" = "$root_disk" ]; then
- echo "DEBUG: Skipping root disk: $device_path"
- continue
- fi
- # Additional check to ensure device exists and is a block device
- if [ -b "$device_path" ]; then
- # Verify this device isn't mounted
- if ! lsblk "$device_path" | grep -q "part /" ; then
- TARGET_DEVICE="$device_path"
- echo "Selected new root device: ${TARGET_DEVICE}"
- return 0
- else
- echo "DEBUG: Device $device_path has mounted partitions, skipping"
- fi
- else
- echo "DEBUG: Device $device_path is not a block device or doesn't exist"
- fi
- done
- echo "Error: No suitable device found for new root! Need an unpartitioned NVMe device."
- echo "DEBUG: Current block devices:"
- lsblk -p
- exit 1
- }
- # Detect the device to use
- detect_nvme_device
- # Create partitions on the new device
- echo "Creating partitions on ${TARGET_DEVICE}..."
- parted -s "${TARGET_DEVICE}" mklabel gpt
- # Create aligned partitions
- parted -s "${TARGET_DEVICE}" unit s \
- mkpart primary 2048 4095 \
- mkpart ESP fat32 4096 198655 \
- mkpart primary xfs 198656 2246655 \
- mkpart primary 2246656 100%
- # Set partition flags
- parted -s "${TARGET_DEVICE}" set 1 bios_grub on
- parted -s "${TARGET_DEVICE}" set 2 esp on
- # Wait for device nodes
- sleep 5
- # Format partitions
- mkfs.fat -F32 "${TARGET_DEVICE}p2"
- mkfs.xfs -f "${TARGET_DEVICE}p3"
- # Setup LVM
- pvcreate -ff -y "${TARGET_DEVICE}p4"
- vgcreate RootVG "${TARGET_DEVICE}p4"
- # Create logical volumes
- lvcreate -L 6G -n rootVol RootVG
- lvcreate -L 2G -n swapVol RootVG
- lvcreate -L 1G -n homeVol RootVG
- lvcreate -L 2G -n varVol RootVG
- lvcreate -L 2G -n varTmpVol RootVG
- lvcreate -L 2G -n logVol RootVG
- lvcreate -L 4G -n auditVol RootVG
- # Format logical volumes
- mkfs.xfs -f /dev/RootVG/rootVol
- mkswap -f /dev/RootVG/swapVol
- mkfs.xfs -f /dev/RootVG/homeVol
- mkfs.xfs -f /dev/RootVG/varVol
- mkfs.xfs -f /dev/RootVG/varTmpVol
- mkfs.xfs -f /dev/RootVG/logVol
- mkfs.xfs -f /dev/RootVG/auditVol
- # Create mount points and mount root filesystem
- echo "Creating mount points..."
- mkdir -p /mnt/chroot
- echo "Mounting root filesystem..."
- mount /dev/RootVG/rootVol /mnt/chroot
- # Create initial directories
- echo "Creating initial mount points..."
- mkdir -p /mnt/chroot/boot/efi
- mkdir -p /mnt/chroot/home
- # Mount boot related filesystems
- echo "Mounting boot filesystems..."
- mount "${TARGET_DEVICE}p2" /mnt/chroot/boot/efi
- mount "${TARGET_DEVICE}p3" /mnt/chroot/boot
- # Sync root filesystem content
- echo "Syncing root filesystem..."
- rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/chroot/
- # Mount and sync home
- mount /dev/RootVG/homeVol /mnt/chroot/home
- rsync -aAXv /home/ /mnt/chroot/home/
- # Create and mount var hierarchy
- echo "Setting up /var hierarchy..."
- mkdir -p /mnt/chroot/var
- mount /dev/RootVG/varVol /mnt/chroot/var
- rsync -aAXv /var/ /mnt/chroot/var/
- # Create and mount /var/tmp
- mkdir -p /mnt/chroot/var/tmp
- mount /dev/RootVG/varTmpVol /mnt/chroot/var/tmp
- rsync -aAXv /var/tmp/ /mnt/chroot/var/tmp/
- # Create and mount /var/log
- mkdir -p /mnt/chroot/var/log
- mount /dev/RootVG/logVol /mnt/chroot/var/log
- rsync -aAXv /var/log/ /mnt/chroot/var/log/
- # Create and mount /var/log/audit
- mkdir -p /mnt/chroot/var/log/audit
- mount /dev/RootVG/auditVol /mnt/chroot/var/log/audit
- rsync -aAXv /var/log/audit/ /mnt/chroot/var/log/audit/
- # Setup virtual filesystems
- echo "Setting up virtual filesystems..."
- mkdir -p /mnt/chroot/{proc,sys,dev/pts}
- mount -t proc proc /mnt/chroot/proc
- mount -t sysfs sys /mnt/chroot/sys
- mount -o bind /dev /mnt/chroot/dev
- mount -o bind /dev/pts /mnt/chroot/dev/pts
- # Ensure etc directory exists and copy resolv.conf
- mkdir -p /mnt/chroot/etc
- cp /etc/resolv.conf /mnt/chroot/etc/
- # Create new fstab
- cat > /mnt/chroot/etc/fstab << EOF
- # Device Mountpoint FStype Options Dump Pass
- ${TARGET_DEVICE}p2 /boot/efi vfat umask=0077,shortname=winnt 0 2
- ${TARGET_DEVICE}p3 /boot xfs defaults 0 0
- /dev/mapper/RootVG-rootVol / xfs defaults 0 0
- /dev/mapper/RootVG-swapVol swap swap defaults 0 0
- /dev/mapper/RootVG-homeVol /home xfs defaults,nosuid,noexec,nodev 0 0
- /dev/mapper/RootVG-varVol /var xfs defaults,nodev 0 0
- /dev/mapper/RootVG-varTmpVol /var/tmp xfs defaults,nodev,nosuid,noexec 0 0
- /dev/mapper/RootVG-logVol /var/log xfs defaults,nodev,nosuid,noexec 0 0
- /dev/mapper/RootVG-auditVol /var/log/audit xfs defaults,nodev,nosuid,noexec 0 0
- tmpfs /dev/shm tmpfs defaults,nodev,nosuid,noexec 0 0
- EOF
- echo "Chroot environment setup complete at /mnt/chroot"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement