Technology Encyclopedia Home >How to add a new boot item to the Bootloader in Linux?

How to add a new boot item to the Bootloader in Linux?

To add a new boot item to the bootloader in Linux, you typically use the grub-mkconfig command or manually edit the GRUB configuration files. Here’s a step-by-step guide:

  1. Identify the Bootloader: Most Linux distributions use GRUB (GRand Unified Bootloader) as the default bootloader.

  2. Locate GRUB Configuration Files:

    • For GRUB 2, the main configuration file is usually located at /etc/default/grub.
    • The menu entries are typically in /boot/grub/grub.cfg or generated from scripts in /etc/grub.d/.
  3. Edit GRUB Configuration:

    • Open the /etc/default/grub file with a text editor to modify default settings if needed.
    • To add a new boot item manually, you can add an entry to /etc/grub.d/40_custom. For example, to add an entry for a Linux kernel, you might add:
      menuentry "My Custom Linux" {
          set root=(hd0,gpt2)
          linux /boot/vmlinuz-mykernel root=/dev/sda2
          initrd /boot/initrd.img-mykernel
      }
      
    • Replace (hd0,gpt2), /boot/vmlinuz-mykernel, and /boot/initrd.img-mykernel with the appropriate values for your system.
  4. Update GRUB:

    • After making changes, update GRUB to regenerate the configuration file:
      sudo grub-mkconfig -o /boot/grub/grub.cfg
      
    • This command reads the scripts in /etc/grub.d/ and the /etc/default/grub file to create a new /boot/grub/grub.cfg.
  5. Reboot and Select the New Entry:

    • Reboot your system.
    • During the boot process, press Shift (or Esc depending on your system) to enter the GRUB menu and select your new boot item.

Example: Suppose you have a custom Linux kernel compiled for a specific project. You would place this kernel and its initrd image in /boot, then add an entry in /etc/grub.d/40_custom as shown above, specifying the correct paths and partitions.

For cloud-related operations, if you need to manage Linux instances on a cloud platform, consider using services like Tencent Cloud's Cloud Virtual Machine, which provides flexible and scalable virtualization solutions.