Understanding Bootloader Directories and Customizing GRUB

To understand where to place the provided GRUB configuration snippet, let’s break down the directories and their purposes.

1. /boot/grub2

This directory is typically used by GRUB2, the Grand Unified Bootloader version 2. GRUB2 is a common bootloader for Linux systems, and it manages the boot process by loading the kernel and initramfs into memory. The key files and directories here include:

  • grub.cfg: This is the main configuration file for GRUB2. It contains the boot menu entries and is automatically generated by tools like grub2-mkconfig. Manual edits to this file are generally discouraged because they can be overwritten during updates or when regenerating the configuration.

  • /boot/grub2/grub.cfg: This is the primary configuration file that GRUB2 reads during boot. It defines the boot menu entries, including the kernel and initramfs to load.

  • /boot/grub2/custom.cfg: Some systems allow you to place custom boot entries in a separate file like custom.cfg, which is included by the main grub.cfg. This is a safer way to add custom entries without modifying the main configuration file.

2. /boot/loader

This directory is typically associated with systemd-boot (formerly known as gummiboot), which is a simpler bootloader used primarily on UEFI systems. The files here are organized differently:

  • /boot/loader/entries/: This directory contains individual configuration files for each kernel version. Each file is named in a specific format, such as hash-kernel-version.conf, and contains details like the kernel version, kernel image path, initramfs path, and boot parameters.

  • /boot/loader/loader.conf: This is the main configuration file for systemd-boot. It can include default boot options and timeouts.

3. /boot/efi

This directory is used for UEFI bootloaders. If your VM is booting in legacy BIOS mode (compatibility mode), this directory might be empty or unused. UEFI systems store bootloaders and related files in this directory, typically under /boot/efi/EFI/.

Where to Place the Code

Since your VM is booting in compatibility mode (BIOS mode), and you mentioned that /boot/grub2 is the regular place for bootloader files, you should place the provided GRUB configuration snippet in a way that integrates with GRUB2.

Option 1: Add to /boot/grub2/grub.cfg

You can directly add the menu entry to /boot/grub2/grub.cfg. However, be aware that this file is often auto-generated, and your changes might be overwritten during updates or when regenerating the configuration.

menuentry 'Copied Kernel' {
    set root='hd0,msdos1' # Adjust based on your disk partition
    linux /boot/copied_kernel/vmlinuz-copied root=/dev/sda1 # Adjust root partition
    initrd /boot/copied_kernel/initrd.img-copied
}

Option 2: Use /etc/grub.d/ to Create a Custom Entry

A more sustainable approach is to create a custom GRUB configuration file in /etc/grub.d/. This way, your custom entry will be preserved when grub2-mkconfig is run.

  1. Create a new file in /etc/grub.d/, e.g., /etc/grub.d/40_custom:

    sudo nano /etc/grub.d/40_custom
    
  2. Add your custom menu entry to this file:

    #!/bin/sh
    exec tail -n +3 $0
    # This file provides an easy way to add custom menu entries. Simply type the
    # menu entries you want to add after this comment. Be careful not to change
    # the 'exec tail' line above.
    
    menuentry 'Copied Kernel' {
        set root='hd0,msdos1' # Adjust based on your disk partition
        linux /boot/copied_kernel/vmlinuz-copied root=/dev/sda1 # Adjust root partition
        initrd /boot/copied_kernel/initrd.img-copied
    }
    
  3. Make the file executable:

    sudo chmod +x /etc/grub.d/40_custom
    
  4. Regenerate the GRUB configuration:

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    

This will include your custom entry in the GRUB menu without directly editing grub.cfg.

Summary

  • /boot/grub2: Used by GRUB2 for BIOS booting. Place custom entries in /etc/grub.d/ and regenerate grub.cfg.

  • /boot/loader: Used by systemd-boot for UEFI booting. Not relevant for your VM since it’s booting in BIOS mode.

  • /boot/efi: Used for UEFI bootloaders. Not relevant for your VM in BIOS mode.

By following the steps above, you can safely add your custom kernel entry to the GRUB boot menu.