#!/bin/bash # Emergency Package Installer for LVM Migration # Handles different Debian/Ubuntu distributions and package availability set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log() { echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1" >&2; } success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; } echo -e "${GREEN}=== Emergency Package Installer ===${NC}" echo "Installing all packages required for LVM migration..." # Check if running as root if [ "$EUID" -ne 0 ]; then error "This script must be run as root. Use: sudo $0" exit 1 fi # Detect distribution if [ -f /etc/os-release ]; then . /etc/os-release DISTRO="$ID" VERSION="$VERSION_ID" log "Detected distribution: $PRETTY_NAME" else DISTRO="unknown" warning "Could not detect distribution" fi # Update package lists log "Updating package lists..." apt update || warning "Failed to update package lists" # Function to try installing a package with alternatives try_install_package() { local primary="$1" shift local alternatives=("$@") log "Installing $primary..." if apt install -y "$primary" >/dev/null 2>&1; then success "Installed $primary" return 0 fi # Try alternatives for alt in "${alternatives[@]}"; do log "Trying alternative: $alt" if apt install -y "$alt" >/dev/null 2>&1; then success "Installed $alt (alternative for $primary)" return 0 fi done warning "Failed to install $primary or any alternatives" return 1 } # Install packages with error handling and alternatives log "Installing core utilities..." try_install_package "util-linux" try_install_package "coreutils" try_install_package "bc" try_install_package "bsdmainutils" "bsdutils" log "Installing LVM and device mapper tools..." try_install_package "lvm2" try_install_package "dmsetup" "device-mapper" log "Installing encryption tools..." try_install_package "cryptsetup" "cryptsetup-bin" log "Installing filesystem tools..." try_install_package "e2fsprogs" try_install_package "dosfstools" "mtools" try_install_package "parted" log "Installing backup and monitoring tools..." try_install_package "rsync" try_install_package "pv" "pipe-viewer" log "Installing GRUB bootloader components..." # Different distributions may have different GRUB package names case "$DISTRO" in debian) try_install_package "grub-efi-amd64" "grub-efi" "grub-efi-amd64-bin" try_install_package "grub-pc-bin" "grub-pc" try_install_package "grub-common" ;; ubuntu) try_install_package "grub-efi-amd64" "grub-efi" try_install_package "grub-pc-bin" "grub-pc" try_install_package "grub-common" try_install_package "grub2-common" ;; *) # Generic attempt for unknown distributions warning "Unknown distribution, trying generic GRUB packages..." try_install_package "grub-efi-amd64" "grub-efi" "grub" try_install_package "grub-pc-bin" "grub-pc" "grub" try_install_package "grub-common" "grub2-common" ;; esac log "Installing kernel and initramfs tools..." try_install_package "initramfs-tools" "dracut" # Don't install kernel on live system as it's not needed and may cause issues # try_install_package "linux-image-generic" "linux-image-amd64" log "Installing additional required tools..." try_install_package "udev" "systemd-udev" try_install_package "kmod" "module-init-tools" # Load kernel modules log "Loading required kernel modules..." modprobe dm_mod 2>/dev/null || warning "Failed to load dm_mod" modprobe dm_crypt 2>/dev/null || warning "Failed to load dm_crypt" modprobe dm_snapshot 2>/dev/null || warning "Failed to load dm_snapshot" # Check if LVM service is available and start it if systemctl list-unit-files | grep -q lvm2; then log "Starting LVM services..." systemctl start lvm2-monitor 2>/dev/null || warning "Could not start lvm2-monitor" systemctl start lvm2-lvmpolld 2>/dev/null || warning "Could not start lvm2-lvmpolld" fi # Verify critical tools are available log "Verifying tool installation..." missing_critical=() # Check for tool availability with alternatives check_tool() { local primary="$1" shift local alternatives=("$@") if command -v "$primary" >/dev/null 2>&1; then return 0 fi for alt in "${alternatives[@]}"; do if command -v "$alt" >/dev/null 2>&1; then return 0 fi done missing_critical+=("$primary") return 1 } check_tool "lvm" "lvm2" check_tool "vgdisplay" "lvm" check_tool "pvcreate" "lvm" check_tool "lvcreate" "lvm" check_tool "cryptsetup" check_tool "rsync" check_tool "parted" check_tool "pv" check_tool "mkfs.ext4" "mke2fs" check_tool "mkfs.fat" "mkfs.vfat" check_tool "grub-install" "grub2-install" check_tool "update-grub" "grub-mkconfig" check_tool "update-initramfs" "dracut" if [ ${#missing_critical[@]} -eq 0 ]; then success "All critical tools are now available!" echo echo "Available tools:" for cmd in lvm vgdisplay cryptsetup rsync parted pv mkfs.ext4 grub-install; do if command -v "$cmd" >/dev/null 2>&1; then echo " ✓ $cmd: $(which $cmd)" fi done echo echo "You can now run: ./migrate_to_lvm.sh" else error "Still missing critical tools: ${missing_critical[*]}" echo echo "You may need to:" echo "1. Check internet connection for package downloads" echo "2. Try different package repositories" echo "3. Install packages manually with different names" echo "4. Use a different live system distribution" exit 1 fi echo echo -e "${GREEN}Installation completed successfully!${NC}" echo "The system is now ready for LVM migration." echo echo "Next steps:" echo "1. Run: ./migrate_to_lvm.sh" echo "2. Follow the interactive prompts" echo "3. Validate with: ./validate_lvm_migration.sh"