How to add a GRUB2 menu entry for booting installed Ubuntu on a USB drive?

147,438

Solution 1

I found out how to use the UUID of the drive, useful if you have multiple drives plugged in at boot time. Credits to oldfred for his note about /vmlinux and /initrd.img symlinks.

Add this to the file /etc/grub.d/40_custom, replacing UUID=XXXX-YYYY with the partition UUID (get UUID with command blkid)

menuentry "Boot from USB Drive" {
    set root=UUID=XXXX-YYYY
    linux /vmlinuz root=UUID=XXXX-YYYY ro quiet splash
    initrd /initrd.img
}

Solution 2

Each time you upgrade kernel on external, you can run this to update boot stanza in grub on the internal drive.

sudo update-grub

But you can also add a boot stanza to grub2's 40_custom that boots the partition, not the specific kernel. Ubuntu installs links in / (root) to boot the most recent install. Adjust example below if necessary to your drive & partition. Boot drive with grub is always hd0, but then other drives are in BIOS reported order which may vary.

Edit with:

gksudo gedit /etc/grub.d/40_custom

then, add:

menuentry "Install on sdb1" {
    set root=(hd1,1)
    linux /boot/vmlinuz root=/dev/sdb1 ro quiet splash
    initrd /boot/initrd.img
}

Update: older installs put links in /, but newer ones now use /boot. Changed example to show /boot

While above works, I find the drive may change when plugging in a flash drive or any other USB device. So I am converting to use labels.

menuentry "Cosmic 18.10 on sdb12 test" {
    search --set=root --label cosmic_b --hint hd2,gpt12
    configfile /boot/grub/grub.cfg 
}

Solution 3

To boot from Ubuntu Live USB menu entry in /etc/grub.d/40_custom should look like that (Replace DRIVE_UUID with your partition's uuid):

menuentry "Boot from LIVE USB Drive" {
   search --set=root --fs-uuid DRIVE_UUID
   linux ($root)/casper/vmlinuz boot=casper quiet splash --
   initrd ($root)/casper/initrd.lz
}

To apply changes run:

sudo update-grub
Share:
147,438

Related videos on Youtube

kiri
Author by

kiri

♫ I'd love to change the world But I don't know what to do So I'll leave it up to you ♫

Updated on September 18, 2022

Comments

  • kiri
    kiri over 1 year

    I am not asking how to boot the LiveCD from a USB.

    I have 2 Ubuntu installations, one on my computer's internal hard drive and another on a USB drive.

    Is there a way to add a GRUB2 menu entry (to the GRUB on my internal hard drive) to boot the Ubuntu OS which I have installed to the USB flash drive and have this same menu entry still work after I've upgraded the Linux kernel on the USB installation?

  • kiri
    kiri over 10 years
    Is there a way to use the partition's UUID rather than /dev/sdb1?
  • matanster
    matanster over 9 years
    how should you figure what should be in place of (hd1,1)? where do you take these numbers from so that they fit? I may need this for setting straight a different problem: askubuntu.com/questions/573502/…
  • oldfred
    oldfred over 9 years
    The first number is the drive as reported by BIOS. But boot drive is always hd0, so drive order can change. Then second number is partition number. Where sda3 is hd0,3 if you boot from the drive that is sda. But if you boot from sdb, then it may be hd1,3. Sometimes you just have to experiment.
  • superl2
    superl2 about 9 years
    sudo update-grub works with adding windows entries as well
  • Demis Palma ツ
    Demis Palma ツ over 8 years
    This is definitely not the answer to the question above. You should have accepted oldfred's reply.
  • Lennart Rolland
    Lennart Rolland almost 6 years
    @kiri: It is considered bad practice to copy someone else's answer and give yourself credit for it. Yes, even if you added the UUID part.
  • v3l0c1r4pt0r
    v3l0c1r4pt0r almost 5 years
    This almost worked for me. I had to wrap whole menuentry snippet with cat<<EOF at the beginning and EOF after newline at the end. This is because grub.d scripts are evaluated as bash scripts, writing to stdout
  • xpt
    xpt over 2 years
    "So I am converting to use labels", that's what I'm looking for. So what is the cosmic_b in your demo code? Is it a label of GPT partition? thx.
  • xpt
    xpt over 2 years
    Oh, it is label of an ext4 file system created by tune2fs -L cosmic_b /dev/sdxx?
  • oldfred
    oldfred over 2 years
    I usually use gparted or Disks to assign labels. Yes tune2fs or e2llabel. And gdisk for gpt labels. Note that now with gpt there are two labels. One is the filesystem and the other is the gpt partition's label. askubuntu.com/questions/147319/… You can see labels: lsblk -e 7 -o name,fstype,size,fsused,label,partlabel,mountpoint