Grub2 "search" line - what is it for?

Questions about Grub, UEFI,the liveCD and the installer
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
viking777

Grub2 "search" line - what is it for?

Post by viking777 »

Although I am still of the opinion that grub2 is nothing but a mistimed April Fool's joke, that doesn't stop me from wanting to understand it. I have read quite a lot about it over the last few weeks, but nothing whatsoever that I have read has bothered to explain the function of the line:

Code: Select all

search --no-floppy --fs-uuid --set 78a2f430-74f4-4d2f-8b60-5dfbd21050a9


that occurs in every boot stanza.

The only thing I have managed to find out about it is that a lot of people end up deleting that line in order to get grub2 to boot anything at all (although that is not the case for me).

I would like to know two things about it. Firstly what it does and secondly what is the correct syntax to get it to use LABEL instead of uuid's.

UUID's are at best a second rate way of identifying disks, because LABEL is human readable and will therfore always be superior, and all my disks have labels. Obviously you can't start out with a LABEL system because a lot of people don't use them but I do and I would like to know how to persuade grub2 to use them too. It is really only the syntax I am after I already know which files to edit and that sort of thing.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

I was going to try the syntax shown on the Arch Linux wiki from grub2: http://wiki.archlinux.org/index.php/GRUB2

If it works, let us know... I'll try it too when I get a chance. In the part about uuid, they dont do a set root= above or below the search line, it seems to be part of it. It also has about using labels...

Also, the syntax used in the 10_linux part of grub.cfg works. I don't know if it is because grub2 finds 2 Linux installs, but the script puts the search above the menu entries. I want to know if this makes the search global for the 2 menu entries (my guess), or does it make it unused. Like I said in other posts, using it in the menuentry has been problematic (inside the curly braces).

But yeah I think search is used to find your boot partition, like /dev/sdb1 is /boot, and has a certain uuid, so you have grub2 search for that.
emorrp1

Re: Grub2 "search" line - what is it for?

Post by emorrp1 »

As you can see from the attachment, a simple "help search" from the grub command line tells you all you need to know (if you excuse Helena's poor grub wallpaper choice). I agree that labels rock, but UUID does have the advantage of being computer-generated, so useful for default system setups.
grub2-search.png
viking777

Re: Grub2 "search" line - what is it for?

Post by viking777 »

Thanks for those replies guys they are both excellent.

breaker

That was just the kind of info I wanted to answer part 2 of my question, I will try that later on and let you know how I get on.

emorrp1

The reason I haven't used the help file from a grub prompt is twofold. Firstly I didn't think of it and secondly I don't know how to get to the grub prompt anyway :oops:

Here is a list of all the shell commands I have that include the word grub:

grub-dumpbios (Manual page for grub-dumpbios (GNU GRUB 1.97~beta4))
grub-editenv (Manual page for grub-editenv (GRUB) 1.97~beta4)
grub-install (Manual page for grub-install (GNU GRUB 1.97~beta4))
grub-mkconfig (Manual page for grub-mkconfig (GNU GRUB 1.97~beta4))
grub-mkdevicemap (Manual page for grub-mkdevicemap (GRUB) 1.97~beta4)
grub-mkelfimage (Manual page for grub-mkimage (GRUB) 1.97~beta4)
grub-mkfont (Manual page for grub-mkfont (GRUB) 1.97~beta4)
grub-mkimage (Manual page for grub-mkimage (GRUB) 1.97~beta4)
grub-mkrescue (Manual page for grub-mkrescue (GNU GRUB 1.97~beta4))
grub-probe (Manual page for grub-probe (GRUB) 1.97~beta4)
grub-reboot (Manual page for grub-install (GNU GRUB ))
grub-set-default (Manual page for grub-install (GNU GRUB ))
grub-setup (Manual page for grub-setup (GRUB) 1.97~beta4)
None of these will give me a grub prompt so how do I get one? (It was so easy with grub legacy)

To both of you (and anyone else who cares to reply):

I still don't really have an answer to the first part of my question. I understand the --no-floppy bit of course. But the rest troubles me. Reading the screenshot that emorrp1 posted it suggests that the boot loader is being told to 'search' for a particular file system uid and when it finds it to give that file system the variable 'root'. That is fair enough I guess, but the bit that puzzles me is that in the line before the 'search' line one I have:

Code: Select all

set root=(hd0,7)
Is this not telling it to do exactly the same thing in another (and far simpler) way?

If that is so, then I guess that explains why quite a few people on various forums have removed the 'search' line and still got a satisfactory (or improved) boot.
emorrp1

Re: Grub2 "search" line - what is it for?

Post by emorrp1 »

Fair enough, I got the grub prompt by pressing 'c' from the boot menu. Yes, it amused me to see that it did the same thing twice too (assuming that the (hd0,7) partition matches the UUID). It's simpler to use (hd0,7), but more powerful to use search, since you can do it with labels and stuff.
viking777

Re: Grub2 "search" line - what is it for?

Post by viking777 »

emorrp1 wrote:Fair enough, I got the grub prompt by pressing 'c' from the boot menu. Yes, it amused me to see that it did the same thing twice too (assuming that the (hd0,7) partition matches the UUID). It's simpler to use (hd0,7), but more powerful to use search, since you can do it with labels and stuff.
Thanks emorrp1 Noted that. See my next post for more info on this topic.
viking777

Re: Grub2 "search" line - what is it for?

Post by viking777 »

Experiments with grub2.

Here is my defult grub.cfg stanza from my Ubuntu installation :

Code: Select all

menuentry "Ubuntu 2.6.31-16-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
	set quiet=0
	insmod ext2
	set root=(hd0,6)
	search --no-floppy --fs-uuid --set 62dda807-0d27-4b87-8146-8c43143c339c
	linux	/boot/vmlinuz-2.6.31-16-generic root=/dev/sda6 ro   nosplash
	initrd	/boot/initrd.img-2.6.31-16-generic
}
First I changed the uuid method to a label method like so (thanks breaker!):

Code: Select all

menuentry "Ubuntu 2.6.31-16-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
	set quiet=0
	insmod ext2
	set root=(hd0,6)
	search --no-floppy --label ubuntu --set 
	linux	/boot/vmlinuz-2.6.31-16-generic root=/dev/sda6 ro   nosplash
	initrd	/boot/initrd.img-2.6.31-16-generic
}
It boots normally.

Then I removed the 'search' line (from the original stanza to keep is simple):

Code: Select all

menuentry "Ubuntu 2.6.31-16-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
	set quiet=0
	insmod ext2
	set root=(hd0,6)
	linux	/boot/vmlinuz-2.6.31-16-generic root=/dev/sda6 ro   nosplash
	initrd	/boot/initrd.img-2.6.31-16-generic
}
It booted normally.

Then I removed the set root line from the original stanza:

Code: Select all

menuentry "Ubuntu 2.6.31-16-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
	set quiet=0
	insmod ext2
	search --no-floppy --fs-uuid --set 62dda807-0d27-4b87-8146-8c43143c339c
	linux	/boot/vmlinuz-2.6.31-16-generic root=/dev/sda6 ro   nosplash
	initrd	/boot/initrd.img-2.6.31-16-generic
}
It booted normally.

Proving that you only need one not both (as emorpp1 said above but he got in before me!).

I then removed the set root line from the 'label' stanza giving:

Code: Select all

menuentry "Ubuntu 2.6.31-16-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
	set quiet=0
	insmod ext2
	search --no-floppy --label ubuntu --set 
	linux	/boot/vmlinuz-2.6.31-16-generic root=/dev/sda6 ro   nosplash
	initrd	/boot/initrd.img-2.6.31-16-generic
}
It booted normally.

I then backed this up because it is the one I am going to use.
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

Yeah, I did some testing too, I wanted to see if I could get using uuid working correctly. I made some test menu entry scripts for our faithful update-grub. This one, called 16_testuuid:

Code: Select all

#!/bin/sh -e
cat << EOF
menuentry "Linux Mint 7 Gloria XFCE (test) search --fs-uuid blah --set root" {
	search --no-floppy --fs-uuid a3a25440-b94a-4210-88c6-6b8ab6909e61 --set root
        linux   /vmlinuz-2.6.28-11-generic root=UUID=e1ada851-ee73-46ce-8f43-58043b9ce21a ro quiet splash
        initrd  /initrd.img-2.6.28-11-generic
}
EOF
Nor: 17_testXP

Code: Select all

#!/bin/sh -e
echo "Adding Win XP to grub2 menu"
cat << EOF
menuentry "WinXP64 (test UUID)" {
	search --no-floppy --fs-uuid 06e4a1eae4a1dbe5 --set root
	chainloader +1
}
EOF
Didn't work with the version of grub2 I had installed from Gloria. It would totally go crazy and say can't find module linux, or chainloader, or even "unknown switch --nofloppy".

So, I went into Helena, checked the version of grub-pc (it's the latest, 1.97beta4 or something), mounted my Gloria installation under /mnt (/, /boot, /home, /usr), did some binding per the Ubuntu grub2 wiki, and did an sudo grub-install --root-directory=/mnt /dev/sda (/dev/sda is my first HDD seen by BIOS, so I use it).

OK, NOW those menuentries work! Search has been fixed.

Now to do one quick edit...
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

emorrp1 wrote:As you can see from the attachment, a simple "help search" from the grub command line tells you all you need to know (if you excuse Helena's poor grub wallpaper choice). I agree that labels rock, but UUID does have the advantage of being computer-generated, so useful for default system setups.
Ooops, I did help, but didn't try help search. Thanks!
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

OK these test cases now work, here are the relevant parts of grub.cfg:

Code: Select all

### BEGIN /etc/grub.d/16_testUUID ###
menuentry "Linux Mint 7 Gloria XFCE (test) search --fs-uuid blah --set root" {
	search -n -u a3a25440-b94a-4210-88c6-6b8ab6909e61 -s
        linux   /vmlinuz-2.6.28-11-generic root=UUID=e1ada851-ee73-46ce-8f43-58043b9ce21a ro quiet splash
        initrd  /initrd.img-2.6.28-11-generic
}
### END /etc/grub.d/16_testUUID ###

### BEGIN /etc/grub.d/17_testXP ###
Adding Win XP to grub2 menu
menuentry "WinXP64 (test UUID)" {
	search -n -u 06e4a1eae4a1dbe5 -s
	chainloader +1
}
### END /etc/grub.d/17_testXP ###
Works with Grub1.97beta4 installed to mbr of first HDD.

Note the line:

Code: Select all

linux   /vmlinuz-2.6.28-11-generic root=UUID=e1ada851-ee73-46ce-8f43-58043b9ce21a ro quiet splash
the root=UUID= points to /dev/sdc2 which is root (/), but my /boot is on /dev/sdc1, so that's the uuid I use for my search line. So, the root for grub, is where /boot is mounted, and the root for linux is where the linux kernel lives, namely /.

Labels are nice too, but UUID seems to work now.,,

EDIT: BUT UUIDs can change when partition sizes change? So, I guess labels are preferable. Hmm, there must be a way to set labels on partitions from the command line...

When I boot using these I see a quick message "booting a command list"

@Viking777 nice menuentry for label usage.
viking777

Re: Grub2 "search" line - what is it for?

Post by viking777 »

Hmm, there must be a way to set labels on partitions from the command line...
e2label /dev/sda5 mint8
will label partition sda5 as mint8
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

Thanks! Here is what I settled on for my Gloria XFCE install:

Partial mount output to show separate partitions:

Code: Select all

leafy@Lamiaceae ~ $ mount -l
/dev/sdc2 on / type ext3 (rw,relatime,errors=remount-ro) [gloria_root]
/dev/sdc1 on /boot type ext3 (rw,relatime) [gloria_boot]
menuentry

Code: Select all

menuentry "Linux Mint 7 Gloria XFCE (test) search label & set root" {
	search -n -l gloria_boot -s
        linux   /vmlinuz-2.6.28-11-generic root=LABEL=gloria_root ro quiet splash
        initrd  /initrd.img-2.6.28-11-generic
}
@viking777 I saw this type of thing in the grub.cfg
recordfail=1
if [ -n ${have_grubenv} ]; then save_env recordfail; fi
What does it do?

Thanks,

breaker
viking777

Re: Grub2 "search" line - what is it for?

Post by viking777 »

recordfail=1
if [ -n ${have_grubenv} ]; then save_env recordfail; fi
What does it do?
I've no idea what it does, and to be honest with you this is one of the things that annoys me most about grub2. Because now, unless you happen to know how to write shell scripts you aren't going to know what anything means any more. The control of your system has been taken away from you. Anyone can read and understand a plain text file, very few can read and understand shell scripts or even worse xml files. The preponderance of these files in 'new' Linux is, to my mind, a complete betrayal of one of the central Linux philosophies - user control. We now shuffle ever closer to the Windows model of 'here be dragons' - don't touch. With this comes the inevitable consequence that perplexed users will download any tool they can find to manipulate these unintelligible files and in the process leave their systems open to attack from unscrupulous sources - again the windows model.

I am realistic enough to know that not everything can be written in plain text, but anything that a user might have reason to interact with - any menu for one thing, including a boot menu - should always be written that way. Linux in general and Mint in particular have made great strides in usability in recent times and to turn back the clock to the era where files are only intelligible if you have skills in programming and scripting languages is the worst backward step it has made in all the years I have been using it.
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

The answers are out there, but I see your point. Not everyone wants to spend that much time learning about a bootloader. In fact, for a while, I used the Windows 7 loader to multiboot (and got rid of windows 7), but it didn't want to chainload the pbr file for OpenBSD.

There are probably some valid reasons the GRUB developers went the way they did, but hopefully it will all work out by the time it's not beta. Although, the old GRUB never left beta, lol.
MountainX

Re: Grub2 "search" line - what is it for?

Post by MountainX »

THANK YOU!!! Thank you viking777 for asking the exact questions that were on my mind, and thank you Linux Mint forum for such great answers.

I wanted to use labels and I couldn't find help anywhere else.

Before I got here I posted over on ubuntuforums: http://ubuntuforums.org/showthread.php?t=1529777
MountainX

Re: Grub2 "search" line - what is it for?

Post by MountainX »

My system is booting successfully using labels now. :)

So what happens on the next kernel update? Seems like we are going to need to edit those /etc/grub.d/ files so that a label-based grub.cfg is generated.

Has anyone figured out how to do that?
MountainX

Re: Grub2 "search" line - what is it for?

Post by MountainX »

MountainX wrote:My system is booting successfully using labels now. :)

So what happens on the next kernel update? Seems like we are going to need to edit those /etc/grub.d/ files so that a label-based grub.cfg is generated.

Has anyone figured out how to do that?
I got a solution for this here:
http://ubuntuforums.org/showthread.php? ... ost9585964

I'll post it below so this thread is complete. This thread is the one that really took me a long way toward the solution, so it should contain the complete discussion. Here's what I did with the help of drs305 and others:

I hope others comment in regard to whether all these steps are necessary and accurate. But this IS WORKING for me.

1. changed /etc/defaults/grub
uncommented this line and used quotes around true:

Code: Select all

GRUB_DISABLE_LINUX_UUID="true"
2. changed /etc/grub.d/10_linux as follows:

Code: Select all

auto_label="`e2label ${GRUB_DEVICE_BOOT} 2>/dev/null`"
  linux_root_device_thisversion="LABEL=${auto_label}"
  cat << EOF
	linux	${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
EOF
3. changed /usr/lib/grub/grub-mkconfig_lib as follows. I know this code could be improved by including it in a proper conditional, but I didn't know how to do that. So it just executes always.

Code: Select all

  # If there's a filesystem UUID that GRUB is capable of identifying, use it;
  # otherwise set root as per value in device.map.
  echo "set root='`${grub_probe} --device ${device} --target=drive`'"
  #if fs_label="`${grub_probe} --device ${device} --target=fs_uuid 2> /dev/null`" ; then
    #echo "search --no-floppy --fs-uuid --set ${fs_uuid}"
  #fi
auto_label="`e2label ${device} 2>/dev/null`"
echo "search --no-floppy --label ${auto_label} --set root"
Those changes generate perfect (as far as I'm concerned) stanzas in grub.cfg. And my test machine boots as expected.

Code: Select all

menuentry "Linux Mint 9, 2.6.32-21-generic (/dev/sda1) -- recovery mode" --class linuxmint --class gnu-linux --class gnu --class os {
	recordfail
	insmod ext2
	set root='(hd0,1)'
	search --no-floppy --label my_label --set root
	echo	'Loading Linux 2.6.32-21-generic ...'
	linux	/boot/vmlinuz-2.6.32-21-generic root=LABEL=my_label ro single 
	echo	'Loading initial ramdisk ...'
	initrd	/boot/initrd.img-2.6.32-21-generic
}
my /etc/fstab also uses labels. Here's an example (although this exact example is from another machine)

Code: Select all

LABEL=home_volume       /home   ext4    noatime,acl,user_xattr 1 2
LABEL=root_volume       /       ext4    noatime,acl,user_xattr 1 1
LABEL=swap_volume       swap    swap    defaults 0 0
LABEL=boot_volume       /boot   ext2    noatime,acl 1 2
breaker

Re: Grub2 "search" line - what is it for?

Post by breaker »

Nice! Now that's handy, thanks for sharing!
Locked

Return to “Installation & Boot”