多引導USB設備

出自 Arch Linux 中文维基

多引導USB啟動盤可以從單個設備引導多個ISO鏡像文件。ISO鏡像文件可以不需要解壓複製到U盤上就可以啟動。以下有多種可用的方案,但不一定兼容所有ISO鏡像。

GRUB引導器及loopback devices實現[編輯 | 編輯原始碼]

本文或本章節的語言、語法或風格需要改進。參考:Help:Style

原因:multiple style issues(在Talk:多引導USB設備討論)

優點:

  • 僅需要一個分區
  • 所有ISO文件鏡像可在同一個文件夾下被檢測到
  • 易於添加或移除ISO鏡像文件

缺點:

  • 並非所有鏡像兼容
  • ISO文件原本的啟動菜單不會顯示
  • 找到可用的啟動項可能並不容易

準備[編輯 | 編輯原始碼]

在USB設備上創建至少一個分區和一個GRUB支持的文件系統。分區的大小取決與要存儲在其中的ISO文件大小和引導程序所計劃需要的空間。

安裝 GRUB引導器[編輯 | 編輯原始碼]

簡單安裝[編輯 | 編輯原始碼]

掛載U盤:

# mount /dev/sdXY /mnt

U盤上創建 /boot文件夾:

# mkdir /mnt/boot

安裝GRUB到U盤:

# grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdX

如果以UEFI啟動鏡像, 你需要按UEFI方式安裝GRUB:

# grub-install --target=x86_64-efi --removable --boot-directory=/mnt/boot --efi-directory=/mnt

對於UEFI, 引導分區必須是FAT32格式, 且啟動分區必須是MBR分區表中的第一個分區.

混合 UEFI GPT + BIOS GPT/MBR 啟動[編輯 | 編輯原始碼]

以下有用的配置可以幫助您創建一份可以在任意地方啟動的通用U盤。首先,您必須在您的設備上創建一份 GPT 分區。您至少需要以下三個分區:

  1. 一個BIOS啟動分區(gdisk代號編碼 EF02),該分區至少需要1 MiB的大小。
  2. 一個EFI系統分區(gdisk代號編碼EF00且它使用FAT32文件系統),該分區至少需要50 MiB的大小。
  3. 您自己的數據分區(使用GRUB支持的文件系統),該分區的大小是由您硬碟的剩餘空間容量來決定的。

接下來您必須創建一個MBR混合分區表。如果沒有它,一個僅BIOS MBR引導啟動的電腦將無法啟動。因為它無法尋找到硬碟裡的實例以啟動。

以下是一個使用gdisk創建MBR混合分區表的例子:

# gdisk /dev/sdX

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): 
Set the bootable flag? (Y/N): N

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

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

Recovery/transformation command (? for help): x
Expert command (? for help): h
Expert 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

注意 Hybird MBR 只可以輸入三個分區。

不要忘了格式化這些分區:

# mkfs.fat -F32 /dev/sdX2
# mkfs.ext4 /dev/sdX3

您現在可以安裝GRUB以同時支持EFI + GPT引導以及BIOS + GPT/MBR引導。GRUB配置(--boot-directory)將會被保持在同一個地方。

首先,您需要掛載您USB驅動器上的EFI系統分區和數據分區。

以下是如何掛載的例子:

# mount /dev/sdX3 /mnt
# mkdir -p /mnt/boot/EFI
# mount /dev/sdX2 /mnt/boot/EFI

然後,您就可以在UEFI上安裝GRUB:

在大多數案例中,EFI_MOUNTPOINT將會對應至您所掛載的U盤上的/mnt/boot/EFI這個子目錄中。

# grub-install --target=x86_64-efi --recheck --removable --efi-directory=/EFI_MOUNTPOINT --boot-directory=/DATA_MOUNTPOINT/boot

在BIOS上安裝GRUB:

# grub-install --target=i386-pc --recheck --boot-directory=/DATA_MOUNTPOINT/boot /dev/sdX

作為後備處理,您也可以把GRUB安裝進您的MBR啟動分區裡面。

# grub-install --target=i386-pc --recheck --boot-directory=/DATA_MOUNTPOINT/boot /dev/sdX3

配置 GRUB[編輯 | 編輯原始碼]

參考模板[編輯 | 編輯原始碼]

There are some git projects which provide some pre-existing GRUB configuration files, and a nice generic grub.cfg which can be used to load the other boot entries on demand, showing them only if the specified ISO files - or folders containing them - are present on the drive.

Multiboot USB: https://github.com/hackerncoder/multibootusb

GLIM (GRUB2 Live ISO Multiboot): https://github.com/thias/glim

手動配置[編輯 | 編輯原始碼]

For the purpose of multiboot USB drive it is easier to edit grub.cfg by hand instead of generating it. Alternatively, make the following changes in /etc/grub.d/40_custom or /mnt/boot/grub/custom.cfg and generate /mnt/boot/grub/grub.cfg using grub-mkconfig.

As it is recommend to use a persistent name instead of /dev/sdxY to identify the partition on the USB drive where the image files are located, define a variable for convenience to hold the value. If the ISO images are on the same partition as GRUB, use the following to read the UUID at boot time:

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

Or specify the UUID explicitly:

/mnt/boot/grub/grub.cfg
# path to the partition holding ISO images (using UUID)
set imgdevpath="/dev/disk/by-uuid/UUID_value"

Alternatively, use the device label instead of UUID:

/mnt/boot/grub/grub.cfg
# path to the partition holding ISO images (using labels)
set imgdevpath="/dev/disk/by-label/label_value"

The necessary UUID or label can be found using lsblk -f. Do not use the same label as the Arch ISO for the USB device, otherwise the boot process will fail.

To complete the configuration, a boot entry for each ISO image has to be added below this header, see the next section for examples.

啟動項[編輯 | 編輯原始碼]

It is assumed that the ISO images are stored in the boot/iso/ directory on the same filesystem where GRUB is installed. Otherwise it would be necessary to prefix the path to ISO file with device identification when using the loopback command, for example loopback loop (hd1,2)$isofile. As this identification of devices is not persistent, it is not used in the examples in this section.

One can use persistent block device naming like so. Replace the UUID according to your ISO filesystem UUID.

# define globally (i.e outside any menuentry)
insmod search_fs_uuid
search --no-floppy --set=isopart --fs-uuid 123-456
# later use inside each menuentry instead
loopback loop ($isopart)$isofile
提示: For a list of kernel parameters, see the kernel's command-line parameter documentation. For more examples of boot entries, see the GRUB upstream documentation or the documentation for the distribution you wish to boot.

Arch Linux monthly release[編輯 | 編輯原始碼]

Also see archiso.

menuentry '[loopback]archlinux-2020.10.01-x86_64.iso' {
	set isofile='/boot/iso/archlinux-2020.10.01-x86_64.iso'
	loopback loop $isofile
	linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
	initrd (loop)/arch/boot/intel-ucode.img (loop)/arch/boot/amd-ucode.img (loop)/arch/boot/x86_64/initramfs-linux.img
}

See README.bootparams for archiso options supported in kernel command line.

Memtest86+[編輯 | 編輯原始碼]

Memtest86+[損壞的連結:無效的章節] is included in the monthly ISO.

menuentry '[loopback]archlinux-2020.10.01-x86_64.iso' {
	set isofile='/boot/iso/archlinux-2020.10.01-x86_64.iso'
	loopback loop $isofile
	linux16 (loop)/arch/boot/memtest
}

archboot[編輯 | 編輯原始碼]

See Archboot Homepage.

menuentry '[loopback]archlinux-2014.11-1-archboot' {
	set isofile='/boot/iso/archlinux-2014.11-1-archboot.iso'
	loopback loop $isofile
	linux (loop)/boot/vmlinuz_x86_64 iso_loop_dev=$imgdevpath iso_loop_path=$isofile
	initrd (loop)/boot/initramfs_x86_64.img
}

Syslinux和memdisk實現[編輯 | 編輯原始碼]

Using the memdisk module, the ISO image is loaded into memory, and its bootloader is loaded. Make sure that the system that will boot this USB drive has sufficient amount of memory for the image file and running operating system.

準備[編輯 | 編輯原始碼]

Make sure that the USB drive is properly partitioned and that there is a partition with file system supported by Syslinux, for example fat32 or ext4. Then install Syslinux to this partition, see Syslinux#Installation on BIOS.

安裝 memdisk 模塊[編輯 | 編輯原始碼]

The memdisk module was not installed during Syslinux installation, it has to be installed manually. Mount the partition where Syslinux is installed to /mnt/ and copy the memdisk module to the same directory where Syslinux is installed:

# cp /usr/lib/syslinux/bios/memdisk /mnt/boot/syslinux/

配置[編輯 | 編輯原始碼]

After copying the ISO files on the USB drive, edit the Syslinux configuration file and create menu entries for the ISO images. The basic entry looks like this:

boot/syslinux/syslinux.cfg
LABEL some_label
    LINUX memdisk
    INITRD /path/to/image.iso
    APPEND iso

See memdisk on Syslinux wiki for more configuration options.

自動製作工具實現(開源軟體)[編輯 | 編輯原始碼]

  • GRUB2 Live ISO Multiboot (GLIM) — A set of GRUB configuration files to turn a VFAT formatted USB memory stick with GNU/Linux distribution ISO images into a multiboot USB drive.
https://github.com/thias/glim || not packaged? search in AUR
  • liveusb-builder — A script suite to create multiboot USB stick for GNU/Linux distributions
https://github.com/mytbk/liveusb-builder || liveusb-builder-gitAUR
  • MultiBootUSB — 一個具有CLI和GUI界面的跨平台Python軟體,可添加或刪除多個Linux LIVE映像環境到U盤。
https://github.com/mbusb/multibootusb || multibootusbAUR
  • MultiSystem — 一個圖形化工具,可添加或刪除多個Linux LIVE映像環境到U盤。
http://liveusb.info/dotclear/ || multisystemAUR
  • Ventoy — 一個開源工具,為ISO/WIM/IMG/VHD(x)/EFI文件創建可引導的USB驅動器。不需要一遍又一遍格式化磁碟,只需要複製文件到U盤即可啟動。
https://www.ventoy.net/ || ventoy-binAUR

更多參考[編輯 | 編輯原始碼]