#!/bin/bash # LVM Migration Troubleshooting Script # Helps diagnose issues with the migration process 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}=== LVM Migration Troubleshooting ===${NC}" echo # Check basic system requirements check_system() { log "Checking system requirements..." # Check if running as root if [ "$EUID" -ne 0 ]; then error "This script must be run as root. Use: sudo $0" return 1 fi # Check if running from live system local root_device=$(df / | tail -1 | awk '{print $1}') if [[ "$root_device" == *"loop"* ]] || [[ "$root_device" == *"overlay"* ]] || [[ "$root_device" == *"tmpfs"* ]]; then success "Running from live system" else warning "Not running from live system - migration may fail" fi # Check available tools local tools=("lvm" "cryptsetup" "rsync" "parted" "pv" "grub-install" "mkfs.ext4" "mkfs.fat" "bc" "wipefs") local missing=() for tool in "${tools[@]}"; do if command -v "$tool" >/dev/null 2>&1; then success "Tool available: $tool" else missing+=("$tool") fi done if [ ${#missing[@]} -gt 0 ]; then error "Missing tools: ${missing[*]}" echo "Run: sudo ./emergency_install.sh to install missing packages" return 1 fi } # Check drives check_drives() { log "Checking available drives..." echo "All block devices:" lsblk -dpno NAME,SIZE,MODEL,VENDOR echo # Look for likely candidates local internal_drives=($(lsblk -dpno NAME | grep -E "nvme|sda")) local usb_drives=() # Check for USB drives for drive in $(lsblk -dpno NAME); do if udevadm info --query=property --name="$drive" | grep -q "ID_BUS=usb"; then usb_drives+=("$drive") fi done echo "Likely internal drives:" for drive in "${internal_drives[@]}"; do local info=$(lsblk -dpno NAME,SIZE,MODEL "$drive") echo " $info" done echo "USB drives (external/migration stick):" for drive in "${usb_drives[@]}"; do local info=$(lsblk -dpno NAME,SIZE,MODEL "$drive") echo " $info" done } # Check LVM status check_lvm() { log "Checking LVM status..." echo "Physical volumes:" pvs 2>/dev/null || echo "No physical volumes found" echo "Volume groups:" vgs 2>/dev/null || echo "No volume groups found" echo "Logical volumes:" lvs 2>/dev/null || echo "No logical volumes found" # Check for existing system-vg if vgs system-vg 2>/dev/null; then warning "system-vg already exists - migration may have partially completed" echo "To restart migration, you may need to:" echo " vgremove -f system-vg" echo " pvremove /dev/sdaX" fi } # Check filesystem space and usage check_filesystems() { log "Checking current filesystem usage..." echo "Current mounts and usage:" df -h | grep -E "/$|/home$|/boot$" echo echo "System memory:" free -h } # Test LVM commands test_lvm_commands() { log "Testing LVM command availability..." # Test basic LVM commands lvm version || error "LVM not working" # Test if we can create a test PV (on a loop device) log "Testing LVM functionality with loop device..." # Create a small test file dd if=/dev/zero of=/tmp/lvm-test.img bs=1M count=100 2>/dev/null local loop_device=$(losetup -f --show /tmp/lvm-test.img) if pvcreate "$loop_device" 2>/dev/null; then success "LVM pvcreate works" if vgcreate test-vg "$loop_device" 2>/dev/null; then success "LVM vgcreate works" if lvcreate -L 50M -n test-lv test-vg 2>/dev/null; then success "LVM lvcreate works" success "All LVM commands working correctly" else error "LVM lvcreate failed" fi # Cleanup lvremove -f test-vg/test-lv 2>/dev/null || true vgremove -f test-vg 2>/dev/null || true else error "LVM vgcreate failed" fi pvremove -f "$loop_device" 2>/dev/null || true else error "LVM pvcreate failed" fi # Cleanup losetup -d "$loop_device" 2>/dev/null || true rm -f /tmp/lvm-test.img } # Generate report generate_report() { echo echo -e "${BLUE}=== Troubleshooting Report ===${NC}" echo "Generated: $(date)" echo "System: $(hostname)" if check_system && check_drives && check_lvm && check_filesystems && test_lvm_commands; then echo success "All checks passed - system should be ready for migration" echo echo "To run migration:" echo "1. sudo ./migrate_to_lvm.sh" echo "2. Follow the interactive prompts" echo "3. Carefully select source and target drives" else echo error "Some checks failed - see messages above" echo echo "Common solutions:" echo "• Run: sudo ./emergency_install.sh (for missing packages)" echo "• Reboot from live USB (if not in live system)" echo "• Check drive connections (if drives not detected)" echo "• Remove existing LVM setup (if system-vg exists)" fi } # Main execution if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then generate_report fi