在Red Hat Enterprise 7上配置Grub2设置Windows和Linux双启动
Grub2在Linux CentOS 7或者Red Hat Enterprise 7中是默认引导加载程序。
我们将通过它配置一个从CentOS 7和Windows7双引导的系统。
Grub2重要文件和目录
在/boot目录下,您有两个子目录,一个用于grub,一个用于grub2。
我们当然将使用grub2。
# cd /boot/grub2
在此目录中,您会看到许多文件和目录:
# ls -l total 32 -rw-r--r--. 1 root root 64 May 22 20:27 device.map drwxr-xr-x. 2 root root 24 May 22 20:27 fonts -rw-r--r--. 1 root root 5044 May 23 21:23 grub.cfg -rw-r--r--. 1 root root 1024 May 23 21:15 grubenv drwxr-xr-x. 2 root root 8192 May 22 21:11 i386-pc drwxr-xr-x. 2 root root 4096 May 22 21:11 locale drwxr-xr-x. 3 root root 19 May 22 20:23 themes
在device.map文件中,您将看到设备和grub2设备名称之间的转换。
比如下面示例中,只有一个磁盘/dev/sda使用grub2设备名称hd0:
# cat device.map # this device map was generated by anaconda (hd0) /dev/sda
另一个重要文件是grub.cfg文件,该文件是保存配置脚本的文件(由grub2-mkconfig保存)。
# head grub.cfg ## DO NOT EDIT THIS FILE ## It is automatically generated by grub2-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub #### BEGIN /etc/grub.d/00_header ###
注意第一行"请勿编辑此文件"。
然后是说明,我们需要使用/etc/grub.d下的grub2模板文件或者grub2默认设置文件/etc/default/grub。
默认Grub配置文件
我们首先来看一下/etc/default下的默认grub2配置文件。
如前所述,这是进行默认配置设置的位置,例如引导默认菜单项之前的时间(默认为5秒)。
# cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us rhgb quiet" GRUB_DISABLE_RECOVERY="true"
将GRUB_TIMEOUT变量更改为10秒。
我们还要添加一个不在默认grub配置中的变量:GRUB_DISABLE_LINUX_UUID =" true",然后保存文件。
最后一个变量将使用绝对设备名称代替UUID,即/dev/sdXY。
现在要激活此配置,您将需要使用工具grub2-mkconfig生成一个新的/boot/grub2/grub.cfg文件。
grub2-mkconfig
如果运行工具grub2-mkconfig,您将看到很多文本(屏幕上生成的脚本)。
将配置写入文件中:
# grub2-mkconfig -o /boot/grub2/grub.cfg
现在应将默认设置写入磁盘(重新启动以进行验证,请注意引导超时)。
在默认配置中使用GRUB_DISABLE_LINUX_UUID条目,当您运行grub2-mkconfig时,您会看到类似以下内容:
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img Found linux image: /boot/vmlinuz-3.10.0-123.4.2.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-123.4.2.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-957d0004a0aa4a6080dd929ffd92555c Found initrd image: /boot/initramfs-0-rescue-957d0004a0aa4a6080dd929ffd92555c.img done
现在我们来演示一下,从默认配置中删除该行并保存文件。
重新运行configure 命令:
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... No device found for PV 84ARqO-Fhcl-dNC0-dVa0-7EpR-Nl9K-pzdyQR. No device found for PV 84ARqO-Fhcl-dNC0-dVa0-7EpR-Nl9K-pzdyQR. No device found for PV 84ARqO-Fhcl-dNC0-dVa0-7EpR-Nl9K-pzdyQR. No device found for PV 84ARqO-Fhcl-dNC0-dVa0-7EpR-Nl9K-pzdyQR. Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img Found linux image: /boot/vmlinuz-3.10.0-123.4.2.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-123.4.2.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-957d0004a0aa4a6080dd929ffd92555c Found initrd image: /boot/initramfs-0-rescue-957d0004a0aa4a6080dd929ffd92555c.img done
请忽略"No device found for PV 84ARqO-Fhcl-dNC0-dVa0-7EpR-Nl9K-pzdyQR."消息,但可以看到现在已使用UUID。
对于而言,UUID可能会非常令人困惑,因为人们可以更轻松地读取绝对设备符号,例如/dev/sda1。
但是UUID可以节省很多时间,因为如果您在分区表中重新排列分区(例如,插入/删除分区)并且更改分区号,则grub无法找到要引导的内核。
如果您使用UUID,即使重新排列分区也不会中断。
/etc/grub.d目录
让我们转到/etc/grub.d目录,看看在该目录中完成了什么。
# ls -l total 68 -rwxr-xr-x. 1 root root 8698 Jun 30 18:16 00_header -rwxr-xr-x. 1 root root 9517 Jun 30 18:16 10_linux -rwxr-xr-x. 1 root root 10275 Jun 30 18:16 20_linux_xen -rwxr-xr-x. 1 root root 2559 Jun 30 18:16 20_ppc_terminfo -rwxr-xr-x. 1 root root 11110 Jun 30 18:16 30_os-prober -rwxr-xr-x. 1 root root 214 May 22 21:55 40_custom -rwxr-xr-x. 1 root root 216 Jun 30 18:16 41_custom
您会看到该目录包含不同的脚本,这些脚本用于通过grub2-mkconfig POSIX Shell脚本生成/boot/grub2/grub.cfg文件。
这些模板按顺序使用(00至41)。
Windows 7和CentOS 7双系统
假设,我们的机器上有多个磁盘,但是我们在同一SSD磁盘上安装了Windows和CentOS。
# fdisk -l |grep "Disk /dev" Disk /dev/sda: 120.0 GB, 120034123776 bytes, 234441648 sectors Disk /dev/sde: 3000.6 GB, 3000592982015 bytes, 5860533168 sectors Disk /dev/sdb: 3000.6 GB, 3000592982015 bytes, 5860533168 sectors Disk /dev/sdc: 3000.6 GB, 3000592982015 bytes, 5860533168 sectors Disk /dev/sdd: 3000.6 GB, 3000592982015 bytes, 5860533168 sectors Disk /dev/mapper/centos-swap: 8396 MB, 8396996608 bytes, 16400384 sectors Disk /dev/mapper/centos-root: 35.9 GB, 35907436544 bytes, 70131712 sectors Disk /dev/mapper/centos-home: 17.5 GB, 17527996416 bytes, 34234368 sectors
我们首先安装了Windows 7,并使用了第一块SSD磁盘的一半空间,而其余部分未分区。
然后,我们安装了CentOS 7(或者Red Hat Enterprise 7)并使用了磁盘的另一半。
如果我们给命令blkid,我们将看到以下分区:
# blkid /dev/block/253:0: UUID="ba700a61-092c-4042-a134-2876ae49e741" TYPE="swap" /dev/sda1: LABEL="Door systeem gereserveerd" UUID="76449D01449CC571" TYPE="ntfs" /dev/sda2: UUID="944A9D9F4A9D7F24" TYPE="ntfs" /dev/sda3: UUID="5abb20e8-9493-46bc-89bd-887452403619" TYPE="xfs" /dev/sda5: UUID="uJpBkk-ugcH-xrOR-DIZu-eJnJ-0g6a-ImDwo2" TYPE="LVM2_member" /dev/dm-2: UUID="5432cf99-6222-486b-9e21-f9dd7a663b48" TYPE="xfs" /dev/dm-1: UUID="57f5fade-253b-442d-be1a-d50906edb3d6" TYPE="xfs"
如您所见,我们的Windows 7分区安装在/dev/sda1上,在Grub2中为hd0,1。
请注意,sda磁盘为0,sdb为1。
但是,分区从sda1的1开始或者第二个磁盘的sdb1开始。
(与grub不同,后者在hd0,0上具有第一个分区)在我们的情况下,Linux分区是/dev/sda3,在grub2 hd0,3中。
但是,如果您查看生成的/boot/grub2/grub.cfg文件:
# cat /boot/grub2/grub.cfg |grep msdos insmod part_msdos set root='hd0,msdos3' search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos3 --hint-efi=hd0,msdos3 --hint-baremetal=ahci0,msdos3 --hint='hd0,msdos3' 76f0fe64-3654-4dc2-b914-45d8a78d4829
您会看到生成了以下符号:" hd0,msdos3"(即sda3)。
现在,我们为Windows 7添加一个模板。
您可以在40_custom中执行此操作,但我们希望在10至20之间添加它。
因此,vi /etc/grub.d/15_windows7:
#! /bin/sh -e echo "Adding Windows 7" >&2 cat <<EOF menuentry "Windows 7" { set root=(hd0,1) chainloader +1 } EOF
注意:确保脚本具有可执行权限,如果没有 则执行 chmod +x /etc/grub.d/15_windows7
如果脚本格式错了,grub2-mkconfig也会无法读取。
您可以看到我们添加了(hd0,1)/dev/sda1来启动Windows(这是Windows的启动管理器所在的位置)。
现在,运行grub2-mkconfig -o /boot/grub2/grub.cfg
以生成并保存新的配置文件。
重新启动并选择要启动的Windows分区(当然,您应该输入磁盘,如果Windows无法启动,请重试,因为您可能选择了错误的磁盘)。
grub2菜单应如下所示:
CentOS 7
CentOS 7 Rescue
Windows 7
默认情况下选择第一个选项(CentOS 7)。
如果想默认选择Windows 7
那么执行命令
grub2-set-default 2