Hybrid_UEFI_MBR_USB_Drive

An Hybrid UEFI GPT + BIOS GPT/MBR boot USB disk is a disk that works on both UEFI and BIOS PCs.

Requisites:

Step 1: get the right /dev

From now on we will use a root shell because almost all commands we will use require it.

root@debianvm:~# fdisk -l

...............

Disk /dev/sdc: 14.9 GiB, 16005464064 bytes, 31260672 sectors
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: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1           2 31260671 31260670 14.9G af HFS / HFS+

Find the right dev (ie: mine was /dev/sdc and I WILL USE IT IN THE GUIDE, you have to use your dev).

Step 2: make GUID partition table and partitions

root@debianvm:~# gdisk /dev/sdc
And then we are in gdisk prompt; from now on just give the commands in bold and the press enter.

GPT fdisk (gdisk) version 0.8.10

Partition table scan:

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

Warning! Main partition table overlaps the first partition by 32 blocks!
You will need to delete this partition or resize it in another utility.

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Command (? for help): n Partition number (1-128, default 1): 
First sector (34-31260638, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-31260638, default = 31260638) or {+-}size{KMGTP}: +1M
Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): EF02
Changed type of partition to 'BIOS boot partition'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-31260638, default = 4096) or {+-}size{KMGTP}:
Last sector (4096-31260638, default = 31260638) or {+-}size{KMGTP}: +50M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI System'

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-31260638, default = 106496) or {+-}size{KMGTP}:
Last sector (106496-31260638, default = 31260638) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 0700
Changed type of partition to 'Microsoft basic data'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
Now gdisk will quit, it's ok.

Step 3: make hybrid GUID/MBR

root@debianvm:~# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): r

Recovery/transformation command (? for help): h

WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,
just hit the Enter key at the below prompt and your MBR partition table will
be untouched.

Type from one to three GPT partition numbers, separated by spaces, to be
added to the hybrid MBR, in sequence: 1 2 3
Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): n

Creating entry for GPT partition #1 (MBR partition #1)
Enter an MBR hex code (default EF): EF02
Set the bootable flag? (Y/N): n

Creating entry for GPT partition #2 (MBR partition #2)
Enter an MBR hex code (default EF): EF00
Set the bootable flag? (Y/N): n

Creating entry for GPT partition #3 (MBR partition #3)
Enter an MBR hex code (default 07): 0700
Set the bootable flag? (Y/N): y

Recovery/transformation command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
gdisk will quit (again) and it's ok.

Step 4: format the partitons

We have to format in FAT32 two of the three partitions, /dev/sdc2 and /dev/sdc3. Umount the partitions if mounted and then:

root@debianvm:~# mkfs.fat -F32 -n GRUB2EFI /dev/sdc2
mkfs.fat 3.0.27 (2014-11-12)

root@debianvm:~# mkfs.fat -F32 -n DATA /dev/sdc3
mkfs.fat 3.0.27 (2014-11-12)

Now the partitons are ready, we will mount /devsdc2 to /mnt/efi and /dev/sdc3 to /mnt/data.

root@debianvm:~# mkdir /mnt/efi
root@debianvm:~# mkdir /mnt/data
root@debianvm:~# mount /dev/sdc2 /mnt/efi/
root@debianvm:~# mount /dev/sdc3 /mnt/data/

Step 5: install grub

In this step we will install grub in EFI partition (mounted in /mnt/efi) and in MBR.

root@debianvm:~# grub-install --target=x86_64-efi --efi-directory=/mnt/efi --boot-directory=/mnt/data/boot --removable --recheck
Installing for x86_64-efi platform.
Installation finished. No error reported.

root@debianvm:~# grub-install --target=i386-pc --boot-directory=/mnt/data/boot --recheck /dev/sdc
Installing for i386-pc platform.
Installation finished. No error reported.

Step 6: create folders for ISOs

In /mnt/data we should now have only one directory, boot; let's create a directory for the ISOs.

root@debianvm:~# mkdir /mnt/data/iso
In /mnt/data we should now have
/mnt/data/
- - - - - - - /boot/
- - - - - - - - - - - /grub/
- - - - - - - /iso/
Now we add 2 folders in /iso, one for the other ISOs and one for Debian (this is a workaround for a known bug).
root@debianvm:~# mkdir /mnt/data/iso/debian
root@debianvm:~# mkdir /mnt/data/iso/others

Let's download some ISOs, here i will explain ho to configure grub to boot Debian XFCE amd64 Installer and Tails i386; here there are instructions for other distros.

Debian

Download:

And put these files into /mnt/data/debian/

Tails

Download the iso torrent. Then create a folder for tails and copy there the iso.

root@debianvm:~# mkdir /mnt/data/others/tails

In /mnt/data we should now have
/mnt/data/
- - - - - - - /boot/
- - - - - - - - - - - /grub/
- - - - - - - /iso/
- - - - - - - - - - - /debian/
- - - - - - - - - - - - - - - - debian-8.5.0-amd64-xfce-CD-1.iso
- - - - - - - - - - - - - - - - vmlinuz
- - - - - - - - - - - - - - - - initrd.gz
- - - - - - - - - - - /others/
- - - - - - - - - - - - - - - - /tails/
- - - - - - - - - - - - - - - - - - - - tails-i386-2.5.iso

Step 7: configure grub

Now we need to configure grub; grub at boot reads its config from a config file (grub.cfg). Create a grub.cfg file with the following content and copy it into /mnt/data/boot/grub/:

# path to the partition holding ISO images (using UUID)
probe -u $root --set=rootuuid
set imgdevpath="/dev/disk/by-uuid/$rootuuid"

insmod exfat

# Setup video
if [ "$grub_platform" = "efi" ]
then
    insmod efi_gop
    insmod efi_uga
fi

if [ "$grub_platform" = "pc" ]
then
    insmod vbe
fi

# Setup font
insmod font

if loadfont ${prefix}/fonts/unicode.pf2
then
    insmod gfxterm
    set gfxmode=auto
    set gfxpayload=keep
    terminal_output gfxterm
fi

# Setup GRUB Screen
insmod jpeg
if background_image /boot/grub/grub_bg.jpg ; then
    set color_normal=white/black
    set color_highlight=white/green
else
    set menu_color_normal=white/black
    set menu_color_highlight=black/light-gray
fi

menuentry "Tails 2.5 i386" {
    set isofile='/iso/others/tails/tails-i386-2.5.iso'
    loopback loop $isofile
    linux (loop)/live/vmlinuz2 boot=live config findiso=${isofile} live-media=removable apparmor=1 security=apparmor nopersistent noprompt timezone=Etc/UTC block.events_dfl_poll_msecs=1000 noautologin module=Tails i2p
    initrd (loop)/live/initrd2.img
}

menuentry 'Debian 8.5.0 amd64 CD 1 XFCE' {
        set isofile='/iso/debian/debian-8.5.0-amd64-xfce-CD-1.iso'
        set initrdfile='/iso/debian/initrd.gz'
        loopback loop $isofile
        linux (loop)/install.amd/vmlinuz iso-scan/ask_second_pass=true
        initrd $initrdfile
}
menuentry "Reboot" {
        echo "System rebooting..."
        reboot
}

menuentry "Shutdown" {
        echo "System shutting down..."
        halt
}

It's done! If you want to customize grub's background copy a .jpg into /mnt/boot/grub/ and rename it grub_bg.jpg. Now unmount the USB drive and test it.
root@debianvm:~# umount /mnt/efi
root@debianvm:~# umount /mnt/data

Sources: arch linux wiki and many other sites.

l'ultima modifica รจ del 2020-10-20 13:51:48, fatta da ngfw-proxy