---
title: "Kickstart 安装 CentOS7"
date: 2019-10-29T21:00:25+08:00
lastmod: 2019-10-29T21:00:25+08:00
keywords: []
tags: ["kickstart", "centos"]
categories: ["os"]
---

## 环境
- CentOS7.6
- genisoimage 1.1.11
- CentOS-7-x86_64-Minimal-1810.iso

## 复制 iso 内容到本地磁盘
```bash
mount -o loop CentOS-7-x86_64-Minimal-1810.iso /mnt/
mkdir -p /home/iso/centos7
cd /mnt && cp -af * .* /home/iso/centos7/
```

## 创建 isolinux/ks.cfg
- mbr 启动，/home/iso/centos7/isolinux/ks.cfg 内容如下
    ```
    # Install OS instead of upgrade
    install
    # Reboot after installation
    reboot
    # 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=cn --xlayouts='cn'
    # System language
    lang zh_CN.UTF-8
    # Firewall configuration
    firewall --disabled
    # SELinux configuration
    selinux --disabled

    # Network information
    #network  --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
    #network  --hostname=localhost.localdomain

    # Root password(111111)
    rootpw --iscrypted $6$kD.hMvv5nCY8a/SM$Gnmb4zspkuyL75BP2Gj.1SGUaWBugXkd/zMFhoDndp9CSi8VP7R5JP7rfWzL4y7fy8crH3ryDT4PFkKCc7/xM.
    # System services
    services --enabled="chronyd"
    # System timezone
    timezone Asia/Shanghai --isUtc
    # Clear the Master Boot Record
    zerombr
    # System bootloader configuration
    bootloader --location=mbr --boot-drive=sda
    # Partition clearing information
    clearpart --none --initlabel
    # Disk partitioning information
    part /boot --fstype="xfs" --ondisk=sda --size=512
    part / --fstype="xfs" --ondisk=sda --grow --size=1

    %packages
    @^minimal
    @core
    chrony

    %end

    %post
    lsblk > /root/lsblk
    %end

    %addon com_redhat_kdump --disable --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
    %end
    ```
- efi 启动，/home/iso/centos7/isolinux/ks.cfg 内容如下
    ```
    # Install OS instead of upgrade
    install
    # Reboot after installation
    reboot
    # 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=cn --xlayouts='cn'
    # System language
    lang zh_CN.UTF-8
    # Firewall configuration
    firewall --disabled
    # SELinux configuration
    selinux --disabled

    # Network information
    #network  --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
    #network  --hostname=localhost.localdomain

    # Root password(111111)
    rootpw --iscrypted $6$kD.hMvv5nCY8a/SM$Gnmb4zspkuyL75BP2Gj.1SGUaWBugXkd/zMFhoDndp9CSi8VP7R5JP7rfWzL4y7fy8crH3ryDT4PFkKCc7/xM.
    # System services
    services --enabled="chronyd"
    # System timezone
    timezone Asia/Shanghai --isUtc
    # Clear the Master Boot Record
    zerombr
    # System bootloader configuration
    bootloader --location=mbr --boot-drive=sda
    # Partition clearing information
    clearpart --none --initlabel
    # Disk partitioning information
    part /boot --fstype="xfs" --ondisk=sda --size=512
    part /boot/efi --fstype="xfs" --ondisk=sda --size=512
    part / --fstype="xfs" --ondisk=sda --grow --size=1

    %packages
    @^minimal
    @core
    chrony

    %end

    %post
    %end

    %addon com_redhat_kdump --disable --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
    %end
    ```

## 修改启动项文件
- mbr 启动，只需修改 isolinux/isolinux.cfg
    - 删除 "label check" 下的 "menu default" 一行
    - 在 "label linux" 一行上方添加如下内容
        ```
        label auto
          menu label ^Auto install CentOS 7
          menu default
          kernel vmlinuz
          append initrd=initrd.img inst.stage2=hd:LABEL=CentOS7 inst.ks=cdrom:/isolinux/ks.cfg quiet
        ```
- efi 启动，只需修改 EFI/BOOT/grub.cfg
    - 修改第一行
        ```
        set default="0"
        ```
    - 在 "### BEGIN /etc/grub.d/10_linux ###" 一行下添加如下内容
        ```
        menuentry 'Auto Install CentOS 7' --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
        }
        ```

## 生成 ISO 镜像
- mbr 启动，执行如下命令
    ```bash
    genisoimage -v -R -J -T -V CentOS7 \
        -b isolinux/isolinux.bin \
        -c isolinux/boot.cat \
        -cache-inodes \
        -joliet-long \
        -no-emul-boot \
        -boot-load-size 4 \
        -boot-info-table \
        -o /home/centos7.iso \
        /home/iso/centos7
    ```
- efi 启动，执行如下命令
    ```bash
    genisoimage -v -R -J -T -V CentOS7 \
       -b images/efiboot.img \
        -c isolinux/boot.cat \
        -cache-inodes \
        -joliet-long \
        -no-emul-boot \
        -boot-load-size 4 \
        -boot-info-table \
        -o /home/centos7-efi.iso \
        /home/iso/centos7
    ```

## 参考
- [https://boke.wsfnk.com/archives/382.html](https://boke.wsfnk.com/archives/382.html)

