Linux 系统裁剪笔记 3
说到裁减Linux,无非是为了减小磁盘占用或者是为了某些特定场合的应用(如嵌入式系统)。以RedHat 7.3为例,其最小安装仍然达到了300M,这不得不让人对一直号称小而全的Linux系统感到疑惑。
作为自己手中课题的一个铺垫,不久前我尝试了对Linux进行裁减,虽然没有达到预期的一张软盘大小,但结果也相当有吸引力。下面我对此一一做说明。
/usr/src/linux-2.4/Documentation/initrd.txt
裁减Linux一般有两种办法,其一是重新生成kernel和文件系统,其二是在原有的系统上删除不必要的文件缩小“体积”
裁减目标:构成一最小Linux系统担负实验室网关工作,系统载体为硬盘,运行使用RAMDISK,从而减小意外断电造成的文件系统修整消耗提高系统可靠性。
目标平台:P2-400,8G/64M,8139LAN adapt x 2
首先裁减kernel,既然是最小系统,则kernel里所有必须的部件都将直接编译进入内核。但是内核对module的支持需要保留。
如何编译内核,不再累述,具体说说哪些选项先:
Code maturity level options ---> 不选
Loadable module support ---> 当中的Set version....的可以不要,其他两个留着
Processor type and features ---> 按照目标系统选择对应的Process Family ,其他的嘛,留下Machine Check Exception、Low Latency....、HIGHMEM Support,其余都可以不要
General setup --->当中,PCI的选上,其他的不要(注意对照你的系统),SYSTEM V IPC、BSD Process accounting、sysctl support留下,Kernel support ELF binary留着,其他的可以不要
Binary emulation of other systems --->
Memory Technology Devices (MTD) --->
Parallel port support --->
以上三项都是可以不要的
Plug and Play configuration ---> 选上,不过如果没有ISA设备,可以不选对ISA P&P的支持(比如我的目标系统)
Block devices --->各取所需了,一般来说,如果你要用软盘,就选上Normal floppy disk support,大多数嵌入式系统是不要的。中间几个也是没有的;Loopback device是一定要的,Network block device我也没把握,可能可以不要不过我选了,呵呵,RAM Disk一定要,Initrd RAM Disk support当然要选。至于Default Ramdisk size就无所谓了,反正可以在启动的时候修改,呵呵。
Networking options --->既然是打算做网关,呵呵,里面大部分东西都要选上而且是[*],编译入内核(前面已经说过了,没有编译为模块的)。从上到下一直选到IP: Virtual Server Configuration --->(从这个开始(含),可以不要了)。
SCSI support --->
Fusion MPT device support --->
I2O device support --->
Amateur Radio support --->
IrDA (infrared) support --->
ISDN subsystem --->
Old CD-ROM drivers (not SCSI, not IDE) --->
上面几个都不用,为什么RedHat那么大,他们有不小的功劳哟~~~
Input core support --->如果你不是用的USB接口鼠标键盘,可以不用选他们。
Character devices --->这里面我只选了Virtual Terminal以及Support for console on virtual terminal,其他好多东西都没有选。
Multimedia devices --->
Crypto Hardware support --->
console drivers->我只选了VGA text console
Sound --->
USB support --->
Additional device driver support --->
Kernel hacking --->
这几个都没有选,make dep;make clean;make bzImage
看看吧,内核大概是700~800k左右
以前我总以为裁减kernel就是裁减Linux了,后来才发现是大错特错。
以前总以为最难的是裁减kernel,后来才发现自己多么无知。学习裁减内核,大概只用了一两天,编译一次内核也就20分钟不到,可是后来居然重建文件系统花了一两个星期,呜呜~~~
lrwxrwxrwx 1 root root 4 Dec 28 09:31 bin -> sbin
drwxr-xr-x 5 root root 1024 Dec 27 13:42 dev
drwxr-xr-x 7 root root 1024 Jan 6 15:14 etc
drwxr-xr-x 2 root root 1024 Dec 12 08:33 initrd
drwxr-xr-x 4 root root 1024 Dec 30 06:52 lib
drwxr-xr-x 2 root root 1024 Dec 11 07:52 mnt
drwxr-xr-x 2 root root 1024 Dec 26 03:03 root
drwxr-xr-x 2 root root 1024 Dec 30 07:28 sbin
drwxr-xr-x 2 root root 1024 Dec 26 03:04 sysroot
drwxr-xr-x 2 root root 1024 Apr 19 2002 tmp
drwxr-xr-x 3 root root 1024 Dec 12 07:45 usr
drwxr-xr-x 5 root root 1024 Dec 12 02:43 var
这几个目录是必须的
先看看bin下面有什么
lrwxrwxrwx 1 root root 6 Dec 30 07:28 ash -> ./bash
-rwxr-xr-x 1 root root 541096 Dec 30 07:27 bash
-rwxr-xr-x 1 root root 16020 Dec 13 08:56 cat
-rwxr-xr-x 1 root root 16680 Dec 27 15:40 chmod
-rwxr-xr-x 1 root root 36360 Dec 28 09:10 cp
-rwxr-xr-x 1 root root 62756 Dec 28 09:25 ftp
-rwxr-xr-x 1 root root 100624 Dec 28 09:14 grep
-rwxr-xr-x 1 root root 8672 Dec 26 03:27 halt
-rwxr-xr-x 1 root root 9624 Dec 28 09:14 hostname
-rwxr-xr-x 1 root root 54316 Dec 28 09:14 ifconfig
-rwxr-xr-x 1 root root 26920 Dec 12 02:42 init
-rwxr-xr-x 1 root root 105768 Dec 27 13:44 ip
-rwxr-xr-x 1 root root 60764 Dec 28 09:15 iptables
-rwxr-xr-x 1 root root 7764 Dec 26 17:26 kill
-rwxr-xr-x 1 root root 19080 Dec 12 02:25 login
-rwxr-xr-x 1 root root 9172 Dec 11 07:54 losetup
-rwxr-xr-x 1 root root 46888 Dec 13 08:55 ls
-rwxr-xr-x 1 root root 10316 Dec 13 08:37 mingetty
-rwxr-xr-x 1 root root 17992 Dec 27 14:15 mkdir
-rwsr-xr-x 1 root root 60104 Dec 11 07:54 mount
-rwxr-xr-x 1 root root 43496 Dec 28 10:02 mv
-rwxr-xr-x 1 root root 22196 Dec 26 02:09 nash
-rwxr-xr-x 1 root root 29464 Dec 28 09:49 ping
-r-xr-xr-x 1 root root 63304 Dec 26 16:57 ps
lrwxrwxrwx 1 root root 4 Dec 26 03:33 reboot -> halt
-rwxr-xr-x 1 root root 26216 Dec 26 17:35 rm
lrwxrwxrwx 1 root root 6 Dec 30 07:28 sh -> ./bash
-rwxr-xr-x 1 root root 14952 Dec 11 09:44 shutdown
-rwxr-xr-x 1 root root 219932 Dec 28 10:06 ssh
-rwxr-xr-x 1 root root 260616 Dec 27 14:04 sshd
lrwxrwxrwx 1 root root 6 Dec 26 16:48 swapoff -> swapon
-rwxr-xr-x 1 root root 7108 Apr 1 2002 swapon
-rwxr-xr-x 1 root root 27208 Dec 27 14:13 syslogd
-rwxr-xr-x 1 root root 78808 Dec 28 09:30 telnet
-rwsr-xr-x 1 root root 30664 Dec 27 14:23 umount
-rwxr-xr-x 1 root root 7832 Dec 12 01:54 update
-rwxr-xr-x 1 root root 386120 Dec 28 09:13 vi
-rwxr-xr-x 1 root root 13896 Dec 30 06:53 who
这里面包含了ftp、telnet、ssh客户端以及sshd服务器常用的命令和网络设置命令,iptables防火墙,vi编辑器,shell用的是 bash,虽然ash很小但是总是不习惯没有auto complete功能,tcsh不大不小功能又全,可是对一些shell脚本的支持不太好。nash用来解析linuxrc,后面会讲到(如果你看了最前面提到的initrd.txt)也会明白。
接着,用ldd命令看bin目录下面的各个可执行文件分别都和哪些动态库连接把他们cp到/mnt/lib目录下,如用ldd看mv命令,结果如下
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
libc.so.6是基本的libc库,好像不同的处理器还不一样,我就在一台P-MMX下面用P2的libc,结果死掉了。查了半天 :(
iptables命令除了其显示的以外,还要把/lib/iptables目录复制到/mnt/lib下面
/lib/security下面是PAM需要的库,最小系统只需要复制/lib/security下面的pam_unix.so、pam_stack.so到/mnt/lib/security下面即可
/etc下面的东西最是麻烦,要改的不是一点点
/etc/inittab init的配置文件,我改的是这样的:
si::sysinit:/etc/rc
1:2345:respawn:/sbin/mingetty tty1
2:23:respawn:/sbin/mingetty tty2
l0:0:wait:/etc/rc0
l6:6:wait:/etc/rc6
none /proc proc defaults 0 0
从inittab里面知道,启动后执行脚本/etc/rc我是这样写的:
/bin/mount -n -t proc /proc /proc
/bin/mount -n -o remount,rw /
/bin/mount -av
/bin/hostname MiniLinux
/etc/rc.network
/etc/rc.firewall
/bin/sshd
设置网络在最小系统里面再也不是像/etc/init.d/network start那么简单,
呵呵,其实也不麻烦。通过命令ip、ifconfig可以很方便的设置。例如rc.network为:
/bin/ifconfig eth0 192.168.0.254
/bin/ifconfig eth1 211.69.200.1
/bin/ip route add default via 211.69.200.2 dev eth1
/bin/ip route replace 192.168.0.0/24 dev eth0 scope link
/bin/ip route replace 211.69.200.0/24 dev eth1 scope link
auth required /lib/security/pam_stack.so service=system-auth
account required /lib/security/pam_stack.so service=system-auth
password required /lib/security/pam_stack.so service=system-auth
session required /lib/security/pam_stack.so service=system-auth
/etc/pam.d/sshd和login的内容一样。从其可知它们调用了system-auth这个服务,则还需要/etc/pam.d/system-auth,内容为:
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth sufficient /lib/security/pam_unix.so likeauth nullok
account required /lib/security/pam_unix.so
password sufficient /lib/security/pam_unix.so nullok md5 shadow
session required /lib/security/pam_unix.so
/sbin/reboot -i -d
[root@MiniLinux etc]# cat rc0
/sbin/halt -i -d -p
[root@MiniLinux etc]# ll
total 891
-rw-r--r-- 1 root root 6639 Apr 19 2002 fonts.cgz
-rw-r--r-- 1 root root 109 Dec 30 06:19 fstab
-rw-r--r-- 1 root root 14 Dec 14 00:10 group
-rw-r--r-- 1 root root 146 Dec 27 15:41 inittab
-rw------- 1 root root 60 Jan 6 15:14 ioctl.save
-rw-r--r-- 1 root root 57 Dec 12 01:26 issue
-rw-r--r-- 1 root root 28436 Apr 19 2002 keymaps.gz
-rw-r--r-- 1 root root 3758 Apr 19 2002 kon.cfg
-rw-r--r-- 1 root root 1281 Apr 19 2002 lang-table
-rw-r--r-- 1 root root 1320 Dec 30 06:55 ld.so.cache
-rw-r--r-- 1 root root 18 Dec 12 07:53 ld.so.conf
-rw-r--r-- 1 root root 54692 Apr 19 2002 loader.tr
-rw-r--r-- 1 root root 1180 Dec 23 09:07 login.defs
-rw-r--r-- 1 root root 30303 Apr 19 2002 minikon.fnt
-rw-r--r-- 1 root root 0 Dec 13 23:39 mtab
-rw-r--r-- 1 root root 270 Dec 23 04:03 nsswitch.conf
drwxr-xr-x 2 root root 1024 Dec 30 04:48 pam.d
-rw-r--r-- 1 root root 28 Dec 30 06:29 passwd
-rwxr-xr-x 1 root root 401 Dec 30 07:43 profile
-rw-r--r-- 1 root root 12359 Apr 19 2002 ramfs.img
lrwxrwxrwx 1 root root 7 Dec 26 03:03 rc -> rc.d/rc
drwxr-xr-x 2 root root 1024 Dec 27 15:33 rc.d
-rwxr-xr-x 1 root root 2631 Jan 6 02:18 rc.firewall
-rwxr-xr-x 1 root root 246 Jan 6 02:17 rc.network
-rwxr-xr-x 1 root root 20 Dec 27 15:38 rc0
-rwxr-xr-x 1 root root 19 Dec 27 15:39 rc6
-r-------- 1 root root 59 Dec 30 06:20 shadow
drwxr-xr-x 2 root root 1024 Dec 26 06:37 ssh
-rw-r--r-- 1 root root 737535 Dec 23 10:18 termcap
[root@MiniLinux /]# cat etc/nsswitch.conf
passwd: files
shadow: files
group: files
hosts: files
services: &n
networks: files
protocols: files
rpc: files
ethers: files
netmasks: files
bootparams: files
automount: files
aliases: files
netgroup: files
publickey: files
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
HISTSIZE=1000
HISTFILESIZE=20
PATH=/bin
PS1='[/u@/h /W]/$ '
HOSTNAME='/bin/hostname'
export PATH HISTSIZE HISTFILESIZE HOSTNAME PS1
alias l.='ls -d .[a-zA-Z]* --color=tty'
alias ls='ls --color=tty'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
关于/dev目录
用不上。我列出在我的最小Linux下面用到的设备文件:
[root@MiniLinux dev]# ls
agpgart hda hda6 input loop3 psaux ptyp3 ram3 tty3 ttyp4
console hda1 hda7 kbd loop4 ptmx ptyp4 ram4 tty4 urandom
fb hda2 hda8 kmem loop5 pts ram shm ttyp0 zero
fb0 hda3 hda9 loop0 mem ptyp0 ram0 tty0 ttyp1
fd0 hda4 initctl loop1 null ptyp1 ram1 tty1 ttyp2
fd1 hda5 initrd loop2 openprom ptyp2 ram2 tty2 ttyp3
其中input、shm、pts是目录,似乎是系统自己生成的,fb连接到fb0,ram连接到ram0 ,关于硬盘的保留了hda?,loop[0-5]用于支持回环设备(loopback devices),tty[0-4]用于支持主机直接操作,ttyp[0-4] & ptyp[0-4]用于共同支持ssh登陆,ram?用于支持虚拟盘,urandom是sshd服务必须的设备。
linuxrc 是一个在initrd.img里面展开后直接自动执行的一个脚本。关于这个脚本的用途,建议大家读一下/usr/src/linux-2.4 /Documentation/initrd.txt,我也是直接把系统提供的initrd-2.4.18-3.img里面带的linuxrc拿来用而已:
[root@MiniLinux /]# cat linuxrc
#!/bin/nash
echo Mounting /proc filesystem
mount -t proc /proc /proc
echo Creating root device
mkrootdev /dev/root
echo 0x0100 > /proc/sys/kernel/real-root-dev
echo Mounting root filesystem
mount --ro -t ext2 /dev/root /sysroot
umount /proc
pivot_root /sysroot /sysroot/initrd
前面列出的最小系统的目录,如果没有特别提到都是留空的。这里要说的,最后的工作就是如何把kernel和文件系统结合起来。
title Test Combine
root (hd0,4)
kernel /bzImage ro ramdisk_size=8192 root=/dev/ram0
initrd /newfs.img
当然,你不可照抄我的配置,需要按照你的系统更改root (hd?,?),如果你建立的文件系统(未压缩前)容量是其他数值的,请用合适的值替代8192,单位是KB(还记得我前面说过的编译内核的时候不需要刻意更改缺省ramdisk容量吗?就在这里指定即可)
好了,可以试试裁减以后的系统了。大概有多大呢?我裁减出来的系统内核大约是<800k,文件系统8M,压缩成.img的是约3M,很小吧。
感想 及 说明
Linux 系统裁剪笔记 3的更多相关文章
- Linux 系统裁剪笔记1
1.什么裁剪? 本篇文章的主要目的是让笔者和读者更深的认识Linux系统的运作方式,大致内容就是把Linux拆开自己一个个组件来组装,然后完成一个微型的Linux系统.下面,让我们来实现吧..写的不好 ...
- Linux 系统裁剪笔记 软盘2
第一步:裁减内核打开终端,输入:cd /usr/src/linux2.4,然后输入make xconfig.现在编译内核正式开始了1.1 "code maturity level optio ...
- Linux 系统裁剪笔记 4 (内核配置选项及删改)
CDROM filesystem support(CONFIG_ISO9660_FS)[Y/m/n/?]有标准光驱的系统应该选Y.Minix fs support(CONFIG_MINIX_FS)[ ...
- Linux系统学习笔记:文件I/O
Linux支持C语言中的标准I/O函数,同时它还提供了一套SUS标准的I/O库函数.和标准I/O不同,UNIX的I/O函数是不带缓冲的,即每个读写都调用内核中的一个系统调用.本篇总结UNIX的I/O并 ...
- linux系统裁剪
linux系统裁剪 1.简介 最近为了更加了解linux系统的启动过程,通过对系统进行裁剪的的方式来了解整个启动流程.这里记录下过程. 2.环境介绍 vmware12 + centos6.5 + xs ...
- Linux系统安全笔记
Linux系统安全笔记 https://insecure.org/https://sectools.org/SecTools.Org:排名前125的网络安全工具 http://www.ibm.com/ ...
- 嵌入式之Linux系统裁剪和定制---(kernel+busyboxy+dropbear+nginx)
本文将介绍通过完全手动定制内核,在此基础上添加 busybox ,并实现远程登陆,使裁剪的 linux 能够运行 nginx . 在此之前介绍一下 linux 系统的启动流程. linux系统启动流程 ...
- Linux 系统裁剪
一.前言 Linux操作系统至1991.10.5号诞生以来,就源其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为 ...
- Linux系统学习笔记(1)
学习地址:http://www.runoob.com/linux/linux-tutorial.html 1.学习Windows和Linux哪个比较难? 前期是Windows容易学习,后期是Linux ...
随机推荐
- form表单中get和post两种提交方式的区别
一.form表单中get和post两种提交方式的区别? 1.get提交表单中的内容在链接处是可见的.post不可见 2.post相比于get是安全的 3.post不收限制大小,get有限制大小(黑马视 ...
- 异常检测算法:Isolation Forest
iForest (Isolation Forest)是由Liu et al. [1] 提出来的基于二叉树的ensemble异常检测算法,具有效果好.训练快(线性复杂度)等特点. 1. 前言 iFore ...
- kali初探:root用户打开wireshark打开失败
启动wireshark后,报错: 该界面提示在init.lua文件中使用dofile函数禁用了使用超级用户运行wireshark.这是因为wireshark工具是使用Lua语言编写的,并且在kali ...
- c# 简单实现 插件模型 反射方式
利用反射方式实现插件模型,wpf控件作为插件,然后用另外的窗体加载. 首先定义插件接口: public interface IUserControlLevel1 { string PluginName ...
- 利用 secureCRT 直接上传下载文件 (sz,rz)
在window下向linux传送文件的方法. 首先在window中安装SecureCRT,然后在快速连接中建立一个到linux的连接,当然,你要先知道你的系统的ip,在终端中键入ifconfig可以查 ...
- 下载vlc遭遇网络劫持
一次偶然的机遇,在公司下载"vlc播放器",下载之后安装完就变成了"搜狗手机助手".而且连续几次下载都是这样,我以为是我安装了搜狗输入法的缘故. ...
- CentOS下mysql数据库data目录迁移和配置优化
目录迁移 关闭数据库服务 service mysqld stop 复制数据库 mv /var/lib/mysql /data/mysql # 或者使用cp -a复制 # 这两个命令都会带权限到新目录去 ...
- C语言实现数据结构中的堆创建,堆排序
#include "stdio.h"#include "stdlib.h"void swap(int *a,int *b)//交换两个数{int t;t=*a; ...
- HTML标签的命名/CSS标准化命名大全
在一个内容较多的HTML页面中,需要设计许多不同的框架,再为这些不同的框架及内容进行分类,给予相应的名称,从而使得网页结构更加清晰,也为工作提供了方便.许多新手朋友在设计一个HTML文件时,可能只会依 ...
- JSONP && CORS
前天面试被问到了跨域的问题,自我感觉回答的并不理想,下面我就分享一下整理后的总结分享给大家 一.为什么要跨域 安全限制 JavaScript或Cookie只能访问同域下的内容——同源策略 同源策略 下 ...