I develop stuff and have opinions on things -
About me/Contact

Articles:

grub-mkconfig: brainlessness considered harmful

(Spoiler: this is Yet Another Rant)

Arch has officially dropped support for the original GRUB in July 2012, which is longer that one could have expected, considering the level of support it was receiving (the last version had like more than twenty package versions, adding hacks to make it work again and again). However, I did not switch to grub2 immediately, mainly because I had no reason to do so ; I then procrastinated until today, when I finally made the switch.

While grub2 is notorious for being awfully complex to configure, with obscure files all over the place, reading the official manual shows a different picture. In fact, grub2 is pretty much the same as grub1 for most configurations, and more logical (e.g. sections delimited by {} instead of ordered lines). The root of the “complex” reputation or grub2 comes from grub-mkconfig.

grub-mkconfig

What about generating automatically a humongous configuration file from dozens of files, when it all boils down to 4 useful lines?

Sounds like a good idea to me.

grub-mkconfig is the recommended tool for building a grub configuration in pretty much any tutorial. But it has a major shortcoming:it sucks. Instead of editing easily your /boot/grub/grub.cfg file, you have to edit /etc/grub/default (why does it look like a file exporting env variables?), and then, files in /etc/grub.d/40_sdfsd.cfg. If your configuration differs from the default grub-mkconfig finds (e.g. LVM, LUKS…), you have to add things all over the place. Oh, and yeah, to make sure you will be able to boot next time, you obviously have to check the file generated by grub-mkconfig.

Grub-mkconfig was designed for distributions like fedora, ubuntu, or whatever, which are distributions providing several kernel versions at the same time, so autodetecting mostly works and lets the system automatically update the entries in the .cfg without worrying the user. But as always, when you try to add apparent simplicty (“just run it, it’ll work”), you add tons of layers of complexity for users who want to customize further than what is supported with the automated tools.

The official guide also says:

those who feel that it would be easier to write grub.cfg directly are encouraged to do so

However, apart from the official documentation, which is incomplete in some cases (and not necessarily up-to-date), every single guide uses grub-mkconfig, and lets the magic of grub-mkconfig happen. Adding a background? Just modify the variable in the /etc/default/grub file and let the magic happen. Adding and selecting a theme? Same thing. Selecting colors? Same thing.

The worst thing is that you have to test everything you do, and the trial and error process is especially long with grub.

For reference, the “Example” grub.cfg file provided with arch:

#
# 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 ###
insmod part_gpt
insmod part_msdos
if [ -s $prefix/grubenv ]; then
  load_env
fi
set default="0"

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 loadfont unicode ; then
  set gfxmode=auto
  load_video
  insmod gfxterm
  set locale_dir=$prefix/locale
  set lang=fr_FR
  insmod gettext
fi
terminal_input console
terminal_output gfxterm
set timeout=5
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Arch GNU/Linux, with Linux lts kernel' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-lts kernel-true-eadd250a-7313-40a6-a89b-5fef789a2acf' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  122a565b-2427-4430-bc75-bce4a21a09c1
    else
      search --no-floppy --fs-uuid --set=root 122a565b-2427-4430-bc75-bce4a21a09c1
    fi
    echo    'Loading Linux lts kernel ...'
    linux    /vmlinuz-linux-lts root=/dev/mapper/cryptpool-cryptroot ro  quiet
    echo    'Loading initial ramdisk ...'
    initrd    /initramfs-linux-lts.img
}
menuentry 'Arch GNU/Linux, with Linux lts kernel (Fallback initramfs)' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-lts kernel-fallback-eadd250a-7313-40a6-a89b-5fef789a2acf' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  122a565b-2427-4430-bc75-bce4a21a09c1
    else
      search --no-floppy --fs-uuid --set=root 122a565b-2427-4430-bc75-bce4a21a09c1
    fi
    echo    'Loading Linux lts kernel ...'
    linux    /vmlinuz-linux-lts root=/dev/mapper/cryptpool-cryptroot ro  quiet
    echo    'Loading initial ramdisk ...'
    initrd    /initramfs-linux-lts-fallback.img
}
menuentry 'Arch GNU/Linux, with Linux core repo kernel' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-true-eadd250a-7313-40a6-a89b-5fef789a2acf' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  122a565b-2427-4430-bc75-bce4a21a09c1
    else
      search --no-floppy --fs-uuid --set=root 122a565b-2427-4430-bc75-bce4a21a09c1
    fi
    echo    'Loading Linux core repo kernel ...'
    linux    /vmlinuz-linux root=/dev/mapper/cryptpool-cryptroot ro  quiet
    echo    'Loading initial ramdisk ...'
    initrd    /initramfs-linux.img
}
menuentry 'Arch GNU/Linux, with Linux core repo kernel (Fallback initramfs)' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-fallback-eadd250a-7313-40a6-a89b-5fef789a2acf' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  122a565b-2427-4430-bc75-bce4a21a09c1
    else
      search --no-floppy --fs-uuid --set=root 122a565b-2427-4430-bc75-bce4a21a09c1
    fi
    echo    'Loading Linux core repo kernel ...'
    linux    /vmlinuz-linux root=/dev/mapper/cryptpool-cryptroot ro  quiet
    echo    'Loading initial ramdisk ...'
    initrd    /initramfs-linux-fallback.img
}

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

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

### BEGIN /etc/grub.d/20_memtest86+ ###
### END /etc/grub.d/20_memtest86+ ###

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

### 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 ###

Mine:

set default=0
set timeout=5
set color_normal=white/black
set color_highlight=light-red/black

menuentry "Arch Linux" {
    set root=(hd0,1)
    linux /vmlinuz-linux root=/dev/mapper/cryptpool-cryptroot cryptdevice=/dev/sda2:sdacrypt init=/usr/lib/systemd/systemd ro
    initrd /initramfs-linux.img
}

menuentry "Arch Linux - Kernel LTS" {
    set root=(hd0,1)
    linux /vmlinuz-linux-lts root=/dev/mapper/cryptpool-cryptroot cryptdevice=/dev/sda2:sdacrypt init=/usr/lib/systemd/systemd ro
    initrd /initramfs-linux-lts.img
}

menuentry "Arch Linux Fallback" {
    set root=(hd0,1)
    linux /vmlinuz-linux root=/dev/mapper/cryptpool-cryptroot cryptdevice=/dev/sda2:sdacrypt ro
    initrd /initramfs-linux-fallaback.img
}

menuentry "Arch Linux Fallback - Kernel LTS"  {
    set root=(hd0,1)
    linux /vmlinuz-linux-lts root=/dev/mapper/cryptpool-cryptroot cryptdevice=/dev/sda2:sdacrypt ro
    initrd /initramfs-linux-lts-fallaback.img
}

Notice a difference?

If you have remarks or suggestions concerning this article, please by all means contact me.