感谢支持
我们一直在努力

CentOS 5.5裁剪笔记

因为要基于PC的小盒子做嵌入式开发,所以一直考虑现有linux发型版的裁剪。


权衡了很久,觉得cdlinux还是不够可靠。因为作者不再维护了


还是选择裁剪RedHat.


参考网上的一篇CentOS裁剪笔记.


手头的as5光盘镜像安装失败,说介质损坏,真的假的,网上下的iso,好几G呢


只好用Fedora14先试了。


先虚拟了一个2G的scsi硬盘,手工分区。/ 1.5G. swap 500M


选择最小化安装


一共198个包,安装完毕500M左右


Vmdisk使用686M


Df –k显示 495M


在虚拟了一个1G 的ide盘。手工分区 / 1023


同样选择最小化安装。


再虚拟了一个500M的ide盘,准备放裁剪后的


问题:


1.  没有是否启用selinux的选项,以前有的应用会出错,必须关闭selinux


2.  没有setup程序,网络配置换命令了?


3.  默认没有sar


4. 没有swap是否可以? 我在1g盘上安装时可以没有swap.(2g盘上有,装完后吧2g盘去掉了)


目标:


通过rpm进一步调整安装包和大小


先不进行裁剪,直接测试内存boot测试


Rpm –qa | wc –l  可以看到一共198个包

因为感觉fedola偏重桌面,所以换CentOS试试。
又搞了一个centos5.4 的镜像。
安装时怎么不认scsi的虚拟硬盘?
算了,先换ide的试试。
第一次4g
自定义安装,选了base和拨号网络,一共367包,一共1G(df –k看905M)
安装完毕,可以连接网络。作为母版
再装一个较小版的2G。
通过linux text方式可以进行精细的定制,保留了很少一部分功能。大约663M,252包
再装一个最小版的1.5G,全部都不选, 186包,574M
这次网络没问题了,但是setup,which等常用命令都没了。


[root@cent5 ~]# rpm -qa
filesystem-2.4.0-2.el5.centos
tzdata-2009k-1.el5
cracklib-dicts-2.8.9-3.3
centos-release-notes-5.4-4
libgcc-4.1.2-46.el5
rootfiles-8.1-1.1.1
zlib-1.2.3-3
glib2-2.12.3-4.el5_3.1
popt-1.10.2.3-18.el5
expat-1.95.8-8.2.1
bash-3.2-24.el5
libsepol-1.15.2-2.el5
nspr-4.7.4-1.el5_3.1
readline-5.1-3.el5
sed-4.1.5-5.fc6
elfutils-libelf-0.137-3.el5
libgpg-error-1.4-2
libgcrypt-1.4.4-5.el5
libpng-1.2.10-7.1.el5_3.2
libacl-2.2.39-3.el5
libcap-1.10-26
libtiff-3.8.2-7.el5_3.4
diffutils-2.8.1-15.2.3.el5
iptables-1.3.5-5.3.el5
slang-2.0.6-4.el5
libXau-1.0.1-3.1
iproute-2.6.18-10.el5
gnutls-1.4.1-3.el5_2.1
grep-2.5.1-55.el5
hmaccalc-0.9.6-1.el5
less-394-6.el5
cpio-2.6-23.el5
iputils-20020927-46.el5
checkpolicy-1.33.1-4.el5
ethtool-6-3.el5
wireless-tools-28-2.el5
libvolume_id-095-14.21.el5
sgpio-1.2.0_10-2.el5
libXdmcp-1.0.1-2.1
udftools-1.0.0b3-0.1.el5
file-4.17-15.el5_3.1
hdparm-6.6-2
centos-release-5-4.el5.centos.1
crontabs-1.10-8
xorg-x11-filesystem-7.1-2.fc6
libXrender-0.9.1-3.1
libXfixes-4.0.1-2.1
libXrandr-1.1.1-3.1
libXinerama-1.0.1-2.1
libselinux-1.33.4-5.5.el5
e2fsprogs-libs-1.39-23.el5
e2fsprogs-1.39-23.el5
findutils-4.2.27-6.el5
krb5-libs-1.6.1-36.el5
Python-2.4.3-27.el5
cairo-1.2.4-5.el5
libsemanage-1.9.1-4.4.el5
rpm-libs-4.4.2.3-18.el5
pango-1.14.9-6.el5.centos
audit-libs-python-1.7.13-2.el5
python-sqlite-1.1.7-1.2.1
cracklib-2.8.9-3.3
udev-095-14.21.el5
SysVinit-2.86-15.el5
openldap-2.3.43-3.el5
passwd-0.73-1
cups-libs-1.3.7-11.el5
device-mapper-event-1.02.32-1.el5
logrotate-3.7.4-9
rpm-python-4.4.2.3-18.el5
libselinux-utils-1.33.4-5.5.el5
net-tools-1.60-78.el5
authconfig-5.3.21-6.el5
system-config-securitylevel-tui-1.6.29.1-2.1.el5
vim-minimal-7.0.109-6.el5
python-iniparse-0.2.3-4.el5
gtk2-2.10.4-20.el5
ecryptfs-utils-75-5.el5
yum-fastestmirror-1.1.16-13.el5.centos
fipscheck-lib-1.2.0-1.el5
dbus-libs-1.1.2-12.el5
dbus-1.1.2-12.el5
dbus-glib-0.73-8.el5
mcstrans-0.2.11-3.el5
dmraid-events-1.0.0.rc13-53.el5
openssh-server-4.3p2-36.el5
sysklogd-1.4.1-44.el5
dhcpv6-client-1.0.10-17.el5
selinux-policy-targeted-2.4.6-255.el5
module-init-tools-3.3-0.pre3.1.54.el5
kernel-2.6.18-164.el5
hal-0.5.8.1-52.el5
pm-utils-0.99.3-10.el5.centos
setup-2.5.58-7.el5
basesystem-8.0-5.1.1.el5.centos
glibc-common-2.5-42
nash-5.1.19.6-54
termcap-5.5-1.20060701.1
gnu-efi-3.0c-1.1
glibc-2.5-42
chkconfig-1.3.30.1-2
mktemp-1.5-23.2.2
audit-libs-1.7.13-2.el5
libtermcap-2.0.8-46.1
info-4.8-14.el5
ncurses-5.5-24.20060715
nss-3.12.3.99.3-1.el5.centos.2
sqlite-3.3.6-5
freetype-2.2.1-21.el5_3
bzip2-libs-1.0.3-4.el5_2
libstdc++-4.1.2-46.el5
gawk-3.1.5-14.el5
libattr-2.4.32-1.1
keyutils-libs-1.2-1.el5
libjpeg-6b-37
db4-4.3.29-10.el5
libxml2-2.6.26-2.1.2.8
atk-1.12.2-1.fc6
libsysfs-2.0.0-6
iptables-ipv6-1.3.5-5.3.el5
keyutils-1.2-1.el5
pcre-6.6-2.el5_1.7
libusb-0.1.12-5.1
procps-3.2.7-11.1.el5
gzip-1.3.5-10.el5.centos
binutils-2.17.50.0.6-12.el5
tcl-8.4.13-4.el5
tcp_wrappers-7.6-40.7.el5
mingetty-1.07-5.2.2
gdbm-1.8.0-26.2.1
dmidecode-2.9-1.el5
cyrus-sasl-lib-2.1.22-5.el5
sysfsutils-2.0.0-6
ed-0.2-39.el5_2
libhugetlbfs-1.3-3.el5
setserial-2.17-19.2.2
RedHat-logos-4.9.99-11.el5.centos
bitstream-vera-fonts-1.10-7
libX11-1.0.3-11.el5
libXext-1.0.1-2.1
libXcursor-1.1.7-1.1
libXi-1.0.1-3.1
grub-0.97-13.5
device-mapper-1.02.32-1.el5
shadow-utils-4.0.17-14.el5
kpartx-0.4.7-30.el5
coreutils-5.97-23.el5
openssl-0.9.8e-12.el5
fontconfig-2.4.1-7.el5
newt-0.52.2-12.el5
cryptsetup-luks-1.0.3-5.el5
libXft-2.1.10-1.1
libselinux-python-1.33.4-5.5.el5
python-elementtree-1.2.6-5
yum-metadata-parser-1.1.2-3.el5.centos
pam-0.99.6.2-6.el5
util-linux-2.13-0.52.el5
m2crypto-0.16-6.el5.6
libuser-0.54.7-2.el5.5
usermode-1.88-3.el5.2
device-mapper-multipath-0.4.7-30.el5
lvm2-2.02.46-8.el5
rpm-4.4.2.3-18.el5
tar-1.15.1-23.0.1.el5
psmisc-22.2-7
MAKEDEV-3.23-1.2
prelink-0.4.0-2.el5
rhpl-0.194.1-1
python-urlgrabber-3.1.0-5.el5
hicolor-icon-theme-0.9-2.1
trousers-0.3.1-4.el5
initscripts-8.45.30-2.el5.centos
yum-3.2.22-20.el5.centos
policycoreutils-1.33.12-14.6.el5
openssh-4.3p2-36.el5
dmraid-1.0.0.rc13-53.el5
kbd-1.12-21.el5
fipscheck-1.2.0-1.el5
openssh-clients-4.3p2-36.el5
setools-3.0-3.el5
dhclient-3.0.5-21.el5
selinux-policy-2.4.6-255.el5
mkinitrd-5.1.19.6-54
hwdata-0.213.16-1.el5
pciutils-2.2.3-7.el5
kudzu-1.2.57.1.21-1.el5.centos
[root@cent5 ~]#

一种思路是使用rpm –e –test 挨个尝试卸载没用的包。


一种思路就是按照网上的思路,直接复制目录结构,再复制有用文件,构造最小系统。


先采用步骤2


同时挂接4G和1.5G 的硬盘,选择从4G启动(可以通过vmware 调整顺序,或者bios调整启动顺序)


可是,faint,启动以后看到的内容却是1.5G盘上的内容,用fdisk –l看明明hda是4G,可是df –k看,hda却只有1G。怀疑是vmware的bug,支持单硬盘?


重新装了一个最小版的1G,安装时双硬盘,装在hdb上,选择boot记录不是MBR,而是hdb的第一分区。这回终于对了。


还有一个问题,就是虚拟机的网络老有问题,麻烦(把无线关了似乎好点)


4g:对应完整版


2g:  无用。已删除。


1.5g:  575M  不能和4G共同启动。可独立启动,用于验证rpm卸载功能。


1g 最小版


512M 用于验证裁剪后的小系统启动


问题:


1G的盘后来发现无法启动了,奇怪。(原来可以的,还改过hostname)


只好用cdlinux修复一下grub(用修复盘和grubinstall都不行)


正常了(可以启动,也可以作为从盘和4g盘一起启动)

首先把hdb mount起来


Mount /dev/hdb1 /mnt/hdb


用df –k看,大小为576M


可以看到dev目录下是空的


先全部复制到2G盘上(免得以后再挂双硬盘)


Cd /root


Cp –cpR /mnt/hdb .


卸载hdb,留待以后可以进一步缩减


通过sftp把相关脚本先传上去cp_and_rm.sh


开始裁剪,首先运行cp_and_rm.sh


少了misc和initrd两个目录


得到的目标目录有156M之多


超过了原作者说的80M


接下来复制usr目录 cp_usr.sh,少了一些命令(最小版没有)


[root@locent5old ~]# ./cp_usr.sh


cp: cannot stat `/usr/share/nmap’: No such file or directory


cp: cannot stat `/usr/bin/ftp-rfc’: No such file or directory


cp: cannot stat `/usr/bin/nmap’: No such file or directory


cp: cannot stat `/usr/sbin/traceroute’: No such file or directory


cp: cannot stat `/usr/sbin/traceroute6′: No such file or directory


cp: cannot stat `/usr/sbin/vsftpd’: No such file or directory


cp: cannot stat `/usr/sbin/x86info’: No such file or directory


cp: cannot stat `/usr/lib/libslang-utf8.so.*’: No such file or directory


cp: cannot stat `/usr/lib/libwrap.so.*’: No such file or directory


[root@locent5old ~]#


一共177M,虽然大了点(例如selinux,我感觉完全可以去掉)

接下来准备把它搞到一个硬盘上


修改/boot/grub/grub.conf  (使用ramdisk)


修改/boot/grub/device.map (先不改)


修改fstab


修改rc.sysinit


#创建256M的ramdisk镜像文件并mount在/mnt/test上


使用原作者提供的mkramdisk.sh


通过gzip压缩(注:没有用cramfs或者squashfs)


得到50M的system.img


比想象中小

再搞一个500M的vm硬盘出来挂上


参照前面吧cdlinux装到硬盘的思路,先做一个可以引导的硬盘


Fdisk –l可以看到没有格式化的hdb


分区


[root@locent5old ~]# fdisk /dev/hdb


Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel


Building a new DOS disklabel. Changes will remain in memory only,


until you decide to write them. After that, of course, the previous


content won’t be recoverable. 


The number of cylinders for this disk is set to 1064.


There is nothing wrong with that, but this is larger than 1024,


and could in certain setups cause problems with:


1) software that runs at boot time (e.g., old versions of LILO)


2) booting and partitioning software from other OSs


   (e.g., DOS FDISK, OS/2 FDISK)


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


 


Command (m for help): p


 


Disk /dev/hdb: 549 MB, 549453824 bytes


16 heads, 63 sectors/track, 1064 cylinders


Units = cylinders of 1008 * 512 = 516096 bytes


 


   Device Boot      Start         End      Blocks   Id  System


 


Command (m for help): n


Command action


   e   extended


   p   primary partition (1-4)


p


Partition number (1-4): 1


First cylinder (1-1064, default 1):


Using default value 1


Last cylinder or +size or +sizeM or +sizeK (1-1064, default 1064):


Using default value 1064


 


Command (m for help): m


Command action


   a   toggle a bootable flag


   b   edit bsd disklabel


   c   toggle the dos compatibility flag


   d   delete a partition


   l   list known partition types


   m   print this menu


   n   add a new partition


   o   create a new empty DOS partition table


   p   print the partition table


   q   quit without saving changes


   s   create a new empty Sun disklabel


   t   change a partition’s system id


   u   change display/entry units


   v   verify the partition table


   w   write table to disk and exit


   x   extra functionality (experts only)


 


Command (m for help): w


The partition table has been altered!


 


Calling ioctl() to re-read partition table.


Syncing disks.


[root@locent5old ~]# fdisk -l


 


Disk /dev/hda: 4294 MB, 4294967296 bytes


255 heads, 63 sectors/track, 522 cylinders


Units = cylinders of 16065 * 512 = 8225280 bytes


 


   Device Boot      Start         End      Blocks   Id  System


/dev/hda1   *           1         382     3068383+  83  Linux


/dev/hda2             383         447      522112+  82  Linux swap / Solaris


 


Disk /dev/hdb: 549 MB, 549453824 bytes


16 heads, 63 sectors/track, 1064 cylinders


Units = cylinders of 1008 * 512 = 516096 bytes


 


   Device Boot      Start         End      Blocks   Id  System


/dev/hdb1               1        1064      536224+  83  Linux


[root@locent5old ~]#


 


格式化


mkfs.ext3 /dev/hdb1


 


Mount


Mkdir –p /mnt/test


Mount /dev/hdb11 /mnt/test


 


 


#复制boot/目录到U盘


cp -cpR my_CentOS/boot /mnt/test/


 


#复制新的initrd文件和相关grub配置文件


cp system.img /mnt/test/boot/


 


再来是安装grub


参照cdlinux的硬盘安装方法(可以用cdlinux或者centos5.5的livecd)


Mkdir –p boot/grub


/usr/share/grub/i386-RedHat


Cp * cp * /mnt/test/boot/grub


Cd /mnt/test//boot/grub


Grub


root (hd0,0)


Setup (hd0) 



boot成功!


 


注:换grubinstall试了试


grub-install –root-directory=/mnt/test /dev/hdb


这个启动会进入grub菜单…


    怀疑是/dev/hdb的问题

换用512M硬盘重启。。。结果进了grub的菜单?


手动boot报缺libc.so.6


重新来过


用Cdlinux的光盘启动来安装grub, 这回启动成功了。但是init报错


还是libc.so.没权限?


离胜利还差一步


 


重新mount sysimg


把启动信息输出到串口


还是没权限


禁用selinux


再试


这回成功进入登陆页面了


不过登陆失败?


离成功越来越近了


 


将boot菜单改为单用户模式。从串口登录成功。


还差一点点了。


怀疑是login 相关的库不全


直接mount上4G盘,一个一个复制


/usr/bin/passwd


/usr/sbin/useradd


/usr/lib/libuser.so.*


/usr/lib/libcrack.so*


/usr/lib/cracklib*


/usr/share/cracklib*


终于,useradd,passwd,su都可用了。


 


重新mk_ramdisk,生成新的system.img,重启,ok了!


庆祝一下


[root@Fedora512 ~]# df -k


df -k


Filesystem           1K-blocks      Used Available Use% Mounted on


/dev/ram0               253871    162844     91027  65% /


none                    257668         0    257668   0% /dev/shm


[root@fedora512 ~]#


[root@fedora512 ~]#


 


top – 20:14:56 up 1 min,  2 users,  load average: 0.23, 0.12, 0.04


Tasks:  37 total,   1 running,  36 sleeping,   0 stopped,   0 zombie


Cpu(s):  0.0%us,  0.0%sy,  0.0%ni, 96.6%id,  0.0%wa,  3.4%hi,  0.0%si,  0.0%st


Mem:    515340k total,   292232k used,   223108k free,     1120k buffers


Swap:        0k total,        0k used,        0k free,    19232k cached


 


[root@fedora512 ~]# mount


mount


/dev/ram0 on / type ext2 (rw)


none on /proc type proc (rw)


none on /sys type sysfs (rw)


none on /dev/pts type devpts (rw,gid=5,mode=620)


none on /dev/shm type tmpfs (rw)


none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)


[root@fedora512 ~]#


[root@fedora512 ~]#


 


个别命令还缺一些库文件,不过无伤大雅了


clear: error while loading shared libraries: libncursesw.so.5: cannot open shared object file: No such file or directory


[root@fedora512 grub]# ldd /usr/bin/clear


ldd /usr/bin/clear


        linux-gate.so.1 =>  (0x0068a000)


        libncursesw.so.5 => not found


        libdl.so.2 => /lib/libdl.so.2 (0x004dd000)


        libc.so.6 => /lib/libc.so.6 (0x00d08000)


        /lib/ld-linux.so.2 (0x00511000)


[root@fedora512 grub]#

初步的裁剪,得到的my_CentOS大小约为190M,放在256M的ramdisk运行


保留了除/usr目录下的全部库文件和配置文件


内核与原发行版相同


下一步的目标,一是继续精简模板的大小(通过rpm –e实现)


二是逐步添加需要的用户命令以及库文件(perl,sar等)


2个用于追踪命令相关库的命令


Ldd


Strace


从裁剪的过程可以了解。最终的系统大小主要取决于:


1)      初始安装的最小系统大小


2)      后期选择的用户程序的大小


由于采用系统安装程序可以定制的不多。


先考虑通过rpm卸载实现。一共


发现和Fedora不同,可以卸载的非常少。。。


就卸了3个包。意义不大。


536468


还是算了


反正现在的pc内存大。


权衡一下,还是抱枕感和最新版一致,更重要。

赞(0) 打赏
转载请注明出处:服务器评测 » CentOS 5.5裁剪笔记
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏