Rescue Grub Flashdrive for UEFI or Legacy.

Questions about Grub, UEFI,the liveCD and the installer
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
bodge99

Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Rescue Grub Flashdrive for UEFI or Legacy.

Useful for those who dual boot with Windows.

Typical Uses: For when your computer now only boots into Windows. You cannot boot Linux.
Or, you have managed to damage your EFI partition contents.
It might be an idea to create a flashdrive **before** any Windows update borks Grub..

This creates a simple flashdrive boot device. Allows a booting choice of either using your system's 'grub.cfg' or a direct kernel boot.

It's often easier to fix Grub problems from within a running system.

Install UEFI and Legacy Grub to a flashdrive.

Format a flashdrive to fat32 and set the 'boot' and 'esp' flags. Note: Don't use GPT here. "USB Stick Formatter" ("Mintstick") is normally fine for this but I would recommend using Gparted for everything.

I'll assume here that your flashdrive is seen as 'sda'. Use 'fdisk -l' to check.

Install UEFI & Legacy Grub:

Code: Select all

sudo su
mount /dev/sda1 /mnt
grub-install --boot-directory=/mnt/EFI/boot --efi-directory=/mnt --target=x86_64-efi /dev/sda
grub-install --boot-directory=/mnt/EFI/boot --target=i386-pc /dev/sda
sync
exit
sudo xed /mnt/EFI/boot/grub/grub.cfg
Copy & paste the following sample 'grub.cfg':
You'll need the filesystem UUID of your Linux installation and the full filenames of your kernel & initrd.

You can create a text file containing these using:

Code: Select all

blkid > myFilenames.txt
ls /boot/vmlinuz* >> myFilenames.txt
ls /boot/initrd* >> myFilenames.txt
Copy & paste the required UUID, kernel and initrd names. I recommend that you use the full kernel/initrd name, just in case the local kernel & initrd symlinks are incorrect. There is an entry here which should help when you've forgotten to update this 'grub.cfg' after upgrading your kernel.
The full kernel and initrd file names are similar to: 'vmlinuz-5.11.16-051116-generic' and 'initrd.img-5.11.16-051116-generic'.

Sample 'grub.cfg' using an example UUID and kernel/initrd names.

Code: Select all

menuentry "Boot using the system's grub.cfg." {
    search --set=root --fs-uuid e8ab7adf-e4b4-43cc-8c43-bbef6d53f6f1
    set prefix=($root)/boot/grub
    configfile $prefix/grub.cfg
}

menuentry "Direct kernel boot." {
    search --set=root --fs-uuid e8ab7adf-e4b4-43cc-8c43-bbef6d53f6f1
    linux /boot/vmlinuz-5.11.16-051116-generic root=UUID=e8ab7adf-e4b4-43cc-8c43-bbef6d53f6f1
    initrd /boot/initrd.img-5.11.16-051116-generic
} 

menuentry "Direct kernel boot. Kernel & initrd symlink" {
    search --set=root --fs-uuid e8ab7adf-e4b4-43cc-8c43-bbef6d53f6f1
    linux /boot/vmlinuz root=UUID=e8ab7adf-e4b4-43cc-8c43-bbef6d53f6f1
    initrd /boot/initrd
} 
Save and exit.

Unmount the flashdrive with:

Code: Select all

sudo umount /mnt
This should be enough to boot your system.

Bodge99.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 6 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
spamegg
Level 14
Level 14
Posts: 5047
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Rescue Grub Flashdrive for UEFI.

Post by spamegg »

Excellent. Should this be in the Tutorials forum?
bodge99

Re: Rescue Grub Flashdrive for UEFI.

Post by bodge99 »

Hi,

I posted this in this section as there seems to be a significant number of Grub problems (caused by Windows) here.

I'll repost in the Tutorials section..

Does anyone want this extended to cover Legacy boot as well??

Bodge99
t42
Level 11
Level 11
Posts: 3734
Joined: Mon Jan 20, 2014 6:48 pm

Re: Rescue Grub Flashdrive for UEFI.

Post by t42 »

bodge99 wrote: Tue May 04, 2021 12:28 pm Does anyone want this extended to cover Legacy boot as well??
For sure someone will. Your method seems to be transparent and easy to implement comparing to creating boot iso with grub-mkrescue.
-=t42=-
bodge99

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Hi,

I've updated this post (and the one in 'Tutorials') to handle Legacy booting. I've also corrected a couple of minor typo's.

Bodge99
bodge99

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Hi,

Further uses of this general technique:

Background:

Many moons ago I was a member of a Windows tablet forum. I had bought a cheap tablet with a view to installing Linux onto it. At one point, whilst I was working on this, I noticed that an SDcard (I had forgotten a card was in the card slot) was identified at the end of the first stage kernel initialisation.

Now the SDcard hardware was not even "seen" by the Bios at all.
This gave me an idea. Would it be possible to install Linux onto the SDcard and actually boot it, given that the SDcard hardware wasn't initialised at all by the Bios?

Long story short.. It is.. You have to place a copy of the installed kernel & initrd into the EFI partition. Grub is installed into the EFI partition and configured to start the boot from here.
At the second stage kernel initialisation, control is passed to the 'real' kernel, which is now accessible on the newly initialised SDcard. The rest of the boot is then completely normal.
I must admit that, apart from the "academic exercise" part.. I did this to annoy the forum moderator.. He was one of those "special characters" who was always complaining about what Windows couldn't do that Linux could, if you know what I mean.. ;>)

I'm now looking at using this technique on hardware that has USB 3.
As there is no Grub module for USB 3 hardware, direct support is required by the Bios. Most kit is.. but some isn't. This also applies to some 3rd party USB 3 cards. This means that you cannot normally boot from them.

I've tried this technique with 4 USB 3 PCIe cards so far (different controllers).. it works! and I can boot successfully from them.

I'm now waiting to obtain access to a laptop that exhibits this problem. I've heard that they do exist..

Bodge99.
Last edited by bodge99 on Wed May 05, 2021 11:01 am, edited 1 time in total.
t42
Level 11
Level 11
Posts: 3734
Joined: Mon Jan 20, 2014 6:48 pm

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by t42 »

bodge99 wrote: Wed May 05, 2021 4:26 am Would it be possible to install Linux onto the SDcard and actually boot it, given that the SDcard hardware wasn't initialised at all by the Bios?

Long story short.. It is.. You have to place a copy of the installed kernel & initrd into the EFI partition. Grub is installed into the EFI partition and configured to start the boot from here.
At the second stage kernel initialisation, control is passed to the 'real' kernel, which is now accessible on the newly initialised SDcard. The rest of the boot is then completely normal.
This is great! You can't boot OS from SD card in the integrated card reader - it is an axiom. So, not any more!

I'm booting Linux from SD card using compact external card reader in legacy mode (customized MX Linux image, as more convenient to create ). Invoke boot menu on any laptop and you are in. Never even thought about EFI!
-=t42=-
t42
Level 11
Level 11
Posts: 3734
Joined: Mon Jan 20, 2014 6:48 pm

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by t42 »

bodge99 wrote: Wed May 05, 2021 4:26 am As there is no Grub module for USB 3 hardware, direct support is required by the Bios. Most kit is.. but some isn't.
Some older motherboards/BIOSes are hopeless, impossible to penetrate. It's nice you can find cards of suitable make.
-=t42=-
bodge99

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Hi,

t42: You've triggered another idea.. I've never thought of attempting to boot Linux from an **external** card reader.. I must look at this when I've got some time. I like getting hardware to work in the way I want it to, not as it was designed for. I'll draw the line at Hollerith cards though!

This is why I like Grub. and UEFI. It's quite... well, relatively easy (and possible) to use both in ways that most folk don't even consider.

Bodge99
bodge99

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Hi,

I forgot to add..

TIP:

If anyone is starting to experiment with Grub & **anything** on UEFI, make a copy of the contents of your EFI partition before you start.

Three guesses why I do this!!

Bodge99
bodge99

Re: Rescue Grub Flashdrive for UEFI or Legacy.

Post by bodge99 »

Hi,

Just to continue..

Booting an SDcard installed Linux from the host system's EFI partition.
The SDcard hardware is **NOT** initialised or "seen" at all by the Bios.

Format the SDcard with a 100MB fat32 partition. Set the 'boot' and 'esp' flags.
Use the rest of the available space as a single Ext4 partition.
Install Mint to the SDcard normally. Use 'Something else' at the disks section.
Reboot, and ensure that the SDcard is **not** seen in the "Boot Override" or "Boot devices" etc. in the Bios.
This proves that the Bios plays no part in the SDcard boot process.

Install Grub to the host computer's EFI partition.

I'll assume here that the host's drive is 'sda'. Adjust to suit after checking with 'fdisk -l'.

Code: Select all

sudo su
mount /dev/sda1 /mnt
Now move the existing EFI **contents** somewhere safe... just in case..
Install Grub to the EFI partition:

Code: Select all

grub-install --boot-directory=/mnt/EFI/boot --efi-directory=/mnt --target=x86_64-efi /dev/sda
Make a second working directory and mount the SDcard's system filesystem to (e.g.) /mnt1 :
Create a '/boot' directory on the host's EFI partition. Copy the SDcard's kernel & initrd to this directory.
I'll use 'vmlinuz-5.11.16-051116-generic' & 'initrd.img-5.11.16-051116-generic' as an example.

Code: Select all

mkdir /mnt1
mkdir /mnt/boot
mount /dev/mmcblk0p2 /mnt1
ls /mnt1/boot/vmlinuz*  
cp /mnt1/boot/vmlinuz-5.11.16-051116-generic /mnt/boot
cp /mnt1/boot/initrd.img-5.11.16-051116-generic /mnt/boot
exit
Now you need the UUID's of the **host** system's EFI filesystem and of the SDcard's **system** partition's filesystem.
You'll also need the host's system UUID so that you can still boot normally.

Code: Select all

blkid | grep sda > myUUIDs.txt
blkid | grep mmcblk0p2 >> myUUIDs.txt
sudo xed /mnt/EFI/boot/grub/grub.cfg
Populate the 'grub.cfg' with the following:

Code: Select all

menuentry "Continue with NORMAL boot." {
    search --set=root --fs-uuid host-system-UUID-here
    set prefix=($root)/boot/grub
    configfile $prefix/grub.cfg
}

menuentry "SDcard Boot." {
    search --set=root --fs-uuid Host-system-EFI-filesystem-UUID-here
    linux /boot/vmlinuz-5.11.16-051116-generic root=UUID=SDcard-system-UUID-here
    initrd /boot/initrd.img-5.11.16-051116-generic
} 
Save & exit.

Code: Select all

sudo umount /mnt /mnt1
Done.
Yes, the SDcard is very slow.. but, just to prove a point.
SystemD slows things down even more.. You will get better results using something like Slackware or Devuan for this.

I've had an idea which should improve things..I'll need to test it. More tomorrow.

Edit: 11/05/21 The idea that I had mostly works.. It' needs more testing.. when I've got the time.
Bodge99
Locked

Return to “Installation & Boot”