Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3652414
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2018-11-20 15:12:10


一、grub2-install 脚本
1.安装 GRUB 文件到 /boot/grub2
2.复制/usr/lib/grub/i386-pc/ 目录下的*.mod、*.lst、*.img到/boot/grub目录下
3.调用grub2-probe扫描计算机并收集磁盘和分区信息
4.调用grub2-mkimage构建一个新的new.img
5.grub2-setup把grub的boot.img写入MBR中,把core.img写进设备的第一个扇区


电脑上安装的系统是Windows10,BIOS+MBR,想利用Grub2安装CentOS,和RemixOS三个操作系统。
为了实现硬盘安装CentOS,需要用到Grub2.

第一步、Grub2下载,文件准备
到Grub2官网ftp://ftp.gnu.org/gnu/grub/ 下载文件grub-2.02-for-windows.zip,将其解压至任意路径下,比如D:\。这样Grub2的主程序目录将位于D:\grub-2.02-for-windows

第二步,Grub2安装
以管理员身份运行CMD,输入命令wmic diskdrive list brief,记录下安装目标磁盘DeviceID,本教程将安装至 \\.\PHYSICALDRIVE0
==================================================================================
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Windows\system32>wmic diskdrive list brief
Caption                DeviceID            Model                  Partitions  Size
WDC WD5000AAKX-001CA0  \\.\PHYSICALDRIVE0  WDC WD5000AAKX-001CA0  3           500105249280
C:\Windows\system32>
==================================================================================
将当前路径切换至Grub2主程序所在路径,输入cd /d d:\grub-2.02-for-windows
==================================================================================
C:\Windows\system32>cd /d d:\grub-2.02-for-windows
d:\grub-2.02-for-windows>
==================================================================================
查看grub-install.exe的帮助文件,可以输入grub-install.exe --help或grub-install.exe -?,显示如下内容:
==================================================================================
Usage: grub-install [OPTION...] [OPTION] [INSTALL_DEVICE]
Install GRUB on your drive.

      --compress=no|xz|gz|lzo   compress GRUB files [optional]
  -d, --directory=DIR        use images and modules under DIR
                             [default=C:\grub-2.02-for-windows/]
      --fonts=FONTS          install FONTS [default=unicode]
      --install-modules=MODULES   install only MODULES and their dependencies
                             [default=all]
  -k, --pubkey=FILE          embed FILE as public key for signature checking
      --locale-directory=DIR use translations under DIR
                             [default=C:\grub-2.02-for-windows/locale]
      --locales=LOCALES      install only LOCALES [default=all]
      --modules=MODULES      pre-load specified modules MODULES
      --themes=THEMES        install THEMES [default=starfield]
  -v, --verbose              print verbose messages.
      --allow-floppy         make the drive also bootable as floppy (default
                             for fdX devices). May break on some BIOSes.
      --boot-directory=DIR   install GRUB images under the directory DIR/grub
                             instead of the boot/grub directory
      --bootloader-id=ID     the ID of bootloader. This option is only
                             available on EFI and Macs.
      --core-compress=xz|none|auto
                             choose the compression to use for core image
      --disk-module=MODULE   disk module to use (biosdisk or native). This
                             option is only available on BIOS target.
      --efi-directory=DIR    use DIR as the EFI System Partition root.
      --force                install even if problems are detected
      --force-file-id        use identifier file even if UUID is available
      --label-bgcolor=COLOR  use COLOR for label background
      --label-color=COLOR    use COLOR for label
      --label-font=FILE      use FILE as font for label
      --macppc-directory=DIR use DIR for PPC MAC install.
      --no-bootsector        do not install bootsector
      --no-nvram             don't update the `boot-device'/`Boot*' NVRAM
                             variables. This option is only available on EFI
                             and IEEE1275 targets.
      --no-rs-codes          Do not apply any reed-solomon codes when
                             embedding core.img. This option is only available
                             on x86 BIOS targets.
      --product-version=STRING   use STRING as product version
      --recheck              delete device map if it already exists
      --removable            the installation device is removable. This option
                             is only available on EFI.
  -s, --skip-fs-probe        do not probe for filesystems in DEVICE
      --target=TARGET        install GRUB for TARGET platform
                             [default=i386-pc]; available targets: arm-efi,
                             arm-uboot, arm64-efi, i386-coreboot, i386-efi,
                             i386-ieee1275, i386-multiboot, i386-pc,
                             i386-qemu, i386-xen, ia64-efi, mips-arc,
                             mips-qemu_mips, mipsel-arc, mipsel-loongson,
                             mipsel-qemu_mips, powerpc-ieee1275,
                             sparc64-ieee1275, x86_64-efi, x86_64-xen
  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

INSTALL_DEVICE must be system device filename.
grub-install copies GRUB images into boot/grub.  On some platforms, it may
also install GRUB into the boot sector.

Report bugs to .
==================================================================================
参数说明:
大体思路是,先指定源文件的路径,如:fonts,locale,i386-pc,然后再通过参数确定安装哪些字体,主题,模块。
--directory 指定镜像和模块的路径,本教程将此参数设置为--directory=D:\grub-2.02-for-windows\i386-pc,如果把grub2压缩包解压至c盘,此参数可以不进行设置,因为default=C:\grub-2.02-for-windows/,platform会根据当前的平台从i386-pc和i386-efi两个路径中进行自动选择。
--fonts 指定字体,默认的为unicode,留空将会在最终安装完成目录下生成unicode.pf2文件。
--install-modules 指定安装模块,留空将安装全部。
--modules 指定预加载的模块。
--locale-directory 指定locale的路径,本教程将此餐宿设置为 --locale-directory=D:\grub-2.02-for-windows\locale,如果把grub2压缩包解压至c盘,此参数可以不进行设置,因为default=C:\grub-2.02-for-windows/locale。
--locales 指定安装的locale,需要将zh_CN文件夹内的grub.mo改名为zh_CN.mo,并放置在locales文件夹目录下
--modules 指定预加载模块。
--themes 指定主题
--boot-directory 指定启动路径,本教程将grub文件夹设置在c:\,故将参数设置为--boot-directory=c:,grub文件夹自动生成。
--force 强制执行,但是遇到32 Sector存在FlexNET的问题,依然无法强制写入,只能是保证后续程序继续运行。
--label-bgcolor 设置启动项标签的背景色
--label-color 设置标签的颜色
--label-font 设置标签的字体
--no-bootsector 不写入启动扇区设置
--recheck 如果存在磁盘映射,将删除磁盘映射
--target 设置安装目标,在本教程中此项可留空。因为默认即为i386-pc,完整命令为--target=i386-pc
完整的运行命令为:

d:\grub-2.02-for-windows>grub-install.exe --directory=D:\grub-2.02-for-windows\i386-pc --locale-directory=D:\grub-2.02-for-windows\locale --locales=zh_CN --themes=starfield --boot-directory=c: --force --recheck --target=i386-pc \\.\PHYSICALDRIVE0
==================================================================================

grub-install.exe
--directory=D:\grub-2.02-for-windows\i386-pc
--fonts=unicode (为默认项,可不写)
--install-modules=all (如果安装全部,参数不能为all,必须删除)
--locale-directory=D:\grub-2.02-for-windows\locale
--locales=zh_CN (默认为all)
--themes=starfield (此项参数为默认,可不写)
--boot-directory=c:
--force
--label-bgcolor=black (默认项,可不写)
--label-color=wite (默认项,可不写)
--label-font =unicode(默认项,可不写)

--recheck
--target=i386-pc (默认项可不写)
\\.\PHYSICALDRIVE0 (安装设备)

如果文件解压路径为C:\,命令可以进行精简,其他的都使用默认设置,其他的使用以上的默认设置:

d:\grub-2.02-for-windows>grub-install.exe \\.\PHYSICALDRIVE0
第三步、grub.cfg文件生成

由于windows环境下无法使用grub-mkcofig生成grub.cfg文件,需要自己编辑生成grub.cfg文件,该文件的编码格式为UTF-8,我们可以使用记事本生成。

首先使用记事本新建一个文本文档,然后输入grub.cfg内容,另存为-将编码格式修改为UTF-8。

具体内容如下:

==================================================================================

#################################################
#设置菜单的超时时间为5秒
set timeout=5
#每一满屏后暂停输出,以免信息太多一闪而过看不清
set pager=1
################################################
#默认启动第一个菜单项
set default=0
#如果第一个菜单项启动失败,转而启动第二个菜单项
set fallback=1
#################################################
#开启密码验证功能,并设置一个名为'admin'的超级用户
set superusers=admin
#################################################
#设置主题模式,支持的模式可以grub引导界面,输入e,进入命令行,输入videoinfo查看。
set gfxmode=1600x900x32,auto
#设置主题
set theme=$prefix/themes/starfield/theme.txt
#################################################
#指定翻译文件(*.mo)的目录,若未明确设置此目录,则无法显示中文界面。
set locale_dir=$prefix/locale
#将GRUB2设置为简体中文界面
set lang=zh_CN
#################################################
function load_video {
insmod efi_gop
insmod efi_uga
insmod video_bochs
insmod video_cirrus
insmod all_video
}
function load_disk {
insmod part_gpt
insmod part_msdos
}
function load_filesystem {
insmod ext2
insmod ext3
insmod ext4
insmod ntfs
}
load_video
load_disk
load_filesystem
#################################################
#激活图形模式的输出终端
insmod gfxterm
terminal_output  gfxterm

insmod gfxmenu
insmod png

loadfont $prefix/themes/starfield/dejavu_bold_14.pf2
export theme
#################################################
#设置'root'用户的哈希密码[通过"grub-mkpasswd-pbkdf2"工具生成]
password_pbkdf2 admin grub.pbkdf2.sha512.10000.29CFC23F7FC74CA1A2AA0CFFCC3CE9D38EB7D3FCFD7A9BFA172AAC1816D580C3076DFC7DCBB2B6944B86765793F10D5B804760FFBBF012CE4B79A5ACD6DB8298.7A3F7ADB7A50D7B387BCF07E425276C6247039F63A37EEC744E91D0191283C8353167B9D32751E9E27E9E99A9EBB8ABBEB40BE5171098096EA47735A5F8CC418
#################################################
menuentry 'Windows 10' --class class --users admin --hotkey "w" --id windows {
search --file --set=root --no-floppy /bootmgr
chainloader +1
}

menuentry 'CentOS 7' --class fedora --class gnu-linux --class gnu --class os --unrestricted --hotkey "c" --id linux {
search --file --set=root --no-floppy /CentOS7.iso
loopback loop0 /CentOS7.iso
linux (loop0)/isolinux/vmlinuz inst.repo=hd:LABEL=WorkData:/CentOS7.iso rootwait inst.lang=zh_CN.UTF-8
initrd (loop0)/isolinux/initrd.img
}

menuentry 'RemixOS-PC' --class class --unrestricted --hotkey "r" --id android {

}
---------------------
作者:xinlan3618
来源:CSDN
原文:https://blog.csdn.net/xinlan3618/article/details/78952698
版权声明:本文为博主原创文章,转载请附上博文链接!
阅读(4503) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~