searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Linux x86 ISO系统镜像定制

2024-07-03 09:53:04
0
0

一、流程简介

  1. 准备目标系统iso镜像,将其解压到某个目录下。(Linux中可以挂载后再复制光盘内容到目录下,或者Windows直接打开然后复制到共享目录下等方法)
  2. 添加需要新增的文件、脚本、软件包等到该目录下,可以自己创建新目录进行管理
  3. 配置kickstart文件,设置用户密码、磁盘分区、安装后运行脚本等
  4. 修改grub.cfg(UEFI安装模式)或isolinux.cfg(BOOT安装),在安装菜单栏选项中补充inst.ks配置,指定kickstart配置文件路径
  5. 通过genisoimage命令生成新镜像

二、定制镜像示例

(一)x86系统

  1. 解压iso镜像到某个目录下,几个关键的目录说明:
    1. configuration是自己创建的文件夹,将ks配置、shell脚本、驱动等文件都放在该目录中
    2. EFI放着UEFI模式安装时使用的引导文件
    3. isolinux放着BOOT模式安装时使用的
    4. Package放着所有的rpm包
  2. 将所需的shell脚本、驱动包等放入configuration中相应的目录下,如drivers、shell目录
  3. 将ks配置文件放到configuration/ks目录下,ks文件可以找一台已经安装好的系统,基于/root/anaconda-ks.cfg为基础进行修改,或者通过kickstart图形化工具配置生成
    kickstart官方文档介绍:第 27 章 Kickstart 安装 Red Hat Enterprise Linux 7 | Red Hat Customer Portal
    这里为BOOT和EFI安装模式分别创建了ks-server-7.cfg和ks-server-7-uefi.cfg的ks配置文件
  4. 修改ks配置文件,以BOOT安装模式的ks-server-7.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 --hostname=localhost.localdomain --activate
    #network  --bootproto=dhcp --device=eth0
    network  --bootproto=dhcp --device=ens33
    # Reboot after installation
    reboot
    # Root password,在这里设置root密码,使用sha512加密
    rootpw --iscrypted $6$16_CHARACTER_SAL$7J8EKTQ9M5Ds81llIXBThGsfeuvOdywLs/whlUgfiroltnTm8tPPULCauZGgB8jybcRJ7ILBSCY4VBA8RGUcZ/
    # SELinux configuration
    selinux --disabled
    # System services
    services --disabled="chronyd"
    # Do not configure the X Window System
    skipx
    # System timezone
    timezone Asia/Shanghai --isUtc --nontp
    # System bootloader configuration 需要保证系统有sda磁盘
    bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
    # Clear the Master Boot Record
    zerombr
    # Partition clearing information
    clearpart --all --initlabel
     
    # Disk partitioning information 磁盘自动分区,需要保证有sda磁盘,size单位为MB
    part /boot --fstype="ext4" --size=2001 --ondisk=sda
    part / --fstype="xfs" --grow --size=153601 --ondisk=sda
    part /home --fstype="xfs" --size=20480 --ondisk=sda
     
    #firewall --enabled --service=ssh
     
    # 指定需要预安装的包,@^minimal和@core的内容可以在光盘目录下的comps.xml查看
    %packages
    @^minimal
    @core
    # ...此处省略一些包
    %end
     
    %addon com_redhat_kdump --enable --reserve-mb='auto'
    %end
     
    # 安装后脚本,使用--nochroot将会在临时系统中执行,此时iso挂载在/run/install/repo,安装系统挂载在/mnt/sysimage
    # 可以在这里将iso中文件的拷贝到安装系统中,如果使用挂载的方式经测试有个问题是不确定iso挂载的盘符,例如有可能是/dev/cdrom、/dev/sdb等,没办法写死
    # --log可以指定日志到安装系统目录中
    %post --nochroot --log=/mnt/sysimage/root/ks-post-stage1.log
    # iso mount on /run/install/repo, centos mount on /mnt/sysimage
    mkdir -p /mnt/sysimage/root/ks-install/drivers
    # 拷贝一些驱动文件和脚本到安装系统中
    cp /run/install/repo/configuration/drivers/xxx.zip /mnt/sysimage/root/ks-install/drivers
    %end
     
    # 安装后脚本,不指定--nochroot,此时将会在安装系统中执行
    # 可以在这里进行一些脚本的运行,配置文件、服务等等操作,等同于开机后的shell环境
    # --log可以指定日志到安装系统目录中
    %post --log=/root/ks-post-stage2.log
    sed -i '/ssh/d' /etc/firewalld/zones/public.xml || true
    # exec shell ...
     
    %end
  5. 修改启动引导文件
    对于BOOT安装模式,修改iso中的isolinux/isolinux.cfg,找到 Install CentOS 7 菜单,补充s配置inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/configuration/ks/ks-server-7.cfg,hd:LABEL=CentOS\x207\x20x86_64为安装介质所在盘符

  6. 对于UEFI安装模式,修改iso中的EFI/BOOT/grub.cfg,找到 Install CentOS 7 菜单,补充配置inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/configuration/ks/ks-server-7-uefi.cfg
  7. genisoimage生成镜像
    #!/bin/sh
    # 指定需要制作的目标镜像的生成路径
    current_dir=`pwd`
    package_name=$current_dir/centos.iso 
    # 指定iso镜像解压并修改后的所在路径
    cdrom=$current_dir/iso_dir
    cd $cdrom
    rm -rf repodata/
    createrepo -g comps.xml ./
    genisoimage -joliet-long -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 -V "CentOS 7 x86_64" -o $package_name $cdrom
    
0条评论
0 / 1000