Linux系统裁剪笔记
1.什么裁剪?
本篇文章的主要目的是让笔者和读者更深的认识Linux系统的运作方式,大致内容就是把Linux拆开自己一个个组件来组装,然后完成一个微型的Linux系统.下面,让我们来实现吧..写的不好的地方请指教.
2.原理
大家都知道,操作系统的启动流程是(主要是Linux):POST—>BIOS—>MBR—kernel-->initrd-->sbin/init,
POST,BIOS都不是我们管的,所以这里我们从MBR开始,Linux的系统引导先主要是用的grub这个软件,grub引导系统了,然后启动内核,内核调用initrd来实现最基本的操作系统,
3.实验 (所有操作均在虚拟机上实现,且定义硬盘时选IDE)
环境:1).在宿主机上安装一台红帽子Linux5.4,/dev/sda上安装操作系统;
2).在宿主机上新建一个新硬盘:/dev/hda,且分区为20M的sdb1,512M的sdb2;
3).另建立一台测试虚拟机,将原有的硬盘删除,再将硬盘/dev/hda的新增到该测试虚拟机;
4).系统服务启动时显示开启状态;
5).新增启动网络模块;
6).新增内核控制模块;
7).用户登录
login: 用户登录验证
nsswitch: Network Service Switch
框架: /etc/passwd, /etc/shadow, /etc/group
库: libnss_file.so,libnss_nis.so, libnsss_ldap.so
配置文件: /etc/nsswitch.conf
[[email protected] sysroot]# cat /etc/nsswitch.conf #查看配置文件
passwd: files
shadow: files
group: files
hosts: files dns
...
[[email protected] sysroot.5]# cp -d /lib/libnss_files* /mnt/sysroot/lib/ #复制库文件
[[email protected] sysroot]# ll lib/libnss_files* #验证
-rwxr-xr-x 1 root root 46680 Nov 27 13:55 lib/libnss_files-2.5.so
lrwxrwxrwx 1 root root 19 Nov 27 13:55 lib/libnss_files.so.2 -> libnss_files-2.5.so
[[email protected] sysroot.5]# cp -d /usr/lib/libnss3.so /usr/lib/libnssckbi.so /usr/lib/libnssutil3.so /mnt/sysroot/usr/lib
[[email protected] lib]# ln -sv ../../lib/libnss_files.so.2 libnss_files.so
[[email protected] sysroot]# ll /mnt/sysroot/usr/lib/ #验证
-rwxr-xr-x 1 root root 1188804 Nov 27 14:41 libnss3.so
-rwxr-xr-x 1 root root 373992 Nov 27 14:41 libnssckbi.so
lrwxrwxrwx 1 root root 27 Nov 27 14:42 libnss_files.so -> ../../lib/libnss_files.so.2
-rwxr-xr-x 1 root root 98532 Nov 27 14:41 libnssutil3.so
...
[[email protected] sysroot]# vim /mnt/sysroot/etc/nsswitch.conf #新建库文件
passwd: files
shadow: files
group: files
hosts: files dns
[[email protected] sysroot]# grep -E "root|hadoop" /etc/passwd #新建用户相关文件
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
hadoop:x:500:500::/home/hadoop:/bin/bash
[[email protected] sysroot]# grep -E "root|hadoop" /etc/passwd >/mnt/sysroot/etc/passwd
[[email protected] sysroot]# grep -E "root|hadoop" /etc/shadow
root:$1$u2o9sj7Z$xz8STP0grPk5Pg3vQ0l0d.:17130:0:99999:7:::
hadoop:$1$nwkmJG/4$aeC.I.TpUnrFG5Q8zb/4i.:17132:0:99999:7:::
[[email protected] sysroot]# grep -E "root|hadoop" /etc/shadow > /mnt/sysroot/etc/shadow
[[email protected] sysroot]# grep -E "^(root|hadoop)" /etc/group
root:x:0:root
hadoop:x:500:
[[email protected] sysroot]# grep -E "^(root|hadoop)" /etc/group >/mnt/sysroot/etc/group
[[email protected] sysroot]# vim etc/inittab #修改登录命令mingetty
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l3:3:wait:/etc/rc.d/rc 3
l6:6:wait:/etc/rc.d/rc 6
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
[[email protected] bin]# cp login /mnt/sysroot/bin/
[[email protected] bin]# ldd login #关联的库文件
linux-gate.so.1 => (0x00eac000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0x024cd000)
libm.so.6 => /lib/libm.so.6 (0x00d63000)
libc.so.6 => /lib/libc.so.6 (0x00c1b000)
/lib/ld-linux.so.2 (0x00bfd000)
[[email protected] rc.d]# mkdir rc1.d
[[email protected] rc.d]# cd rc1.d/
[[email protected] rc1.d]# ln -sv ../init.d/network K
K33tserver K90network
[[email protected] rc1.d]# ln -sv ../init.d/network K90network
[[email protected] rc1.d]# ln -sv ../init.d/tserver K33tserver
[[email protected] etc]# vim rc.d/init.d/single
#!/bin/bash
#
#chkconfig:
# description:
#
case $1 in
start)
;;
*)
echo "Usage: single start."
;;
esac
exec /sbin/init S
[[email protected] etc]# chmod +x rc.d/init.d/single
[[email protected] etc]# cd rc.d/rc1.d
[[email protected] rc1.d]# ln -sv ../init.d/single S99single
[[email protected] etc]# ll rc.d/rc1.d/
total 0
lrwxrwxrwx 1 root root 17 Nov 27 20:06 K33tserver -> ../init.d/tserver
lrwxrwxrwx 1 root root 17 Nov 27 20:05 K90network -> ../init.d/network
lrwxrwxrwx 1 root root 16 Nov 27 20:13 S99single -> ../init.d/single