欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 定制Centos镜像

定制Centos镜像

2025/5/13 22:55:30 来源:https://blog.csdn.net/qq_50247813/article/details/145286244  浏览:    关键词:定制Centos镜像

环境准备:
一台最小化安装的干净的系统,这里使用Centos7.9,一个Centos镜像,镜像也使用Centos7.9的。

[root@localhost ~]# cat /etc/system-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# rpm -qa | wc -l
306
[root@localhost ~]# du -sh CentOS-7-x86_64-DVD-2009.iso
4.4G    CentOS-7-x86_64-DVD-2009.iso
[root@localhost ~]# ls
anaconda-ks.cfg  CentOS-7-x86_64-DVD-2009.iso
  1. 将CentOS-7-x86_64-DVD-2009.iso挂在,并复制到新的目录中

    挂载前,确保挂在点是一个空目录

    [root@localhost ~]# mount -o loop CentOS-7-x86_64-DVD-2009.iso /mnt
    mount: /dev/loop0 is write-protected, mounting read-only
    [root@localhost ~]# cp -rp /mnt Centos-install
    [root@localhost ~]# ls
    anaconda-ks.cfg  CentOS-7-x86_64-DVD-2009.iso  Centos-install
    
  2. 获取最小化安装的所有的包,清空Centos-install中Packages,repodata目录

    [root@localhost ~]# rpm -qa > install.txt
    [root@localhost ~]# rm -rf Centos-install/{Packages,repodata}/*  
    
  3. 根据install.txt文件中的包,从 /mnt 复制到 Centos-install/Packages/

    安装 createrepo 工具:yum -y install createrepo -y

    [root@localhost ~]# awk '{ print $0 }' install.txt | xargs -i cp /mnt/Packages/{}.rpm Centos-install/Packages/
    [root@localhost ~]# ls Centos-install/Packages/ | wc -l
    306
    
  4. /mnt/repodata中将 *-comps.xml 复制到 Centos-install/repodata/comps.xml ,并重新生成软件包组信息的 Yum 软件仓库元数据。

    下载 createrepo 命令:yum -y install createrepo
    在使用 createrepo -g 如果需要指定时comps.xml文件时必须使用 绝对路径,如果comps.xml文件在当前目录下就不用指定路径。

    [root@localhost ~]# cp /mnt/repodata/*-comps.xml Centos-install/repodata/comps.xml
    [root@localhost ~]# ls Centos-install/repodata/
    comps.xml
    [root@localhost ~]# createrepo -g /root/Centos-install/repodata/comps.xml ./Centos-install
    [root@localhost ~]# ls Centos-install/repodata/
    02b45f1cc69b21bf60d81c3fa1d6d7eb5aaa199d1d5f8f3b95e0dfba03db5f4a-other.xml.gz
    0cc929b79a939ad31e0b1d149acfaec55983e818ed245826478b181469161412-primary.sqlite.bz2
    467105b47343286ebebc4d4019c162b4d8810c7479221acb162b5e53c9640b92-primary.xml.gz
    9e702e7199868a3272a66aad7784bc31693aa03931b9c470ce894bdcbaa69da1-filelists.sqlite.bz2
    a4e2b46586aa556c3b6f814dad5b16db5a669984d66b68e873586cd7c7253301-comps.xml.gz
    cca56f3cffa18f1e52302dbfcf2f0250a94c8a37acd8347ed6317cb52c8369dc-comps.xml
    d67ef1422af3b7925046da4e083b1f8a583a698b5a910ab246aefadb4a40c4ea-filelists.xml.gz
    fbeecdbfda867a1415e997673f8ed0773152d6dee49f0fc35f7eaa721e46bede-other.sqlite.bz2
    repomd.xml
    
  5. 创建ks文件

    bios 和 uefi 的ks文件的区别就在与 bios 没有 efi 分区。

    5.1.1. 创建bios引导的ks文件。

    我的 root 密码是 adm@123 ,使用 openssl passwd -1 “yourpasswd” 生成你的密码
    将ks文件放到 Centos-install/isolinux ,放到哪里都可以 / 目录在 Centos-install

    [root@localhost ~]# cat  Centos-install/isolinux/bios.cfg
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8# Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=localhost.localdomain# Root password
    rootpw --iscrypted $6$RtMoOaOfFr8GOMd1$hKlvaWqJD8Ch32drhZVOB2NYFl/zhSsNvj8p1Urvoi8DPpfAFejz06wZ5gKfdoPVhUFZNvOMF/MlUkEXT5L5S.
    # System services
    services --enabled="chronyd"
    # System timezone
    timezone America/New_York --isUtc
    # System bootloader configuration
    bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
    # Partition clearing information
    clearpart --all --drives=sda
    # Disk partitioning information
    part /boot --fstype="ext4" --ondisk=sda --size=200
    part / --fstype="xfs" --ondisk=sda --grow
    %packages
    @^minimal
    @core
    chrony
    kexec-tools%end%addon com_redhat_kdump --enable --reserve-mb='auto'%end%anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %endfirewall --disable
    selinux  --disable
    reboot
    

    5.1.2. 创建uefi引导的ks文件

    将ks文件放到 Centos-install/EFI/BOOT ,放到哪里都可以 / 目录在 Centos-install
    相比与 bios ks文件,uefi ks文件 多了一个 uefi分区 :
    part /boot/efi --fstype="efi" --ondisk=sda --size=200 --fsoptions="umask=0077,shortname=winnt"

    [root@192 ~]# cat Centos-install/EFI/BOOT/uefi.cfg
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8# Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=localhost.localdomain# Root password
    rootpw --iscrypted $6$RtMoOaOfFr8GOMd1$hKlvaWqJD8Ch32drhZVOB2NYFl/zhSsNvj8p1Urvoi8DPpfAFejz06wZ5gKfdoPVhUFZNvOMF/MlUkEXT5L5S.
    # System services
    services --enabled="chronyd"
    # System timezone
    timezone America/New_York --isUtc
    # System bootloader configuration
    bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
    # Partition clearing information
    clearpart --all --drives=sda
    # Disk partitioning information
    part /boot --fstype="ext4" --ondisk=sda --size=200
    part /boot/efi --fstype="efi" --ondisk=sda --size=200 --fsoptions="umask=0077,shortname=winnt"
    part / --fstype="xfs" --ondisk=sda --grow
    %packages
    @^minimal
    @core
    chrony
    kexec-tools%end%addon com_redhat_kdump --enable --reserve-mb='auto'%end%anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %endfirewall --disable
    selinux  --disable
    reboot
    
  6. 修改引导文件

    显示安装 DVD 卷名称: yum -y install genisoimage
    [root@192 ~]# isoinfo -d -i CentOS-7-x86_64-DVD-2009.iso | grep “Volume id” | sed -e ‘s/Volume id: //’ -e ‘s/ /\x20/g’
    CentOS\x207\x20x86_64

    6.1. 修改bios引导文件

    [root@192 ~]# cat Centos-install/isolinux/isolinux.cfg
    default linux
    prompt 1
    timeout 3display boot.msglabel linuxmenu label ^Audo install Centos7.9 systemmenu defaultkernel vmlinuzappend initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/bios.cfg  quiet
    

    6.2. 修改uefi引导文件
    [root@192 ~]# cat Centos-install/EFI/BOOT/grub.cfg

    [root@192 ~]# cat Centos-install/EFI/BOOT/grub.cfg
    set default=0
    set timeout=3echo -e "\nWelcome to the Centos 7.9 installer!\n\n"menuentry 'Auto install Centos7.9 system' {linuxefi  /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 inst.ks=hd:LABEL=CentOS\x207\x20x86_64/EFI/BOOT/uefi.cfg  quietinitrdefi /images/pxeboot/initrd.img
    }
    
  7. 创建自定义的iso镜像

    CentOS 7 x86_64 就是将之前的DVD 卷名称CentOS\x207\x20x86_64\x20 替换为空格

    [root@192 ~]# cd Centos-install/
    [root@192 Centos-install]# mkisofs -untranslated-filenames -volid "CentOS 7 x86_64" -J -joliet-long -rational-rock -translation-table -input-charset utf-8 -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 -o /root/Centos-build-20250127.iso -graft-points /root/Centos-install
    Warning: creating filesystem that does not conform to ISO-9660.
    Size of boot image is 4 sectors -> No emulation
    Size of boot image is 17600 sectors -> No emulation1.02% done, estimate finish Mon Jan 27 07:26:09 20252.05% done, estimate finish Mon Jan 27 07:26:09 20253.07% done, estimate finish Mon Jan 27 07:26:09 20254.10% done, estimate finish Mon Jan 27 07:26:09 2025
    ... ...97.24% done, estimate finish Mon Jan 27 07:26:13 202598.26% done, estimate finish Mon Jan 27 07:26:13 202599.28% done, estimate finish Mon Jan 27 07:26:13 2025
    Total translation table size: 87138
    Total rockridge attributes bytes: 38415
    Total directory bytes: 71680
    Path table size(bytes): 140
    Max brk space used 6d000
    488518 extents written (954 MB)
    
  8. 使用创建好的Centos-build-20250127.iso 镜像测试bios和uefi引导安装
    此处忽略自行测试。

redhat 官网自定义镜像安装手册:

https://docs.redhat.com/zh-cn/documentation/red_hat_enterprise_linux/7/html/installation_guide/sect-simple-install-kickstart#sect-simple-install-kickstart

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词