2

Please provide step by step instructions for making a simple hand made Persistent USB that boots both BIOS and UEFI. No ISO9660 or ISO Booters please, I want to be able to easily modify the filesystem.

C.S.Cameron
  • 19,519

2 Answers2

3

Simple Hand Made Persistent USB

Making a Grub2 booter that uses Persistent partitions is easy.*

  • Boot Installed Ubuntu or Live USB.
  • Insert Target USB.
  • Start GParted.
  • Create a GPT partition table on Target USB.
  • Create a 1MB BIOS boot partition on the left, formatted as unformatted.
  • Add a 300MB FAT32 EFI boot partition next to it.
  • Add an ext4 root partition large enough for the Ubuntu ISO's contents, (~ 3GB for 20.04).
  • Create an ext4 partition labeled casper-rw
  • Add a NTFS data partition if desired.
  • Apply all operations.
  • Flag partition 1 bios_grub.
  • Flag Partition 2 boot, esp
  • Close GParted.

gparted

  • Open the ISO's folder as Administrator, open the ISO using Archive Manager.
  • Extract the ISO's contents to the Target's root partition.
  • Drag and drop the boot and EFI folders from Archive Manager window to the EFI boot partition.
  • Edit sdx2/boot/grub/grub.cfg adding set root=(hd0,3) after the first line that starts with menuentry.
  • Add a space and the word "persistent" after ---.

grub.cfg

  • Install grub, if in BIOS mode or booted from USB drive run:

      sudo mount /dev/sdx2 /mnt
    

    sudo grub-install --boot-directory=/mnt/boot /dev/sdx

  • If in UEFI mode on installed system, boot into the Target drive and run the above.

*Booting based on mkusb by sudodus

C.S.Cameron
  • 19,519
  • Will this singöe grub-instlall command make it boot both in BIOS mode and UEFI mode, so that it is enough to 'copy boot and EFI folders to to the EFI partition'? --In which versions of Ubuntu does it work? Does it work when creating the drive in a system booted in BIOS mode as well as in an installed system booted in UEFI mode? In that case it is a really great step forward :-) – sudodus Apr 15 '20 at 08:27
  • You may want to compare this method not only to mkusb-dus, but also to the extracting method described at this do-it-yourself link. The reason for starting with the comptressed image file is that grub-pc for booting in BIOS mode and grub-efi for booting in UEFI mode cannot be installed alongside each other in installed systems, at least not in older systems, It would be nice if there is no longer such a restriction in the newer versions of Ubuntu. I should check that ... – sudodus Apr 15 '20 at 08:35
  • @sudodus: The only thing new about this is booting the files extracted from the ISO file rather than an ISO9660 clone or an ISO file. Got into a bit of an argument with Akeo, this was sort of proof of concept. So far seems to work with Ubuntu 18.04 and 20.04 Beta. I am still testing. I am pretty sure it does not matter if it is created in BIOS mode or UEFI mode. Things are just copied to the disk, not installed. I have been using a similar mkusb hack for installed systems for ages. – C.S.Cameron Apr 15 '20 at 08:44
  • I think one crucial point is if when booted into an installed system in UEFI mode, you can create a BIOS bootlloader. I don't think it is easy in 16.04 LTS or early 18.04 LTS. It is great if your method works in up to date 18.04.x LTS systems and newer versions (of course including Focal Fossa). that means that we can gradually make mkusb independent of compressed image files (which makes FOSS gurus happy). – sudodus Apr 15 '20 at 08:51
  • Thanks @Gryu, sometimes my Code highlighting does not work. – C.S.Cameron Apr 18 '20 at 05:46
  • Just add eight spaces instead of four for codes in marked lists. You could also see history of changes by clicking edit link – Gryu Apr 18 '20 at 05:55
  • 1
    @Sudodus: I have had 100% success rate using above revised instructions with 18.04, 19.10 and 20.04 Beta building drives in BIOS and UEFI modes. Note: grub should be reinstalled to sdx2, not sdx3. extraction works best when ISO folder is opened as root, (ie sudo nautilus). I have not had to mess around with image file. – C.S.Cameron Apr 20 '20 at 05:06
  • @C.S.Cameron, After you added "Insert and boot Live USB", I understand what you are doing and that it works :-) It is also my experience that in a live or persistent live system, you can create a BIOS bootloader also when booted in UEFI mode. The problem is when booted into an installed system in UEFI mode. So, yes, I agree with you that this method works and I am willing to recommend it for people who want to use basic tools (instead of installing mkusb or some other dedicated tool for creating persistent live USB drives). – sudodus Apr 20 '20 at 06:43
  • @sudodus: In that case, my last five days of blood, sweat and tears have not been in vane. Good luck on finding someone who would prefer using basic tools to mkusb though. – C.S.Cameron Apr 20 '20 at 07:42
  • 1
    Sudo nautilus used above should read sudo -H nautilus. – C.S.Cameron Apr 20 '20 at 13:21
  • @C.S.Cameron, I am currently trying to follow your instructions. I stuck at line "Edit sdx2/boot/grub/grub.cfg adding set root=(hd0,3) after the "Try Ubuntu ..." line." There is no "Try Ubuntu ..." line in boot/grub/grub.cfg in Ubuntu 20.04. What should I do? – user244413 Nov 02 '20 at 17:51
  • @user244413: Make set root=(hd0,3) the first line after the { in the first menuentry. Ubuntu 20.04.1 is different than 20.04, thank you for informing me. – C.S.Cameron Nov 03 '20 at 01:16
1

I tried to combine the methods

I installed a brand new Lubuntu Focal Fossa operating system in UEFI mode. Running that system I created a USB boot drive starting by creating the partition table with gparted instead of cloning from a compressed image file.

  • MSDOS partition table
  • A FAT32 partition where the content of the iso file is extracted
  • An ext4 partition to be used for persistence

It did not work to create a BIOS bootloader out of the box

sudo mount /dev/sdx1 /mnt/sd1
sudo grub-install --boot-directory=/mnt/sd1/boot /dev/sdx

This is what I was afraid of :-(

I could install the program package grub-pc but it was not used, Instead the system complained that it did not work to install for the efi system. So I removed the program package grub-efi-amd64-bin and after that

sudo grub-install --boot-directory=/mnt/sd1/boot /dev/sdx

worked :-)

I re-installed grub-efi-amd64-bin into my installed system to have it 'complete' for UEFI tasks.

The USB pendrive works both in BIOS mode and UEFI mode. I tested with and without persistence via custom made menuentries.


This is not too difficult, but I am not willing to let a tool, that is made for users without much experience remove and reinstall the program package grub-efi-amd64-bin because it is an important part of the boot system. In other words, I will keep the compressed image files in mkusb and in the instructions at this do-it-yourself link.

sudodus
  • 46,324
  • 5
  • 88
  • 152
  • For my tests I used a gpt partition table, not BIOS and extracted the ISO to an ext4 partition, not FAT32. I first tried a FAT32 partition for extraction but that did not work at all. To install grub I used sudo grub-install --boot-directory=/mnt/boot /dev/sdx on BIOS and UEFI computers. So far I have tested it with 16.04, 18.04, 20.04 Ubuntu and Lubuntu, on BIOS and UEFI computers. the only thing the same with each test was using 18.04, (on different computers), to install grub. – C.S.Cameron Apr 15 '20 at 13:28
  • Did you try to create the USB boot drive from an installed system booted in UEFI mode? (My experience is that things work well when creating the USB boot drive from a system running in BIOS mode and a live system booted in UEFI mode. But as I describe in this answer, there are complications to create the BIOS bootloader, when running an installed system in UEFI mode.) So my question is: have you solved that problem? – sudodus Apr 15 '20 at 13:53
  • Most of the work concerns GParted, does that work differently in BIOS or UEFI? The extraction process should be the same in both modes. Editing grub would be identical. That leaves only installing grub, which I did in both BIOS and UEFI modes. I will try the full process tomorrow on the UEFI computer booting from a 20.04 Beta Full install with a zeroed flash drive. – C.S.Cameron Apr 15 '20 at 14:21
  • Sorry. I tried to follow your instructions as exactly as possible while in an installed Lubuntu Focal Fossa system booted in UEFI mode. I had the same problems as described in the answer above. The result was a USB drive that only boots in UEFI mode. I have to fiddle with the grub program packages in order to install the grub bootloader for BIOS mode. – sudodus Apr 15 '20 at 14:54
  • @C.S.Cameron, Coming back to this thread I think we are concerned about different things. You are concerned about making a system from scratch with similar boot and persistence features as mkusb (that it works in all current versions of Ubuntu). I am concerned about making a safe and user friendly system that works in all boot modes and configurations of Ubuntu Desktop for PC computers -- I see a problem to make the BIOS bootloader when running anstalled system in UEFI mode. You are solving the problem to make persistence work with a partition in 18.04 LTS and previous versions. – sudodus Apr 17 '20 at 11:51
  • When I install grub in UEFI it only boots in UEFI but when I install grub in BIOS it boots in both. If I use dus booted in UEFI it boots in both. what is the easiest way to install grub-pc in UEFI? – C.S.Cameron Apr 17 '20 at 13:25
  • 1
    @C.S.Cameron, mkusb uses a compressed image file with bootloaders preinstalled to fix things when running an installed system in UEFI mode. In this answer I describe a way to do it from scratch without any compressed image file: I uninstall grub-efi-amd64-bin in order to make it work to install the BIOS bootloader (and then reinstall grub-efi-amd64-bin to have a fully working system in UEFI mode). I used that method in Focal Fossa (and I used a similar but not exactly the same method when I created the master system for the compressed image file). Use the method that you like best :-) – sudodus Apr 17 '20 at 13:36
  • 1
    This is not a complete answer, but just concerning sudodus' problem of having to remove and the later re-add the package grub-efi-amd64-bin I think as of today, you can avoid having to do that by just adding another flag to the command, like this: grub-install --target=i386-pc... I don't know since when that option is available, maybe it is something new. – marcingo Jun 23 '22 at 20:52