Any Grub gurus?

tl;dr: I want to adapt this:

to automatically boot from the highest available disk.

Background

I recently bought a HPE Microserver Gen8 to act as a backup server for my parents (and also as an offsite backup for me). It’s a nice little piece of kit–small, quiet, energy-efficient, but still with a decent amount of CPU horsepower and four drive bays. And pretty reasonably priced on the used market.

The system has a fifth SATA port that serves an optical drive, but due to some curious decision-making on HPE’s part, it will not boot from that port. And it’s also a little picky about USB boot devices, so I wasn’t able to just install TrueNAS to a USB SSD and boot from that (at least, not with the USB SSD I had around).

Enter the forum thread I linked above–that user came up with a clever way to handle this, by installing Grub to a flash device (I used a microSD card, since the system has an internal reader for those), and then, using a hand-coded grub.cfg file, telling Grub to continue booting from the SSD where I had TrueNAS installed. So I can boot from a SSD (rather than what tend to be very unreliable USB flash drives), but it doesn’t have to occupy one of my four drive bays that I’d rather be able to use for storage.
image

The grub.cfg, as I’ve slightly modified it, goes like this:

set default="0"
set timeout=5

menuentry "TrueNAS CORE 12" --class freebsd --class bsd --class os {
	insmod zfs
	insmod ext2
	insmod part_gpt
	echo			Chainloading hd3 ...
	set root=(hd3)
	chainloader +1
}

The problem

If you look carefully above, you’ll see that it’s booting from a hard-coded hd3, the third drive on the system (right now, I have two spinners plus the SSD; the SSD is the third drive). And this is fine for right now, as there are only the three drives. But if I need more storage in the system, I’d install another pair of drives, and the SSD will then become hd5–and now it won’t boot without editing that grub.cfg.

What I’d like to be able to do is tell Grub, “boot from the last drive.” To narrow it down, I could say, “boot from the last drive of hd{1…5}”, as this system has only the five ports. But a web search is finding almost nothing about preparing your own grub.cfg file–everything I’m finding says “here be dragons, don’t do it, let your system generate it automatically”, which just isn’t very helpful here. The Grub manual on gnu.org suggests that it does support conditional logic and looping:
GNU GRUB Manual 2.06

But I don’t see anything there, or anywhere else I’ve seen, to do anything like what I’m wanting to do. Any ideas?

I think the search option could help. It seems, it’s used instead of set root, see ArchLinux Wiki.

1 Like

Thanks. It seems like it may take some tinkering, but Free/TrueNAS uses GPT UUIDs to identify partitions, so that looks promising. What concerns me a little is that the IDs used in Linux seem to be only eight characters (four bytes) long (e.g., --fs-uuid 763A-9CB6 from that page), while the GPT UUIDs are much longer:

=>       40  488397088  ada2  GPT  (233G)
         40       1024     1  83bd6b9d-7f41-11dc-be0b-001560b84f0f  (512K)
       1064   33554432     3  516e7cb5-6ecf-11d6-8ff8-00022d09712b  (16G)
   33555496  454819840     2  516e7cba-6ecf-11d6-8ff8-00022d09712b  (217G)
  488375336      21792        - free -  (11M)

That first partition is of type freebsd-boot, so it should be the right one–I guess I’ll need to see how Grub deals with that.

1 Like

No luck so far. Grub apparently can’t find the GPT UUIDs, nor can it find partition labels applied using either gpart or glabel under FreeBSD. It’s doubtless possible to apply a label using Linux, and it may be possible to find a UUID there that would do the job, but it’s looking like this is going to be a pain…

Here it seems search only works when you directly load the bootloader instead of chainloading:

1 Like

That looks like a good find, thanks. Won’t be able to test it yet tonight, but I’ll have to see if I can give it a shot tomorrow.

1 Like