基于CentOS制作Linux发行版光盘

Posted by – 2011-12-28

#############################################
# Custom Linux ISO Bulid
# Author: 楚霏
# Company: chengyongxu.com
# Date: 2011-04-28
# Update: 2011-12-23
# Env: CentOS 5.5 x86_64 && CentOS 6.2 x86_64
#############################################
一、安装制作发行版所需的基本软件包并创建工作目录

1yum -y install anaconda-runtime createrepo yum-utils anacondaanaconda-help busybox-anaconda mkisofs
2mkdir /os
3mkdir /mnt/cdrom

二、复制母盘文件

01#挂载光盘镜像
02mount -t iso9660 -o loop /usr/local/src/CentOS-5.5-x86_64-bin-DVD-1of2.iso  /mnt/cdrom
03mount -t iso9660 -o loop /usr/local/src/CentOS-6.2-x86_64-bin-DVD1.iso  /mnt/cdrom
04cd /mnt/cdrom
05#将光盘文件拷到工作目录
06tar -cf - . | ( cd /os ; tar -xvpf - )
07#删除无用的文件
08cd /os/
09rm RELEASE-NOTES-* -f
10 
11#centos5下是:
12rm -rf /os/CentOS/*
13#centos6下是:
14rm -rf /os/Packages/*

三、定制package.list软件包清单

01#以一台最小安装的线上服务器作为模板,将这台模板服务器安装时的/root/anaconda-ks.cfg拷贝到本地
02#然后把这台模板服务器的RPM包信息导出拷贝到本地,导出方法是:
03rpm -qa |awk -F- '{print $1}' >> packages.list
04#可以统计一下条数
05wc -l /root/packages.list
06#如果你指定了alias cp=cp -i,刚先取消
07alias cp=cp
08 
09#把需要的RPM包列表中的包从光盘镜像拷贝到目标目录的相应位置
10#centos5下是:
11for i in `cat /root/packages.list`;do cp -vrf /mnt/cdrom/CentOS/$i* /os/CentOS/;done
12#centos6下是:
13for i in `cat /root/packages.list`;do cp -vrf /mnt/cdrom/Packages/$i* /os/Packages/;done
14 
15#把想安装系统之后放入系统的源码包(chengyongxu.com.tar.gz)放入工作目录下
16mv chengyongxu.com.tar.gz /os/
17#下载光盘镜像中没有,但又需要的RPM包,例如:
18#centos5下是:
21 
22#执行createrepo程序生成repodata下的comps.xml文件
23cd /os
24#centos6下需要:
25mv repodata/*-comps.xml repodata/comps.xml
26 
27createrepo -g repodata/comps.xml .

四、配置kickstart

001cd /os
002#把模板服务器上anaconda-ks.cfg拷贝到工作目录
003#订制完光盘默认密码是模板服务器装系统时的密码
004cp anaconda-ks.cfg /os/ks.cfg
005vi/os/isolinux/ks.cfg 并修改脚本如下:
006vi ks.cfg
007#CentOS 5的配置如下
009#----------------------------引用文字-开始----------------------------
010# Kickstart file automatically generated by anaconda.
011 
012install
013cdrom
014lang en_US.UTF-8
015keyboard us
016network --device eth0 --bootproto dhcp --hostname localhost
017rootpw --iscrypted $1$kAVanbBp$QjTH30VQgnj/.gYkQ6aN0
018firewall --enabled --port=22:tcp
019authconfig --enableshadow --enablemd5
020selinux --enforcing
021timezone --utc Asia/Shanghai
022bootloader --location=mbr --driveorder=sda
023# The following is the partition information you requested
024# Note that any partitions you deleted are not expressed
025# here so unless you clear all partitions first, this is
026# not guaranteed to work
027#clearpart --linux
028clearpart --all --initlabel
029part /boot --fstype ext3 --size=200 --asprimary
030part / --fstype ext3 --size=40965
031part swap --size=10245
032part /data --fstype ext3 --size=1 --grow
033 
034%packages
035@base
036@core
037@dialup
038@editors
039@text-internet
040keyutils
041trousers
042fipscheck
043device-mapper-multipath
044autoconf
045automake
046bison
047bzip2-devel
048compat-gcc-34-g77
049compat-libstdc++-296
050compat-libstdc++-33
051curl
052curl-devel
053e2fsprogs-devel
054flex
055freetype-devel
056gcc
057gcc-c++
058gettext-devel
059krb5-devel
060libidn-devel
061libjpeg-devel
062libpng-devel
063libtiff-devel
064libtool
065libxml2
066libxml2-devel
067ncurses-devel
068ntp
069openssl-devel
070pam-devel
071sysstat
072mhash-devel
073zlib-devel
074 
075# chengyongxu.com System Install
076%post --nochroot
077 
078# Mount CDROM
079mkdir -p /mnt/cdrom
080mount -r -t iso9660 /tmp/cdrom /mnt/cdrom
081 
082# Copy our files, extract it ,then move to right locations
083cp /mnt/cdrom/chengyongxu.com.tar.gz /mnt/sysimage/tmp/chengyongxu.com.tar.gz> /dev/null
084cd /mnt/sysimage/tmp/
085tar -zxvf chengyongxu.com.tar.gz > /dev/null
086cp -R /mnt/sysimage/tmp/src/* /mnt/sysimage/usr/local/src/ >/dev/null 2>/dev/null
087 
088# Umount CDROM
089umount /mnt/cdrom
090 
091%post
092 
093# ssh configration
094sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
095 
096# Disable IPV6
097sed -i 's/\IPV6\=yes/\IPV6\=no/g' /etc/sysconfig/network
098echo 'alias net-pf-10 off
099alias ipv6 off' >> /etc/modprobe.conf
100 
101# Vim syntax on
102VI_IN_BASH=`grep vim /root/.bashrc`
103if [ -z "$VI_IN_BASH" ]
104then
105echo "alias vi='vim'" >> /root/.bashrc
106source /root/.bashrc
107fi
108 
109# rsync configration
110echo "alias rsync='rsync -apur --partial -e \"ssh -p 22 -c arcfour\"'" >> /root/.bashrc
111 
112# Show detail when use the "history" command
113echo "HISTTIMEFORMAT=\"%Y-%m-%d %H:%M:%S \"" >> /etc/profile
114 
115# Turn off unnecessary services
116SERVICE_NEED="apmd|atd|auditd|cpuspeed|crond|mdmonitor|mdmpd|microcode-ctl|network|ntpd|readahed_early|readahead_later|smartd|sshd|syslog|irqbalance"
117SERVICE_NONEED=`chkconfig --list|awk '{print $1}'|grep -vE $SERVICE_NEED`
118SERVICE_NEED_LIST=`echo $SERVICE_NEED|sed "s/|/ /g"`
119for i in $SERVICE_NONEED; do chkconfig --level 2345 $i off; service $i stop; done
120for i in $SERVICE_NEED_LIST; do chkconfig --level 2345 $i on; service $i start; done
121#----------------------------引用文字-结束----------------------------
122 
123#CentOS 6的配置如下
124#----------------------------引用文字-开始----------------------------
125# Kickstart file automatically generated by anaconda.
126 
127#version=RHEL6
128install
129cdrom
130lang en_US.UTF-8
131keyboard us
132#network --device eth0 --bootproto dhcp --hostname localhost
133rootpw  --iscrypted $6$5q5hu2jMre1MaP4f$DUEVi7i0qo/4Vrh7iiuemzwLVv96pGWVO.T/SzRLE15qYDY9ZP/UfVNXpsoYTZsNLWrlce.yudFo.L7/BlG1
134firewall --service=ssh
135authconfig --enableshadow --passalgo=sha512 --enablefingerprint
136selinux --enforcing
137timezone --utc Asia/Shanghai
138bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
139# The following is the partition information you requested
140# Note that any partitions you deleted are not expressed
141# here so unless you clear all partitions first, this is
142# not guaranteed to work
143#clearpart --none --drives=sda
144clearpart --all --initlabel
145part /boot --fstype=ext4 --size=200 --asprimary
146part / --fstype=ext4 --size=40965
147part swap --size=10245
148part /data --fstype=ext4 --size=1 --grow
149 
150repo --name="CentOS"  --baseurl=file:///mnt/source --cost=100
151 
152%packages
153@base
154@client-mgmt-tools
155@console-internet
156@core
157@server-policy
158autoconf
159automake
160bison
161bzip2-devel
162cmake
163compat-gcc-34-g77
164compat-libstdc++-296
165compat-libstdc++-33
166curl
167curl-devel
168device-mapper-multipath
169e2fsprogs-devel
170flex
171freetype-devel
172ftp
173gcc
174gcc-c++
175gettext-devel
176htop
177iptraf
178krb5-devel
179lftp
180libidn-devel
181libjpeg-devel
182libpng-devel
183libtiff-devel
184libtool
185libxml2
186libxml2-devel
187logwatch
188lrzsz
189make
190ncurses-devel
191ntp
192openssl-devel
193pam-devel
194patch
195sgpio
196sysstat
197trousers
198mhash-devel
199unix2dos
200unzip
201zlib-devel
202%end
203 
204# chengyongxu.com System Install
205# CentOS 6 Can not use post --nochroot
206%post --nochroot
207 
208# Mount CDROM
209mkdir -p /mnt/cdrom
210mount -r -t iso9660 /dev/cdrom /mnt/cdrom
211 
212# Copy our files, extract it ,then move to right locations
213cd /mnt/cdrom/
214tar -zmxvf chengyongxu.com.tar.gz -C /mnt/sysimage/usr/local/src/ > /dev/null
215cd /mnt
216 
217# Umount CDROM
218umount /mnt/cdrom
219 
220%post
221 
222# ssh configration
223sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
224 
225# Disable IPV6
226echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
227echo 'alias net-pf-10 off
228options ipv6 disable=1' >> /etc/modprobe.d/ipv6-off.conf
229 
230# Vim syntax on
231VI_IN_BASH=`grep vim /root/.bashrc`
232if [ -z "$VI_IN_BASH" ]
233then
234echo "alias vi='vim'" >> /root/.bashrc
235source /root/.bashrc
236fi
237 
238# rsync configration
239echo "alias rsync='rsync -apur --partial -e \"ssh -p 22 -c arcfour\"'" >> /root/.bashrc
240 
241# Show detail when use the "history" command
242echo "HISTTIMEFORMAT=\"%Y-%m-%d %H:%M:%S \"" >> /etc/profile
243 
244# Add a crontab task
245echo "0 */8 * * * /usr/sbin/ntpdate 210.72.145.44" >> /var/spool/cron/root
246 
247# If forget this, will output error : "sudo: sorry, you must have a tty to run sudo" on system boot
248sed -i 's/Defaults    requiretty/#Defaults    requiretty/g' /etc/sudoers
249 
250# Install Nginx PHP MySQL Memcached GD Libxml Libmcrypt Libevent Sphinx Nrpe
251tar -zmxvf /usr/local/src/Etc_Init.d.tar.gz -C /etc/init.d/ > /dev/null
252tar -zmxvf /usr/local/src/my.cnf.tar.gz -C /etc/ > /dev/null
253tar -zmxvf /usr/local/src/Usr_Local.tar.gz -C /usr/local/ > /dev/null
254 
255# Add Users
256groupadd www -g 88
257useradd -u 88 -g www www -m
258useradd mysql
259useradd nagios
260 
261# Add services
262chkconfig --add nginx
263chkconfig --add memcached
264chkconfig --add mysqld
265chkconfig --add nrpe
266chkconfig --add sphinx
267 
268# Turn off unnecessary services
269SERVICE_NEED="apmd|atd|auditd|cpuspeed|crond|irqbalance|memcached|mdmonitor|mdmpd|microcode-ctl|network|nginx|ntpd|nrpe|readahed_early|readahead_later|smartd|sphinx|sshd|syslog"
270SERVICE_NONEED=`chkconfig --list|awk '{print $1}'|grep -vE $SERVICE_NEED`
271SERVICE_NEED_LIST=`echo $SERVICE_NEED|sed "s/|/ /g"`
272for i in $SERVICE_NONEED; do chkconfig --level 2345 $i off; service $i stop; done
273for i in $SERVICE_NEED_LIST; do chkconfig --level 2345 $i on; service $i start; done
274 
275# By 楚霏 (yxcx@yahoo.cn<script type="text/javascript">
276/* <![CDATA[ */
277(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
278/* ]]> */
279</script>)
280#----------------------------引用文字-结束----------------------------

五、让系统从kickstart配置启动

01#修改disk/isolinux/isolinux.cfg文件,如下是我的一个配置文件
02vi isolinux/isolinux.cfg
03#把default选项改为ks
04#append ks initrd=initrd.img改为append ks=cdrom:/ks.cfg initrd=initrd.img
05#----------------------------引用文字-开始----------------------------
06default ks
07prompt 1
08timeout 600
09display boot.msg
10F1 boot.msg
11F2 options.msg
12F3 general.msg
13F4 param.msg
14F5 rescue.msg
15label linux
16  kernel vmlinuz
17  append initrd=initrd.img
18label text
19  kernel vmlinuz
20  append initrd=initrd.img text
21label ks
22  kernel vmlinuz
23  append ks=cdrom:/ks.cfg initrd=initrd.img
24label local
25  localboot 1
26label memtest86
27  kernel memtest
28  append -
29#----------------------------引用文字-结束----------------------------
30 
31#CentOS 6的配置如下
32#----------------------------引用文字-开始----------------------------
33default ks
34prompt 1
35timeout 60
36 
37display boot.msg
38 
39menu background splash.jpg
40menu title Welcome to CentOS 6.2!
41menu color border 0 #ffffffff #00000000
42menu color sel 7 #ffffffff #ff000000
43menu color title 0 #ffffffff #00000000
44menu color tabmsg 0 #ffffffff #00000000
45menu color unsel 0 #ffffffff #00000000
46menu color hotsel 0 #ff000000 #ffffffff
47menu color hotkey 7 #ffffffff #ff000000
48menu color scrollbar 0 #ffffffff #00000000
49 
50label linux
51  menu label ^Install or upgrade an existing system
52  menu default
53  kernel vmlinuz
54  append initrd=initrd.img
55label ks
56  kernel vmlinuz
57  append ks=cdrom:/ks.cfg initrd=initrd.img
58label vesa
59  menu label Install system with ^basic video driver
60  kernel vmlinuz
61  append initrd=initrd.img xdriver=vesa nomodeset
62label rescue
63  menu label ^Rescue installed system
64  kernel vmlinuz
65  append initrd=initrd.img rescue
66label local
67  menu label Boot from ^local drive
68  localboot 0xffff
69label memtest86
70  menu label ^Memory test
71  kernel memtest
72  append -
73#----------------------------引用文字-结束----------------------------

六、生成光盘镜像

1mkisofs -R -J -T -V Boot --allow-multidot --allow-leading-dots -no-bak -input-charset UTF-8 -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot --boot-load-size 4 --boot-info-table -o /tmp/chengyongxu.com-Linux-Release-20111223-x64.iso .
2#可以计算一下md5并保存信息
3md5sum /tmp/Chengyonxu.com-Linux-Release-20111223-x64.iso >> /tmp/md5.txt

七、参考文档

http://bbs.linuxtone.org/thread-2448-1-1.html

http://bbs.linuxtone.org/thread-1240-1-1.html

http://hi.baidu.com/zj8la8la/blog/item/8aac2b02b8fd5b85d53f7c7c.html

Logo

更多推荐