3

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?

10
  • I have gone through this ( dedoimedo.com/computers/grub-2.html ) aswell, however it didn't proved useful. Hope someone may come with some brilliant idea to have this on installed ubuntu. This is possible when using live media CD, however i was unable to reverse engineer that as well. May 25, 2012 at 9:47
  • 1
    Added a bounty to it, be prepared to check out solutions, ping me anytime for anything or if someone has a working solution that you like (so I can reward the bounty). Jun 1, 2012 at 12:16
  • @BrunoPereira, I am really thankful to you. Since i am new; I don't know how to revert back to you. Are these comments only way to communicate or some other way do exist? Jun 1, 2012 at 12:24
  • 1
    What (and where) is the operating system on the hard disk? Do you have grub2 already set up on your usb stick to load Ubuntu from the stick? Jun 2, 2012 at 14:17
  • 1
    @Z9iT np, if you need anything else just ping ;) Jun 6, 2012 at 9:36

2 Answers 2

4
+150

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.
timeout=5 

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
    continue
  fi

  # Make a menu entry with the device name of the drive in the title
  menuentry "Chainload $drive" "$drive" {
    drive="$2"
    root="$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
  }
done

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.

8
  • Thanks for sharing this.. Even tough the comments are pretty sufficient to explain what is going in the background, I would like to exclaim something. The requirement is that, whenever Ubuntu will boot up.. It will halt for 5 seconds and ask weather to boot to Ubuntu or boot from the physical Hard Disk having whatever OS weather windows/MAC or linux. Kindly assume that the BIOS boot order is 1)CD_Drive 2)USB_Device 3)HDD Jun 5, 2012 at 13:08
  • I wonder which would be more safe and fail_safe workout. Either this option or as answered by virpara Jun 5, 2012 at 13:13
  • "The requirement is that, whenever Ubuntu will boot up.. It will halt for 5 seconds and ask weather to boot to Ubuntu or boot from the physical Hard Disk having whatever OS weather windows/MAC or linux." Jun 5, 2012 at 19:45
  • 1
    Sorry, I accidentally hit Enter to try to add a newline to my comment, what I meant to write was this: "The requirement is that, whenever Ubuntu will boot up.. It will halt for 5 seconds and ask weather to boot to Ubuntu or boot from the physical Hard Disk having whatever OS weather windows/MAC or linux." That is what my solution does. The way that it "asks" is by providing a standard grub menu. How do you want it to present this option instead? As far as which solution is better, virpara's answer won't actually work and mine does. Jun 5, 2012 at 19:52
  • @Jordan Uggla: Would SYSLINUX or EXTLINUX provide for an easier solution to what the OP is looking for? You obviously know more about this than I do. What do you think?
    – Chris
    Jun 6, 2012 at 3:02
2

1.

boot into ubuntu from USB. Open terminal.

2.1 Add entry to boot Hard drive

gksudo gedit /etc/grub.d/40_custom

append below entry. Do not remove any line.

menuentry "Boot From Hard disk" {
    set root=(hd0,1)
    chainloader +1
}

2.2 Change default entry to boot

gksudo gedit /etc/default/grub

change GRUB_DEFAULT=0 to GRUB_DEFAULT=X here X is number of entry to boot minus one. If you want to boot 3rd entry in bootloader then X should be 2 (grub counts entry from zero ).

Save it.

2.3 Change timeout to boot into default os

gksudo gedit /etc/default/grub

change GRUB_TIMEOUT=30 to GRUB_TIMEOUT=X here X is number of seconds to wait before boot into default os. (I have set it 3 seconds)

3.

sudo update-grub

4.

restart and select Boot From Hard disk

4
  • It would be useful only if it prompts for "Boot from first hard disk" and if no response is given in 5 seconds, then ubuntu will be loaded from USB stick. The above answer sounds good, however the key requirement of 5 seconds is omitted in that.. Jun 4, 2012 at 13:05
  • @Z9iT 5 sec delay before the default OS boots already described in 2.3 Jun 4, 2012 at 13:39
  • 1
    The menu will display both Boot from first hard disk and the Ubuntu entry installed by grub2, with the selected default highlighted. After the timeout, grub2 will proceed with the default set above. I think that is probably as close as you will get to what is described. Jun 4, 2012 at 15:19
  • 2.1: I personally prefer to use /boot/grub/custom.cfg. It doesn't require running update-grub afterward, doesn't break update-grub if you accidentally add code with a syntax error, and doesn't require any header (just put the code you want into the file). set root=(hd0,1) is wrong because A: (hd0,1) is a partition, not a drive and B: When booted from USB (hd0) will be the USB drive, not the internal drive. 2.3: While this is fine for upstream grub and other distros using grub (like Debian) Ubuntu's grub-mkconfig will ignore GRUB_TIMEOUT if it thinks it's the only OS. Jun 5, 2012 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .