天天看點

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

目錄

1.分區工具及分區類型

1.1 顯示分區表和分區資訊

1.1.1 fdisk -l

1.1.2 gdisk -l

1.1.3 parted -l

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

2.1 輸入“?”擷取幫助

2.2 備份和恢複分區表

2.3 Recover GPT header

3.parted

3.1 加-h選項擷取幫助

4.參考頁面

5.給U盤分區

5.1 環境

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

5.2.1 分區表及分區操作

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

5.2.1.2使用gdisk标記bios_grub标記分區

5.2.1.3 分區結果

5.2.2 安裝grub到U盤

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

5.2.2.2 grub安裝到MBR;

5.2.2.3 grub安裝到ESP;

5.2.3 啟動菜單操作

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

5.2.3.2 添加硬碟ISO鏡像檔案啟動菜單(僅對目前主機硬碟有效)

5.2.3.3 添加U盤ISO鏡像檔案啟動菜單()

5.3 支援大檔案的BIOS模式

  1. 分區工具及分區類型
    安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

• 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 常見分區類型

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

1.3 分區樣例

1.3.1 BIOS/MBR example layout

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

1.3.2 UEFI/GPT example layout

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

1.3.3 BIOS/GPT example layout

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)
  1. 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裝置,删除已有分區。

Device 裝置\建立分區表,選擇gpt;
Partition 分區\建立新分區,fat32;
選擇分區,右鍵菜單\管理辨別: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分區,沒有檔案系統,也不需要格式化。

安裝grub到U盤分區,實作多系統引導(BIOS,UEFI)

5.2.2安裝grub到U盤

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

mount U盤//可加-o uid= U S E R , g i d = USER,gid= 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

sztom [email protected] CC-BY-NC-SA

https://gitee.com/abcfy2/grub-cfg/blob/master/grub.cfg

支援(0) 反對(0)

osoft 評論于 2019-04-20 13:49

#2樓 [樓主]

https://gitee.com/abcfy2/grub-cfg

支援(0) 反對(0)

osoft 評論于 2019-04-20 13:50

#3樓 [樓主]

http://xstarcd.github.io/wiki/Linux/boot-multiple-iso-from-usb-via-grub2-using-linux.html

支援(0) 反對(0)

osoft 評論于 2019-04-20 13:50

#4樓 [樓主]

https://wiki.archlinux.org/index.php/GRUB/Tips_and_tricks

https://wiki.archlinux.org/index.php/GRUB/Tips_and_tricks_(簡體中文)

圖形化配置工具

grub-customizer — 定制bootloader(GRUB or BURG)

https://launchpad.net/grub-customizer || grub-customizer

繼續閱讀