Monday, April 15, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1094  / 1 Year ago, sun, april 2, 2023, 12:02:54

I am having Ubuntu 12.04 minimal installed on my USB pen-drive; which I use on several systems as portable OS.

I was thinking to edit the boot parameters so that before the main Ubuntu OS is loaded, it shows an option to Boot from Hard Drive for 5 seconds, and then boots to Ubuntu.

This way i'll be able to have an option to boot to the OS installed on the Hard-Drive, without removing or unplugging my ubuntu USB stick.

How do I edit the boot parameters to achieve this?

More From » 12.04


This is actually a much more complicated scenario than it would seem at first, due to the annoying tendency that almost all BIOSs have of reordering how they present drives such that the drive you booted from is always the "first" drive, and that is also what some bootloaders (like the Microsoft's) expect (i.e. things won't work correctly if you try to chainload into Windows if it appears that the drive containing Windows is not the "first" as listed by the BIOS). You don't get this problem when booting from CD because CDs are treated separately from hard drives and don't change the order. There is also the fact that there may be more than one internal drive to choose from, so you should be able to select which of them you want to boot from in that case.

To add to this Ubuntu has a change to grub-mkconfig such that the GRUB_TIMEOUT setting in /etc/default/grub is ignored unless another OS is detected (with the idea being that if you don't have another OS, your computer should boot faster by not waiting on the grub menu unless you hold the shift key). The below solution handles all of these problems and I hope has enough comments that it's clear what's being done.

Run gksudo gedit /boot/grub/custom.cfg and copy and paste the following into it :

# Set grub's timeout to 5 secons. By setting it here we are overriding any
# settings for the timeout in /etc/default/grub. This is to be sure that we get
# a five second timeout even if Ubuntu's grub-mkconfig thinks it's the only
# Operating System and disables showing of the menu.

insmod regexp

# Grab just the drive portion of $prefix, to determine what drive we booted
# from.
# The third parameter in the following command is a regular expression which
# says to capture just the "hdX" portion of a prefix like
# "(hd1,msdos5)/boot/grub". Note that the parentheses in the regular expression
# denote what needs to be captured (like in perl and most other regular
# expression engines), they're not the parentheses that denote a device in grub.
regexp --set=current_drive '(hd.)' "$prefix"

# Loop through all drives (but not partitions)
for drive in *; do

# If the drive is the same as the one we're booted from just continue on to
# the next without creating a menu entry for it.
if [ "$drive" = "(${current_drive})" ]; then

# Make a menu entry with the device name of the drive in the title
menuentry "Chainload $drive" "$drive" {

# Swap the drive currently designated as (hd0) with the drive we want to
# chainload. Thus the drive we are about to chainload will appear to be the
# first drive to the bootloader we chainload.
drivemap -s "(hd0)" "$drive"

chainloader +1

Then just save the file and you're done.

When you boot, if there are other drives available to be chainloaded into you will see menu entries for them (and if there aren't, you won't). The default menu entry will remain unchanged, which means that Ubuntu should boot by default and you will have a 5 second timeout before that happens. Since you're editing /boot/grub/custom.cfg rather than /etc/grub.d/40_custom you don't even need to run update-grub.

[#38156] Sunday, April 2, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 114
Total Questions: 130
Total Answers: 101

Location: Cook Islands
Member since Sat, Oct 16, 2021
3 Years ago
atchcommo questions
Fri, May 13, 22, 08:22, 2 Years ago
Wed, Mar 9, 22, 20:14, 2 Years ago