<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>Hybrid_UEFI_MBR_USB_Drive</title><revhistory><revision><revnumber>19</revnumber><date>2020-10-20 13:51:48</date><authorinitials>ngfw-proxy.verwaltung.uni-muenchen.de</authorinitials><revremark>grub.conf -&gt; grub.cfg</revremark></revision><revision><revnumber>18</revnumber><date>2016-09-14 16:00:48</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>17</revnumber><date>2016-09-14 15:59:24</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>16</revnumber><date>2016-09-14 15:55:34</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>15</revnumber><date>2016-09-14 15:54:31</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>14</revnumber><date>2016-09-14 15:52:44</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>13</revnumber><date>2016-09-14 15:33:23</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>12</revnumber><date>2016-09-14 14:43:19</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>11</revnumber><date>2016-09-14 13:48:52</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>10</revnumber><date>2016-09-14 13:45:38</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>9</revnumber><date>2016-09-14 13:43:54</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>8</revnumber><date>2016-09-14 13:37:03</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>7</revnumber><date>2016-09-14 13:28:55</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>6</revnumber><date>2016-09-14 13:24:31</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>5</revnumber><date>2016-09-14 12:52:12</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>4</revnumber><date>2016-09-14 12:43:15</date><authorinitials>91.253.54.36</authorinitials></revision><revision><revnumber>3</revnumber><date>2016-09-14 11:59:47</date><authorinitials>104-102rev.amm.uniroma1.it</authorinitials></revision><revision><revnumber>2</revnumber><date>2016-09-14 11:20:19</date><authorinitials>104-102rev.amm.uniroma1.it</authorinitials></revision><revision><revnumber>1</revnumber><date>2016-09-14 11:19:31</date><authorinitials>104-102rev.amm.uniroma1.it</authorinitials></revision></revhistory></articleinfo><para>An Hybrid UEFI GPT + BIOS GPT/MBR boot USB disk is a disk that works on both UEFI and BIOS PCs. </para><section><title>Requisites:</title><itemizedlist><listitem><para>A working Linux installation with the following packages: <emphasis>grub2, grub2-efi, gdisk</emphasis> (on Debian Jessie: <emphasis>grub-pc-bin, grub-efi, grub-efi-amd64, gdisk</emphasis>) </para></listitem><listitem><para>An USB drive </para></listitem><listitem><para>Some Linux ISO.  </para></listitem></itemizedlist><section><title>Step 1: get the right /dev</title><para>From now on we will use a <emphasis role="strong">root shell</emphasis> because almost all commands we will use require it. </para><para><code>root@debianvm:~#</code> <emphasis role="strong">fdisk -l</emphasis></para><para><code>...............</code> </para><para><code>Disk /dev/sdc: 14.9 GiB, 16005464064 bytes, 31260672 sectors</code></para><para> <code>Units: sectors of 1 * 512 = 512 bytes</code></para><para> <code>Sector size (logical/physical): 512 bytes / 512 bytes</code></para><para> <code>I/O size (minimum/optimal): 512 bytes / 512 bytes</code></para><para> <code>Disklabel type: dos</code></para><para> <code>Disk identifier: 0x00000000</code></para><para><code>Device     Boot Start      End  Sectors  Size Id Type</code></para><para> <code>/dev/sdc1           2 31260671 31260670 14.9G af HFS / HFS+</code></para><para>Find the right dev (ie: mine was <emphasis role="strong">/dev/sdc</emphasis> and <emphasis>I WILL USE IT IN THE GUIDE</emphasis>, you have to use your dev). </para></section><section><title>Step 2: make GUID partition table and partitions</title><para><code>root@debianvm:~#</code> <emphasis role="strong">gdisk /dev/sdc</emphasis> </para><para>   And then we are in gdisk prompt; from now on just give the commands in <emphasis role="strong">bold</emphasis> and the press enter. </para><para><code>GPT fdisk (gdisk) version 0.8.10</code></para><para><code>Partition table scan:</code></para><itemizedlist><listitem override="none"><para><code>MBR: MBR only</code></para><para> <code>BSD: not present</code></para><para> <code>APM: not present</code></para><para> <code>GPT: not present</code></para></listitem></itemizedlist><para><code>***************************************************************</code></para><para> <code>Found invalid GPT and valid MBR; converting MBR to GPT format</code></para><para> <code>in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by</code></para><para> <code>typing 'q' if you don't want to convert your MBR partitions</code></para><para> <code>to GPT format!</code></para><para> <code>***************************************************************</code></para><para><code>Warning! Main partition table overlaps the first partition by 32 blocks!</code></para><para> <code>You will need to delete this partition or resize it in another utility.</code></para><para><code>Warning! Secondary partition table overlaps the last partition by</code></para><para> <code>33 blocks!</code></para><para> <code>You will need to delete this partition or resize it in another utility.</code></para><para><code>Command (? for help): </code><emphasis role="strong">o</emphasis></para><para> <code>This option deletes all partitions and creates a new protective MBR.</code></para><para> <code>Proceed? (Y/N):</code><emphasis role="strong"> y</emphasis></para><para><code>Command (? for help):</code> <emphasis role="strong">n</emphasis> <code>Partition number (1-128, default 1): </code></para><para> <code>First sector (34-31260638, default = 2048) or {+-}size{KMGTP}:</code></para><para>  <code>Last sector (2048-31260638, default = 31260638) or {+-}size{KMGTP}:</code> <emphasis role="strong">+1M</emphasis></para><para> <code>Current type is 'Linux filesystem'</code> <code>Hex code or GUID (L to show codes, Enter = 8300):</code> <emphasis role="strong">EF02</emphasis> </para><para> <code>Changed type of partition to 'BIOS boot partition'</code> </para><para><code>Command (? for help):</code><emphasis role="strong"> n</emphasis></para><para> <code>Partition number (2-128, default 2):</code></para><para>  <code>First sector (34-31260638, default = 4096) or {+-}size{KMGTP}:</code></para><para>  <code>Last sector (4096-31260638, default = 31260638) or {+-}size{KMGTP}:</code> <emphasis role="strong">+50M</emphasis> </para><para> <code>Current type is 'Linux filesystem'</code></para><para> <code>Hex code or GUID (L to show codes, Enter = 8300):</code> <emphasis role="strong">EF00</emphasis></para><para> <code>Changed type of partition to 'EFI System'</code></para><para><code>Command (? for help):</code> <emphasis role="strong">n</emphasis> </para><para> <code>Partition number (3-128, default 3):</code></para><para>  <code>First sector (34-31260638, default = 106496) or {+-}size{KMGTP}:</code></para><para>  <code>Last sector (106496-31260638, default = 31260638) or {+-}size{KMGTP}: </code></para><para> <code>Current type is 'Linux filesystem'</code></para><para> <code>Hex code or GUID (L to show codes, Enter = 8300):</code> <emphasis role="strong">0700</emphasis></para><para> <code>Changed type of partition to 'Microsoft basic data'</code></para><para><code>Command (? for help):</code> <emphasis role="strong">w</emphasis> </para><para><code>Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING</code></para><para> <code>PARTITIONS!!</code></para><para><code>Do you want to proceed? (Y/N):</code> <emphasis role="strong">y</emphasis></para><para> <code>OK; writing new GUID partition table (GPT) to /dev/sdc.</code></para><para> <code>The operation has completed successfully.</code></para><para> Now gdisk will quit, it's ok. </para></section><section><title>Step 3: make hybrid GUID/MBR</title><para><code>root@debianvm:~#</code> <emphasis role="strong">gdisk /dev/sdc</emphasis></para><para> <code>GPT fdisk (gdisk) version 0.8.10</code></para><para> <code>Partition table scan:</code></para><para> <code>  MBR: protective</code></para><para> <code>  BSD: not present</code></para><para> <code>  APM: not present</code></para><para> <code>  GPT: present</code></para><para> <code>Found valid GPT with protective MBR; using GPT.</code></para><para> <code>Command (? for help):</code> <emphasis role="strong">r</emphasis></para><para> <code>Recovery/transformation command (? for help):</code><emphasis role="strong"> h</emphasis></para><para> <code>WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,</code></para><para> <code>just hit the Enter key at the below prompt and your MBR partition table will</code></para><para> <code>be untouched.</code></para><para> <code>Type from one to three GPT partition numbers, separated by spaces, to be</code></para><para> <code>added to the hybrid MBR, in sequence:</code> <emphasis role="strong">1 2 3</emphasis></para><para> <code>Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N):</code> <emphasis role="strong">n</emphasis></para><para> <code>Creating entry for GPT partition #1 (MBR partition #1)</code></para><para> <code>Enter an MBR hex code (default EF):</code> <emphasis role="strong">EF02</emphasis></para><para> <code>Set the bootable flag? (Y/N):</code> <emphasis role="strong">n</emphasis></para><para> <code>Creating entry for GPT partition #2 (MBR partition #2)</code></para><para> <code>Enter an MBR hex code (default EF):</code> <emphasis role="strong">EF00</emphasis></para><para> <code>Set the bootable flag? (Y/N):</code> <emphasis role="strong">n</emphasis></para><para> <code>Creating entry for GPT partition #3 (MBR partition #3)</code></para><para> <code>Enter an MBR hex code (default 07):</code> <emphasis role="strong">0700</emphasis></para><para> <code>Set the bootable flag? (Y/N):</code> <emphasis role="strong">y</emphasis></para><para> <code>Recovery/transformation command (? for help):</code> <emphasis role="strong">w</emphasis></para><para> <code>Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING</code></para><para> <code>PARTITIONS!!</code></para><para> <code>Do you want to proceed? (Y/N):</code> <emphasis role="strong">y</emphasis></para><para> <code>OK; writing new GUID partition table (GPT) to /dev/sdc.</code></para><para> <code>The operation has completed successfully.</code></para><para> gdisk will quit (again) and it's ok. </para></section><section><title>Step 4: format the partitons</title><para>We have to format in FAT32 two of the three partitions, <emphasis><emphasis role="strong">/dev/sdc2</emphasis></emphasis> and <emphasis><emphasis role="strong">/dev/sdc3</emphasis></emphasis>. Umount the partitions if mounted and then: </para><para><code>root@debianvm:~#</code> <emphasis role="strong">mkfs.fat -F32 -n GRUB2EFI /dev/sdc2</emphasis></para><para> <code>mkfs.fat 3.0.27 (2014-11-12)</code></para><para><code>root@debianvm:~#</code> <emphasis role="strong">mkfs.fat -F32 -n DATA /dev/sdc3</emphasis></para><para> <code>mkfs.fat 3.0.27 (2014-11-12)</code></para><para>Now the partitons are ready, we will mount <emphasis><emphasis role="strong">/devsdc2</emphasis></emphasis> to <emphasis><emphasis role="strong">/mnt/efi</emphasis></emphasis> and <emphasis><emphasis role="strong">/dev/sdc3</emphasis></emphasis> to <emphasis><emphasis role="strong">/mnt/data</emphasis></emphasis>. </para><para><code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/efi</emphasis></para><para> <code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/data</emphasis></para><para> <code>root@debianvm:~#</code> <emphasis role="strong">mount /dev/sdc2 /mnt/efi/</emphasis></para><para> <code>root@debianvm:~#</code> <emphasis role="strong">mount /dev/sdc3 /mnt/data/</emphasis></para></section><section><title>Step 5: install grub</title><para>In this step we will install grub in EFI partition (mounted in /mnt/efi) and in MBR. </para><para><code>root@debianvm:~#</code> <emphasis role="strong">grub-install --target=x86_64-efi --efi-directory=/mnt/efi --boot-directory=/mnt/data/boot --removable --recheck</emphasis></para><para> <code>Installing for x86_64-efi platform.</code></para><para> <code>Installation finished. No error reported.</code></para><para><code>root@debianvm:~#</code> <emphasis role="strong">grub-install --target=i386-pc --boot-directory=/mnt/data/boot --recheck /dev/sdc</emphasis></para><para> <code>Installing for i386-pc platform.</code></para><para> <code>Installation finished. No error reported.</code></para></section><section><title>Step 6: create folders for ISOs</title><para>In <emphasis><emphasis role="strong">/mnt/data</emphasis></emphasis> we should now have only one directory, <emphasis role="strong">boot</emphasis>; let's create a directory for the ISOs. </para><para><code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/data/iso</emphasis></para><para> In <emphasis><emphasis role="strong">/mnt/data</emphasis></emphasis> we should now have </para><para> <emphasis><emphasis role="strong">/mnt/data/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - /boot/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - /grub/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - /iso/</emphasis></emphasis></para><para> Now we add 2 folders in <emphasis><emphasis role="strong">/iso</emphasis></emphasis>, one for the other ISOs and one for Debian (this is a workaround for a <ulink url="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=701772">known bug</ulink>).</para><para> <code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/data/iso/debian</emphasis></para><para> <code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/data/iso/others</emphasis></para><para>Let's download some ISOs, here i will explain ho to configure grub to boot Debian XFCE  amd64 Installer and Tails i386; <ulink url="https://wiki.archlinux.org/index.php/Multiboot_USB_drive#Boot_entries">here</ulink> there are instructions for other distros. </para><section><title>Debian</title><para>Download: </para><itemizedlist><listitem><para>iso from <ulink url="http://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-8.5.0-amd64-xfce-CD-1.iso">http</ulink> or <ulink url="http://cdimage.debian.org/debian-cd/current/amd64/bt-cd/debian-8.5.0-amd64-xfce-CD-1.iso.torrent">torrent</ulink> </para></listitem><listitem><para><ulink url="https://mirrors.kernel.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/vmlinuz">vmlinuz</ulink> </para></listitem><listitem><para><ulink url="https://mirrors.kernel.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/initrd.gz">initrd</ulink> </para></listitem></itemizedlist><para>And put these files into <emphasis><emphasis role="strong">/mnt/data/debian/</emphasis></emphasis> </para></section><section><title>Tails</title><para>Download the iso <ulink url="https://tails.boum.org/torrents/files/tails-i386-2.5.torrent">torrent</ulink>. Then create a folder for tails and copy there the iso. </para><para><code>root@debianvm:~#</code> <emphasis role="strong">mkdir /mnt/data/others/tails</emphasis></para><para>In <emphasis><emphasis role="strong">/mnt/data</emphasis></emphasis> we should now have </para><para> <emphasis><emphasis role="strong">/mnt/data/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - /boot/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - /grub/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - /iso/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - /debian/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - - - - - - debian-8.5.0-amd64-xfce-CD-1.iso</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - - - - - - vmlinuz</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - - - - - - initrd.gz</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - /others/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - - - - - - /tails/</emphasis></emphasis></para><para> <emphasis><emphasis role="strong">- - - - - - - - - - - - - - - - - - - - tails-i386-2.5.iso</emphasis></emphasis></para></section></section><section><title>Step 7: configure grub</title><para>Now we need to configure grub; grub at boot reads its config from a config file (grub.cfg). Create a <emphasis role="strong">grub.cfg</emphasis> file with the following content and copy it into <emphasis><emphasis role="strong">/mnt/data/boot/grub/</emphasis></emphasis>: </para><screen><![CDATA[# path to the partition holding ISO images (using UUID)
probe -u $root --set=rootuuid
set imgdevpath="/dev/disk/by-uuid/$rootuuid"
]]><![CDATA[
insmod exfat
]]><![CDATA[
# Setup video
if [ "$grub_platform" = "efi" ]
then
    insmod efi_gop
    insmod efi_uga
fi
]]><![CDATA[
if [ "$grub_platform" = "pc" ]
then
    insmod vbe
fi
]]><![CDATA[
# Setup font
insmod font
]]><![CDATA[
if loadfont ${prefix}/fonts/unicode.pf2
then
    insmod gfxterm
    set gfxmode=auto
    set gfxpayload=keep
    terminal_output gfxterm
fi
]]><![CDATA[
# 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
]]><![CDATA[
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
}
]]><![CDATA[
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
}
]]><![CDATA[
menuentry "Shutdown" {
        echo "System shutting down..."
        halt
}]]></screen><para>It's done! If you want to customize grub's background copy a .jpg into <emphasis><emphasis role="strong">/mnt/boot/grub/</emphasis></emphasis> and rename it <emphasis role="strong">grub_bg.jpg</emphasis>. Now unmount the USB drive and test it.</para><para> <code>root@debianvm:~#</code> <emphasis role="strong">umount /mnt/efi</emphasis></para><para> <code>root@debianvm:~#</code> <emphasis role="strong">umount /mnt/data</emphasis></para><para>Sources: <ulink url="https://wiki.archlinux.org/index.php/Multiboot_USB_drive">arch linux wiki</ulink> and many other sites. </para></section></section></article>