You are not logged in.

#1 2023-05-01 20:49:34

fortuna
Member
Registered: 2021-02-09
Posts: 76

[SOLVED] migrate USB flash drive to UEFI

I am trying to do something that should not be rocket science as it must've been done many times:
adapt a USB flash drive to be used in a UEFI only PC. I am stuck at the installation of GRUB (again).

So the USB flash drive has been booting in a BIOS only laptop successfully for a few years and I don't want to break that.

After creating the EFI system partition using this help page.

user@laptop:~$ sudo gdisk -l /dev/sdb

now gives:

GPT fdisk (gdisk) version 1.0.9.1

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 62530624 sectors, 29.8 GiB
Model: Cruzer Edge     
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 4053BB53-1A95-4F17-8CE1-81A5F3BF2097
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 62530590
Partitions will be aligned on 2048-sector boundaries
Total free space is 3069 sectors (1.5 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        51202047   24.4 GiB    8300  
   2        52289536        62529535   4.9 GiB     8300  
   3        51202048        51234815   16.0 MiB    EF02  
   4        51234816        52289535   515.0 MiB   EF00  

which seems alright. BTW, partition 1 is arch, 2 is home. So on to the next step: GRUB.

This page seems to provide a manual for this, but:

this page on GRUB says:

When installing (GRUB) to use UEFI it is important to boot the installation media in UEFI mode, otherwise efibootmgr will not be able to add the GRUB UEFI boot entry. Installing to the fallback boot path will still work even in BIOS mode since it does not touch the NVRAM.

For now the simple question is: how do you boot a USB flash drive in a UEFI only PC without having installed GRUB first?

Last edited by fortuna (2023-05-05 16:19:26)

Offline

#2 2023-05-01 20:55:43

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,463

Re: [SOLVED] migrate USB flash drive to UEFI

grub-install has a --removable option for installing onto removable disks.

Online

#3 2023-05-03 13:08:29

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

Interesting.

But, how do you boot a USB flash drive in a UEFI only PC without having installed GRUB first?

Offline

#4 2023-05-03 13:25:49

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

By installing it to the stick with the removable option, when you do that then the stick will show up with it's internal name in the bootloader of the PC e.g. "[UEFI] Sandisk USB 2.0" or whatever because that will point to ESP/BOOT/BOOTx64.EFI on the stick -- which will be GRUB if you used the --removable option of grub-install . efibootmgr and UEFI entries are irrelevant when the --removable option is used: https://wiki.archlinux.org/title/Unifie … ble_drives

Last edited by V1del (2023-05-03 13:27:39)

Offline

#5 2023-05-03 13:32:14

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] migrate USB flash drive to UEFI

fortuna wrote:

the USB flash drive has been ... for a few years ... I don't want to break that.

Off topic, but do you recognize how strange these statements are side by side?  Putting a bootable system on a flash drive is fun for tinkering with, and can be useful for recovery.  But they don't last.  If this has already been in use for a few years, it's lasted longer than most flash drives would for such use.  This might not be a concern for an ordinary recovery flash drive (with a simple / replaceable iso system on it), but saying you don't want to break it suggests you have valuable content on it.  This does not seem wise.

Flash drives are disposable.  We should certainly not be in any hurry to break them, but if not wanting to break it is actually a concern, you may have bigger issues.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2023-05-03 15:22:36

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

It's a flash drive with arch I use to backup Windows PC data. So I boot arch in that PC and run my backup script.

Why would I want to break that flash drive arch installation?

Problem is, since the Windows PC was upgraded to a Dell OptiPlex 3000 PC Desktop the flash drive stopped booting. Thanks to UEFI.

What I don't understand is how to install grub if:

When installing (GRUB) to use UEFI it is important to boot the installation media in UEFI mode, otherwise efibootmgr will not be able to add the GRUB UEFI boot entry. Installing to the fallback boot path will still work even in BIOS mode since it does not touch the NVRAM.

and I can't boot the flash drive in the Dell OptiPlex 3000 PC Desktop because UEFI doesn't let me.

Offline

#7 2023-05-03 15:27:30

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,463

Re: [SOLVED] migrate USB flash drive to UEFI

And again:

Scimmia wrote:

grub-install has a --removable option for installing onto removable disks.

'
The first part of that statement is not applicable in this case.

Last edited by Scimmia (2023-05-03 15:32:16)

Online

#8 2023-05-03 15:58:19

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

Maybe this helps:

When installing (GRUB) to use UEFI it is important to boot the installation media in UEFI mode, otherwise efibootmgr will not be able to add the GRUB UEFI boot entry. Installing to the fallback boot path will still work even in BIOS mode since it does not touch the NVRAM.

The --removable option will do the part outlined in bold, the first sentence is irrelevant

Offline

#9 2023-05-04 16:36:08

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

OK. I hope you are right.

Before trying, I found this line in https://wiki.archlinux.org/title/Multib … lling_GRUB:

For UEFI, the partition has to be the first one in an MBR partition table and formatted with FAT32.

These are the USB flash drive partitions:

user@laptop:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 29,82 GiB, 32015679488 bytes, 62530624 sectors
Disk model: Cruzer Edge     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 4053BB53-1A95-4F17-8CE1-81A5F3BF2097

Device        Start      End  Sectors  Size Type
/dev/sdb1      2048 51202047 51200000 24,4G Linux filesystem
/dev/sdb2  52289536 62529535 10240000  4,9G Linux filesystem
/dev/sdb3  51202048 51234815    32768   16M BIOS boot
/dev/sdb4  51234816 52289535  1054720  515M EFI System

Partition table entries are not in disk order.

So the USB flash drive is not partitioned correctly, right? How do I correct that? How do I move /dev/sdb4 to /dev/sdb1 without erasing everything first?

Offline

#10 2023-05-04 16:43:14

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

Unlikely, since you did the hybrid MBR+GPT shenanigans then this is likely to not be an issue. This disclaimer is only for MBR only parittions and early UEFIs that actually supported MBR instead of GPT. FWIW for your purposes so far your stick would either be fcked already, or if it isn't adding GRUB to the ESP will not break the "previously" working config in any way.

Again The only thing running grub-install with the removable flag is to populate esp/EFI/BOOTx64.efi where esp is the --efi-directory mount point argument you give to GRUB.  One thing you need to do in your case when running that command is not mask /boot. mount the partitions like you always would if using/manipulating the stick normally, chroot in. mount the ESP to e.g. /efi ,  and do a

grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB --removable

this might only not work in so far that grub might not be able to read the existing grub config but the only thing you can break from that is that the EFI grub would not see any entries.

Last edited by V1del (2023-05-04 16:58:37)

Offline

#11 2023-05-04 17:22:38

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

mount the partitions like you always would if using/manipulating the stick normally, chroot in. mount the ESP to e.g. /efi

Isn't it easier to boot from the USB flash drive instead? I.e. in a BIOS only laptop.

Last edited by fortuna (2023-05-04 17:25:17)

Offline

#12 2023-05-04 18:29:00

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

You can if you want I was assuming you were doing this manipulation from some already running system? It does literally not matter if you pay attention to what you are doing and in which context you run which commands. I'm honestly quite unsure of what you are so deathly afraid of. Do you know why all of these partitions exist and how they logically map on the running (from USB) system? The only partition you need to mount is the root partition, and assuming you can chroot into that afterwards you can mount everything in your (USBs) fstab with

mount -a

then mount the ESP and then run the grub-install command. If you're afraid of chrooting you can also run grub-install from your current host system (... but but that has a chance of having an incompatible grub version and you need to correctly configure ---boot-directory= to refer to the mounted /boot mount point). Regarding an additional safety net, that last one --boot-directory= you can also set to something different like /boot/grub-efi so you clearly and surely have a distinction between the EFI and the BIOS mode grub, in which case you'd have to regenerate the config with

grub-mkconfig -o /boot/grub-efi/grub.cfg

as well.

Offline

#13 2023-05-04 19:05:52

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

I'm honestly quite unsure of what you are so deathly afraid of.

Grub and I, it's just not a great combination. I hardly ever need it, which is why when I do, I need to read all the docs, tricks, troubleshooting etc all over again. And when I finally use it, something that was working perfectly is screwed up and what seemed easy turns out to be a terrible headache.

You can if you want I was assuming you were doing this manipulation from some already running system?

The USB flash drive is (still) running fine in the BIOS only laptop. If this reduces the risk of grub-install touching the laptop grub, that would be appreciated.

Using grub is all about reducing risks. If the USB flash drive ends up booting in UEFI, that would be a complete success.

Offline

#14 2023-05-04 19:13:04

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

--boot-directory=/boot/grub-efi

So EFI and BIOS can use a different GRUB installation? That is a good idea.

Offline

#15 2023-05-04 20:24:49

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

The worst that can happen is that the BIOS GRUB breaks in which case you can just reinstall the BIOS GRUB again. You'd have to jump through serious hoops to make any form of the grub-install command lead to irreversible damage

Offline

#16 2023-05-04 21:02:24

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

Yes. That's right. But you're not amused if you install grub in a USB flash drive and you end up with an unbootable PC.
There are too many tricks out there, easy to copy but none off them apply to your specific configuration.

Anyway, I tried:

sudo grub-install --target=x86_64-efi --efi-directory=/efi --boot-directory=/boot/grub-efi --bootloader-id=GRUB --removable

and everything still boots with BIOS. Which is good news!

Perhaps you didn't expect that it created a grub folder inside of /boot/grub-efi/. No problem. Even I could solve that:

sudo grub-mkconfig -o /boot/grub-efi/grub/grub.cfg

Tomorrow I'll try EFI. To be continued. Thanks so far!

Offline

#17 2023-05-05 11:50:36

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

The good news is: the USB flash drive boots in Dell OptiPlex 3000 albeit after disabling Secure Boot in UEFI settings.

The bad news is: an error occurs:

EFI boot error

The interesting thing is that the UUID shown is the / partition that is mounted perfectly when booting in BIOS. It is in fstab, in /boot/grub/grub.cfg and in /boot/grub-efi/grub/grub.cfg.

I don't understand the /new_root part.

The shell is unusable because the keyboard is not working.

Any idea?

Offline

#18 2023-05-05 12:43:41

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,463

Re: [SOLVED] migrate USB flash drive to UEFI

Boot the fallback initramfs

Online

#19 2023-05-05 14:25:39

fortuna
Member
Registered: 2021-02-09
Posts: 76

Re: [SOLVED] migrate USB flash drive to UEFI

Scimmia wrote:

Boot the fallback initramfs

Wow! That does the trick. And then:

# mkinitcpio -P

and all's up and running!

Thanks Scimmia and V1del for your incredible help and admirable patience. Amazing. It's much appreciated!

Last edited by fortuna (2023-05-05 14:28:12)

Offline

#20 2023-05-05 15:54:05

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] migrate USB flash drive to UEFI

Great smile , please mark as [SOLVED] by editing the title in your first post.

Offline

Board footer

Powered by FluxBB