Centos7.5的定制化安装

一、前言

  关于定制化centos7.5的镜像真的是历经波折,前前后后、来来回回尝试了不少于20次,上网找了各种关于定制7系统的方法,都没有成功。。。

  但最终功夫不负有心人终于解决了,O(∩_∩)O哈哈~

二、关于报错

  按照之前制作6.9的定制系统的步骤,总是有一部出错,出现如下错误,并且总是卡到安装filesystem:

  

  真的是很让人无语。

三、制作步骤

# mkdir /data/OS
# rsync -a --exclude=Packages/ --exclude=repodata/ /mnt/cdrom/ /data/OS/

  这里需要注意:如果没有拷贝.discinfo文件,将导致使用定制后的ISO安装系统时,在初始化过程中提示找不到光盘!

  之后拷贝已安装的软件包:

# mkdir -p /data/OS/Packages
# vim /root/copy-rpms.sh

内容如下:
#!/bin/bash
rpm -qa  > package-list.txt
old_rpms='/mnt/cdrom/Packages'
new_rpms='/data/OS/Packages'
while read line; do
    cp ${old_rpms}/${line}*.rpm ${new_rpms} || echo "${line} not exist..."
done < package-list.txt
rm -f package-list.txt

# bash /root/copy-rpms.sh

 下面在isolinux目录中文件中创建ks.cfg

# Kickstart file automatically generated by anaconda.
# version=DEVEL

#-------------------------------------------------------------------------------
# Command section --- must include the required options.
#-------------------------------------------------------------------------------

# Text mode or graphical mode?
text
firstboot --disable

# Install or upgrade?
install

# Use CDROM installation media
cdrom

# Network information
# network --onboot yes --device eth0 --bootproto dhcp --noipv6
# network --hostname=localhost.localdomain

# System authorization information
auth --enableshadow --passalgo=sha512

# Root password
rootpw --iscrypted $6$tbGvXZz7E2ZHKiFw$krdZKp7WUY4iNierOFh4ZbV1FhroTuomNr71.0Ns52xARVrfr39DnU64sHqZv/K2iWUvMw0cI6DO64VgQWYRg0

# System services,禁用chronyd,防止与ntpd冲突
services --disabled="chronyd"

# System timezone
timezone Asia/Shanghai

# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'

# System language
lang en_US.UTF-8

# Firewall configuration
firewall --disabled

# SELinux configuration
selinux --disabled

# Installation logging level
logging --level=info

# Reboot after installation
reboot

#-------------------------------------------------------------------------------
# The %pre sections
#-------------------------------------------------------------------------------
%include /tmp/part-include
%pre
#!/bin/sh

#-------------------------------------------------------------------------------
# 非超微4U4Node机型时,设置系统盘标示符: 如果只有一个磁盘,则不论磁盘大小多大;
# 如果有多个磁盘,则按自定义的规则选择系统安装盘
#-------------------------------------------------------------------------------
function get_osdisk() {
    disk_count=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | wc -l)
    if [ ${disk_count} -eq 1 ]; then
        osdisk=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | cut -d: -f1 | cut -d/ -f3)
    else
        osdisk=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | awk '{if ($3 >=50 && $3 < 2000) print $0}' | cut -d: -f1 | cut -d/ -f3)
    fi
}

#-------------------------------------------------------------------------------
# 设置swap大小,如果内存mem<=2G,则swap设置为与物理内存大小一样;
# 如果内存2G<mem<=16G,swap=8G; 如果内存mem>16G,则swap=16G
# 注意:/proc/meminfo中单位是kb,而kickstart中指定swap大小时单位是mb
#-------------------------------------------------------------------------------
function get_swap() {
    act_mem=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
    if [ ${act_mem} -le $((2*1024*1024)) ]; then
        swap_size=$((2*1024))
    elif [ ${act_mem} -gt $((2*1024*1024)) ] && [ ${act_mem} -le $((16*1024*1024)) ]; then
        swap_size=$((8*1024))
    else
        swap_size=$((16*1024))
    fi
}

#-------------------------------------------------------------------------------
# 如果是超微4U4Node机型,使用了主板自带的BIOS RAID set
# 假设创建RAID 1的名称为Volume0,则它的磁盘标示符为Volume0_0,是/dev/md126的软连接
# 如果不是超微4U4Node机型,且有SATA或SCSI磁盘,则不论是否还有IDE磁盘,都使用sdx;
# 如果仅有IDE磁盘,则使用hdx
#-------------------------------------------------------------------------------
if $(grep -q 'md.*$' /proc/partitions); then
    osdisk="Volume0_0"
    get_swap    # 调用函数
elif $(grep -q 'sd.*$' /proc/partitions); then
    osdisk_prefix='sd'
    get_osdisk  # 调用函数
    get_swap    # 调用函数
elif $(grep -q 'hd.*$' /proc/partitions); then
    osdisk_prefix='hd'
    get_osdisk  # 调用函数
    get_swap    # 调用函数
else
    print "Lost disks, please install disks first!"
    exit 1
fi

#-------------------------------------------------------------------------------
# 生成/tmp/part-include临时文件
#-------------------------------------------------------------------------------
cat >> /tmp/part-include << EOF
# System bootloader configuration
bootloader --location=mbr --boot-drive=${osdisk}
# Partition clearing information
clearpart --all --initlabel
# Clear the Master Boot Record
zerombr
# Disk partitioning information
part biosboot --fstype="biosboot" --ondisk=${osdisk} --size=1  # 当磁盘大于2T时,以BIOS方式安装CentOS到GPT分区表的磁盘,需要创建1MB的biosboot分区
part /boot --fstype="xfs" --ondisk=${osdisk} --size=500
part /boot/efi --fstype="xfs" --ondisk=${osdisk} --size=500
part pv.01 --fstype="lvmpv" --ondisk=${osdisk} --size=1 --grow
volgroup centos --pesize=4096 pv.01
logvol swap  --fstype="swap" --size=${swap_size} --name=swap --vgname=centos
logvol /  --fstype="xfs" --grow --maxsize=102400 --size=1024 --name=root --vgname=centos
EOF

%end

#-------------------------------------------------------------------------------
# The %packages section
#-------------------------------------------------------------------------------
%packages
@^developer-workstation-environment
@additional-devel
@base
@compat-libraries
@core
@debugging
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-apps
@gnome-desktop
@guest-desktop-agents
@input-methods
@internet-applications
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@performance
@perl-runtime
@print-client
@ruby-runtime
@virtualization-client
@virtualization-hypervisor
@virtualization-tools
@web-server
@x11
kexec-tools

%end

# 启用kdump
%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

#-------------------------------------------------------------------------------
# The %post sections
#-------------------------------------------------------------------------------
%post
#!/bin/sh
# 设置Bash Color
echo 'PS1="\[\e[32;1m\][\u@\h \W]\\$ \[\e[0m\]"' >> /root/.bashrc
. /root/.bashrc

%end

   下面是有一定更改后的ks文件:

# Kickstart file automatically generated by anaconda.
# version=DEVEL

#-------------------------------------------------------------------------------
# Command section --- must include the required options.
#-------------------------------------------------------------------------------

# Text mode or graphical mode?
text
firstboot --disable

# Install or upgrade?
install

# Use CDROM installation media
cdrom

# Network information
# network --onboot yes --device eth0 --bootproto dhcp --noipv6
# network --hostname=localhost.localdomain

# System authorization information
auth --enableshadow --passalgo=sha512

# Root password
rootpw --iscrypted $1$n5Jfcfwa$//2gZpFMJypdiXEF8ld6O.

# System services,禁用chronyd,防止与ntpd冲突
services --disabled="chronyd"

# System timezone
timezone Asia/Shanghai

# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'

# System language
lang en_US.UTF-8

# Firewall configuration
firewall --disabled

# SELinux configuration
selinux --disabled

# Installation logging level
logging --level=info

# Reboot after installation
reboot

#-------------------------------------------------------------------------------
# The %pre sections
#-------------------------------------------------------------------------------
%include /tmp/part-include
%pre
#!/bin/sh

#-------------------------------------------------------------------------------
# 非超微4U4Node机型时,设置系统盘标示符: 如果只有一个磁盘,则不论磁盘大小多大;
# 如果有多个磁盘,则按自定义的规则选择系统安装盘
#-------------------------------------------------------------------------------
function get_osdisk() {
    disk_count=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | wc -l)
    if [ ${disk_count} -eq 1 ]; then
        osdisk=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | cut -d: -f1 | cut -d/ -f3)
    else
        osdisk=$(fdisk -l | grep "Disk /dev/${osdisk_prefix}" | awk '{if ($3 >=50 && $3 < 2000) print $0}' | cut -d: -f1 | cut -d/ -f3)
    fi
}

#-------------------------------------------------------------------------------
# 设置swap大小,如果内存mem<=2G,则swap设置为与物理内存大小一样;
# 如果内存2G<mem<=16G,swap=8G; 如果内存mem>16G,则swap=16G
# 注意:/proc/meminfo中单位是kb,而kickstart中指定swap大小时单位是mb
#-------------------------------------------------------------------------------
function get_swap() {
    act_mem=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
    if [ ${act_mem} -le $((2*1024*1024)) ]; then
        swap_size=$((2*1024))
    elif [ ${act_mem} -gt $((2*1024*1024)) ] && [ ${act_mem} -le $((16*1024*1024)) ]; then
        swap_size=$((8*1024))
    else
        swap_size=$((16*1024))
    fi
}

#-------------------------------------------------------------------------------
# 如果是超微4U4Node机型,使用了主板自带的BIOS RAID set
# 假设创建RAID 1的名称为Volume0,则它的磁盘标示符为Volume0_0,是/dev/md126的软连接
# 如果不是超微4U4Node机型,且有SATA或SCSI磁盘,则不论是否还有IDE磁盘,都使用sdx;
# 如果仅有IDE磁盘,则使用hdx
#-------------------------------------------------------------------------------
if $(grep -q 'md.*$' /proc/partitions); then
    osdisk="Volume0_0"
    get_swap    # 调用函数
elif $(grep -q 'sd.*$' /proc/partitions); then
    osdisk_prefix='sd'
    get_osdisk  # 调用函数
    get_swap    # 调用函数
elif $(grep -q 'hd.*$' /proc/partitions); then
    osdisk_prefix='hd'
    get_osdisk  # 调用函数
    get_swap    # 调用函数
else
    print "Lost disks, please install disks first!"
    exit 1
fi

#-------------------------------------------------------------------------------
# 生成/tmp/part-include临时文件
#-------------------------------------------------------------------------------
cat >> /tmp/part-include << EOF
# System bootloader configuration
bootloader --location=mbr --boot-drive=${osdisk}
# Partition clearing information
clearpart --all --initlabel
# Clear the Master Boot Record
zerombr
# Disk partitioning information
part biosboot --fstype="biosboot" --ondisk=${osdisk} --size=1  # 当磁盘大于2T时,以BIOS方式安装CentOS到GPT分区表的磁盘,需要创建1MB的biosboot分区
part /boot --fstype="ext4" --ondisk=${osdisk} --size=1024
part /boot/efi --fstype="ext4" --ondisk=${osdisk} --size=1024
part / --fstype="ext4" --ondisk=${osdisk} --size=1 --grow
part swap  --fstype="swap" --size=${swap_size}
EOF

%end

#-------------------------------------------------------------------------------
# The %packages section
#-------------------------------------------------------------------------------
%packages
@^developer-workstation-environment
@additional-devel
@base
@compat-libraries
@core
@debugging
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-apps
@gnome-desktop
@guest-desktop-agents
@input-methods
@internet-applications
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@performance
@perl-runtime
@print-client
@ruby-runtime
@virtualization-client
@virtualization-hypervisor
@virtualization-tools
@web-server
@x11
kexec-tools

%end

# 启用kdump
%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

#-------------------------------------------------------------------------------
# The %post sections
#-------------------------------------------------------------------------------
%post --nochroot --log=/mnt/sysimage/root/postinstall_stage1.log
mkdir -p /mnt/source
mount -o loop /dev/cdrom /mnt/source
cp /mnt/source/software/netgainagent_v3.tar.gz /mnt/sysimage/usr/
#cp /mnt/source/software/netgainagent_v4.tar.gz /mnt/sysimage/usr/
cp /mnt/source/software/openssh-7.7p1.tar.gz /mnt/sysimage/usr/local
cp /mnt/source/software/openssl-1.0.1t.tar.gz /mnt/sysimage/usr/local
cp /mnt/source/software1/ftp-0.17-67.el7.x86_64.rpm /mnt/sysimage/tmp/
umount -f /mnt/source
%end
%post --log=/root/postinstall_stage2.log
#agent
cd /usr
tar zxvf netgainagent_v3.tar.gz
echo "==>Uncompress netgainagent ok!\n" >> /root/postinstall_stage2.log
#ftp libstdc++
rpm -ivh /tmp/ftp-0.17-67.el7.x86_64.rpm
#openssl and openssh
#set /etc/resolv.conf
cat > /etc/resolv.conf << EOF
nameserver 218.241.99.50
nameserver 218.241.118.144
EOF
echo "==>Set OS DNS ok!\n" >> /root/postinstall_stage2.log
#ntp
cat >> /var/spool/cron/root << EOF
*/3 * * * * /usr/sbin/ntpdate ntp.cnnic.cn && /sbin/hwclock -w
# */3 * * * * /usr/sbin/ntpdate 10.10.1.12 && /sbin/hwclock -w
# */3 * * * * /usr/sbin/ntpdate 10.20.2.53 && /sbin/hwclock -w
EOF
echo "==>Set OS NTP ok!\n" >> /root/postinstall_stage2.log
#delete tar.gz file
rm -rf /usr/netgainagent_v3.tar.gz
#erase firewalld
yum erase firewalld -y
yum erase dnsmasq -y
yum erase cups -y
yum erase postfix -y
#history size
cat >> /etc/bashrc <<EOF
HISTSIZE=2000
HISTFILESIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
EOF
#!/bin/sh
# 设置Bash Color
echo 'PS1="\[\e[32;1m\][\u@\h \W]\\$ \[\e[0m\]"' >> /root/.bashrc
. /root/.bashrc

%end

 

 

  注意:kickstart文件的 %pre%post 前后不能有空格,否则会报错 Unable to open input kickstart file

 之后提供启动菜单,在这里我终于明白了为何之前我制作的镜像在虚拟机挂在和光盘安装都是 好的,但刻录在U盘安装时候就会报错的原因了

  • inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg ,通过LABEL指定 kickstart 文件位置的方式,制作的ISO文件可以用于U盘启动(注意别少冒号)
  • inst.ks=cdrom:/isolinux/ks.cfg,这种指定 kickstart 文件位置的方式,制作的ISO文件只能用虚拟机挂载安装或者刻录成光盘安装,无法制作成U盘安装,因为U盘安装时,U盘不是光盘而被当作一个磁盘
    label Install CentOS 7.5 BasicWebServer with Kickstart
      menu label ^Install CentOS 7.5 BasicWebServer with Kickstart
      menu default
      kernel vmlinuz
      append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=hd:LABEL=CentOS7:/isolinux/ks.cfg
    

     这是在isolinux.cfg中添加的内容

    之后重新生成repodata目录及其中的文件
# yum -y install createrepo
# mkdir /tmp/iso/repodata
# cp /mnt/cdrom/repodata/*-comps.xml /data/OS/repodata/comps.xml
# cd /tmp/iso
# createrepo -g /tmp/iso/repodata/comps.xml /data/OS

 

  编辑了comps.xml,要重新生成新的,因为comps.xml的sha值会改变,这样就会导致跟repomd.xml中所记录的不同,否则安装的时候会报错An error occurred umounting the CD. Please make sure you’are not accessing /mnt/source from the shell on tty2 an the click OK retry

至此,comps.xml文件和其他相关联的文件已被重新生成到repodata目录下。

  生成新的ISO镜像,并嵌入MD5值

genisoimage -o /data/CentOS-7.5-AutoInstall.iso -R -J -T -V CentOS7 -v -cache-inodes -joliet-long -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot /data/OS/

 

implantisomd5 /data/CentOS-7.5-AutoInstall.iso

   这样就生成了iso文件,可以拷贝出来进行安装系统了,但最后需要手工进行接收使用centos7条款协议,参照英文提示输入相应的英文或数字进行接收即可。

 

参考文章:

http://www.madmalls.com/blog/post/customize-centos-7-3-autoinstall-iso/

 

 

 

posted @ 2018-07-23 08:34  KaShing  阅读(2284)  评论(0编辑  收藏  举报