limine.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. # NAME: Script to transition from systemd-boot to Limine bootloader
  3. # REQUIRES: sudo
  4. set -euo pipefail
  5. echo "do you confirm this transition?"
  6. read -r -p "Are you sure? [y/N] " response
  7. if [[ "$response" != "y" ]]; then
  8. exit 0
  9. fi
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. BLUE='\033[0;34m'
  14. NC='\033[0m'
  15. print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
  16. print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
  17. print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
  18. print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
  19. if [[ $EUID -ne 0 ]]; then
  20. print_error "This script must be run as root (use sudo)"
  21. exit 1
  22. fi
  23. if ! command -v pacman &>/dev/null; then
  24. print_error "This script is designed for Arch Linux / CachyOS systems"
  25. exit 1
  26. fi
  27. print_status "Starting transition from systemd-boot to Limine..."
  28. ESP_PATH=""
  29. if command -v bootctl &>/dev/null; then
  30. ESP_PATH=$(bootctl --print-esp-path 2>/dev/null || echo "")
  31. fi
  32. if [[ -z "$ESP_PATH" ]]; then
  33. if [[ -d "/boot/EFI" ]]; then
  34. ESP_PATH="/boot"
  35. elif [[ -d "/efi/EFI" ]]; then
  36. ESP_PATH="/efi"
  37. else
  38. print_error "Could not detect ESP path"
  39. exit 1
  40. fi
  41. fi
  42. print_success "ESP detected at: $ESP_PATH"
  43. BACKUP_DIR="/root/bootloader-backup-$(date +%Y%m%d-%H%M%S)"
  44. mkdir -p "$BACKUP_DIR"
  45. [[ -d "$ESP_PATH/loader" ]] && cp -r "$ESP_PATH/loader" "$BACKUP_DIR/" 2>/dev/null || true
  46. [[ -d "$ESP_PATH/EFI/systemd" ]] && cp -r "$ESP_PATH/EFI/systemd" "$BACKUP_DIR/" 2>/dev/null || true
  47. efibootmgr -v >"$BACKUP_DIR/efibootmgr-backup.txt" 2>/dev/null || true
  48. print_success "Backup created at: $BACKUP_DIR"
  49. CURRENT_CMDLINE=""
  50. if [[ -f "/etc/kernel/cmdline" ]]; then
  51. CURRENT_CMDLINE=$(cat /etc/kernel/cmdline)
  52. print_status "Found existing /etc/kernel/cmdline: $CURRENT_CMDLINE"
  53. else
  54. CURRENT_CMDLINE=$(cat /proc/cmdline | sed 's/BOOT_IMAGE=[^ ]* //')
  55. print_status "Using current /proc/cmdline: $CURRENT_CMDLINE"
  56. fi
  57. print_status "Installing Limine and related packages..."
  58. pacman -S --needed --noconfirm limine limine-snapper-sync limine-mkinitcpio-hook
  59. print_success "Limine packages installed"
  60. print_status "Installing Limine bootloader to ESP..."
  61. mkdir -p "$ESP_PATH/EFI/Limine"
  62. if [[ -f "/usr/share/limine/limine_x64.efi" ]]; then
  63. cp /usr/share/limine/limine_x64.efi "$ESP_PATH/EFI/Limine/"
  64. elif [[ -f "/usr/share/limine/BOOTX64.EFI" ]]; then
  65. cp /usr/share/limine/BOOTX64.EFI "$ESP_PATH/EFI/Limine/limine_x64.efi"
  66. else
  67. print_error "Could not find Limine EFI files"
  68. exit 1
  69. fi
  70. print_success "Limine files installed to ESP"
  71. print_status "Creating UEFI boot entry for Limine..."
  72. ESP_DEVICE=$(df "$ESP_PATH" | tail -1 | awk '{print $1}')
  73. ESP_DISK=$(echo "$ESP_DEVICE" | sed 's/[0-9]*$//')
  74. ESP_PART_NUM=$(echo "$ESP_DEVICE" | sed 's/.*[^0-9]//')
  75. efibootmgr -c -d "$ESP_DISK" -p "$ESP_PART_NUM" -L "Limine" -l "\\EFI\\Limine\\limine_x64.efi" || {
  76. print_warning "Could not create UEFI boot entry automatically"
  77. }
  78. print_success "Limine UEFI boot entry created"
  79. print_status "Configuring kernel command line..."
  80. echo "$CURRENT_CMDLINE" >/etc/kernel/cmdline
  81. print_success "Kernel command line configured: $CURRENT_CMDLINE"
  82. print_status "Configuring Limine..."
  83. [[ ! -f "/etc/default/limine" ]] && cat >/etc/default/limine <<EOF
  84. SPACE_NUMBER=2
  85. ENABLE_VERIFICATION=yes
  86. BACKUP_THRESHOLD=8
  87. BOOT_ORDER="*, *fallback, Snapshots"
  88. ENABLE_SORT=no
  89. ENABLE_LIMINE_FALLBACK=no
  90. FIND_BOOTLOADERS=yes
  91. EOF
  92. print_status "Generating Limine configuration..."
  93. limine-update
  94. print_success "Limine configuration created"
  95. print_status "Setting Limine as default boot option..."
  96. LIMINE_ENTRY=$(efibootmgr | grep "Limine" | head -1 | cut -c5-8)
  97. if [[ -n "$LIMINE_ENTRY" ]]; then
  98. CURRENT_ORDER=$(efibootmgr | grep "BootOrder" | cut -d' ' -f2)
  99. NEW_ORDER=$(echo "$CURRENT_ORDER" | sed "s/$LIMINE_ENTRY,//g" | sed "s/,$LIMINE_ENTRY//g" | sed "s/^$LIMINE_ENTRY$//g")
  100. if [[ -n "$NEW_ORDER" ]]; then
  101. efibootmgr -o "$LIMINE_ENTRY,$NEW_ORDER"
  102. else
  103. efibootmgr -o "$LIMINE_ENTRY"
  104. fi
  105. print_success "Limine set as default boot option"
  106. else
  107. print_warning "Could not find Limine boot entry to set as default"
  108. fi
  109. print_status "Keeping systemd-boot as fallback (recommended for first boot)"
  110. if [[ -f "$ESP_PATH/limine.conf" ]] || [[ -f "$ESP_PATH/limine.cfg" ]]; then
  111. print_success "Limine configuration file exists"
  112. else
  113. print_error "Limine configuration file missing!"
  114. fi
  115. if [[ -f "$ESP_PATH/EFI/Limine/limine_x64.efi" ]]; then
  116. print_success "Limine EFI bootloader exists"
  117. else
  118. print_error "Limine EFI bootloader missing!"
  119. fi
  120. echo
  121. print_success "TRANSITION COMPLETED SUCCESSFULLY!"
  122. echo
  123. print_status "Summary:"
  124. echo " • Limine bootloader installed to: $ESP_PATH/EFI/Limine/"
  125. echo " • Configuration file: $ESP_PATH/limine.conf"
  126. echo " • Kernel command line: /etc/kernel/cmdline"
  127. echo " • Backup created at: $BACKUP_DIR"
  128. echo " • Boot order updated"
  129. echo " • systemd-boot kept as fallback"
  130. echo
  131. print_warning "IMPORTANT: Reboot to test the new Limine setup!"
  132. print_success "Script completed. Please reboot when ready."