赞
踩
本次制作U启实现引导本地系统,引导pe镜像,引导安装光盘。本次制作使用grub2。
U盘:64G,实际57.8G
PE镜像:优启通、Ubuntu、CentOS
系统光盘:Ubuntu-19.10-amd64、CentOS-7-x86-64、Windows10_1909_x64
以上所有没有特定关联要求。
在grub2引导的一些说明:
本次制作U启以GPT分区为例。并且后文按照分区一步一步制作。
#不清楚parted的硬盘显示size为撒偏大? [root@localhost ~]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 1 57.8G 0 disk ├─sdb1 8:17 1 100M 0 part ├─sdb2 8:18 1 1G 0 part ├─sdb3 8:19 1 20G 0 part ├─sdb4 8:20 1 20G 0 part └─sdb5 8:21 1 16.7G 0 part [root@localhost ~]# parted /dev/sdb print Model: Kingston DataTraveler 3.0 (scsi) Disk /dev/sdb: 62.0GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 106MB 105MB fat16 boot, esp 2 106MB 1180MB 1074MB ntfs hidden, msftdata 3 1180MB 22.7GB 21.5GB ext4 hidden 4 22.7GB 44.1GB 21.5GB ntfs hidden, msftdata 5 44.1GB 62.0GB 17.9GB ntfs msftdata
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): g
Created a new GPT disklabel (GUID: E26FDC8B-0190-654B-AF00-B4B5D8A563D0).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-121110494, default 2048): #2048指扇区数,一个扇区512k,也就是2048*512/1024/1024=1M
Last sector, +sectors or +size{K,M,G,T,P} (2048-121110494, default 121110494): +100M
Created a new partition 1 of type 'Linux filesystem' and of size 100 MiB.
Command (m for help): n #新建bios分区
Partition number (2-128, default 2):
First sector (206848-121110494, default 206848): 206848
Last sector, +sectors or +size{K,M,G,T,P} (206848-121110494, default 121110494): +1M
Created a new partition 3 of type 'Linux filesystem' and of size 1 MiB.
Command (m for help): t #更改uefi分区类型为EFI System
Partition number (1,2, default 2): 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.
Command (m for help): t #更改bios分区类型为BIOS boot
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 4
Changed type of partition 'Linux filesystem' to 'BIOS boot'.
Command (m for help): p
Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors
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: 9D28857F-1269-A840-90F7-3F0E391B53AF
Device Start End Sectors Size Type
/dev/sdb1 2048 206847 204800 100M EFI System
/dev/sdb2 206848 208896 2048 1M BIOS boot
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mkfs.vfat /dev/sdb1
mkfs.fat 4.1 (2017-01-24)
grub2需要用到packages,grub2-tools提供程序,执行制作、安装、配置等操作,grub2-pc-modules提供mbr启动所需模块,grub2-efi-i386-modules提供efi-x32位启动所需模块,grub2-efi-x64-modules提供efi-x64位启动所需模块。在grub-install中target参数指定。
[root@localhost ~]# rpm -ql grub2-tools | grep -P "(bin)|(sbin)"
/usr/bin/grub2-mkimage #grub2的BOOTX64.EFI制作
/usr/bin/grub2-script-check #grub.cfg配置文件语法检测
/usr/sbin/grub2-install #grub2引导安装
/usr/sbin/grub2-mkconfig #自动生成grub.cfg配置文件
/usr/sbin/grub2-probe #扫描计算机并收集磁盘和分区信息
……
[root@localhost ~]# rpm -ql grub2-pc-modules
/usr/lib/grub/i386-pc
/usr/lib/grub/i386-pc/acpi.mod
/usr/lib/grub/i386-pc/adler32.mod
/usr/lib/grub/i386-pc/affs.mod
……
[root@localhost ~]# rpm -ql grub2-efi-x64-modules
/usr/lib/grub/x86_64-efi
/usr/lib/grub/x86_64-efi/acpi.mod
/usr/lib/grub/x86_64-efi/adler32.mod
/usr/lib/grub/x86_64-efi/affs.mod
……
有了以上包组,我们可以执行安装操作了:
[root@localhost ~]# mkdir /tmp/sdb1
[root@localhost ~]# mount /dev/sdb1 /tmp/sdb1
[root@localhost ~]# grub2-install --target=i386-pc --boot-directory=/tmp/sdb1/boot/ /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.
[root@localhost ~]# grub2-install --target=x86_64-efi --efi-directory=/tmp/sdb1/ --boot-directory=/tmp/sdb1/boot/ --removable
Installing for x86_64-efi platform.
Installation finished. No error reported.
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): d #可以删除bios分区 Partition number (1-2, default 2): 2 Partition 3 has been deleted. Command (m for help): p Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors 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: 9D28857F-1269-A840-90F7-3F0E391B53AF Device Start End Sectors Size Type /dev/sdb1 2048 206847 204800 100M EFI System Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
menuentry 'Linux' { if search --no-floppy --file --set=root /grub2/grub.cfg ; then configfile /grub2/grub.cfg fi } menuentry 'Windows' { if [ 'pc' == $grub_platform ] ; then if search --file --set /bootmgr ; then chainloader +1 elif search --file --set /ntldr ; then chainloader +1 fi elif [ 'efi' == $grub_platform ] ; then if search --file --set=root /EFI/Microsoft/Boot/bootmgfw.efi ; then chainloader /EFI/Microsoft/Boot/bootmgfw.efi fi fi }
[root@localhost ~]# tree /tmp/sdb1/
/tmp/sdb1/
├── boot
│ └── grub2
│ ├── fonts
│ │ └── unicode.pf2
│ ├── grub2.cfg
│ ├── grubenv #环境快,保存用户操作过程中的一些状态信息
│ ├── i386-pc #由grub2-pc-modules包提供
│ │ └── *.mod
│ └── x86_64-efi #由grub2-efi-x64-modules包提供
│ └── *.mod
└── EFI
└── BOOT
└── BOOTX64.EFI #uefi引导文件,可由grub2-mkimage生成
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n #新建分区 Partition number (2-128, default 2): First sector (206848-121110494, default 206848): Last sector, +sectors or +size{K,M,G,T,P} (206848-121110494, default 121110494): +1G Created a new partition 2 of type 'Linux filesystem' and of size 1 GiB. Command (m for help): t #更改分区类型 Partition number (1,2, default 2): 2 Partition type (type L to list all types): 11 Changed type of partition 'Linux filesystem' to 'Microsoft basic data'. Command (m for help): p Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors 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: 16AEEA32-1965-E74A-8270-FD3FCB09E6D5 Device Start End Sectors Size Type /dev/sdb1 2048 206847 204800 100M EFI System /dev/sdb2 206848 2303999 2097152 1G Microsoft basic data Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
[root@localhost ~]# yum -y install ntfsprogs.x86_64
[root@localhost ~]# mkfs.ntfs /dev/sdb2
[root@localhost ~]# mkdir /tmp/sdb2
[root@localhost ~]# mount -t ntfs /dev/sdb2 /tmp/sdb2/
[root@localhost ~]# yum -y install syslinux-nonlinux
[root@localhost ~]# mount /dev/sdb1 /tmp/sdb1/
[root@localhost ~]# cp `rpm -ql syslinux-nonlinux | grep memdisk` /tmp/sdb1/boot/grub2/i386-pc/
[root@localhost ~]# ls /tmp/sdb2/
WIN10PE_x64.iso
menuentry 'WinPE x64' {
if [ 'pc' == $grup_platform ] ; then
if search --file --set=root /WIN10PE_x64.iso ; then
linux $prefix/i386-pc/memdisk iso raw
initrd /WIN10PE_x64.iso
fi
fi
}
[root@localhost ~]# tree /tmp/sdb2/
/tmp/sdb2/
├── BOOT
│ └── BOOT.SDI
├── EFI
│ ├── BOOT
│ │ └── bootx64.efi
│ └── MICROSOFT
│ └── BOOT
│ └── BCD
└── SOURCES
│ └── WIN10PE_x64.WIM
└── WIN10PE_x64.iso
BCD文件(有所调整):
menuentry 'WinPE x64' {
if [ 'efi' == $grub_platform ] ; then
if search --file --set=root /SOURCES/WIN10PE_x64.WIM ; then
chainloader /EFI/BOOT/bootx64.efi
fi
fi
}
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition number (3-128, default 3): First sector (2304000-121110494, default 2304000): Last sector, +sectors or +size{K,M,G,T,P} (2304000-121110494, default 121110494): +20G Created a new partition 3 of type 'Linux filesystem' and of size 20 GiB. Command (m for help): p Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors 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: 16AEEA32-1965-E74A-8270-FD3FCB09E6D5 Device Start End Sectors Size Type /dev/sdb1 2048 206847 204800 100M EFI System /dev/sdb2 206848 2303999 2097152 1G Microsoft basic data /dev/sdb3 2304000 44247039 41943040 20G Linux filesystem Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
[root@localhost ~]# mkfs.ext4 /dev/sdb3
[root@localhost ~]# mkdir /tmp/sdb3
[root@localhost ~]# mount /dev/sdb3 /tmp/sdb3/
[root@localhost ~]# ls /tmp/sdb3/
CentOS-7-x86_64-LiveGNOME-1908.iso CentOS-7-x86_64-Minimal-1908.iso
ubuntu-19.10-desktop-amd64.iso
说明:
(1)这里就会用到我们说的实模式和保护模式,这也是我之前一直不能理解为什么已经loopbac iso,linux vmlinux引导内核启动,为什么还要传递iso-scan/filename,inst.stage2这样指明光盘和根这样的内容。
(2)linux发行版本众多,配置不尽相同,我们应参照光盘中grub.cfg来配置。
(3)经多次测试,loopback光盘后去configfile grub.cfg这个文件不能正确引导光盘执行操作,缺少参数。
menutry 'CentOS-7' { set centos=CentOS-7-x86_64-LiveGNOME-1908.iso if search --file --set=root /$centos ; then #加载linux内核,传递iso-scan/filename内核参数,由内核加载光盘文件启动pe loopback loop /$centos linux (loop)/isolinux/vmlinuz0 root=live:LABEL=CentOS-7-x86_64-LiveGNOME-1908 iso-scan/filename=/$centos rd.live.image initrd (loop)/isolinux/initrd0.img fi } menutry 'Ubuntu 19.10' { set ubuntu=ubuntu-19.10-desktop-amd64.iso if search --file --set=root /$ubuntu ; then set gfxpayload=keep loopback loop /$ubuntu #加载linux内核,传递iso-scan/filename内核参数,由内核加载光盘文件启动pe linux (loop)/casper/vmlinuz iso-scan/filename=/$ubuntu initrd (loop)/casper/initrd fi } }
menuentry 'CentOS 7' { set centos=CentOS-7-x86_64-Minimal-1908.iso if search --file --set=root /$centos ; then loopback loop /$centos probe -u (loop) --set=loopuuid set id=UUID=$loopuuid #加载linux内核,传递iso-scan/filename内核参数,由内核加载光盘文件启动安装程序 linux (loop)/images/pxeboot/vmlinuz inst.stage2=hd:$id iso-scan/filename=/$centos initrd (loop)/images/pxeboot/initrd.img fi } menuentry 'Ubuntu 19.10' { set ubuntu=ubuntu-19.10-desktop-amd64.iso if search --file --set=root /$ubuntu ; then loopback loop /$ubuntu #加载linux内核,传递iso-scan/filename内核参数,由内核加载光盘文件启动安装程序 linux (loop)/casper/vmlinuz iso-scan/filename=/$ubuntu only-ubiquity initrd (loop)/casper/initrd fi }
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n #新建分区 Partition number (4-128, default 4): First sector (44247040-121110494, default 44247040): Last sector, +sectors or +size{K,M,G,T,P} (44247040-121110494, default 121110494): +20G Created a new partition 2 of type 'Linux filesystem' and of size 20 GiB. Command (m for help): t #更改分区类型 Partition number (1-4 default 4): 4 Partition type (type L to list all types): 11 Changed type of partition 'Linux filesystem' to 'Microsoft basic data'. Command (m for help): p Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors 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: 16AEEA32-1965-E74A-8270-FD3FCB09E6D5 Device Start End Sectors Size Type /dev/sdb1 2048 206847 204800 100M EFI System /dev/sdb2 206848 2303999 2097152 1G Microsoft basic data /dev/sdb3 2304000 44247039 41943040 20G Linux filesystem /dev/sdb4 44247040 86190079 41943040 20G Microsoft basic data Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
[root@localhost ~]# yum -y install ntfsprogs.x86_64
[root@localhost ~]# mkfs.ntfs /dev/sdb4
[root@localhost ~]# mkdir /tmp/sdb4
[root@localhost ~]# mount -t ntfs /dev/sdb4 /tmp/sdb4/
[root@localhost ~]# yum -y install syslinux-nonlinux
[root@localhost ~]# mount /dev/sdb1 /tmp/sdb1/
[root@localhost ~]# cp `rpm -ql syslinux-nonlinux | grep memdisk` /tmp/sdb1/boot/grub2/i386-pc/
[root@localhost ~]# ls /tmp/sdb2/
[root@localhost ~]# ls /tmp/sdb4/iso/
Win10_1909.iso
menuentry 'Win10 1909' {
set win=Win10_1909.iso
if [ 'pc' == $grub_platform ] ; then
if search --file --set=root /iso/$win ; then
linux $prefix/i386-pc/memdisk iso raw
initrd /iso/$win
fi
fi
}
[root@localhost ~]# tree -d -L 1 /tmp/sdb4
/tmp/sdb4
├── boot
├── efi
└── sources
menuentry 'Win10 1909' {
if [ 'efi' == $grub_platform ] ; then
if search --file --set=root /iso/$win ; then
chainloader /efi/boot/bootx64.efi
fi
fi
}
U盘为mbr分区格式下,BIOS引导执行过程:
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. #bios Command (m for help): o Created a new DOS disklabel with disk identifier 0x967a6aa2. The old gpt signature will be removed by a write command. #efi(100M)分区 #用命令a设置efi分区为bootable flag #用命令t设置efi分区类型为EFI #mbr格式时,bios启动可使用ntldr /bootmgr来引导win pe Command (m for help): p Disk /dev/sdb: 57.8 GiB, 62008590336 bytes, 121110528 sectors 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: dos Disk identifier: 0x967a6aa2 Device Boot Start End Sectors Size Id Type /dev/sdb1 * 2048 206847 204800 100M ef EFI (FAT-12/16/32)
[root@localhost ~]# tree /tmp/sdb2/
/tmp/sdb2/
├── BOOT
│ ├── BCD
│ └── BOOT.SDI
├── BOOTMGR
└── SOURCES
└── WIN10PE_x64.WIM
BOOTMGR:
BCD:
3. 添加配置
menuentry 'WinPE x64' {
if [ 'pc' == $grup_platform ] ; then
if search --file --set=root /WIN10PE_x64.WIM ; then
ntldr /BOOTMGR
fi
fi
}
set default=0 set timeout=5 insmod lvm insmod fat insmod exfat insmod ntfs insmod iso9660 insmod ext2 insmod xfs insmod part_gpt submenu 'Start System' { menuentry 'Linux' { if search --no-floppy --file --set=root /grub2/grub.cfg ; then configfile /grub2/grub.cfg fi } menuentry 'Windows' { if [ 'pc' == $grub_platform ] ; then if search --file --set /bootmgr ; then chainloader +1 elif search --file --set /ntldr ; then chainloader +1 fi elif [ 'efi' == $grub_platform ] ; then if search --file --set=root /EFI/Microsoft/Boot/bootmgfw.efi ; then chainloader /EFI/Microsoft/Boot/bootmgfw.efi fi fi } } submenu 'Start PE' { menuentry 'WinPE x64' { if search --set=device --no-floppy --file /boot/grub2/grub.cfg ; then probe --set=part --partmap $device if [ 'mbr' == $part -a 'pc' == $grub_platform ] ; then if search --file --set=root /SOURCES/WIN10PE_x64.WIM ; then ntldr /BOOTMGR fi fi if [ 'gpt' == $part -a 'pc' == $grub_platform ] ; then if search --file --set=root /WIN10PE_x64.iso ; then linux $prefix/i386-pc/memdisk iso raw initrd /WIN10PE_x64.iso fi fi fi if [ 'efi' == $grub_platform ] ; then if search --file --set=root /SOURCES/WIN10PE_x64.WIM ; then chainloader /EFI/BOOT/bootx64.efi fi fi } menuentry 'CentOS 7' { set centos=CentOS-7-x86_64-LiveGNOME-1908.iso if search --file --set=root /$centos ; then loopback loop /$centos linux (loop)/isolinux/vmlinuz0 root=live:LABEL=CentOS-7-x86_64-LiveGNOME-1908 iso-scan/filename=/$centos rd.live.image initrd (loop)/isolinux/initrd0.img fi } menuentry 'Ubuntu 19.10' { set ubuntu=ubuntu-19.10-desktop-amd64.iso if search --file --set=root /$ubuntu ; then set gfxpayload=keep loopback loop /$ubuntu linux (loop)/casper/vmlinuz iso-scan/filename=/$ubuntu initrd (loop)/casper/initrd fi } } submenu 'Install System' { menuentry 'CentOS 7' { set centos=CentOS-7-x86_64-Minimal-1908.iso if search --file --set=root /$centos ; then loopback loop /$centos probe -u (loop) --set=loopuuid set id=UUID=$loopuuid linux (loop)/images/pxeboot/vmlinuz inst.stage2=hd:$id iso-scan/filename=/$centos initrd (loop)/images/pxeboot/initrd.img fi } menuentry 'Ubuntu 19.10' { set ubuntu=ubuntu-19.10-desktop-amd64.iso if search --file --set=root /$ubuntu ; then loopback loop /$ubuntu linux (loop)/casper/vmlinuz iso-scan/filename=/$ubuntu only-ubiquity initrd (loop)/casper/initrd fi } menuentry 'Win10 1909' { set win=Win10_1909.iso if [ 'pc' == $grub_platform ] ; then if search --file --set=root /iso/$win ; then linux $prefix/i386-pc/memdisk iso raw initrd /iso/$win fi fi if [ 'efi' == $grub_platform ] ; then if search --file --set=root /iso/$win ; then chainloader /efi/boot/bootx64.efi fi fi } } submenu 'Power Manager' { menuentry 'reboot' { reboot } menuentry 'halt' { halt } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。