Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been trying to find a way to install grub2 to a usb drive, in such a way that I can boot a PC in UEFI mode into Ubuntu, without modifying the UEFI firmware settings in any way or replacing the pre-installed Windows 7 x64 bootloader with grub. Here's the current gpt partition layout of the internal SSD drive as given by gparted, when run from an Ubuntu 13.04 x64 live-dvd session:

/dev/sda1 100mb fat32 /boot (efi system partition)

/dev/sda2 128mb --- /msfres (Windows 7 reserved partition)

/dev/sda3 100gb ntfs (Windows 7 system partition)

/dev/sda4 10gb ext4 (Ubuntu root '/' partition)

So far, I've tried booting my live-dvd in UEFI mode, creating a root partition for Ubuntu (as seen above), then telling the installer to put grub on a 512mb usb pen drive (/dev/sdi) that I had previously formatted in gparted as follows: 1) create a gpt partition layout; 2) create a single primary partition; 3) format the partition as fat32; 4) set the partition flag as '/boot'.

I instructed the Ubuntu installer to install grub to /dev/sdi1 (the partition I created above -- since the preceding 1mb was unused space). However, when I rebooted the PC, after the completion of the Ubuntu installer, with the usb pen plugged-in, the firmware boot menu detected the pen as an UEFI device but then proceeded to boot Windows 7 -- no Ubuntu or grub menu.

I've tried Google'ing for solutions, and whilst there are many sites that give details on how to dual boot Windows and Linux on UEFI systems, they all assume that you will be able to modify the UEFI firmware settings or replace the Windows 7 bootloader (neither option is available to me, as I don't own the PC).

Therefore, could anyone give me a detailed, step-by-step method of installing grub2 to a usb pen so that I can boot into the Ubuntu partition in UEFI mode, without changing any of the system firmware settings or modifying/replacing the existing Windows 7 bootloader?

The system board is an ASUS Z87-K (bios rev 0412), the SSD is an 120gb OCZ-Vertex3 (firmware rev 2.15)

An update: It turns out that my UEFI firmware DOES have secure boot enabled (because Windows 7 (and other OSes in 'live mode') booted fine, I assumed it was turned off). My apologies if I've been leading people up the wrong path! Since I don't own this PC, I can't turn secure boot off, so I will have to abandon the attempt at an Ubuntu installation for now. Although, according to the Canonical documentation, Ubuntu 12.04.2 and later should be able to boot on a PC with secure boot enabled? Since I can't disable secure boot to narrow-down the problem which is preventing me from successfully booting Ubuntu, I'll leave this question open, but any future installations I attempt will be done on my own hardware so I have the freedom to experiment in greater depth.

Thanks, once again, to all who responded!

share|improve this question
add comment

3 Answers

You're almost there. Removable media like usb will boot from a boot able EFI partition (and that's one bootable EFI partition PER DEVICE, not total), using the bootloacer /EFI/Boot/bootx64.efi (for x64 architectures). Simply copy your /EFI/ubuntu/grubx64.efi to /EFI/Boot/bootx64.efi, and have a copy of grub.cfg in /EFI/ubuntu/grub.cfg and it should boot. Of course, the grub.cfg must be set up to boot the hard disk like you want. Simply copying the grub.cfg off the hard disk's installation will work, but then you will need to recopy every time you update kernels, so you can make the /EFI/ubuntu/grub.cfg a few lines which import the maintained /boot/grub/grub.cfg (like 13.04 does I think). * For Secure boot, use shim.efi for the /EFI/Boot/bootx64.efi and have a copy of the SIGNED grubx64.efi in /EFI/Boot also. grub.cfg is in the same place, /EFI/ubuntu. Your live media uses this mechanism to boot, so if it works, this method will work.

share|improve this answer
    
I mounted the gpt-system partition on the SSD as read-only, copied the contents of EFI/ubuntu/ to my pen drive, then copied EFI/ubuntu/grubx64.efi from the SSD system partition to EFI/Boot/bootx64.efi on my pen drive. First I tried to boot the pen drive using the grub.cfg from the /EFI/ubuntu/grub.cfg located on the SSD system partition, then from the grub.cfg located in /boot/grub/grub.cfg on the SSD's ubuntu partition. Just to be clear, does grub.cfg have to be in /EFI/Boot or /EFI/ubuntu ? –  Paul H Aug 8 '13 at 13:38
    
grub.cfg in /EFI/ubuntu is what worked for me. –  ubfan1 Aug 8 '13 at 15:48
add comment

You cannot set two /boot partitions with boot-flag ! There is only one boot-flag necessary for grub2 in /boot partition - so simply deactivate boot-flag in your partition /dev/sda1

After this activate boot-flag in /boot partition of /dev/sdi - sure ? this is correct ? You have a /boot partition in /dev/sdi ? So there you need to activate boot-flag ( simply with gparted ).

As you say ... grub2 is already installed in /dev/sdi ? If not now follows (necessary because you modified boot-flags) :

sudo grub-install /dev/sdi

sudo update-grub2

Before this all your machine was booting into Windows 7 of /dev/sda3 via /dev/sda1 - but now boot-flag is deactivated and set to /boot partition of /dev/sdi - and your machine should now boot to installation of your usb-drive.

Only in case that in UEFI-mode your machine is not able to boot into usb-drive - then you should disable UEFI-mode in BIOS-settings and enable there EFI-mode instead.

share|improve this answer
    
Whilst your solution appears simple, that would involve modifying (even in a small way) my gpt-system partition, which, since the PC in question is not mine, I am not allowed do. –  Paul H Aug 8 '13 at 13:37
add comment

There is no such thing as a "/boot flag." GParted and parted do have a "boot flag" (no leading slash), but this terminology is confusing at best. The GParted/parted "boot flag" is really a way to set the type code on a partition to identify it as an EFI System Partition (ESP), from which the firmware reads boot loaders. Contrary to what dschinn1001 seems to be saying, you should not set the type code for your Linux /boot partition to the ESP code. It's legal to have multiple ESPs on a single disk, although Windows is allergic to such setups, so I recommend against it. The ESP must have a FAT filesystem (technically FAT32, although FAT16 often works in practice), so unless your Linux /boot partition uses FAT, you should not set the GParted/parted "boot flag" on the /boot partition.

Installing GRUB on a USB flash drive is technically possible, but it's a bit limiting. The problem is GRUB's hard-coded configuration, which gives you two options:

  • You can build GRUB in such a way that it looks for grub.cfg in the same directory as the grubx64.efi binary (that is, on the USB flash drive). This will normally work on just one computer, though, since grub.cfg contains all sorts of system-specific details.
  • You can build GRUB in such a way that it looks for grub.cfg on your hard disk. This may work on more computers, but it will require that each computer have a working grub.cfg file on it, and in the same location on every system. Thus, it won't work on all computers, just those that are configured in a similar way.

Ubuntu builds its grubx64.efi binary to work in the second way. Note that ubfan1's procedure assumes the first method of building GRUB, so it will work only with a custom-built GRUB binary (or a GRUB binary taken from Fedora or some other distribution that works in this way). OTOH, ubfan1's procedure might work in the second way if the systems are properly configured -- but then copying grub.cfg to the USB flash drive will be pointless.

If you're looking for a general-purpose emergency boot disk, GRUB isn't really the best solution -- at least, not unless Super GRUB 2 Disk comes out with EFI support or somebody else builds something similar. At the moment, the best option for an emergency EFI boot disk is my rEFInd boot manager. This program builds a list of other EFI boot programs on a computer, enabling you to boot any of them. The result is that you'll be able to boot from any boot loader (GRUB, ELILO, the Windows boot loader, etc.) that's installed on the hard disk, even if you've lost your EFI NVRAM entries. The boot loader must be properly configured, though.

Because Linux kernels since version 3.3.0 include built-in EFI boot loaders, rEFInd can launch them directly, too. The catch is that rEFInd needs to figure out what options to pass to the kernel -- most importantly, what your Linux root filesystem is. rEFInd can figure this out if the kernel is in the root filesystem (that is, if there's no separate /boot partition) or if a rEFInd-specific configuration file (refind_linux.conf) exists in the same directory as the kernel. Ubuntu normally doesn't use a separate /boot partition, so rEFInd on a USB flash drive or CD-R can usually launch Ubuntu directly, and should be more flexible and easier to set up than GRUB for emergency use. (The rEFInd page includes download links for CD-R and USB flash drive images.) If you've got a more complex setup and you happen to remember the name of your root filesystem (or any other kernel options you need to boot), you can edit or add them by hitting F2 or Insert twice, which opens a line editor so that you can modify the kernel's options.

share|improve this answer
    
I used win32imgwriter to write the rEFInd .img file to my pen drive and then attempted to boot. No success. The UEFI firmware boot menu prefixes my usb-pen device name with 'UEFI' but when I select it, Windows 7 loads. Perhaps the UEFI firmware doesn't like the presence of 2 efi-system partitions? The fact that rEFInd didn't boot suggests the UEFI firmware, when presented with 2 or more gpt-system partitions, is defaulting to the one on the SSD and ignoring anything else, especially since my Ubuntu live session WILL boot in UEFI mode, but the usb-drive it is stored on has an mbr layout. –  Paul H Aug 8 '13 at 13:36
    
It's at least as likely that the Windows utility didn't write the image correctly. Try a Linux session and use dd, as in dd if=refind-flashdrive-0.7.2.img of=/dev/sdc (or whatever the USB flash device is). Multiple ESPs are explicitly permitted by the EFI spec. Although Windows has problems with multiple ESPs, any such problem in the EFI itself (and especially one that prevents use of an ESP on a removable disk) would constitute a very serious bug. –  Rod Smith Aug 8 '13 at 16:23
    
@RodSmith - my answer above is wrong then ? should I delete it ? I was thinking that machine can only decide one way to boot ? only there where boot-flag is set, machine knows then, which partition to mount next ?! - I have not said that there is /boot-flag - was writing about boot-flag. I know of earlier installation at my machine, that OS (Linux too ! ) cannot decide between two boot-flags, which one to follow. You would need knoppix then to set one single boot-flag. –  dschinn1001 Aug 8 '13 at 18:27
    
Somebody referred to a "/boot flag," but it's been edited away -- I think it was in the original question. As to the rest, multiple ESPs are definitely legal. (The term "boot flag" in reference to GPT is specific to libparted-based tools and so is not general. If you use gdisk or another OS to partition, "boot flag" will be meaningless.) An EFI-based computer decides on the boot order based on NVRAM variables set via the efibootmgr utility (in Linux; or various other tools in other OSes). The EFI/BOOT/bootx64.efi file is a ballback loader in case this fails. –  Rod Smith Aug 8 '13 at 23:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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