环境: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!
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
-----------------------------------------------------------------------
# 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;作者: 友联电脑 时间: 2018-11-15 16:06
GRUB2 UEFI网络启动,bootp无法初始化网卡,不知怎么处理,特来看看。非legacy模式,是efi