Skip to main content

How to set up a GRUB 2 theming playground (on Gentoo)

In this post I am going to present a quick guide to set up a basic environment to test and develop GRUB 2 themes on Gentoo Linux. Its core feature is the use of virtualization so you restart virtual hardware rather than physical one. Ingredients:

  • KVM (or QEMU) — i.e. package app-emulation/qemu-kvm or app-emulation/qemu
  • GNU parted — package sys-block/parted
  • kpartx — from sys-fs/multipath-tools
  • GNU GRUB 2 — sys-boot/grub:2

Despite care and best intents: For what comes next, NO WARRENTY of any kind! sudo and root permission stuff is involved. First, let's install the required software:

$ sudo GRUB_PLATFORMS='pc' emerge --update sys-boot/grub:2 \
    sys-fs/multipath-tools sys-block/parted app-emulation/qemu-kvm

Now, let's create a sparse, virtual hard disk of 500MB in size and create a partition table with a single partition covering all available space. We use bash syntax to do the math.

$ touch grub-theme-disk

$ truncate --size=$((500*1024**2)) grub-theme-disk

$ /usr/sbin/parted grub-theme-disk mklabel msdos
WARNING: You are not superuser.  Watch out for permissions.

$ /usr/sbin/parted grub-theme-disk mkpart primary ext2 0% 100%
WARNING: You are not superuser.  Watch out for permissions.

Next, we make a block device for that partition and format it as ext2. We also store the location of the whole disk and the partition in dedicated variables for re-use.

$ sudo kpartx -p p -a -v grub-theme-disk
add map loop0p1 (254:9): 0 1021952 linear /dev/loop0 2048

$ GRUB_PART=/dev/mapper/$(sudo kpartx -p p -l grub-theme-disk \
    | grep -oE 'loop[0-9]p1')

$ GRUB_DEV=$(sudo kpartx -p p -l grub-theme-disk | grep -oE '/dev/loop[0-9]')

$ echo "${GRUB_DEV}, ${GRUB_PART}"
/dev/loop0, /dev/mapper/loop0p1

$ sudo mkfs.ext2 "${GRUB_PART}"
mke2fs 1.42.4 (12-June-2012)
Writing superblocks and filesystem accounting information: done

Now we mount the file system and create a minimal GFX GRUB 2 config in it using theme "starfield" that comes shipped with GRUB 2. For the menu, two dummy entries to restart and shutdown the machine are used.

$ mkdir grub-theme-disk-root

$ sudo mount "${GRUB_PART}" grub-theme-disk-root

$ sudo mkdir -p grub-theme-disk-root/boot/grub2/

$ cat <<"GRUB_CFG" | sudo sh -c 'cat > grub-theme-disk-root/boot/grub2/grub.cfg'
set theme=$prefix/themes/starfield/theme.txt
insmod all_video
insmod gfxterm
insmod png
terminal_output gfxterm
menuentry Reboot { reboot }
menuentry Shutdown { halt }

We are ready to install GRUB 2. That includes copying files to /boot/grub2 (e.g. the starfield theme) as well as writing the bootloader to the master boot record of the virtual disk file (or more precisely the related loop device). We need to flush the write cache using the "sync" command so we do not boot from half-flushed data in the next step.

$ sudo grub2-install --boot-directory=grub-theme-disk-root/boot "${GRUB_DEV}"
Installation finished. No error reported.

$ sync

At that point, our disk image is ready to be booted as hard drive by KVM (or QEMU):

$ sudo qemu-kvm -hdd "${GRUB_DEV}"

You should see something like this (click to enlarge):

Now for the most important part: If you do make a theme please make sure the overall theme is legally sound! For instance that means:

  1. If you use the original, rendered "g" Gentoo logo, the related files have to comply with the CCPL-Sampling-Plus-1.0 license.
  2. If you use Larry the cow graphics, the theme has to comply with the CC-BY-SA/2.5 license.

This can hardly be over-emphasized. If you fail to respect licenses properly Gentoo will not be able to include your theme anywhere, really. Other than that, happy theming! Feel free to post links to your Gentoo GRUB 2 themes in the comments below. For more graphical building blocks and logos that you might want to use, please check out the Gentoo Artwork page and the vector remake of GDM 2.x background "gentoo-cow". The theming file format of GRUB 2 is described in detail in the official GRUB manual.