1

In our system there are two Ubuntu operating systems installed.

At boot time GRUB asks which Ubuntu to boot from, but now I have to add a new menu item into GRUB and that menu item will allow user to change default Linux to boot.

enter image description here

In picture above, I have to add third menu item, this menu allows user to select new default boot operating system. If user selects "Set Linux 2 as default", next time on reboot boot order will be like in picture below.

enter image description here

I found documentations about changing boot order but those documents describe the process within Ubuntu, I want to achieve this within GRUB itself. How can I do that?

2

We can achieve this using Grub's Environment-block. The Environment-block is stored in /boot/grub/grubenv and this file is a bit special, it has a preallocated size of 1024 bytes. We should not edit this file with a text-editor even if you can open this file with a text-editor to display it's content.

We can edit /boot/grub/grubenv with different tools:

Terminal-command sudo grub-editenv [OPTION...] FILENAME COMMAND, see also man grub-editenv

Menu-entry-command save_env [-f file] var, see details in GNU Grub Manual

If the file /boot/grub/grubenv does not exit, you can create it with

sudo grub-editenv /boot/grub/grubenv create

We will have to create some custom menu-entries, https://help.ubuntu.com/community/Grub2/CustomMenus provides helpful information how to do that. Edit /etc/grub.d/40_custom so that it looks like this:

#!/bin/sh
exec tail -n +3 $0
# 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.
#
submenu 'Set boot default' {
    menuentry 'Set first entry as default' {
            set saved_entry=0
                save_env saved_entry
                reboot  
    }
    menuentry 'Set second entry as default' {
        set saved_entry=1
                save_env saved_entry
                reboot
    }
}

If we already have custom menu-entries in the file, we just append the lines after the comment lines to our file. The commands in the menu-entries act as follows:

set saved_entry    set the environment variable saved_entry to the specified value.
Save_env saved_entry    stores the variable saved_entry in /boot/grub/grubenv.
reboot                  reboot the machine

In addition we will need to edit /etc/default/grub which holds the basic Grub-configuration. Again, in the GNU Grub Manual we will find detailed information.

In /etc/default/grub we have to change the value for GRUB_DEFAULT to GRUB_DEFAULT=saved. This will force Grub to load the default-menu-entry from /boot/grub/grubenv when we boot. If you find a line with GRUB_SAVEDEFAULT=true, remove this line.

To apply the changes we made to Grub's main configuration-file /boot/grub/grub.cfg we have to run sudo update-grub.

As next step we run sudo grub-editenv - set saved_entry=0 in a terminal. We do that to avoid conflicts, when Grub boots next time, Grub will look up for saved-entry in /boot/grub/grubenv and is able to find it.

Now we can reboot, when the Grub-menu appears, we will see the added submenu in the list, we open it and select one of the entries. Grub will set saved_entry to the specified value, save it to /boot/grub/grubenv and then reboot. Grub-menu appears again and we will see, that the new default is selected automatically.

One more thing to think about

In which operating system we have to make these changes? We have two Linux-distributions installed, and both ship with Grub. With the information from a Boot-Info-summary and the terminal-command findmnt we should be able to identify the distribution which holds and controls the configuration files. In this distribution we have to perform the above steps then. In the other distribution Grub is not needed and should be removed or even purged, just to avoid any kind of conflicts.

Additional Information:

Tested this on my system with Grub version 2.02~beta2-36ubuntu3.8.

0

cd /etc/default/

vim grub

change the GRUB_DEFAULT=0 to the one which you want to.

OR

cd /boot/grub

vim grub.cfg

here be careful while editing

Rearrange the functions of menuentries ,and you'll be able to see that on boot loader.

  • if you read the question I am asking how change the order within grub itself. I am asking how to create a new menu item in grub and within that menu I want to be able to change boot order. – zenprogrammer Mar 22 '17 at 7:08
  • Editing /boot/grub.conf is not a good idea. A mistake might leave your system unbootable. The changes you make in grub.conf will be overwritten whenever you run update-grub. For example, this will be the case when an update installs a new kernel. – mook765 Mar 22 '17 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.