CentOS7 定制ISO镜像

centos7官方的ISO镜像太大且安装时间太久,线上应用往往只需要使用部分的包,定制化可以有效精简镜像和减少安装时间,通过定制化的KS文件更是可以进行操作系统的初始化配置 。
一、获取官方ISO镜像CentOS7的官方ISO镜像自行下载,下载后挂载到自己的虚拟并将ISO文件拷贝出来
mount /dev/cdrom /mediamkdir centos ; cd centos ; cp -ra /media/* .二、安装镜像制作工具yum -y install createrepo mkisofs三、KS文件制作3.1 KS文件的语法自行学习,可以从一台已经安装好的服务器的/root/anaconda-ks.cfg拷下来修改并放到镜像文件里的isolinux目录 。
cp /root/anaconda-ks.cfg /root/centos/isolinux/ks.cfginstallcdrom # Use CDROM installation mediatext # Use text install# Keyboard layoutskeyboard --vckeymap=us --xlayouts='us'# System languagelang en_US.UTF-8# System servicesservices --disabled="chronyd"# System timezonetimezone Asia/Shanghai --isUtc# SELinux configurationselinux --disabled# Firewall configurationfirewall --disabled# Network informationnetwork--bootproto=dhcp --device=eth0 --onboot=yes --ipv6=no --no-activatenetwork--hostname=localhost.localdomain# System authorization informationauth --enableshadow --passalgo=sha512# user passwordrootpw --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 bootfirstboot --disabled# Clear the Master Boot Recordzerombrignoredisk --only-use=sda# System bootloader configurationbootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda# Partition clearing informationclearpart --all --initlabel --drives=sda# Disk partitioning informationpart / --asprimary --fstype="ext4" --size=51200part /boot --asprimary --fstype="ext4" --size=1024part swap --fstype="swap"--size=8192part /home --fstype="ext4" --grow --size=1%packages@base@compat-libraries@corekexec-toolsvimlrzsz%end%addon com_redhat_kdump --enable --reserve-mb='auto'%end%postmv /bin/vi /bin/vi.bak && ln -s /bin/vim /bin/visystemctl disable NetworkManagersystemctl stop NetworkManager# SSHsed -i "s@#Port 22@Port 8222@g" /etc/ssh/sshd_configsed -i "s@#PermitRootLogin yes@PermitRootLogin no@g" /etc/ssh/sshd_configsed -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.confsed -i "s@*@#*@g" /etc/security/limits.d/20-nproc.confsed -i "s@root@#root@g" /etc/security/limits.d/20-nproc.confcat > /etc/sysctl.conf << EOF# TCP net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 3600net.ipv4.tcp_keepalive_intvl = 15net.ipv4.tcp_keepalive_probes = 3net.ipv4.tcp_max_tw_buckets= 20480net.ipv4.tcp_max_syn_backlog = 65535net.ipv4.tcp_syncookies = 1net.ipv4.tcp_synack_retries = 2net.ipv4.tcp_syn_retries = 2net.ipv4.tcp_timestamps = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_orphan_retries = 3net.ipv4.tcp_max_orphans = 65536net.ipv4.ip_forward = 1net.ipv4.ip_local_port_range = 102465535net.core.netdev_max_backlog = 30000net.core.somaxconn = 65535fs.file-max = 262144# TCP memorynet.ipv4.tcp_rmem = 4096 4096 16777216net.ipv4.tcp_wmem = 4096 4096 16777216net.ipv4.tcp_mem = 786432 2097152 3145728net.core.rmem_default = 262144net.core.wmem_default = 262144net.core.rmem_max = 16777216net.core.wmem_max = 16777216# ipv6net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1EOF%end# Reboot after installationreboot3.2 修改引导加载程序
cd /root/centos/isolinux; vi isolinux.cfglabel linuxmenu label ^Install CentOS 7menu defaultkernel 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/Packages4.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【CentOS7 定制ISO镜像】4.3 重新生成repodata目录和文件
cd /root/centos; createrepo -g repodata/comps.xml .四、生成ISO镜像cd /root/centosgenisoimage -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


推荐阅读