您的位置:首页 > 运维架构 > Linux

Linux启动(续)

2015-08-06 17:20 621 查看
runlevel (启动级别): 查看命令 :who -r 或 runlevel

0:halt 关机

1:单用户模式,直接以管理员身份登录,不需要密码

2:多用户模式,但是不能挂载nfs

3:多用户模式

4:保留未用

5:图形界面

6:reboot重启

默认运行级别在/etc/inittab:

id:3:initdefault: 默认为3

我们知道bootloader有lilo、grub、spfdisk等,目前最常用的就是grub;

grub是放在MBR中,但是MBR总共只有512Bytes,分区表占64Bytes,grub只能用到446Bytes

grub被分为2个阶段:

stage 1 :存放grub主程序

stage 2 :主要配置文件,一般为/boot/grub/menu.lst ————> 其实是链接/boot/grub/grub.conf

来看一下grub.conf:
default=0			#设定默认启动的title编号,读秒时间结束后,就会启动第一个title
timeout=5			#设置默认读秒为5秒,给用户选择
splashimage=(hd0,0)/grub/splash.xpm.gz	#grub背景图片在磁盘的位置
hiddenmenu			#隐藏菜单
password --md5 $1$ImLrM$VqU.vu9UCq.M7M.Q0hN6c1 	#给grub设定密码
title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64) #内核标题,系统名等,可以修改
root (hd0,0)	#内核所在的磁盘,(hd0,0)表示第一块盘,第一个分区
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=e39a2639-0c77-4b7e-8c6b-bb34bd658eb4
#内核文件及路径
initrd /initramfs-2.6.32-431.el6.x86_64.img
#虚拟根文件系统
当root用户密码忘记时,我们可以选择单用户模式来修改密码,但是什么人都可以重启然后进入单用户模式

再就轻易的修改root密码,那是绝对不行的。所以我们可以给grub设置一个密码,先生成密码,然后再加到title上面那一行

[root@Linux ~]# grub-md5-crypt
Password:
Retype password:
$1$ImLrM$VqU.vu9UCq.M7M.Q0hN6c1
我们可以尝试修复和安装一个grub:

[root@Linux ~]# grub
grub> root (hd0,0)			#判断内核在磁盘的位置
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)  			#检查并修复grub
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引导加载内核:

先创建两个分区:/dev/hda1、/dev/hda2
[root@Linux ~]# mkdir /mnt/{boot,root}	#创建两个目录,boot做引导区,root为根目录
[root@Linux root]# mkdir  bin dev home proc sbin tmp var etc lib mnt root  selinux  sys  usr
[root@Linux ~]# mke2fs -j /dev/hda1		#初始化磁盘为ext3格式
[root@Linux ~]# mke2fs -j /dev/hda2
[root@Linux ~]# mount /dev/hda1 /mnt/boot	#分别挂载
[root@Linux ~]# mount /dev/hda2 /mnt/root
[root@Linux ~]# grub-install --root-directory=/mnt /dev/hda		#安装grub指定安装目录
[root@Linux ~]# cp /boot/vmlinuz-2.6.18-194.el5 /mnt/boot/vmlinuz	#将内核和虚拟根目录直接复制过来
[root@Linux ~]# cp /boot/initrd-2.6.18-194.el5.img /mnt/boot/
[root@Linux boot]# mv initrd-2.6.18-194.el5.img initrd-2.6.18-194.el5.img.gz #用file查看initrd文件类型并修改,再解压
[root@Linux boot]# gzip -d initrd-2.6.18-194.el5.img.gz
[root@Linux boot]# cpio -id <initrd-2.6.18-194.el5.img			#再用file查看,用 cpio 解压
[root@Linux boot]# vim init										#修改init,指定真的根目录,这一步本来是启动过程中,执行程序的结果
mkrootdev -t ext3 -o defaults,ro /dev/VolGroup00/LogVol00 ——> /dev/hda2
[root@Linux boot]# find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz		#改完后再用cpio压缩为initrd.gz
[root@Linux boot]# vim grub/grub.conf 					#配置自己的grub.conf
default=0
timeout=5
title Linux (LFL Computer)
root (hd0,0)
kernel /vmlinuz
initrd /initrd.gz
[root@Linux root]# cp /sbin/init /mnt/root/sbin			#复制系统init和bash
[root@Linux root]# cp /bin/bash /mnt/root/bin
并用ldd命令查看他们所依赖的库文件,然后复制到/mnt/root/lib里面
[root@Linux ~]# chroot /mnt/root 	#测试
[root@Linux root]# vim etc/inittab						#设置init配置文件
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
[root@Linux root]# vim etc/rc.d/rc.sysinit				#设置默认运行环境
echo -e "Welcome to LFLong Computer "
/bin/bash
[root@Linux root]# chmod +x etc/rc.d/rc.sysinit
续 cpio 命令的用法:

cpio(copy in/out)主要是用来建立或者还原备份档的工具

        -i或--extract  执行copy-in模式,还原备份档。
        -d或--make-directories  如有需要cpio会自行建立目录
        -H<备份格式>  指定备份时欲使用的文件格式。
        --quiet  不显示复制了多少区块。
        -o或--create  执行copy-out模式,建立备份档。
        -c:使用旧ASCII备份格式;
            cpio -id < initrd-2.6.18-194.el5.img    #将该文件(输入重定向)还原
            find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz    #将当前目录下所有文件已newc格式建立备份并压缩为.gz格式
将这个配置好的磁盘拿下来,装在别的没有系统的主机上,开机就会出现自己配置的界面了,这就是基本的开机过程。这只是一个简单的模拟,而真正的启动过程会比这个复杂很多值得我们去学习和研究。

参考:鸟哥Linux私房菜,马哥Linux教学视频。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: