centos7官方的ISO镜像太大且安装时间太久,线上应用往往只需要使用部分的包,定制化可以有效精简镜像和减少安装时间,通过定制化的KS文件更是可以进行操作系统的初始化配置。
CentOS7的官方ISO镜像自行下载,下载后挂载到自己的虚拟并将ISO文件拷贝出来
mount /dev/cdrom /media
mkdir centos ; cd centos ; cp -ra /media/* .
yum -y install createrepo mkisofs
3.1 KS文件的语法自行学习,可以从一台已经安装好的服务器的/root/anaconda-ks.cfg拷下来修改并放到镜像文件里的isolinux目录。
cp /root/anaconda-ks.cfg /root/centos/isolinux/ks.cfg
install
cdrom # Use CDROM installation media
text # Use text install
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0 --onboot=yes --ipv6=no --no-activate
network --hostname=localhost.localdomain
# System authorization information
auth --enableshadow --passalgo=sha512
# user password
rootpw --iscrypted $6$z4ecbtnCwYNRgJVy$F.vv.twuNIfJQZF6tcEbyJ7Dcyw2ZYt4X0zwFG6nf0FRGVIEYJtDTpr4qmblRjZSd7UTt9XUUlp2S7LQDM9Fs/
user --name=Apps --password=$6$gAIUkrddD/qYdtb1$utKAaA6T/8qWcI/79SrP3xcVvMuA6/8SjHOrJzuSv5tKIwrzVBggkuUL7hLbPUPJNHfbs90jJIUH2NnKDCULm1 --iscrypted --gecos="apps"
# Run the Setup Agent on first boot
firstboot --disabled
# Clear the Master Boot Record
zerombr
ignoredisk --only-use=sda
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Disk partitioning information
part / --asprimary --fstype="ext4" --size=51200
part /boot --asprimary --fstype="ext4" --size=1024
part swap --fstype="swap"--size=8192
part /home --fstype="ext4" --grow --size=1
%packages
@base
@compat-libraries
@core
kexec-tools
vim
lrzsz
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%post
mv /bin/vi /bin/vi.bak && ln -s /bin/vim /bin/vi
systemctl disable NetworkManager
systemctl stop NetworkManager
# SSH
sed -i "s@#Port 22@Port 8222@g" /etc/ssh/sshd_config
sed -i "s@#PermitRootLogin yes@PermitRootLogin no@g" /etc/ssh/sshd_config
sed -i "s@GSSAPIAuthentication yes@GSSAPIAuthentication no@g" /etc/ssh/sshd_config
# 系统优化
echo -e "* soft nofile 262144n* hard nofile 262144n* soft nproc 65535n* hard nproc 65535" >>/etc/security/limits.conf
sed -i "s@*@#*@g" /etc/security/limits.d/20-nproc.conf
sed -i "s@root@#root@g" /etc/security/limits.d/20-nproc.conf
cat > /etc/sysctl.conf << EOF
# TCP
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 3600
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_max_orphans = 65536
net.ipv4.ip_forward = 1
net.ipv4.ip_local_port_range = 1024 65535
net.core.netdev_max_backlog = 30000
net.core.somaxconn = 65535
fs.file-max = 262144
# TCP memory
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
EOF
%end
# Reboot after installation
reboot
3.2 修改引导加载程序
cd /root/centos/isolinux; vi isolinux.cfg
label linux
menu label ^Install CentOS 7
menu default
kernel vmlinuz
#在quit后面添加inst.ks=cdrom:/isolinux/ks.cfg net.ifnames=0 bIOSdevname=0
#LABEL=CentOS7与下面生成镜像指定的保持一致
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 quiet inst.ks=cdrom:/isolinux/ks.cfg net.ifnames=0 biosdevname=0
4.1 进入Packages目录 下,根据自己的实际情况精简不需要用的安装包。
cd /root/centos/Packages
4.2 制作comps文件,将repodata目录下除“*-comps.xml”的所有文件删除,并将“*-comps.xml”文件改为comps.xml
cd /root/centos/repodata; rm -rf *.bz2 *.gz repomd.xml TRANS.TBL
mv cca56f*-comps.xml comps.xml
4.3 重新生成repodata目录和文件
cd /root/centos; createrepo -g repodata/comps.xml .
cd /root/centos
genisoimage -joliet-long -V CentOS7 -o /root/CentOS7-v1-x86_64.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -cache-inodes -T -eltorito-alt-boot -e images/efiboot.img -no-emul-boot /root/centos
至此、精简镜像的工作就完成了,我这里精简后的镜像还有1.1G。