推荐连接:
grub应用
grub简介
grub: Grand Uniform Bootloader
两个版本:
- GRUB 0.X : Grub Legacy 传统版
- GRUB 1.X : Grub2,CentOS 7 (基本完全重写)
GRUB legacy:传统版
- stage1:mbr
- stage1.5:mbr之后的扇区,让stage1中的bootleader能识别stage2所在的分区上的文件系统;
- stage2: 磁盘分区 /boot/grub目录
配置文件:/boot/grub/grub.conf <-- /etc/grub.conf
简述:当系统启动时,要加载grub所在的磁盘时,会读取该磁盘的MBR并加载到stage1,加载完stage1后会尝试读取随后扇区的stage1.5,读到1.5阶段后从而能够驱动真正的第二阶段所在的磁盘分区了;
加载的磁盘分区不仅有第二阶段,还有内核文件、ramdisk等等都在该分区上放着;
演示: [root@centos6 ~]# ls -l /etc/grub.conf lrwxrwxrwx. 1 root root 22 Dec 7 11:22 /etc/grub.conf -> ../boot/grub/grub.conf
stage1.5:提供文件系统驱动
stage2及内核等通常放置于一个基本磁盘分区
grub功用
(1)提供菜单、并提供交互式接口 e:编辑模式,用于编辑菜单; c:命令模式,交互式接口; (2) 加载用户选择的内核或操作系统 允许传递参数给内核 可隐藏此菜单 (3) 为菜单提供了保护机制 为编辑菜单进行认证 为启用内核或操作系统进行认证
如何识别设备
boot单独分区的原因:是想把根/做的很复杂,不使用逻辑卷boot和根可以使用一个分区的;
grub是否单独分区绝对其访问路径是不一样的;
使用root命令设定设备
(hd#,#)
hd#:磁盘编号,用数字表示;从0开始编号
#:分区编号,用数字表示;从0开始编号
grub的命令行接口
help:获取帮助列表
help KEYWORD:详细帮助信息
find (hd#,#)/PATH/TO/SOMEFILE; #指明磁盘分区找文件,支持tab补全;快速定位文件存在性;
grub> find (hd0,0)/vmlinuz-2.6.32-573.el6.x86_64
root (hd#,#) :设置当前使用的根设备;根设备指第二阶段所在磁盘分区;
kernel /PATH/TO/KERNEL_FILE:设定本次启动时用到的内核文件;额外还可以添加许多内核文件使用的cmdline参数;
kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash init=表示哪个文件当Init程序 vmlinuz中z表示压缩存放;
initrd /PATH/TO/INITRAMFS_FILE: 设定为选定的内核提供额外文件的ramdisk;
注意:ramdisk必须与提供的内核完全匹配;
initrd /initramfs.img
boot: 引导启动选定的内核;
手动在grub命令行接口启动系统:
1指明根 grub> root (hd#,#) 2指明内核 grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE 3指明ramfs文件 grub> initrd /initramfs-VERSION-RELEASE.img 4启动 grub> boot
grub之help命令
grub之find命令
grub之root、kernel、initrd
配置文件: /boot/grub/grub.conf
grub在启动时会读取配置文件,并设置启动
配置项: default=#:设定默认启动的菜单项:菜单项(title)编号从0开始 timeout=#:指定菜单项等待选项选择的时长; splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜单背景图片文件路径; hiddenmenu:隐藏菜单; password [--md5] STRING:菜单编辑认证;可以用openssl、gurb-md5-crpyt命令生成; title TITLE:定义菜单项“标题”,可出现多次; root (hd#,#):grub查找stage2及kernel文件所在的设备分区;为grub的"根"; kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核 initrd /PATH/TO/INITRAMFS_FILE:内核匹配的ramfs文件; password [--md5] STRING :启动选定的内核或操作系统时进行认证; grub-md5-crypt命令:生成对应要保护的认证密码
演示: [root@centos6 ~]# cat /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg0-root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 :默认启动菜单编号 timeout=5 :菜单等待时长 splashimage=(hd0,0)/grub/splash.xpm.gz :菜单背景 hiddenmenu :隐藏菜单 title CentOS 6 (2.6.32-573.el6.x86_64) :标题 root (hd0,0) :grub的根所在分区 kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/vg0-root rd_NO_LUKS rd_NO_DM LANG=en_US.UTF-8 rd_LVM_LV=vg0/swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg0/root KEYBOARDTYPE=pc KEYTABLE=us rhgb crashkernel=auto quiet rhgb quiet initrd /initramfs-2.6.32-573.el6.x86_64.img :指明内核匹配的rmfs文件 [root@centos6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg0-root 20G 302M 19G 2% / [root@centos6 ~]# grub-md5-crypt --help Usage: grub-md5-crypt [OPTION] Encrypt a password in MD5 format. -h, --help print this message and exit -v, --version print the version information and exit --grub-shell=FILE use FILE as the grub shell Report bugs to <bug-grub@gnu.org>. [root@centos6 ~]# grub-md5-crypt #生成对应要保护的认证密码 Password: #键入两次密码 Retype password: $1$JXhra$G.vdkp88V79CdqYsVdmPx/
grub定制背景图:gimp软件 xpm图片格式要求 1、图片必须是xpm格式 2、图片大小必须是640×480 3、图片只能有14种颜色
背景制作参考:http://liguoqing.blog.51cto.com/327222/1729382
grub之配置文件grub.conf
grub之菜单认证界面
grub之内核认证界面
进入单用模式
(1) 编辑grub菜单(选定要编辑的title,而后使用e命令)
(2) 在选定的kernel后附加
1,s,S或single都可以;
(3) 在kernel所在行,键入"b"命令;
安装grub
应用场景:
当前系统没有grub,而是借助光盘启动;
两块硬盘,给其中一块安装grub;
双系统互存
需求:windows的bootloader不能引导linux,先装Linux再装windows会把linux的bootloader覆盖成windows自己的;
后装linux,grub会保留windows的bootloader作为一个启动项菜单;如果是先装win再装linux,win坏重装win,
一样会覆盖成win的bootloader;
解决:linux的U盘或光盘启动盘,在启动的linux系统中用grub-install命令重装grub
(1) grub-install grub-install --root-directory=ROOT /dev/DISK (2) grub grub> root (hd#,#) grub> setup (hd#) 注意:需要有grub目录才可以用这种方法修复;
演示:制作微型linux 1)新增硬盘分区 [root@centos6 ~]# fdisk -l /dev/sd[a-z] Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 boot分区大小=200Mb=10个内核100M左右 + grub; 1个内核(~5MB)+ramdisk(~6MB) [root@centos6 ~]# fdisk /dev/sdb Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 :第一分区给boot,100M First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +100M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 :第二分区给swap,2G First cylinder (15-2610, default 15): Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): +2G Command (m for help): t :设定swap类型 Partition number (1-4): 2 Hex code (type L to list codes): 82 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 :第三分区给根/ ,5G First cylinder (277-2610, default 277): Using default value 277 Last cylinder, +cylinders or +size{K,M,G} (277-2610, default 2610): +5G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos6 ~]# partx -a /dev/sdb :激活分区 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@centos6 ~]# cat /proc/partitions major minor #blocks name ... 8 17 112423 sdb1 8 18 2104515 sdb2 8 19 5253255 sdb3 2)格式化 [root@centos6 ~]# mke2fs -t ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Writing inode tables: done [root@centos6 ~]# mke2fs -t ext4 /dev/sdb3 mke2fs 1.41.12 (17-May-2010) Writing inode tables: done [root@centos6 ~]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 2104508 KiB no label, UUID=b3f5bb0b-b9ab-4aa2-9b02-f89055dc5990 3)挂载grub根 [root@centos6 ~]# mkdir /mnt/boot [root@centos6 ~]# mount /dev/sdb1 /mnt/boot 4)安装grub,--root-directory=/mnt自动找mnt下boot目录 [root@centos6 ~]# grub-install --root-directory=/mnt /dev/sdb Probing devices to guess BIOS drives. This may take a long time. Installation finished. No error reported. This is the contents of the device map /mnt/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'. (fd0) /dev/fd0 (hd0) /dev/sda (hd1) /dev/sdb [root@centos6 ~]# ls /mnt/boot/ grub lost+found [root@centos6 ~]# ls /mnt/boot/grub/ device.map fat_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5 e2fs_stage1_5 ffs_stage1_5 jfs_stage1_5 reiserfs_stage1_5 stage2 vstafs_stage1_5 5)自己写配置文件及内核文件 [root@centos6 ~]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz [root@centos6 ~]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img [root@centos6 ~]# cat /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS (EXPRESS) root (hd0,0) #实验需要;目的拆除主机的硬盘,启动新硬盘grub kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash initrd /initramfs.img #注意selinux 要写在init前面,init指定init程序 6) sda3 当根文件系统用,就需要创建根目录结构 [root@centos6 ~]# mkdir /mnt/sysroot [root@centos6 ~]# mount /dev/sdb3 /mnt/sysroot/ [root@centos6 ~]# cd /mnt/sysroot/ [root@centos6 sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt 7)复制bash环境,几个命令;注意动态编译的同时需要拷贝库文件; [root@centos6 sysroot]# cp /bin/bash /mnt/sysroot/bin/ [root@centos6 sysroot]# ldd /bin/bash #查看依赖的库文件 linux-vdso.so.1 => (0x00007ffcaa7ee000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003bc3800000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003bbb400000) libc.so.6 => /lib64/libc.so.6 (0x0000003bbb800000) /lib64/ld-linux-x86-64.so.2 (0x0000003bbb000000) [root@centos6 sysroot]# for i in $(ldd /bin/bash | grep -o '/[^[:space:]]\+[0-9]\>'); do cp $i /mnt/sysroot/lib64 ;done [root@centos6 sysroot]# ls /mnt/sysroot/lib64/ ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libtinfo.so.5 [root@centos6 sysroot]# chroot /mnt/sysroot/ #chroot 切换根系统,测试 bash-4.1# bash-4.1# bash-4.1# ls bash: ls: command not found bash-4.1# exit exit [root@centos6 sysroot]# [root@centos6 ~]# cat /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS (EXPRESS) root (hd0,0) #实验需要;目的拆除主机的硬盘,启动新硬盘grub kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash initrd /initramfs.img [root@centos6 sysroot]# sync 8)记录虚机中建好的硬盘位置,新建虚机,载入该硬盘(选择现有的虚拟磁盘)
制作好的bash系统
其他参考:
手把手打造属于自己的迷你Linux!:http://foreveryan.blog.51cto.com/3508502/657568
私人定制---打造属于自己的linux小系统 http://nmshuishui.blog.51cto.com/1850554/1368807
制作微型linux http://laoguang.blog.51cto.com/6013350/1061950
图解制作Mini Linux系统 http://xxrenzhe.blog.51cto.com/4036116/1370114
破坏MBR:虚拟机实验,在不重启条件小修复;重启肯定是坏了; 第一阶段坏了,grub就引导不了,只能在紧急救援模式下修复; 方法1:grub-install会自动把第一阶段重装一次 1)先保存当前MBR [root@centos6 ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.000737578 s, 694 kB/s 2)只要不破坏分区表,小于446字节都可以 [root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 1+0 records in 1+0 records out 200 bytes (200 B) copied, 0.000529102 s, 378 kB/s [root@centos6 ~]# sync #同步到磁盘 3)grub-install会自动把第一阶段重装一次 [root@centos6 ~]# grub-install --root-directory=/ /dev/sda Installation finished. No error reported. This is the contents of the device map //boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'. # this device map was generated by anaconda (hd0) /dev/sda [root@centos6 ~]# sync 方法2:grub命令提示符修复 前提:确保grub有第一阶段和1.5阶段 [root@centos6 ~]# ls /boot/grub/ device.map grub.conf minix_stage1_5 stage2 e2fs_stage1_5 iso9660_stage1_5 reiserfs_stage1_5 ufs2_stage1_5 fat_stage1_5 jfs_stage1_5 splash.xpm.gz vstafs_stage1_5 ffs_stage1_5 menu.lst stage1 xfs_stage1_5 1)破坏 [root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 1+0 records in 1+0 records out 200 bytes (200 B) copied, 0.000254989 s, 784 kB/s [root@centos6 ~]# sync 2)grub命令提示符修复;读取grub目录下第一阶段,复制 [root@centos6 ~]# grub grub> root (hd0,0) root (hd0,0) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd0) setup (hd0) Checking if "/boot/grub/stage1" exists... no Checking if "/grub/stage1" exists... yes Checking if "/grub/stage2" exists... yes Checking if "/grub/e2fs_stage1_5" exists... yes Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded. succeeded Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded Done. grub> quit
紧急救援模式
只要bootloader坏了,该硬盘上所有操作系统都将无法引导;只能进入救援模式或拆掉硬盘放其他主机上修复;
装载安装光盘,选择紧急救援模式或者按ESC 命令行提示符键入:linux rescue 接下来根WINPE类似的系统了 /mnt/sysimage conutinue 打开shell提示符 切换根 chroot /mnt/sysimage 执行grub-insatll 重启就可以了