安裝grub到U盤分區,實現多系統引導


 

1. 分區工具及分區類型

• fdisk(變體:cfdisk,sfdisk),MBR格式分區工具,只能識別2Tb以內的磁盤空間。
• gdisk(變體:cgdisk,sgdisk),GPT格式分區工具。
• parted(圖形界面工具:gparted),同時支持MBR及GPT的分區工具。

1.1 顯示分區表和分區信息

以 /dev/sdc 磁盤為例:注:此優盤同時支持BIOS和UEFI模式

1.1.1 fdisk -l

$ sudo fdisk -l /dev/sdc
Disk /dev/sdc: 7.4 GiB, 7985954816 bytes, 15597568 sectors
Disk model: Flash Reader
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: gpt
Disk identifier: 1F02DD53-1D97-435E-832A-E12C86CEE311
Device Start End Sectors Size Type
/dev/sdc1 2048 15595519 15593472 7.4G EFI System
/dev/sdc2 15595520 15597534 2015 1007.5K BIOS boot

1.1.2 gdisk -l

$ sudo gdisk -l /dev/sdc //或者使用 sgdisk -p
GPT fdisk (gdisk) version 1.0.4
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sdc: 15597568 sectors, 7.4 GiB
Model: Flash Reader
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 1F02DD53-1D97-435E-832A-E12C86CEE311
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 15597534
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 15595519 7.4 GiB EF00
2 15595520 15597534 1007.5 KiB EF02 BIOS boot partition

1.1.3 parted -l

$ sudo parted -l
Model: Multi Flash Reader (scsi)
Disk /dev/sdc: 7986MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 7985MB 7984MB fat32 boot, esp
2 7985MB 7986MB 1032kB BIOS boot partition bios_grub

1.2 常見分區類型

 

1.3 分區樣例

1.3.1 BIOS/MBR example layout

1.3.2 UEFI/GPT example layout

 

1.3.3 BIOS/GPT example layout

2. gdisk

GPT fdisk 是編輯 GPT(Globally Unique Identifier Partition Table)硬盤的文本模式工具集。由 gdisk, sgdisk 和 cgdisk 組成. 用於 GPT 而不是老的 MBR(Master Boot Record) 分區表。

2.1 輸入“?”獲取幫助

$ sudo gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.4
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): ?
b back up GPT data to a file // 將GPT數據備份到文件
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) // 創建新的空GUID分區表(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 // 打印此菜單

2.2 備份和恢復分區表

sgdisk可以創建一個二進制備份,包含MBR,GPT主表頭,GPT備份表頭和分區表。
下面示例將 /dev/sda 的分區表信息備份到 sgdisk-sda.bin:
# sgdisk -b=sgdisk-sda.bin /dev/sda
通過下面命令恢復備份:
# sgdisk -l=sgdisk-sda.bin /dev/sda
如果要復制分區到其它磁盤,例如從 /dev/sda 復制到 /dev/sdc:
# sgdisk -R=/dev/sdc /dev/sda
如果兩個磁盤位於同一個計算機,使用下面命令設置隨機的分區 GUIDs:
# sgdisk -G /dev/sdc

2.3 Recover GPT header

GPT 在硬盤末尾存儲了第二分區表。這個數據結構默認有 33512B 空間。
如果主GPT標頭或備用GPT標頭損壞,您可以使用gdisk從另一個恢復。/dev/sda在此示例中使用。
#gdisk / dev / sda
選擇r恢復和轉換選項(僅限專家)。從那里選擇其中之一
b:使用備份GPT標頭(重建主要)
d:使用主GPT標頭(重建備份)
完成后將表寫入磁盤並通過w命令退出。

3. parted

3.1 加-h選項獲取幫助

$ sudo parted -h
Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run in
interactive mode.
OPTIONs:
-h, --help displays this help message // 顯示此幫助消息
-l, --list lists partition layout on all block devices // 列出所有塊設備上的分區布局
-m, --machine displays machine parseable output // 顯示機器可解析的輸出
-s, --script never prompts for user intervention // 從不提示用戶干預
-v, --version displays the version // 顯示版本
-a, --align=[none|cyl|min|opt] alignment for new partitions // 新分區的對齊方式
COMMANDs:
align-check TYPE N check partition N for TYPE(min|opt) alignment // 檢查分區N是否為TYPE(min | opt)對齊
help [COMMAND] print general help, or help on COMMAND // 打印一般幫助,或幫助COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table) // 創建一個新的disklabel(分區表)
mkpart PART-TYPE [FS-TYPE] START END make a partition // 做一個分區
name NUMBER NAME name partition NUMBER as NAME // 將分區NUMBER命名為NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition // 顯示分區表,可用設備,可用空間,所有找到的分區或特定分區
quit exit program // 退出計划
rescue START END rescue a lost partition near START and END // 在START和END附近救出丟失的分區
resizepart NUMBER END resize partition NUMBER // 調整分區NUMBER
rm NUMBER delete partition NUMBER // 刪除分區NUMBER
select DEVICE choose the device to edit // 選擇要編輯的設備
disk_set FLAG STATE change the FLAG on selected device // 更改所選設備上的FLAG
disk_toggle [FLAG] toggle the state of FLAG on selected device // 在所選設備上切換FLAG的狀態
set NUMBER FLAG STATE change the FLAG on partition NUMBER // 更改分區NUMBER上的FLAG
toggle [NUMBER [FLAG]] toggle the state of FLAG on partitio NUMBER // 在partitio NUMBER上切換FLAG的狀態
unit UNIT set the default unit to UNIT // 將默認單位設置為UNIT
version display the version number and copyright information of GNU Parted // 顯示GNU Parted的版本號和版權信息

4. 參考頁面

http://man.linuxde.net/parted
https://linux.cn/article-9536-1.html
http://www.rodsbooks.com/gdisk/
https://www.rodsbooks.com/gdisk/mbr2gpt.html
https://wiki.archlinux.org/index.php/GPT_fdisk
https://wiki.archlinux.org/index.php/Parted
https://wiki.archlinux.org/index.php/GRUB#GUID_Partition_Table_(GPT)_specific_instructions
https://wiki.archlinux.org/index.php/Partitioning
https://wiki.archlinux.org/index.php/Dual_boot_with_Windows
https://help.ubuntu.com/community/Installation/FromUSBStick
https://help.ubuntu.com/community/Installation/UEFI-and-BIOS/original-attempt

5. 給U盤分區

5.1 環境

Linux系統:Arch Linux
空白U盤
Linux下支持GPT分區的工具: parted, gdisk。
軟件:Grub2,
Windows環境下有Grub4Dos, Grub2win等軟件可使用。

5.2 同時支持BIOS和UEFI模式的分區方式

參考:https://my.oschina.net/abcfy2/blog/491140
[grub實現]U盤引導多個linux鏡像安裝,同時支持BIOS和UEFI模式
注:可能由於硬件環境或系統環境,軟件版本不同,該文內啟動菜單搬到我當前的環境下無法使用。
可以引導硬盤上的各種操作系統,硬盤上Linux的iso鏡像文件,U盤里各種Linux的iso鏡像文件。

5.2.1分區表及分區操作

主要步驟:

  • 先將U盤創建GPT分區表。這樣efi文件可以和MBR共存,實現UEFI和BIOS雙支持。
  • 建立新分區,然后標記ESP。如果用gdisk,只要給分區標記EF00編號即可;如果用parted,給分區boot標記即可。
  • 標記bios_grub分區。不需要格式化的1M分區,GRUB將其core.img嵌入此分區。

5.2.1.1 使用gparted創建GTP分區表,建立ESP分區

選擇使用圖形化的gparted,主要是操作簡單。
步驟:建立GPT分區表,建立分區,格式化為FAT32,標記分區為boot
(注: 盡管ESP支持多種分區,但是為了通用性與兼容性還是建議FAT32,缺點是不支持大於4Gb的iso鏡像文件。默認最大空間,能夠最大化利用有限的U盤存儲空間。)
操作:打開gparted軟件,右上角選擇sdc設備,刪除已有分區。

  1. Device 設備\創建分區表,選擇gpt;
  2. Partition 分區\創建新分區,fat32;
  3. 選擇分區,右鍵菜單\管理標識:boot; 

5.2.1.2 使用gdisk標記bios_grub標記分區

如果要讓grub在GPT上使用MBR模式安裝的話,需要設置這個標記。這個分區有以下幾個特點:

  • 1MB容量,
  • 無文件系統,不需要格式化,無掛載點,
  • 需要設置bios_grub標記
  • GRUB會將其core.img嵌入此分區。

如果用gdisk, parted, gparted這些工具分區的時候,你會發現總會有一個1MB的剩余空間,就是這個用途,只要給這個剩余空間分區,並打上bios_grub標記就行了(EF02),不用格式化。

$ sudo gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.4
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 //new新建分區
Partition number (2-128, default 2): 2 //第二個分區
First sector (34-15597534, default = 15595520) or {+-}size{KMGTP}: //直接回車
Last sector (15595520-15597534, default = 15597534) 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/sdc: 15597568 sectors, 7.4 GiB
Model: Flash Reader
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 1F02DD53-1D97-435E-832A-E12C86CEE311
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 15597534
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 15595519 7.4 GiB EF00
2 15595520 15597534 1007.5 KiB EF02 BIOS boot partition
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.
$

5.2.1.3分區結果

獲得2個分區:
第一個是EF00標記的ESPboot分區,FAT32格式。
第二個是EF02標記的BIOSboot分區,沒有文件系統,也不需要格式化。

5.2.2安裝grub到U盤

5.2.2.1掛載第一分區,建立boot目錄

mount U盤//可加-o uid=$USER,gid=$USER 編輯文件不需要使用sudo
$ sudo mount/dev/sdc1 /mnt/sc1
$ sudo mkdir/mnt/sc1/boot

5.2.2.2grub安裝到MBR;

$ sudo grub-install--target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdb
//$ sudo grub-install --boot-directory=/mnt/sc1/boot /dev/sdc
Installing for i386-pc platform.
Installation finished. No error reported.
安裝后查看該目錄:/mnt/sc1/boot/grub,
包含內容:353items, totalling 10.6 MiB (11,071,082 bytes)

5.2.2.3 grub安裝到ESP;

特別注意--removable參數,安裝到移動設備上一定要用這個參數
$ sudo grub-install --target=x86_64-efi --efi-directory=/mnt/sc1 --boot-directory=/mnt/sc1/boot –removable
報錯:grub-install: error: efibootmgr: not found.
由於本機是老機器,本身並不支持UEFI模式,所以Linux系統默認也沒有相應的軟件包。只需要手動安裝efibootmgr包即可,注意:不同的發行版本,相關的包名稱可能不同。
$ sudo pacman -S efibootmgr
$ sudo grub-install --target=x86_64-efi --efi-directory=/mnt/sc1 --boot-directory=/mnt/sc1/boot –removable
Installing for x86_64-efi platform.
EFI variables are not supported on this system.
EFI variables are not supported on this system.
Installation finished. No error reported.
安裝后查看該目錄:/mnt/sc1/boot/grub,
增加了目錄:/mnt/sc1/boot/grub/x86_64-efi
新增文件夾包含:269 items, totalling 2.6 MiB (2,694,596 bytes)

5.2.3 啟動菜單操作

5.2.3.1 生成硬盤系統的啟動菜單(僅對當前系統硬盤有效)

可使用如下命令,查找現有系統生成啟動菜單,也可以將已有的grub.cfg復制到如下文件夾。
$ sudo grub-mkconfig -o /mnt/sc1/boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-linux
Found initrd image: /boot/initramfs-linux.img
Found fallback initrd image(s) in /boot: initramfs-linux-fallback.img
Found Windows Server 2008 R2 on /dev/sdb1
done

這樣U盤就可以直接啟動硬盤上已安裝的Windwos系統及Linux系統了。
使用上面命令查找生成的啟動菜單,默認使用分區的uuid,通常可以直接被使用,不需要修改。

若是手動添加啟動菜單內容,使用分區編號時,不確定分區編號。
可使用U盤啟動,在啟動菜單界面,按c進入command模式,輸入ls查看分區信息,參考修改即可。

5.2.3.2 添加硬盤ISO鏡像文件啟動菜單(僅對當前主機硬盤有效)

參考硬盤啟動安裝Linux一文,將啟動菜單內容添加到U盤的grub.cfg文件內。
注:指定磁盤部分需要微調一下。
硬盤啟動時,硬盤本身默認(hd0,1);
U盤啟動時,U盤本身默認為(hd0,1), 硬盤則為(hd1,1)
實際內容可在啟動菜單頁面按c進入命令行界面,輸入ls查看分區狀況。

menuentry "Ubuntu LiveCD" {
set root=(hd1,3)
linux /ubuntu18/vmlinuz boot=casper iso-scan/filename=/ubuntu18/ubuntu-18.iso ro quiet splash locale=zh_CN.UTF-8
initrd /ubuntu18/initrd
}
menuentry "Mint LiveCD" {
set root=(hd1,3)
linux /mint191/vmlinuz boot=casper iso-scan/filename=/mint191/linuxmint-19.1-xfce-64bit.iso ro quiet splash locale=zh_CN.UTF-8
initrd /mint191/initrd.lz
}
menuentry "Install Arch" {
set root=(hd1,3)
linux /arch/vmlinuz archisolabel=ARCH_201903
initrd /arch/archiso.img
}
menuentry "manjaro.xfce LiveCD" {
set root=(hd1,3)
linux /manjaro/vmlinuz-x86_64 misolabel=M1803 boot=boot iso-scan/filename=/manjaro/manjaro-xfce-18.0.3-stable-x86_64.iso locale=zh_CN.UTF-8
initrd /manjaro/initramfs-x86_64.img
}

5.2.3.3 添加U盤ISO鏡像文件啟動菜單()

復制ISO文件,編輯菜單
7.4Gb的空間放了4個iso鏡像文件,啟動菜單如下

menuentry "Mint usb-boot-iso-mint" {
set root=(hd0,1)
linux /boot/iso/mint/vmlinuz boot=casper iso-scan/filename=/boot/iso/mint/linuxmint-19.1-xfce-64bit.iso ro quiet splash locale=zh_CN.UTF-8
initrd /boot/iso/mint/initrd.lz
}
menuentry "Arch usb-boot-iso-arch" {
set root=(hd0,1)
linux/boot/iso/arch/vmlinuz archisolabel=ARCH_201903
initrd/boot/iso/arch/archiso.img
}
menuentry "manjarousb-boot-iso-manjaro" {
set root=(hd0,1)
linux /boot/iso/manjaro/vmlinuz-x86_64 boot=boot iso-scan/filename=/boot/iso/manjaro/manjaro-xfce-18.0.3-stable-x86_64.iso locale=zh_CN.UTF-8
initrd /boot/iso/manjaro/initramfs-x86_64.img
}

5.3 支持大文件的BIOS模式

https://www.pendrivelinux.com/boot-multiple-iso-from-usb-via-grub2-using-linux/
http://xstarcd.github.io/wiki/Linux/boot-multiple-iso-from-usb-via-grub2-using-linux.html
使用grub2制作U盤引導iso

將U盤分為兩個分區:
sdb1:用於放數據(NTFS),使用剩余空間(Windows只能識別到U盤上的第一個分區)。
sdb2:用於引導(FAT32),設置boot標志,安裝grub,放置用於啟動文件,約2GB(grub/winpe/CDlinux/linuxmint),若加上win7安裝包,約需6GB。

具體沒有實測,請參考原文,原文有大量啟動菜單代碼可供參考。

其他參考:

http://xstarcd.github.io/wiki/Linux/boot-multiple-iso-from-usb-via-grub2-using-linux.html
使用grub2制作U盤引導iso (有多系統引導范例可供參考!)
grub2基礎教程-修訂版: http://forum.ubuntu.org.cn/viewtopic.php?t=290405

創建U盤多引導ISO Update:2017-06-17
# 舊版本grub使用:--root-directory=/media/USB,如:Ubuntu 10.04,grub-install (GNU GRUB 1.98-1ubuntu13)
grub-install --force --no-floppy --root-directory=/media/uboot /dev/sdb
# 新版本grub使用:--boot-directory=/media/USB/boot
#grub-install --force --no-floppy --boot-directory=/media/uboot/boot --target=i386-pc --recheck /dev/sdb
grub-install --force --no-floppy --boot-directory=/media/uboot/boot /dev/sdb

http://www.cnblogs.com/f-ck-need-u/p/7094693.html
grub2詳解(翻譯和整理官方手冊)

https://help.ubuntu.com/community/Installation/UEFI-and-BIOS#Make_a_system_bootable_in_UEFI_as_well_as_BIOS
安裝 / UEFI和BIOS
https://help.ubuntu.com/community/Installation/FromUSBStick
https://help.ubuntu.com/community/DiskSpace
BIOS-Boot或EFI分區(GPT磁盤上需要)
如果要在GPT磁盤上安裝Ubuntu (可以通過'sudo parted -l'命令檢查),則需要EFI分區(如果BIOS設置為EFI模式)或 BIOS-Boot分區(如果您的BIOS設置為傳統模式)。

BIOS-Boot分區:
裝載點:無
類型:無文件系統
描述:BIOS啟動分區包含GRUB 2的核心。如果您在GPT磁盤上安裝Ubuntu,並且固件(BIOS)設置為傳統(非EFI)模式,則必須執行此操作。它必須位於GPT磁盤的起始處,並且具有“bios_grub”標志。
尺寸:1MB。

EFI分區:
掛載點:/ boot / efi(無需設置此掛載點,因為安裝程序會自動執行此操作)
類型:FAT(通常為FAT32)
描述:EFI分區(也稱為ESP)包含一些引導文件。如果固件(BIOS)設置為以EFI模式啟動HDD(默認情況下是越來越現代的,> 2011年的計算機),則是必要的。它必須位於GPT磁盤的起始位置,並具有“引導”標志。
尺寸:100~250MB

https://help.ubuntu.com/community/Installation/UEFI-and-BIOS/original-attempt


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM