0

I have dual boot Ubuntu and Arch, and I think there must be a way to set the default boot entry for GRUB to boot second OS, e.g., when I reboot from Arch it will boot to Ubuntu, and vice versa. Maybe it’s possible by creating a new grub entry, but I don’t know how to check previous boot entry, and so select another one.

2 Answers 2

1

You can write a systemd script (a service) which checks what system are you currently on (e.g., by checking if there is a lsb_release command -- it should be in Ubuntu, but not in Arch). Then check the contents of the GRUB configuration file (/boot/grub/grub.cfg see this wiki page on GRUB). Then, based on which system are you on, you switch the entries in the GRUB config. There are several ways to do that. The not recommended way is to "manually" (i.e., with the script/service) edit the main configuration file. Theoretically you should not do that, because it is really easy to make a mistake, which will make it impossible for you to boot to any system. The recommended way to edit GRUB entries is to use custom entries, but handling them from scripts (or command line in general) can be a bit tricky.

There are of course some pitfalls in the script/systemd approach (e.g., you should in fact has two of such scripts: one on Arch and the other on Ubuntu, both implementing the same functionality), but it is as close at is gets to the functionality you want. By the way, you wrote:

I don’t know how to check previous boot entry and so select another one

I highly recommend to you reading through the Arch Linux wiki on GRUB -- several times. It helped me a lot to finally understand how GRUB works. I admire people who created this documentation, and I'd say that it is a magnificent piece of instruction, one of the best on the web (as an example of a wiki page).

Just to sum up, there is no simple way to achieve what you wanted in your question, but on the other hand, where is the fun in "simple" solutions? :) Good luck, happy coding, and let me know if you will need some more advice.

1

In modern versions of GRUB, there are two commands you could use for this purpose: grub-set-default and grub-reboot.

grub-set-default will persistently change the default boot entry, but it requires having GRUB_DEFAULT=saved in /etc/default/grub (and updating the actual GRUB configuration with the generic grub-mkconfig or its Debian/Ubuntu-specific wrapper update-grub to make the change take effect).

grub-reboot specifies an one-time boot target for the next boot only. Despite the name, it won't actually reboot the system.

Both of these will take either a number, a menu item title, or a menu item identifier as an argument. If the desired menu item is in a submenu, you would specify the name/identifier of the main menu item, the > character, and the name of the sub-menu item.

For example, on Debian the first (and the normal default) menu item is usually named Debian GNU/Linux, and the second menu item is titled Advanced options for Debian GNU/Linux, and it opens a sub-menu of currently installed kernels. For each kernels, there are two entries in the sub-menu: Debian GNU/Linux, with <kernel version> and Debian GNU/Linux, with <kernel version> (recovery mode).

To boot with the previous kernel version in normal mode once, you could do:

grub-reboot 'Advanced options for Debian GNU/Linux>Debian GNU/Linux, with <kernel version>'

You can see if the grub-reboot command has been used in the current boot cycle by running grub-editenv list and seeing if the next_entry= variable has a value or not. If grub-reboot has never been used, the variable might not exist at all.

Likewise, if there is a saved_entry= variable, then the grub-set-default command has been used; but unlike next_entry=, that value won't be automatically removed after one boot attempt.

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.