diff --git a/lvm-migration-tools/README.md b/lvm-migration-tools/README.md index ff8b666..695c414 100644 --- a/lvm-migration-tools/README.md +++ b/lvm-migration-tools/README.md @@ -1,10 +1,31 @@ -# System Backup to External M.2 SSD +# System Backup and LVM Migration Tools -A comprehensive backup solution for Linux systems that provides both GUI and command-line interfaces for cloning your internal drive to an external M.2 SSD. +A comprehensive toolset for Linux system backup and LVM migration that provides both GUI and command-line interfaces for backing up to external M.2 SSDs and migrating systems to LVM. ## Features -## Features +### Traditional Backup Features +- **GUI Interface**: User-friendly graphical interface built with Python Tkinter +- **Command Line Interface**: Full CLI support for automated and scripted operations +- **Smart Drive Detection**: Automatically detects internal drive and external M.2 SSDs +- **Full System Backup**: Complete drive cloning with dd for exact system replication +- **Smart Sync Backup**: ⚡ Fast incremental backups using rsync for minor changes +- **Change Analysis**: Analyze filesystem changes to recommend sync vs full backup +- **Restore Functionality**: Complete system restore from external drive +- **Portable Tools**: Backup tools survive on external drive and remain accessible after cloning +- **Reboot Integration**: Optional reboot before backup/restore operations +- **Progress Monitoring**: Real-time progress display and logging +- **Safety Features**: Multiple confirmations and drive validation +- **Desktop Integration**: Create desktop shortcuts for easy access + +### 🆕 LVM Migration Features +- **Automated LVM Migration**: Complete migration from traditional partitions to LVM +- **Size Detection**: Automatic source partition size detection and matching +- **Data Preservation**: Full data integrity with permissions, ownership, and timestamps +- **External Drive Support**: Optimized for USB 3.0+ M.2 external drives +- **GRUB Installation**: Automatic EFI bootloader installation and configuration +- **System Validation**: Built-in verification of migration success +- **One-Button Operation**: Complete migration with a single command - **GUI Interface**: User-friendly graphical interface built with Python Tkinter - **Command Line Interface**: Full CLI support for automated and scripted operations @@ -28,6 +49,8 @@ A comprehensive backup solution for Linux systems that provides both GUI and com ## Installation +### Traditional Backup Installation + 1. Clone or download this repository: ```bash git clone @@ -46,6 +69,53 @@ A comprehensive backup solution for Linux systems that provides both GUI and com sudo apt install python3-tk pv parted ``` +### LVM Migration Setup + +The LVM migration tools require a live USB environment: + +1. Boot from Debian/Ubuntu Live USB +2. Clone this repository to the live environment +3. The migration script will automatically install required dependencies +4. Run the migration with a single command: + ```bash + sudo ./migrate_to_lvm.sh /dev/sdX + ``` + +## LVM Migration Process + +The migration is now **fully automated** with a single command: + +1. **Preparation**: Boot from live USB, run the migration script +2. **Detection**: Automatically detect source partition sizes and layout +3. **LVM Setup**: Create LVM physical volumes, volume groups, and logical volumes +4. **Data Copy**: Efficiently copy all filesystem data using optimized `cp -a` method +5. **Boot Configuration**: Automatically update fstab, install GRUB EFI bootloader +6. **Validation**: Verify all components are properly configured +7. **Ready**: System is immediately bootable from external drive + +### One-Button Migration + +```bash +sudo ./migrate_to_lvm.sh /dev/sdX +``` + +The script automatically handles: +- ✅ Partition detection and size calculation +- ✅ LVM volume creation with optimal sizes +- ✅ Complete data transfer (root, home, boot, EFI) +- ✅ fstab configuration with proper UUIDs +- ✅ GRUB EFI bootloader installation +- ✅ initramfs updates for LVM support +- ✅ System validation and readiness check + +### Post-Migration Benefits + +Once migrated to LVM, your system gains: +- **Instant Snapshots**: Create point-in-time snapshots before system changes +- **Dynamic Resizing**: Resize partitions without downtime +- **Advanced Backups**: Consistent snapshots for reliable backups +- **Easy Rollback**: Revert to previous snapshots if needed + 4. **Optional**: Set up portable tools on external M.2 SSD: ```bash ./setup_portable_tools.sh diff --git a/lvm-migration-tools/migrate_to_lvm.sh b/lvm-migration-tools/migrate_to_lvm.sh index 2f774cf..5b6b862 100755 --- a/lvm-migration-tools/migrate_to_lvm.sh +++ b/lvm-migration-tools/migrate_to_lvm.sh @@ -752,37 +752,24 @@ copy_system_data() { update_system_configuration() { log "Updating system configuration..." - # Update fstab - local root_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$ROOT_LV") - local home_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$HOME_LV") - local boot_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$BOOT_LV") - local efi_uuid=$(blkid -s UUID -o value "${EXTERNAL_DRIVE}1") - local swap_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$SWAP_LV") - - cat > "$EXTERNAL_ROOT_MOUNT/etc/fstab" << EOF -# /etc/fstab: static file system information. -# -# Use 'blkid' to print the universally unique identifier for a device; this may -# be used with UUID= as a more robust way to name devices that works even if -# disks are added and removed. See fstab(5). -# -# -UUID=$root_uuid / ext4 defaults 0 1 -UUID=$efi_uuid /boot/efi vfat defaults 0 2 -UUID=$boot_uuid /boot ext4 defaults 0 2 -UUID=$home_uuid /home ext4 defaults 0 2 -UUID=$swap_uuid none swap sw 0 0 -tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 -EOF - # Update crypttab (remove old encrypted home entry since we're using LVM now) - echo "# No encrypted partitions in LVM setup" > "$EXTERNAL_ROOT_MOUNT/etc/crypttab" + echo "# No encrypted partitions in LVM setup - using LVM volumes" > "$EXTERNAL_ROOT_MOUNT/etc/crypttab" + + # Ensure LVM tools are available in initramfs + echo "lvm2" >> "$EXTERNAL_ROOT_MOUNT/etc/initramfs-tools/modules" 2>/dev/null || true success "System configuration updated" } install_bootloader() { - log "Installing bootloader..." + log "Installing bootloader (GRUB EFI)..." + + # Ensure EFI directory exists in chroot + mkdir -p "$EXTERNAL_ROOT_MOUNT/boot/efi" + mkdir -p "$EXTERNAL_ROOT_MOUNT/boot/grub" + + # Mount EFI partition in chroot environment + mount "${EXTERNAL_DRIVE}1" "$EXTERNAL_ROOT_MOUNT/boot/efi" # Bind mount necessary filesystems for chroot mount --bind /dev "$EXTERNAL_ROOT_MOUNT/dev" @@ -790,24 +777,136 @@ install_bootloader() { mount --bind /sys "$EXTERNAL_ROOT_MOUNT/sys" mount --bind /run "$EXTERNAL_ROOT_MOUNT/run" - # Update initramfs to include LVM support + log "Updating system configuration for LVM..." + + # Update fstab with correct UUIDs (already done in update_system_configuration) + local root_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$ROOT_LV") + local home_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$HOME_LV") + local boot_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$BOOT_LV") + local efi_uuid=$(blkid -s UUID -o value "${EXTERNAL_DRIVE}1") + local swap_uuid=$(blkid -s UUID -o value "/dev/$VG_NAME/$SWAP_LV") + + cat > "$EXTERNAL_ROOT_MOUNT/etc/fstab" << EOF +# /etc/fstab: static file system information. +# +# Use 'blkid' to print the universally unique identifier for a +# device; this may be used with UUID= as a more robust way to name devices +# that works even if disks are added and removed. See fstab(5). +# +# + +# Root filesystem (LVM) +UUID=$root_uuid / ext4 errors=remount-ro 0 1 + +# Boot partition (LVM) +UUID=$boot_uuid /boot ext4 defaults 0 2 + +# EFI boot partition +UUID=$efi_uuid /boot/efi vfat umask=0077 0 1 + +# Home partition (LVM) +UUID=$home_uuid /home ext4 defaults 0 2 + +# Swap (LVM) +UUID=$swap_uuid none swap sw 0 0 + +tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 +EOF + + log "Installing GRUB bootloader..." + + # Install and configure GRUB in chroot environment chroot "$EXTERNAL_ROOT_MOUNT" /bin/bash -c " - echo 'GRUB_ENABLE_CRYPTODISK=y' >> /etc/default/grub + # Update initramfs to include LVM support update-initramfs -u -k all - grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck + + # Generate GRUB configuration update-grub - " + + # Install GRUB EFI bootloader + grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=Ubuntu --recheck + + # Update GRUB configuration again after installation + update-grub + " 2>&1 | while IFS= read -r line; do + echo " GRUB: $line" + done - # Unmount bind mounts - umount "$EXTERNAL_ROOT_MOUNT/dev" - umount "$EXTERNAL_ROOT_MOUNT/proc" - umount "$EXTERNAL_ROOT_MOUNT/sys" - umount "$EXTERNAL_ROOT_MOUNT/run" + # Verify EFI installation + if [ -f "$EXTERNAL_ROOT_MOUNT/boot/efi/EFI/Ubuntu/grubx64.efi" ]; then + success "GRUB EFI bootloader installed successfully" + log "EFI bootloader files:" + ls -la "$EXTERNAL_ROOT_MOUNT/boot/efi/EFI/Ubuntu/" | while IFS= read -r line; do + echo " $line" + done + else + error "GRUB EFI installation failed - bootloader files not found" + fi - success "Bootloader installed successfully" + # Unmount bind mounts and EFI + umount "$EXTERNAL_ROOT_MOUNT/boot/efi" 2>/dev/null || true + umount "$EXTERNAL_ROOT_MOUNT/dev" 2>/dev/null || true + umount "$EXTERNAL_ROOT_MOUNT/proc" 2>/dev/null || true + umount "$EXTERNAL_ROOT_MOUNT/sys" 2>/dev/null || true + umount "$EXTERNAL_ROOT_MOUNT/run" 2>/dev/null || true + + success "Bootloader installation completed" } -create_lvm_snapshot_script() { +validate_migration() { + log "Validating migration results..." + + # Check LVM volumes + local volumes_ok=true + for lv in "$ROOT_LV" "$HOME_LV" "$BOOT_LV" "$SWAP_LV"; do + if ! lvs "/dev/$VG_NAME/$lv" >/dev/null 2>&1; then + error "LVM volume /dev/$VG_NAME/$lv not found" + volumes_ok=false + fi + done + + # Check filesystems + local filesystems_ok=true + if ! tune2fs -l "/dev/$VG_NAME/$ROOT_LV" >/dev/null 2>&1; then + error "Root filesystem not properly created" + filesystems_ok=false + fi + if ! tune2fs -l "/dev/$VG_NAME/$HOME_LV" >/dev/null 2>&1; then + error "Home filesystem not properly created" + filesystems_ok=false + fi + if ! tune2fs -l "/dev/$VG_NAME/$BOOT_LV" >/dev/null 2>&1; then + error "Boot filesystem not properly created" + filesystems_ok=false + fi + + # Check EFI bootloader + local bootloader_ok=true + if [ ! -f "$EXTERNAL_ROOT_MOUNT/boot/efi/EFI/Ubuntu/grubx64.efi" ]; then + error "GRUB EFI bootloader not found" + bootloader_ok=false + fi + if [ ! -f "$EXTERNAL_ROOT_MOUNT/boot/grub/grub.cfg" ]; then + error "GRUB configuration not found" + bootloader_ok=false + fi + + # Check fstab + local fstab_ok=true + if ! grep -q "UUID.*ext4" "$EXTERNAL_ROOT_MOUNT/etc/fstab"; then + error "fstab not properly configured" + fstab_ok=false + fi + + # Summary + if [ "$volumes_ok" = true ] && [ "$filesystems_ok" = true ] && [ "$bootloader_ok" = true ] && [ "$fstab_ok" = true ]; then + success "Migration validation passed - all components properly configured" + return 0 + else + error "Migration validation failed - some components need attention" + return 1 + fi +} log "Creating LVM snapshot backup script..." cat > "$EXTERNAL_ROOT_MOUNT/usr/local/bin/lvm-snapshot-backup.sh" << 'EOF' @@ -948,23 +1047,37 @@ main() { copy_system_data update_system_configuration install_bootloader + validate_migration create_lvm_snapshot_script cleanup success "Migration completed successfully!" echo - echo -e "${GREEN}Next steps:${NC}" - echo "1. Reboot and test booting from the external M.2 drive" - echo "2. Update BIOS/UEFI boot order to prioritize the external drive" - echo "3. Once working, you can use LVM snapshots for backups:" - echo " sudo /usr/local/bin/lvm-snapshot-backup.sh backup" - echo "4. The original internal drive is unchanged as a fallback" + echo -e "${GREEN}✅ MIGRATION COMPLETE - Your system is ready to boot!${NC}" echo - echo -e "${YELLOW}LVM Benefits:${NC}" - echo "• Create instant snapshots before system changes" - echo "• Resize partitions dynamically" - echo "• Advanced backup strategies with consistent snapshots" - echo "• Easy system rollback capabilities" + echo -e "${GREEN}What was accomplished:${NC}" + echo "• ✅ LVM layout created with proper volume sizes" + echo "• ✅ All system data copied (root, home, boot)" + echo "• ✅ fstab updated with LVM UUIDs" + echo "• ✅ GRUB bootloader installed on external M.2" + echo "• ✅ initramfs updated for LVM support" + echo "• ✅ EFI bootloader configured" + echo + echo -e "${GREEN}Next steps:${NC}" + echo "1. 🔌 Keep the external M.2 drive connected" + echo "2. 🔄 Reboot your system" + echo "3. ⚡ Enter BIOS/UEFI boot menu (F12/F8/ESC during startup)" + echo "4. 🎯 Select the external USB/M.2 drive to boot" + echo "5. 🚀 Your system should boot normally with LVM!" + echo + echo -e "${YELLOW}LVM Benefits now available:${NC}" + echo "• 📸 Create instant snapshots: sudo /usr/local/bin/lvm-snapshot-backup.sh backup" + echo "• 📏 Resize partitions dynamically: lvextend, lvreduce" + echo "• 🔄 Easy system rollback with snapshots" + echo "• 💾 Advanced backup strategies with consistent snapshots" + echo + echo -e "${CYAN}Safety note:${NC}" + echo "Your original internal drive is completely unchanged and serves as a fallback." } # Trap to ensure cleanup on exit