Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
grub.cfg example that may be of interest to newbies
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message

Joined: 07 Mar 2007
Posts: 835

PostPosted: Sun May 19, 2019 1:21 pm    Post subject: grub.cfg example that may be of interest to newbies Reply with quote

<edit>Improved the example's readability</edit>

I deferred migrating from Grub Legacy (version 0.9x) to Grub 2 because there seemed to be a lot of bloat combined with the need to run a script to create its grub.cfg menu. However, I needed to install Ubuntu on a new Raid 5 array on the same machine, it was clear Grub Legacy wouldn't hack it.

Digging around the web, the first thing I learnt was that you don't need to run the script to create grub.cfg; at its most basic, it can be a simple translation from the legacy grub.conf. But grub 2 brings (a) a bunch of modules that can handle many more installation use cases (GPT, RAID. network), and (b) a bash-like scripting language. I thought I'd share the results, as it may offer encouragement to others in a similar situation.

My grub.cfg handles:

1) booting my Gentoo BIOS-partition setup using my translated lines from grub.conf. These rely on vmlinuz/vmlinuz.old being a symlink to the latest/previous kernel (with a built-in initramfs) as maintained by the kernel's "install" make target, and the use of partition labels.

2) finding the RAID array containing Ubuntu, and scanning its boot directory to find the latest kernel and initramfs (maintained by Ubuntu's updater).

with just a bit of scripting.

You could just as easily move the loop that searches for the latest Ubuntu kernel outside of the menuentry, and instead generate a menuentry for each extant Ubuntu kernel, but I'm not sure if that would need sorting. Anyway, herewith my example:

# Grub 2 configuation to boot Gentoo invarious ways, and Ubuntu in a RAID array
# Paul Gover - 19th May 2019

default 0
timeout 30

# Insert grub modules for BIOS partition tables, filtering, ext/2/3/4 and regex
insmod part_msdos
insmod diskfilter
insmod ext2
insmod regexp   # Enables filename globbing in for loops

# Funtion to find the newest file version with filename $1-*
# Result will be in $latest - grub functions don't seem to return strings
function findlatest {
  set latest=
  for file in $1-*
  do if [ "$latest" == "" -o $file -nt $latest ]
     then set latest=$file

# Now build the menu entries
menuentry "Gentoo on RAID, with initramfs" {
  # Find my boot partiton by label, and set grub's root
  search --no-floppy --set root --label boot
  linux /boot/vmlinuz mdadm rootfs=LABEL=raidroot noagp net.ifnames=0

menuentry "Ubuntu for Steam" {
  # Modules to handle  RAID5
  insmod mdraid1x       # Handle Raid 1.x superblocks
  insmod raid5rec       # makes "md/0" into a valid root filesystem on RAID5

  set root="md/0"       # identify the array by mdadm "name", in this case 0

  # Use findlatest to locate kernel and initramfs in the boot directory
  findlatest "/boot/vmlinuz"
  set kernel=$latest

  findlatest "/boot/initrd.img"
  set initrd=$latest

  if [ "$kernel" == "" ]
    echo No kernel found
    # Find the array's uuid for the kernel boot parameters
    probe --set uuid --fs-uuid $root

    linux $kernel root=UUID=$uuid ro

    if [ "$initrd" != "" ]
      initrd $initrd

# Various other Gentoo and escape menu items
menuentry "Gentoo Linux - previous kernel revision" {
  search --no-floppy --set root --label boot
  linux /boot/vmlinuz.old mdadm rootfs=LABEL=raidroot net.ifnames=0

menuentry "Gentoo rescue" {
  search --no-floppy --set root --label boot
  linux /boot/vmlinuz rescue

menuentry "Reboot" {

menuentry "Shutdown" {

Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum