制作自动安装系统的centos7镜像

2024-01-11
3分钟阅读时长

1.挂载centos7镜像到本地任意目录,将iso镜像内文件拷贝出来

mkdir /centos7
mount CentOS-7-x86_64-Minimal-2009.iso /centos7
cp -rf /centos7 /root/
[root@kvm centos7]# ls /root/centos7
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  isolinux.cfg  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL

常见目录用途说明:

  • isolinux:存放光盘启动时的安装界面信息
  • images:包含了必要的启动映像文件
  • Packages:存放安装软件包及信息
  • repodata:存放rpm包的依赖信息
  • .discinfo:此文件是安装介质的识别信息

2.配置自动安装的脚本

/root/centos7/isolinux/ks.cfg ,密码为admin ,网卡dhcp模式 ,下面自动判断bois还是efi模式 来自动分区

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# 键盘样式
keyboard 'cn'
# 网络模式
network --bootproto=dhcp
# Root password 可调用python模块生成加密后的密码 python -c 'import crypt,getpass;pw="admin";print(crypt.crypt(pw))'
rootpw --iscrypted $6$cAYWJLiTQ1P6FnlX$09dPD7qwdfzWYIPfCteBUL9Guhcnc7YqOfJEwYrDwN1dU1z7SSrgPXWTxGzG8P/KexwutVANvUi7P.FGzpO7x/
# 系统语言
lang zh_CN.UTF-8
# 时区
timezone Asia/Shanghai
# 系统认证方式
auth  --useshadow  --passalgo=sha512
# 使用cdrom安装
cdrom
# 文本模式安装
text
# 跳过X窗口配置
skipx

firstboot --disable
# 禁用selinux
selinux --disabled
# 禁用防火墙
firewall --disabled
# 安装完后自动重启
#poweroff
reboot


%include /tmp/part-include
%pre
disk=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "nvme0n1") print $4} END{}';done < /proc/partitions)
size=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "nvme0n1") print $3} END{}';done < /proc/partitions)
if [ -z "$disk" ];then
    disk=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "sda") print $4} END{}';done < /proc/partitions)
    size=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "sda") print $3} END{}';done < /proc/partitions)
    if [ -z "$disk" ];then
        disk=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "vda") print $4} END{}';done < /proc/partitions)
        size=$(while read line;do awk 'BEGIN{} {if ($2 == "0" && $4 == "vda") print $3} END{}';done < /proc/partitions)
    fi
fi

# 分区 
if [ $size -lt 2000000000 ];then
# 如果是bois引导方式
cat > /tmp/part-include << EOF
# 引导分区设置
bootloader --location=mbr --boot-drive=$disk
# 清除所有分区
clearpart --all --initlabel --drives=$disk
# 分区详细配置
#part /boot/efi --fstype="efi" --ondisk=$disk --size=600 --fsoptions="umask=0077,shortname=winnt"
#part biosboot --fstype="biosboot" --ondisk=$disk --size=1
part /boot --fstype="ext4" --ondisk=$disk --size=1024
part /     --fstype="xfs" --grow --ondisk=$disk --size=1
EOF
else
# 如果是uefi引导方式
cat > /tmp/part-include << EOF
# 引导分区设置
bootloader --location=mbr --boot-drive=$disk
# 清除所有分区
clearpart --all --initlabel --drives=$disk
# 分区详细配置
#part /boot/efi --fstype="efi" --ondisk=$disk --size=600 --fsoptions="umask=0077,shortname=winnt"
part biosboot --fstype="biosboot" --ondisk=$disk --size=1
part /boot    --fstype="ext4" --ondisk=$disk --size=1024
part /        --fstype="xfs" --grow --ondisk=$disk --size=1
EOF
fi

if [ -d /sys/firmware/efi ] ; then
        sed -i 's/^[^#].*biosboot*/#&/g' -i /tmp/part-include
        sed -i '/^#.*efi/s/^#//g'   -i /tmp/part-include
fi

%end

%packages
@^minimal
@core
chrony
kexec-tools

%end

3.配置/root/centos7/isolinux/isolinux.cfg

default auto
timeout 5
menu title CentOS 7

label auto
  menu label ^Auto install  CentOS 7.9 --How
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CENTOS7 inst.ks=cdrom:/isolinux/ks.cfg quiet

4.配置/root/centos7/EFI/BOOT/grub.cfg

set default="1"

function load_video {
  insmod efi_gop
  insmod efi_uga
  insmod video_bochs
  insmod video_cirrus
  insmod all_video
}

load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2

set timeout=3
### END /etc/grub.d/00_header ###

search --no-floppy --set=root -l 'CentOS 7 x86_64'

menuentry 'Auto Install CentOS 7.9 --How' --class fedora --class gnu-linux --class gnu --class os {
       linuxefi /images/pxeboot/vmlinuz inst.ks=cdrom:/isolinux/ks.cfg inst.stage2=hd:LABEL=CENTOS7 quiet
       initrdefi /images/pxeboot/initrd.img
}

5.配置/root/centos7/isolinux/grub.conf

#debug --graphics
default=1
splashimage=@SPLASHPATH@
timeout 3
hiddenmenu
title Install CentOS 7
	findiso
	kernel @KERNELPATH@ @ROOT@ quiet
	initrd @INITRDPATH@

6.生成镜像

cd /root/centos7
genisoimage -v -cache-inodes -joliet-long -R -J -T -V CENTOS7 -o /root/centos7.iso -c isolinux/boot.cat -bisolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b images/efiboot.img -no-emul-boot .