你的显示设备太小了

top

U盘 启动 ISO 文件(GRUB 版)

本文首次发布日期为 ,最近更新日期为

1. 前言

  之前一直在想如何把多个系统弄到一个 U盘 上引导启动,但没找到解决办法,所以只好用这种歪门邪道了。通过引导程序引导 ISO 文件启动,差不多是光盘一样,而且替换更新也方便,也能一个 U盘 能放多个 ISO 文件形成多系统引导。

2. 步骤

2.1 准备工作

  1. 空的U盘一个,大小随意,够地方放东西就行。

  2. Linux 系统,只要能用就行。

  3. ISO 光盘文件,我这里准备了 Arch Linux  的。

2.2 格式化 U盘

  用 gparted  去弄。

  首先给 U盘创建分区表:

创建分区表1
创建分区表2

  然后创建分区:

创建分区1
创建分区2

  上面一步之后要点击应用。

  接着给分区添加标记:

管理标志1
管理标志2

  到这一步,gparted 的任务完成了,不用忍受那要命的速度了。

  然后打开终端输入:

$ sudo gdisk -l /dev/sdb  # 根据你的U盘的名字修改dev设备

  然后会看到类似下面的东西:

GPT fdisk (gdisk) version 1.0.1

Partition table scan:
  MBR: protective        # <========================= 保护性的 MBR,这个是 GPT 兼容 MBR 的一种设计
  BSD: not present
  APM: not present
  GPT: present           # <========================= 看这里,已经是 GPT 了

Found valid GPT with protective MBR; using GPT.                # <========== 这里的显示也说明是 GPT 分区
Disk /dev/sdb: 62013440 sectors, 29.6 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2C913250-C820-411C-B018-FDC486253FFB
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 62013406
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        62011391   29.6 GiB    EF00        # <======== 这里很关键,看 Code 是 EF00,想要分区成为 ESP 必须设置这个标记,如果手工用 gdisk 别忘了这一步

  再接着输入:

$ sudo parted /dev/sdb print

  就会看到这样的东西:

Model: hp x755w (scsi)
Disk /dev/sdb: 31.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt                                            # <======= GPT 分区
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  31.7GB  31.7GB  fat32              boot, esp    # <===== ESP 已经设置成功

  然后输入:

$ sudo gdisk /dev/sdb

  接着对着下面来操作:

GPT fdisk (gdisk) version 1.0.1

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-62013406, default = 62011392) or {+-}size{KMGTP}:
Last sector (62011392-62013406, default = 62013406) or {+-}size{KMGTP}:
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): p
Disk /dev/sdb: 62013440 sectors, 29.6 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2C913250-C820-411C-B018-FDC486253FFB
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 62013406
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        62011391   29.6 GiB    EF00
   2        62011392        62013406   1007.5 KiB  EF02  BIOS boot partition  # <== EF02对应的就是bios_grub这个标记

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.

  此时输入:

$ sudo parted /dev/sdb print

  会看到这样:

Model: hp x755w (scsi)
Disk /dev/sdb: 31.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name                 Flags
 1      1049kB  31.7GB  31.7GB  fat32                             boot, esp
 2      31.7GB  31.8GB  1032kB               BIOS boot partition  bios_grub  # <== 要的就是这个!

2.3 安装 grub  到 U盘

  这样:

# mount /dev/sdb1 /mnt && \
grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdb && \
grub-install --target x86_64-efi --efi-directory /mnt --boot-directory=/mnt/boot --removable

  顺利的话会看到这样:

Installing for i386-pc platform.
Installation finished. No error reported.
Installing for x86_64-efi platform.
Installation finished. No error reported.

2.4 添加 grub 菜单

  在 /mnt/boot/grub 这个目录下,新建 grub.cfg 配置文件就行了。grub2 的语法很复杂,图省事就从网上各种摘抄拼接就行了。比如我把 Linux 发行版的 iso 镜像都扔到了 iso 这个目录,于是乎我的 grub.cfg 成了这样:

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

# define globally (i.e outside any menuentry)
insmod search_fs_uuid
search --no-floppy --set=isopart --fs-uuid $rootuuid

# grub模块配置
loadfont unicode
set lang=zh_CN
insmod all_video
insmod gfxterm
insmod gettext
insmod gfxmenu
insmod png
insmod part_msdos
insmod xfs
terminal_output gfxterm
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue
gfxmode keep
set vt_handoff=vt.handoff=7

# 主题配置
set gfxmode=1920x1080
set timeout_style=menu
set timeout=5
set theme=$prefix/themes/breeze/theme.txt
export theme

menuentry "Arch Linux ISO" --class arch {
	set isofile='/iso/archlinux-2016.11.01-dual.iso'
	loopback loop $isofile
	linux (loop)/arch/boot/x86_64/vmlinuz archisodevice=/dev/loop0 img_dev=$imgdevpath g_loop=$isofile earlymodules=loop
	initrd (loop)/arch/boot/x86_64/archiso.img
}

menuentry "Deepin 15 ISO" --class deepin {
	set isofile='/iso/deepin-15.3-amd64.iso'
	loopback loop ($isopart)$isofile
	echo '载入Deepin ...'
	linux (loop)/live/vmlinuz.efi boot=live config findiso=$isofile noeject noprompt cales=zh_CN.UTF-8 --
	echo '载入初始化内存盘...'
	initrd (loop)/live/initrd.lz


menuentry "System shutdown" --class shutdown {
	echo "System shutting down..."
	halt
}

menuentry "System restart" --class restart {
	echo "System rebooting..."
	reboot
}
grub.cfg