Multiboot-USB-Stick mit GRUB ############################ Stand: 16. Juni 2020 Vorbereitungen ============== Bitte vorher ein BACKUP vom USB-Stick machen (falls wichtige Daten vorhanden sind)! Zuerst einmal muss die Gerätedatei, über die man Stick anprechen kann, ermittelt werden. Das kann mittels ``lsblk`` oder ``lsblk -f`` geschehen. Hat man den Stick gerade erst angesteckt bzw. im Falle von Troubleshooting ist ``dmesg | tail -15`` ratsam. Im Folgenden gehen wir davon aus, dass das 2. Gerät unser USB-Stick ist, also via ``/dev/sdb`` referenziert wird. ACHTUNG: Wer im Zweifel ist, ob es sich tatsächlich um den betreffenden USB-Stick handelt, einfach den Stick wieder aus dem System entfernen, mit ``lsblk`` nachschauen, welche Geräte noch verblieben sind und wieder neu anstecken. Wiederum mit ``lsblk`` nachschauen - das nunmehr neu aufgetauchte Gerät ist der Stick. Partitionierung und Dateisysteme ================================ In der Praxis hat sich gezeigt, dass es sinnvoll ist, den MBR des Sticks und noch ein wenig darüber hinaus mit binären Nullen zu überschreiben, weil sonst oft das spätere ``grub-install`` scheitert (ACHTUNG: Datenverlust!): .. highlight:: shell-session :: root@debian10# dd if=/dev/zero of=/dev/sdb bs=1M count=3 3+0 Datensätze ein 3+0 Datensätze aus 3145728 bytes (3,1 MB, 3,0 MiB) copied, 0,577411 s, 5,4 MB/s root@debian10# Nach dem Entfernen des Sticks aus dem Gast und Abziehen/Neuanstecken sollten keine Partitionsnummern mehr zu sehen sein, nur noch das Hauptgerät ``/dev/sdb``: :: root@debian10# lsblk -f /dev/sdb NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sdb root@debian10# Wenn unser USB-Stick möglichst universell einsetzbar sein soll, ist es am einfachsten, einen Hybrid MBR mit dem Programm ``gdisk`` interaktiv zu erzeugen. Der grundlegende Ablauf: 1. *Create blank GPT*: ``2`` 2. Erzeugen neuer Partitionen: ``n``, Typ änderbar mit ``t``: a) 1 MB für 'BIOS boot partition' (hexadezimal EF02) b) 50 MB für 'EFI system partition' (hexadezimal EF00) c) 6 GB für 'Linux filesystem' (hexadezimal 8300) 3. Umwandeln in einen hybriden MBR: a) *recovery and transformation options (experts only)*: ``r`` b) *make hybrid MBR*: ``h`` - *Type from one to three GPT partition numbers... added to...*: ``3`` - *Place EFI GPT (0xEE) partition first in MBR?*: ``Y`` - Einfach Enter drücken (default 83) - *Set the bootable flag?* : ``N`` - *Unused partition space(s) found. Use one to protect more partitions?*: ``N`` - Über die Partitionen Infos ausgeben: ``i`` - Die protektiven MBR-Daten ausgeben: ``o`` 4. Abschließend das Ganze festschreiben: ``w``, danach ``Y`` Siehe hierzu nun diese etwas längere Shell-Sitzung, in der das eben Ausgeführte wiederzuentdecken ist: :: artix:[root]:~# gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.5 Caution: invalid main GPT header, but valid backup; regenerating main header from backup! Warning: Invalid CRC on main header data; loaded backup partition table. Warning! Main and backup partition tables differ! Use the 'c' and 'e' options on the recovery & transformation menu to examine the two tables. Warning! Main partition table CRC mismatch! Loaded backup partition table instead of main partition table! Warning! One or more CRCs don't match. You should repair the disk! Main header: ERROR Backup header: OK Main partition table: ERROR Backup partition table: OK Partition table scan: MBR: not present BSD: not present APM: not present GPT: damaged Found invalid MBR and corrupt GPT. What do you want to do? (Using the GPT MAY permit recovery of GPT data.) 1 - Use current GPT 2 - Create blank GPT Your answer: 2 Command (? for help): n Partition number (1-128, default 1): First sector (34-61439966, default = 2048) or {+-}size{KMGTP}: Last sector (2048-61439966, default = 61439966) or {+-}size{KMGTP}: +1M Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): L Type search string, or to show all codes: boot 3000 ONIE boot 4100 PowerPC PReP boot a000 Android bootloader a001 Android bootloader 2 a002 Android boot 1 a00b Android fastboot/tertiary a036 Android boot 2 a501 FreeBSD boot a581 Midnight BSD boot be00 Solaris boot e100 ONIE boot ea00 Freedesktop $BOOT ef02 BIOS boot partition Hex code or GUID (L to show codes, Enter = 8300): ef02 Changed type of partition to 'BIOS boot partition' Command (? for help): p Disk /dev/sdb: 61440000 sectors, 29.3 GiB Model: Rainbow Line Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 6C667608-497E-4C8B-B788-6D4CA0F1F42A 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 61439966 Partitions will be aligned on 2048-sector boundaries Total free space is 61437885 sectors (29.3 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 4095 1024.0 KiB EF02 BIOS boot partition Command (? for help): n Partition number (2-128, default 2): First sector (34-61439966, default = 4096) or {+-}size{KMGTP}: Last sector (4096-61439966, default = 61439966) or {+-}size{KMGTP}: +50M Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): EF00 Changed type of partition to 'EFI system partition' Command (? for help): p Disk /dev/sdb: 61440000 sectors, 29.3 GiB Model: Rainbow Line Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 6C667608-497E-4C8B-B788-6D4CA0F1F42A 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 61439966 Partitions will be aligned on 2048-sector boundaries Total free space is 61335485 sectors (29.2 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 4095 1024.0 KiB EF02 BIOS boot partition 2 4096 106495 50.0 MiB EF00 EFI system partition Command (? for help): n Partition number (3-128, default 3): First sector (34-61439966, default = 106496) or {+-}size{KMGTP}: Last sector (106496-61439966, default = 61439966) or {+-}size{KMGTP}: +6G Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sdb: 61440000 sectors, 29.3 GiB Model: Rainbow Line Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 6C667608-497E-4C8B-B788-6D4CA0F1F42A 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 61439966 Partitions will be aligned on 2048-sector boundaries Total free space is 48752573 sectors (23.2 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 4095 1024.0 KiB EF02 BIOS boot partition 2 4096 106495 50.0 MiB EF00 EFI system partition 3 106496 12689407 6.0 GiB 8300 Linux filesystem Command (? for help): ? b back up GPT data to a file c change a partition's name d delete a partition i show detailed information on a partition l list known partition types n add a new partition o create a new empty GUID partition table (GPT) p print the partition table q quit without saving changes r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu Command (? for help): r Recovery/transformation command (? for help): ? b use backup GPT header (rebuilding main) c load backup partition table from disk (rebuilding main) d use main GPT header (rebuilding backup) e load main partition table from disk (rebuilding backup) f load MBR and build fresh GPT from it g convert GPT into MBR and exit h make hybrid MBR i show detailed information on a partition l load partition data from a backup file m return to main menu o print protective MBR data p print the partition table q quit without saving changes t transform BSD disklabel partition v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu 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: 3 Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): Y Creating entry for GPT partition #3 (MBR partition #2) Enter an MBR hex code (default 83): Set the bootable flag? (Y/N): N Unused partition space(s) found. Use one to protect more partitions? (Y/N): N Recovery/transformation command (? for help): p Disk /dev/sdb: 61440000 sectors, 29.3 GiB Model: Rainbow Line Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 6C667608-497E-4C8B-B788-6D4CA0F1F42A 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 61439966 Partitions will be aligned on 2048-sector boundaries Total free space is 48752573 sectors (23.2 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 4095 1024.0 KiB EF02 BIOS boot partition 2 4096 106495 50.0 MiB EF00 EFI system partition 3 106496 12689407 6.0 GiB 8300 Linux filesystem Recovery/transformation command (? for help): ? b use backup GPT header (rebuilding main) c load backup partition table from disk (rebuilding main) d use main GPT header (rebuilding backup) e load main partition table from disk (rebuilding backup) f load MBR and build fresh GPT from it g convert GPT into MBR and exit h make hybrid MBR i show detailed information on a partition l load partition data from a backup file m return to main menu o print protective MBR data p print the partition table q quit without saving changes t transform BSD disklabel partition v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu Recovery/transformation command (? for help): i Partition number (1-3): 1 Partition GUID code: 21686148-6449-6E6F-744E-656564454649 (BIOS boot partition) Partition unique GUID: 9A90EDD0-A634-4856-A779-9E94930447B4 First sector: 2048 (at 1024.0 KiB) Last sector: 4095 (at 2.0 MiB) Partition size: 2048 sectors (1024.0 KiB) Attribute flags: 0000000000000000 Partition name: 'BIOS boot partition' Recovery/transformation command (? for help): i Partition number (1-3): 2 Partition GUID code: C12A7328-F81F-11D2-BA4B-00A0C93EC93B (EFI system partition) Partition unique GUID: A091D7AC-5437-4E86-9A22-1E3C97EB38CC First sector: 4096 (at 2.0 MiB) Last sector: 106495 (at 52.0 MiB) Partition size: 102400 sectors (50.0 MiB) Attribute flags: 0000000000000000 Partition name: 'EFI system partition' Recovery/transformation command (? for help): i Partition number (1-3): 3 Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem) Partition unique GUID: 5FB2DF44-C4CA-4CA4-B47E-1A604FF7A630 First sector: 106496 (at 52.0 MiB) Last sector: 12689407 (at 6.0 GiB) Partition size: 12582912 sectors (6.0 GiB) Attribute flags: 0000000000000000 Partition name: 'Linux filesystem' Recovery/transformation command (? for help): ? b use backup GPT header (rebuilding main) c load backup partition table from disk (rebuilding main) d use main GPT header (rebuilding backup) e load main partition table from disk (rebuilding backup) f load MBR and build fresh GPT from it g convert GPT into MBR and exit h make hybrid MBR i show detailed information on a partition l load partition data from a backup file m return to main menu o print protective MBR data p print the partition table q quit without saving changes t transform BSD disklabel partition v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu Recovery/transformation command (? for help): o Disk size is 61440000 sectors (29.3 GiB) MBR disk identifier: 0x00000000 MBR partitions: Number Boot Start Sector End Sector Status Code 1 1 106495 primary 0xEE 2 106496 12689407 primary 0x83 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/sdb. The operation has completed successfully. artix:[root]:~# artix:[root]:~# Im nächsten Schritt erzeugen wir die Dateisysteme: :: artix:[root]:~# mkfs.vfat -F 32 /dev/sdb2 mkfs.fat 4.1 (2017-01-24) artix:[root]:~# artix:[root]:~# artix:[root]:~# mkfs.ext4 -L boot /dev/sdb3 mke2fs 1.45.6 (20-Mar-2020) Ein Dateisystem mit 1572864 (4k) Blöcken und 393216 Inodes wird erzeugt. UUID des Dateisystems: 2626ecda-b1a2-42a5-8181-b3600cfcf23c Superblock-Sicherungskopien gespeichert in den Blöcken: 32768, 98304, 163840, 229376, 294912, 819200, 884736 beim Anfordern von Speicher für die Gruppentabellen: erledigt Inode-Tabellen werden geschrieben: erledigt Das Journal (16384 Blöcke) wird angelegt: erledigt Die Superblöcke und die Informationen über die Dateisystemnutzung werden geschrieben: erledigt artix:[root]:~# Zur Kontrolle des Ganzen geben wir ein: :: artix:[root]:~# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 1 29,3G 0 disk ├─sdb1 8:17 1 1M 0 part ├─sdb2 8:18 1 50M 0 part └─sdb3 8:19 1 6G 0 part artix:[root]:~# artix:[root]:~# artix:[root]:~# lsblk -f /dev/sdb NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sdb ├─sdb1 ├─sdb2 vfat 5036-9C23 └─sdb3 ext4 boot 2626ecda-b1a2-42a5-8181-b3600cfcf23c artix:[root]:~# Bootloader installieren ======================= Nun wird es spannend: der Bootloader wird installiert und sogar zwei mal: für den alten MBR und für EFI: :: artix:[root]:~# mount /dev/sdb3 /mnt/ artix:[root]:~# ls -l /mnt/ insgesamt 16 drwx------ 2 root root 16384 13. Jun 14:22 lost+found artix:[root]:~# artix:[root]:~# grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdb i386-pc wird für Ihre Plattform installiert. installation beendet. Keine Fehler aufgetreten. artix:[root]:~# artix:[root]:~# ls -F /mnt/boot/grub/ fonts/ grubenv i386-pc/ locale/ themes/ artix:[root]:~# artix:[root]:~# artix:[root]:~# mkdir /tmp/efi artix:[root]:~# mount /dev/sdb2 /tmp/efi artix:[root]:~# grub-install --target=x86_64-efi --recheck --removable --efi-directory=/tmp/efi --boot-directory=/mnt/boot x86_64-efi wird für Ihre Plattform installiert. installation beendet. Keine Fehler aufgetreten. artix:[root]:~# artix:[root]:~# ls -l /tmp/efi/ insgesamt 1 drwxr-xr-x 3 root root 512 13. Jun 14:34 EFI artix:[root]:~# artix:[root]:~# ls -l /tmp/efi/EFI/ insgesamt 1 drwxr-xr-x 2 root root 512 13. Jun 14:35 BOOT artix:[root]:~# ls -l /tmp/efi/EFI/BOOT/ insgesamt 132 -rwxr-xr-x 1 root root 135168 13. Jun 14:35 BOOTX64.EFI artix:[root]:~# artix:[root]:~# l /mnt/boot/ insgesamt 4 drwxr-xr-x 7 root root 4096 13. Jun 14:34 grub artix:[root]:~# l /mnt/boot/grub/ insgesamt 40 drwxr-xr-x 3 root root 4096 13. Jun 14:28 themes drwxr-xr-x 2 root root 4096 13. Jun 14:28 fonts -rw-r--r-- 1 root root 1024 13. Jun 14:28 grubenv drwxr-xr-x 2 root root 12288 13. Jun 14:29 i386-pc drwxr-xr-x 2 root root 4096 13. Jun 14:35 locale drwxr-xr-x 2 root root 12288 13. Jun 14:35 x86_64-efi artix:[root]:~# artix:[root]:~# # >> Hier findet sich natürlich noch keine 'grub.cfg'! Damit ist ein gutes Stück Arbeit getan, aber es fehlen noch die Linux-ISOs und die grub.cfg... Linux-Distributionen bereitstellen ================================== In Frage kommen fürs Erste folgende ISO-Dateien, die nach dem Wechseln in Verzeichnis "/mnt/ISO" mittels ``wget`` direkt heruntergeladen werden können: - http://mirror.slitaz.org/iso/rolling/slitaz-rolling-core.iso - https://get.refracta.org/files/stable/refracta10.3_xfce_amd64-20210205_2234.iso - https://osdn.net/projects/systemrescuecd/storage/releases/6.1.1/systemrescuecd-amd64-6.1.1.iso Dabei ist die Microdistribution "slitaz" besonders für ältere, leistungsschwache 32Bit-Rechner geeignet (keine grafische Oberfläche beim Starten im EFI-Modus mit der unten vorgestellten Konfiguration). GRUB-Konfiguration erstellen ============================ Fehlt zum guten Schluss noch die Konfigurationsdatei ``/mnt/boot/grub/grub.cfg``, die wir als neue Datei mit diesem Inhalt befüllen: .. highlight:: bash :: set timeout=10 set default=0 set isopath=/ISO insmod efi_gop menuentry "Boot from Hard Disk" { insmod chain set root=(hd1) chainloader +1 } menuentry "SystemRescueCd x86/amd64" { set isofile="$isopath/systemrescuecd-amd64-6.1.1.iso" search --no-floppy --label boot --set=root loopback loop $isofile linux (loop)/sysresccd/boot/x86_64/vmlinuz img_label=boot img_loop=$isofile archisobasedir=sysresccd setkmap=de initrd (loop)/sysresccd/boot/x86_64/sysresccd.img } menuentry "Refracta 10.3 Persistence" { set isofile="$isopath/refracta10.3_xfce_amd64-20210205_2234.iso" loopback loop $isofile linux (loop)/live/vmlinuz boot=live findiso=$isofile persistence initrd (loop)/live/initrd.img } menuentry "SliTaz Linux Core 32Bit" { echo Starting SliTaz live... set isofile="$isopath/slitaz-rolling-core.iso" loopback loop $isofile linux (loop)/boot/bzImage rw root=/dev/null autologin isofrom=$isofile noeject noprompt lang=de kmap=de initrd (loop)/boot/rootfs.gz } Testen des USB-Multiboot-Sticks =============================== Am besten, man installiere sich Qemu und starte den Bootstick - sofern möglich - mit kvm-Unterstützung: - Via EFI: ``qemu-system-x86_64 -enable-kvm -m 1600 -hda /dev/sdb -vga std -bios /usr/share/ovmf/x64/OVMF.fd`` - Via MBR: ``qemu-system-x86_64 -enable-kvm -m 1600 -hda /dev/sdb -vga std`` TIPP: Man sollte die Tests besser als eingeschränkter User duchführen, nach dem Anstecken des USB-Sticks einfach als 'root' das kommando ``chown myUSER /dev/sdb`` ausführen. HyperLinks ========== - `Hybrid MBRs: The Good, the Bad, and the So Ugly `_ - `Universal stand-alone grub für BIOS und EFI (Ubuntuusers) `_ - `Folien zum Skript "write_usb" `_ - `Skript zum automatischen Partitionieren `_ - `How to create multiboot USB with Linux `_ - `Arch Linux mit UEFI auf einem Thinkpad (Modul efi_gop) `_