无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站广告联系 微信:wuyouceo QQ:184822951
查看: 8781|回复: 5
打印 上一主题 下一主题

[原创] grub2如何PXE启动

  [复制链接]
跳转到指定楼层
1#
发表于 2018-10-26 18:07:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 jie_china 于 2018-10-31 16:36 编辑

GRUB2版本下载地址:
http://alpha.gnu.org/gnu/grub/
http://ftp.gnu.org/gnu/grub/

GRUB2说明:
https://www.gnu.org/software/grub/manual/grub/grub.html
http://www.jinbuguo.com/linux/grub.cfg.html
https://www.cnblogs.com/f-ck-need-u/p/7094693.html

GRUB2 PXE启动方法:
http://www.manobit.com/pxe-multi ... os-and-efi-support/

制作PXE启动文件grub2pxe.0
@echo off
del/f/s/q .\grub2pxe.0
grub-mkimage -d .\i386-pc -O i386-pc-pxe -o ./grub2pxe.0 --prefix=(tftp)/boot/grub2/ -c start.cfg -v pxe pxechain net tftp http biosdisk blocklist boot configfile linux linux16 reboot normal search cat progress lvm part_gpt part_msdos cpio ext2 fat hfs hfsplus iso9660 ntfs tar udf ufs1 ufs2 xfs zfs loopback newc
pause

start.cfg文件:
normal (tftp)/boot/grub2/grub.cfg
2#
 楼主| 发表于 2018-10-26 22:53:10 | 只看该作者
本帖最后由 jie_china 于 2018-10-26 22:55 编辑

http://forum.ubuntu.org.cn/viewtopic.php?t=324418

Linux下定制安装Grub2内核实战

玩grub2也有一段时间了,曾经对grub2的实现很不理解,不过经过把grub2的官方英文文档重点部分详细研究一番之后,总算收获不小,现在分享给大家!
grub2功能确实很强大,还可以用BURG实现非常炫的启动项选择画面,挺好玩的,个人感觉比G4D好,但也有不足,例如启动PE似乎有点困难,你如果有方法,分享一下嘛!
注意:1)确定你已经理解本文,再进行操作!2)本文已在openSUSE系统及U盘上实战过,均成功安装,其他linux版本可能会有些不同,在虚拟机ubuntu上看了下,似乎有点不同;3)本文不免有错误的地方,抛砖引玉而已!

定制安装grub2内核到硬盘MBR

环境:openSUSE系统,/dev/sda3挂载在根目录,/boot在单独的分区上,/dev/sda1挂载到/boot目录,如果你没有单独分区,需做相应修改;
实现目标:把grub2安装到/dev/sda的MBR上,grub2的所有文件放到/dev/sda1的/bootin/grub2目录中,步骤如下:
1.首先创建/boot/bootin/grub2目录,并把/usr/lib/grub2/i386-pc/下的所有文件拷贝到此目录中;注意,此目录中原先并没有grub.cfg和core.img文件,即这两个文件都是在系统安装grub2到硬盘的时候才生成的。
2.定制内核
使用的命令为:grub2-mkimage,首先看下其帮助文档,了解此命令的用法;
# grub2-mkimage --help
Usage: grub2-mkimage [OPTION]... [MODULES]
Make a bootable image of GRUB.
-d, --directory=DIR use images and modules under DIR [default=/usr/lib/grub2/i386-pc]
#指定定制内核所需模块等文件所在目录;
-p, --prefix=DIR set grub_prefix directory [default=/boot/grub2]
#指定prefix变量值,将来grub2启动后会在此目录寻找模块等文件;
-m, --memdisk=FILE embed FILE as a memdisk image
-f, --font=FILE embed FILE as a boot font
-c, --config=FILE embed FILE as boot config
-o, --output=FILE output a generated image to FILE [default=stdout]
#指定所定制的内核输出到哪个文件,例如/boot/grub2/core.img;
-h, --help display this message and exit
-V, --version print version information and exit
-v, --verbose print verbose messages
此处定制内核具体使用的命令如下:
代码: 全选
grub2-mkimage -p /bootin/grub2 -o /boot/bootin/grub2/core.img biosdisk part_msdos ntfs fat ext2
注释:1)-d使用默认值/usr/lib/grub2/i386-pc;2)-p指定prefix变量为/bootin/grub2;3)-o即输出定制的内核到/boot/bootin/grub2/core.img文件中;4)biosdisk part_msdos ntfs fat ext2,内核所包含的模块,不需要太多;
3.创建device.map文件
创建文件/boot/bootin/grub2/device.map,此文件内容的含义具体在下文解释,若是要安装到硬盘MBR,device.map文件内容如下:
代码: 全选
(hd0) /dev/sda
4.安装到MBR
所用命令为grub2-setup,首先看下其帮助文档,了解此命令的用法;
grub2-setup --help
Usage: grub2-setup [OPTION]... DEVICE
Set up images to boot from DEVICE.
#安装grub到硬盘MBR或分区PBR;
DEVICE must be a GRUB device (e.g. `(hd0,1)').
-b, --boot-image=FILE use FILE as the boot image [default=boot.img]
-c, --core-image=FILE use FILE as the core image [default=core.img]
-d, --directory=DIR use GRUB files in the directory DIR [default=/boot/grub2]
#此目录即安装grub2时所需的boot.img和core.img等文件所在目录;
-m, --device-map=FILE use FILE as the device map [default=/boot/grub2/device.map]
#指定device.map文件所在目录;
-r, --root-device=DEV use DEV as the root device [default=guessed]
#此设备即将来启动grub2时所需的模块等文件所在的分区,即通常情况下/boot/grub2目录所在的分区;(个人理解,应该没错)
-f, --force install even if problems are detected
-s, --skip-fs-probe do not probe for filesystems in DEVICE
-h, --help display this message and exit
-V, --version print version information and exit
-v, --verbose print verbose messages
此处安装到硬盘MBR具体使用的命令如下:
代码: 全选
grub2-setup -d /boot/bootin/grub2 -m /boot/bootin/grub2/device.map -r \(hd0,1\) -v \(hd0\)
注释:1)-d即安装grub2时所需的boot.img和core.img等文件所在目录为/boot/bootin/grub2;2)-m即指定device.map文件所在目录为/boot/bootin/grub2/device.map;3)末尾的-r (hd0,1)即将来启动grub时所需文件在本地硬盘第一个分区;后面(hd0)即安装grub到本地硬盘MBR;4)在shell中,小括号”(“和”)”有特殊意义,因而命令末尾的(hd0,1)使用了反斜杠来转义;
解释下device.map文件的意义:首先,其实上述命令中的(hd0)并不意味着grub会安装到“本地硬盘”的MBR中,(hd0,1)也并不意味着grub所需文件在“本地硬盘”第一个分区上;因为在实际写入到设备时,(hd0)会经过device.map文件的映射;只有当device.map文件内容为(hd0) /dev/sda时,上面的说法才成立;而当device.map文件内容为(hd0) /dev/sdb时,比如你插入一个U盘,这时,(hd0)被映射为/dev/sdb,即你的U盘;上述命令将会写入grub到你U盘的MBR上,而将来启动时也会在你U盘上的第一个分区中查找/boot/grub下的模块等文件;这也意味着,如果你要安装grub到U盘,只要将device.map文件内容改为(hd0) /dev/sdb就可以了! 上述命令会有以下输出:
代码: 全选
grub2-setup: info: the size of hd0 is 312581808.
      … … … …
      grub2-setup: info: the size of hd0 is 312581808.
      grub2-setup: info: getting the size of /boot/bootin/grub2/boot.img.
      grub2-setup: info: reading /boot/bootin/grub2/boot.img.
      grub2-setup: info: getting the size of /boot/bootin/grub2/boot.img.
      grub2-setup: info: getting the size of /boot/bootin/grub2/core.img.
      grub2-setup: info: reading /boot/bootin/grub2/core.img.
      grub2-setup: info: getting the size of /boot/bootin/grub2/core.img.
      grub2-setup: info: the size of hd0 is 312581808.
      grub2-setup: info: the size of hd0 is 312581808.
      grub2-setup: info: setting the root device to `hd0,1'.
      grub2-setup: info: dos partition is 0, bsd partition is -1.
      grub2-setup: info: the core image will be embedded at sector 0x1.
这表示写入成功!
5.创建grub.cfg文件
在/boot/bootin/grub2/目录中创建grub.cfg文件,并拷贝原先系统/boot/目录下的vmlinuz及initrd文件到/boot/bootin/目录下,并在grub.cfg文件中创建对应的启动菜单,就可以重启了,All Over!

定制安装grub2内核到U盘MBR

环境:openSUSE系统,U盘在linux下为/dev/sdb;并挂载到/mnt/usb/目录;
实现目标:把grub2安装到U盘的MBR上,grub2的所有文件放到U盘的/bootin/grub2目录中,步骤跟安装到硬盘很相似,区别只有一步,具体如下:
1.挂载U盘到/mnt/usb/目录,在U盘创建/bootin/grub2目录,并把/usr/lib/grub2/i386-pc/下的所有文件拷贝到此目录中;
2.定制内核
具体使用的命令如下:
代码: 全选
grub2-mkimage -p /bootin/grub2 -o /mnt/usb/bootin/grub2/core.img biosdisk part_msdos ntfs fat ext2
注释:1)-d使用默认值/usr/lib/grub2/i386-pc;2)-p指定prefix变量为/bootin/grub2;3)-o即输出定制的内核到/mnt/usb/bootin/grub2/core.img文件中;4)biosdisk part_msdos ntfs fat ext2,内核所包含的模块;
3.创建device.map文件
这是唯一区别的地方!创建文件/mnt/usb/bootin/grub2/device.map,文件内容如下:
代码: 全选
(hd0) /dev/sdb   
#/dev/sdb需据实际情况修改;
4.安装到MBR
具体使用的命令如下:
代码: 全选
grub2-setup -d /mnt/usb/bootin/grub2 -m /mnt/usb/bootin/grub2/device.map -r \(hd0,1\) -v \(hd0\)
注释:1)-d即安装grub2时所需的boot.img和core.img等文件所在目录为/mnt/usb/bootin/grub2;2)-m即指定device.map文件所在目录为/mnt/usb/bootin/grub2/device.map;3)-r (hd0,1)即将来启动grub时所需文件在U盘第一个分区;后面(hd0)即安装grub到U盘MBR;
在执行这步时,会发现U盘的指示灯闪呀闪,验证了grub正在安装到U盘上;
5.创建grub.cfg文件
在U盘/bootin/grub2/目录中创建grub.cfg文件,并在grub.cfg文件中创建对应的启动菜单,就可以重启了,All Over!!

注意:在安装到硬盘上时,定制的内核文件需要包含blocklist.mod,否则有时会出现不能启动的问题!!U盘上无此问题!!!
上次由 瑟瑟秋风 在 2011-04-19 20:28,总共编辑 8 次。
回复

使用道具 举报

3#
 楼主| 发表于 2018-10-26 22:54:00 | 只看该作者
本帖最后由 jie_china 于 2018-10-31 20:35 编辑

本帖最后由 zhangyi514 于 2017-12-16 05:07 编辑

编译时指定内置配置文件,内置配置文件加载外部配置,DHCP下http,tftp正常,自定义IP未测试通过。
如下:

if search --no-floppy -f --set=root /grub/grub.cfg ; then
configfile (tftp)/grub.cfg
fi

if search --no-floppy -f --set=root /grub/grub.cfg ; then
configfile (http)/grub.cfg
fi


--------------------------------------------------------------

menuentry "Archlinux-2013.05.01-dual.iso" --class iso {
  set isofile="/archives/archlinux-2013.05.01-dual.iso"
  set partition="6"
  loopback loop (hd0,$partition)/$isofile
  linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop
  initrd (loop)/arch/boot/x86_64/archiso.img
}

上边这段是摘自archlinux的wiki,于是尝试用量产为cdlinxu的u盘启动电脑,我的c盘根目录有个win7pe.iso,是个win7pe维护内置版!
在现实启动菜单是,按下e键,这个和grub4dos相同,进入命令行界面:
lsmod        //查看已经载入的模块,发现有ntfs模块,我的c盘为ntfs
loopback loop0 (hd0,msdos1)/winpe.iso         //当然也可以是loop1、loop2……
ls (loop0)/                     //看看虚拟设备的根目录,可以访问
insmod ntldr                  //bootmgr 和ntldr都需要这个模块
ntldr (loop0)/7777/7pe_mgr
boo
回复

使用道具 举报

4#
发表于 2018-10-29 20:05:43 | 只看该作者
讲解的很详细
回复

使用道具 举报

5#
 楼主| 发表于 2018-10-30 23:43:30 | 只看该作者
本帖最后由 jie_china 于 2018-10-30 23:47 编辑

http://bbs.wuyou.net/forum.php?mod=viewthread&tid=403145

@echo off

rmdir /s /q pxeboot
mkdir pxeboot\i386-pc
mkdir pxeboot\locale\fm

::::中文支持
.\bin\msgfmt.exe grub\locale\zh_CN.po -o pxeboot\locale\zh_CN.mo
.\bin\msgfmt.exe lang\zh_CN\fm.po -o pxeboot\locale\fm\zh_CN.mo
copy lang\zh_CN\lang.sh  boot\grub\
copy lang\zh_CN\text.txt boot\grub\themes\slack\
copy lang\zh_CN\theme.txt boot\grub\themes\slack\

.\bin\grub-mkimage.exe -d .\grub\i386-pc -O i386-pc-pxe --output=managerpxe --prefix=(pxe) pxe tftp newc http net linux biosdisk loopback tar part_msdos fat exfat ntfs search_fs_file all_video video_bochs video_cirrus  font gfxterm gfxmenu gettext jpeg png


copy managerpxe pxeboot\ /y

:::::copy legacy\config.cfg pxeboot\grub.cfg /y

::::::::ren pxeboot\config.cfg grub.cfg
echo normal (tftp)/init.sh >pxeboot\grub.cfg

xcopy grub\i386-pc pxeboot\i386-pc /s /e /y
xcopy grub\locale pxeboot\locale /s /e /y
xcopy boot\grub pxeboot /s /e /y
xcopy legacy pxeboot /s /e /y

copy init.txt pxeboot\init.sh /y
copy net.iso pxeboot\net.iso /y

-----------------------------------------------------------------------
# Grub2-FileManager
# Copyright (C) 2016,2017  A1ive.
#
# Grub2-FileManager is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Grub2-FileManager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Grub2-FileManager.  If not, see <http://www.gnu.org/licenses/>.

set pager=1;
if regexp 'pc' "$grub_platform"; then
        modlist="all_video bitmap bitmap_scale blocklist bsd cat cmp cpuid crc datetime dd disk drivemap elf file getkey gfxmenu gfxterm gfxterm_background gfxterm_menu gptsync hashsum hexdump jpeg loadenv lsapm macho memdisk multiboot multiboot2 net offsetio parttool png procfs progress random search_fs_uuid search_label sendkey squash4 syslinuxcfg terminfo tga time trig true vbe vga video video_bochs video_cirrus video_colors video_fb videoinfo xnu";
else
        modlist="all_video video_bochs video_cirrus efi_gop efi_uga gfxterm gfxterm_background gfxmenu jpeg png tga font";
        search -s -f -q /efi/microsoft/boot/bootmgfw.efi;
        if regexp 'i386' "$grub_cpu"; then
                search -s -f -q /efi/boot/bootia32.efi;
        else
                search -s -f -q /efi/boot/bootx64.efi;
        fi;
fi;
for module in $modlist; do
        insmod $module;
done;
set enable_progress_indicator=0; export enable_progress_indicator;
loadfont ${prefix}/fonts/unicode.xz;
set locale_dir=${prefix}/locale;set secondary_locale_dir=${prefix}/locale/fm;
export locale_dir; export secondary_locale_dir;
source ${prefix}/lang.sh; export lang;
set gfxmode=1024x768; export gfxmode;
set gfxpayload=keep; export gfxpayload;
terminal_output gfxterm;
set color_normal=white/black;
set color_highlight=black/white;
set encoding="utf8"; export encoding;
echo "正在挂载$net_default_server服务器上的镜像..稍候";
loopback boot (http)/boot.iso;
path="(boot)/"; export path;
configfile $prefix/main.sh;
回复

使用道具 举报

6#
发表于 2018-11-15 16:06:21 | 只看该作者
GRUB2 UEFI网络启动,bootp无法初始化网卡,不知怎么处理,特来看看。非legacy模式,是efi
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2024-4-19 19:16

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表