Maintenance free system multi booting
Tags: grub, boot, menu, efi
Published: 2021-10-29 03:18:03


---
title: Maintenance free system multi booting
series: maintenance free grub boot
published: true
description: Simplified maintenance-free system multi booting.
tags: grub,boot,menu,EFI
cover_image: https://user-images.githubusercontent.com/422244/139099880-93d47280-beab-49c5-a0f1-4ed00e5a3a57.png
---

## The journey continues

First, recap from the [previous article](https://dev.to/suntong/journey-towards-a-maintenance-free-system-booting-3613#conclusion), a single `grub` boot menu entry can be as simple as:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os0_default.cfg %}

With it, we don't need to hunt for the UUIDs, and we don't need to remember to mount boot partition and update the boot entries any more. It can boot to whatever latest kernel it is, and it will still work even we reformat the partition and install a new Linux, and it will still work even we change the distro from Debian, to Mint, Arch, Fedora, or openSUSE etc.

In this article, we'll first look at those few more boilerplates that enable you to only write the above few lines and everything will work out magically. Then we'll move on to how to make things even simpler, with the help of [easygen](https://github.com/go-easygen/easygen/wiki).

## The supporting files

To break free from the rigid format requirement of `40_custom` and allow people only write the above few lines, or to **order boot entries** in the order we want, instead of being _determined by their partition orders_, we need to build our `grub.cfg` file from ground up ourselves. And, it is not that scary either and could be really straightforward. Here are all the code it needs:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=grub.cfg %}

The `grub.cfg` file that replace `grub`s default `.cfg` file. Deliberately made so simple so that it can be easily restored if it was accidentally overwritten.

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=grub-main.cfg %}

The `grub-main.cfg` that the `grub.cfg` file actually calls. The idea comes from [grml](https://grml.org). We can see that it is sourcing a header, a footer, and the main/real `.cfg` files, like the entry shown above. 

Arrange those `.cfg` files so that their order show up in the order you want in the grub menu. For example, this is how I arrange my grub menu:

![image](https://user-images.githubusercontent.com/422244/139169315-432b6d5f-2951-4f08-9189-36c7aa9a59f0.png)

I.e., in my preference, I want my grub menu to be in the order of:

- My current Linux
- Live Linux from ISO, in case I need to check/repair anything
- My Windows
- My old Linux versions

They are ordered from most probably used to the least probably used. Changing the order is simple, just shift them around in the `.cfg` text file.

The grub menu's look-and-feel is defined in the header, and the "Shutdown / Reboot" is defined in footer, showing below respectively.

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=grub-header.cfg %}

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0/c918b0840bd4076bff6cbd13f6507a77d6000635 file=grub-footer.cfg %}

<a name="iso"/>

## ISO Booting

The bonus and [prestige](https://en.wikiquote.org/wiki/The_Prestige_(film)) of the whole article, here is how ISO files are automatically discovered and listed:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0/d997948ce6345951b8c16a62b831357bf68db717 file=iso-all.cfg %}

It can automatically discover and list whatever ISO available on disk:

![image](https://user-images.githubusercontent.com/422244/139169388-8e85f402-06fa-49bf-9e91-60bc97bd1540.png)

and boot from them:

![image](https://user-images.githubusercontent.com/422244/139169415-e2ec398c-6adb-48eb-a96a-e6f28e3950a8.png)

The red background color is chosen for the grub menu, to symbolize the ***live*** system, and also as the notification sign that this will be the last grub menu level, after which the system will be in booting mode.

## Can it be even simpler

So far, we can see that using some simple scripts/configs, we can let the grub to show whatever we want it to show, in whatever order we want as well. Now, 

Can it be even simpler?

As pointed out in the ["Conclusion"](https://dev.to/suntong/journey-towards-a-maintenance-free-system-booting-3613#conclusion) section in the last article, the only things that matter in a grub menu entry are the `'Debian GNU/Linux'` and the `'hd0,gpt2'`, the rest are just the boilerplate that don't normally need to be changed.

What if I only want to specify the `'Debian GNU/Linux'` and the `'hd0,gpt2'` part, and let the machine to generate the grub menu entry/entries for me? -- Not a problem, if with the help of [easygen](https://github.com/go-easygen/easygen/wiki).

This is all what you need to specify:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os2grub-1.yaml %}

Feed that to `easygen` as:

`easygen os2grub-1`

then you'll get:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os2grub-1.gold %}

The secret is the easygen's template:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os2grub.tmpl %}

Customize it to whatever you like.


But, most people not just has a single boot menu. How do you deal with that? Simple -- with another small tool of `grub2yaml.sh`:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=grub2yaml.sh %}

Just run it with 

`grub2yaml.sh /boot/grub/grub.cfg | tee /tmp/myOSs.cfg`

Slightly edit it, and you'll get more entries like this:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os2grub.yaml %}

With multiple entries like that, `easygen` really starts to shine. You can get the following without a drop of sweat:

{% gist https://gist.github.com/suntong/cafb2fc8eaaeb77871927abae16c54f0 file=os2grub.gold %}

## Enjoy

IMHO, the way `grub2` generates boot entries automatically, at least as of this article is written, is still a big mess. You may not agree with that, but I do enjoy my grub menu system to be plain and simple,  straightforward and out of my way. And, I enjoy my freedom to hand-craft my own boot menus again, just like the good old days, but without an extra spare letter to waste. 

Hope you enjoy that as well. 

Cheers