§2023-08-25

ChatGPT

Setting up GRUB (GRand Unified Bootloader) in a Gentoo Linux installation involves a series of steps to ensure that your system can boot properly. Here's a general process for installing and configuring GRUB on a new Gentoo OS:

¶1. Install GRUB Package:

emerge sys-boot/grub

¶2. Generate GRUB Configuration:

After installing GRUB, generate the GRUB configuration file using the grub-mkconfig command. This command will scan your system for installed operating systems and generate a configuration file (grub.cfg) accordingly. By default, the configuration file is placed in the /boot/grub/ directory.

(chroot) x8664Arch / # mkfs.vfat /dev/sdb1
mkfs.fat 4.2 (2021-01-31)
(chroot) x8664Arch / # mount /dev/sdb1 boot
(chroot) x8664Arch / # ls boot/

chroot) x8664Arch / # mkdir boot/grub
(chroot) x8664Arch / # grub-mkconfig -o boot/grub/grub.cfg
Generating grub configuration file ...
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
(chroot) x8664Arch / # ls boot/grub/
grub.cfg
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}

function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
insmod part_msdos
insmod ext2
set root='hd1,msdos3'
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint-bios=hd1,msdos3 --hint-efi=hd1,msdos3 --hint-baremetal=ahci1,msdos3  3804224b-7025-4e18-a1fd-f242ebc770ee
else
  search --no-floppy --fs-uuid --set=root 3804224b-7025-4e18-a1fd-f242ebc770ee
fi
    font="/usr/share/grub/unicode.pf2"
fi

if loadfont $font ; then
  set gfxmode=auto
  load_video
  insmod gfxterm
fi
terminal_output gfxterm
if [ x$feature_timeout_style = xy ] ; then
  set timeout_style=menu
  set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=5
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/30_uefi-firmware ###
menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {
	fwsetup
}
### END /etc/grub.d/30_uefi-firmware ###

### BEGIN /etc/grub.d/40_custom ###
# 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.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
  source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
  source $prefix/custom.cfg
fi
### END /etc/grub.d/41_custom ###

¶ 3. Install GRUB to the Bootloader Area:

Use the grub-install command to install GRUB to the Master Boot Record (MBR) or the partition's boot sector, depending on your system's setup. The exact command might vary based on your hardware and preferences. Here are a couple of examples:

grub-install /dev/sdX
chroot) x8664Arch / # mount /dev/sdb1 /boot/efi
(chroot) x8664Arch / # lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 223.6G  0 disk 
├─sda1   8:1    0 213.6M  0 part 
├─sda2   8:2    0    60G  0 part /etc/resolv.conf
├─sda3   8:3    0     9G  0 part [SWAP]
└─sda4   8:4    0 154.4G  0 part 
sdb      8:16   0 232.9G  0 disk 
├─sdb1   8:17   0   512M  0 part /boot/efi
├─sdb2   8:18   0    16G  0 part 
└─sdb3   8:19   0    60G  0 part /
sdc      8:32   1     0B  0 disk 
(chroot) x8664Arch / #  grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GentooX86_64
Installing for x86_64-efi platform.
Installation finished. No error reported.

¶ 4.  initramfs-linux-fallback.img  initramfs-linux.img  vmlinuz-linux

(chroot) x8664Arch /usr/src/linux-6.1.46-gentoo/arch/x86_64/boot # cd ../../../ (chroot) x8664Arch /usr/src/linux-6.1.46-gentoo # ls COPYING Kconfig Module.symvers block distro init lib modules.order scripts usr vmlinux.o CREDITS LICENSES README built-in.a drivers io_uring mm net security virt Documentation MAINTAINERS System.map certs fs ipc modules.builtin rust sound vmlinux Kbuild Makefile arch crypto include kernel modules.builtin.modinfo samples tools vmlinux.a (chroot) x8664Arch /usr/src/linux-6.1.46-gentoo # ls System.map System.map (chroot) x8664Arch /usr/src/linux-6.1.46-gentoo # cp -v System.map /boot/ 'System.map' -> '/boot/System.map'


  
    bash
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=Gentoo

    Adjust /dev/sdX and /boot/efi according to your system's disk and EFI partition.

    Verify Installation:
    Verify that GRUB was installed correctly by ensuring that the appropriate files are in place and that there were no errors during the installation process.

    Customize Configuration (Optional):
    The generated grub.cfg file might contain entries for all the operating systems it found on your system. You can customize this configuration file to your preferences. However, be cautious when editing this file, as incorrect changes could render your system unbootable.

    Setting Boot Options (Optional):
    You can set various boot options in the grub.cfg file, such as kernel parameters, boot timeout, default boot entry, etc.

    Generate Initramfs (if needed):
    If your system requires an initramfs (an initial RAM filesystem), create it with the appropriate tools (genkernel, dracut, etc.) and update your GRUB configuration to include the initramfs.

    Reboot:
    After installing and configuring GRUB, reboot your system to test whether it boots properly.

Remember that these steps are a general guide. The specifics might vary depending on your hardware architecture (BIOS or UEFI), disk layout, and other factors. Always refer to the official Gentoo documentation and resources for the most accurate and up-to-date instructions. Mistakes in the GRUB configuration can potentially render your system unbootable, so proceed with caution and make backups before making any significant changes.


- [GNU Grub](https://www.gnu.org/software/grub/)

GRUB 2 has replaced what was formerly known as GRUB (i.e. version 0.9x), which has, in turn, become GRUB Legacy. Enhancements to GRUB are still being made, but the current released versions are quite usable for normal operation.

- [GRUB2 Quick Start](https://wiki.gentoo.org/wiki/GRUB2_Quick_Start/en)

1. Add `GRUB_PLATFORMS="efi-64" into /etc/portage/make.conf

2. emerge the software: `emerge --ask sys-boot/grub`