GRUB: Boot another OS once

From Sidvind
Jump to: navigation, search

Description[edit]

If you like me have GNU/Linux as the primary OS and a Windows system for gaming you sometimes have to reboot the system and wait for grub to start. Once grub has started you have a couple of seconds to change to Windows or GNU/Linux will boot. As I often don't feel like waiting while the system reboots I do something else and misses grub and have to reboot again. This guide will tell you how to make grub start windows upon the next reboot only and how to use this method to do other similar tasks like toogle OS upon restart.

As I've understood some distributions (e.g. opensuse and other rpm-based) ship a tool called "grubonce" which "should" replace grub-set-default (method 2), however it is not part of grub and is merely a wrapper script. It does the same thing as "echo --once | grub --batch" except method 1 works on all grub installations.

Method 1 (preferred)[edit]

In some newer versions of GNU/Linux, there is no /sbin/grub-set-default (eg. Debian 3.1, Fedora Core 4,5). While some distributions like Gentoo still has /sbin/grub-set-default, this method is preferred. With this method you don't need to edit /boot/grub/menu.lst, or anything else either.

Code: Updated: Restart system to another OS
[root@localhost ~]# echo "savedefault --default=2 --once" | grub --batch
[root@localhost ~]# reboot

Remember to replace "2" to match your own config!

When you reboot your computer (echo "savedefault --default=2 --once" | grub --batch; reboot), GRUB will bypass (only once) "timeout" and "default" settings from /boot/grub.conf, and boot to your selected OS. After next reboot, GRUB will use "timeout" and "default" settings from /boot/grub.conf, again.

Sample[edit]

File: /boot/grub.conf
#boot=/dev/hdb
default saved
timeout 1
hiddenmenu

# VESA VGA:
# vga=0x317 - 1024x768
# vga=0x31A - 1280x1024

title Fedora Core (2.6.15.7-ViaEpiaSP13000)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.15.7-ViaEpiaSP13000 ro root=/dev/hdb1 quiet vga=0x31A
        initrd /boot/initrd-2.6.15.7-ViaEpiaSP13000.img
title Fedora Core (2.6.17-1.2174_FC5)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.17-1.2174_FC5 ro root=/dev/hdb1 quiet rhgb vga=0x317
        initrd /boot/initrd-2.6.17-1.2174_FC5.img
title Wintendo
        rootnoverify (hd0,3)
        chainloader +1
        makeactive

Method 2 (deprecated)[edit]

This method is deprecated and won't work on some distributions.

Edit the /boot/grub.conf (/boot/grub/menu.lst on some systems) and change "default" to "saved". I'm using the following settings:

File: /boot/grub.conf
timeout 5
default saved
fallback 0

Note: To make this work you have to run /sbin/grub-set-default followed by the entry you wish to use as default before you restart the system.

Changing the default settings from a script[edit]

This is really simple, either just run /sbin/grub-set-default to change or use a small wrapper like this:

Code: Restart system to another OS
#!/bin/bash
grub-set-default 2
reboot

/sbin/grub-set-default creates the file /boot/grub/default containing which OS to start. GRUB can change this from it's shell and thus when the bootscreen appears. Remember to replace "2" to match your own config!

Make GRUB restore to the primary OS[edit]

Edit /boot/grub.conf to run savedefault when starting Windows.

File: /boot/grub.conf
title Wintendo
savedefault 1
unhide (hd0,0)
rootnoverify (hd0,0)
chainloader +1
makeactive

That should do it. Now running the script changes the default entry to 2 (Wintendo) and when GRUB starts that entry it changes the default entry to 1.

Sample: Normally boot GNU/Linux[edit]

File: /boot/grub.conf
timeout 5
default saved
color black/white
splashimage /boot/grub/splash.xpm.gz
fallback 0

title  Gentoo Linux (backup)
root (hd1,0)
kernel /kernel-backup root=/dev/hdb3

title Gentoo Linux
root (hd1,0)
kernel /kernel-2.6 root=/dev/hdb3 quiet video=vesafb:ywrap,mtrr,1024x768-32@70
initrd /fbsplash-linux-1024x768

title Wintendo
savedefault 1
unhide (hd0,0)
rootnoverify (hd0,0)
chainloader +1
makeactive

title=Memtest86
root (hd1,0)
kernel /memtest86/memtest.bin 

Sample: Alternate between two entries[edit]

File: /boot/grub.conf
timeout 5
default saved

title Gentoo Linux
savedefault 1
root (hd1,0)
kernel /kernel-2.6 root=/dev/hdb3 quiet video=vesafb:ywrap,mtrr,1024x768-32@70
initrd /fbsplash-linux-1024x768

title Wintendo
savedefault 0
unhide (hd0,0)
rootnoverify (hd0,0)
chainloader +1
makeactive