使用应答文件自动安装linux系统

admin 2022年12月09日 688次浏览

1、系统安装过程

Linux安装过程

  • 加载 boot loader
  • 加载启动安装菜单
  • 加载内核和 initrd 文件
  • 加载根系统
  • 运行 anaconda 的安装向导

1.1、Linux安装光盘的安装相关文件

在系统光盘的 isolinux 目录下有和安装相关的文件

  • boot.cat:相当于 grub 的第一阶段
  • isolinux.bin:光盘引导程序,在 mkisofs 的选项中需要明确给出文件路径
  • memtest:内存检测程序
  • vmlinuz:内核镜像
  • initrd.img:ramfs 文件
  • isolinux.cfg:启动菜单的配置的文件,当光盘启动后,会自动去找 isolinux.cfg 文件
  • splash.png:光盘启动菜单界面的背景图
  • vesamenu.c32:光盘启动后的启动菜单图形界面,其提供纯文本的菜单

1.2、安装菜单的内核参数

通过在 /isolinux/isolinux.cfg 中设置不同参数,从而实现不同的安装过程

1.2.1、isolinux.cfg 文件关键参数

  • 加载内核:/isolinux/vmlinuz
  • 向内核传递参数: append initrd=initrd.img 参数设置

1.2.2、指定内核参数的方法

  • 在启动菜单界面,选中一种安装方法,然后按【tab】键,在后面增加参数
  • 在启动菜单界面,任意选中一项安装方法,按【esc】键,然后在 boot: 后面输入 linux 参数设置

1.2.3、常用内核参数

  • text:默认启动 GUI 界面,可以指定为文本方式安装界面

  • rescue:进入救援模式

  • inst.repo=path

    centos6

    光驱:repo=cdrom :device

    硬盘:repo=hd:device/path

    HTTP Server:repo=http://host/path

    HTTPS Server:repo=https://host/path

    FTP Server:repo=ftp://username:password@host/path

    NFS Server:repo=nfs:server:/path

    NFS 上的 ISO:repo=nfsiso:server:/path

    centos7:

    光驱:inst.repo=cdrom

    硬盘:inst.repo=hd:device/path

    HTTP Server:inst.repo=http://host/path

    HTTPS Server:inst.repo=https://host/path

    FTP Server:inst.repo=ftp://username:password@host/path

    NFS Server:inst.repo=nfs:[options:]server:/path

  • ks=path:指定自动化安装应答文件路径,如:initrd=initrd.img inst.ks=http://192.168.1.10/ksdir/centos7.cfg

  • ip=:指定IP地址信息

    ip=method,method 可以为 dhcp

    ip=interface:method 指定特定接口

    ip=ip::gateway:netmask:hostname:interface:none 静态ip

1.2.4、anaconda安装向导

anaconda 是 Linux系统安装程序,可以提供图形窗口(GUI)和基于图形库 cures 的文本窗口(tui)

  • anaconda的工作过程

    • 安装过程使用的语言
    • 键盘类型
    • 时区和时间
    • 安装源文件路径
    • 选定要安装的程序包
    • 安装目标存储设备及分区设置
    • KDUMP功能
    • 设定主机名和配置网络接口
    • 安全策略
    • 管理员密码
    • 创建一个普通用户
  • anaconda的配置方式

    • 交互配置方式

    • 通过读取配置文件自动完成配置,并配置内核参数:ks=/path 指定 kickstart 文件的位置

      光驱:ks=cdrom:/PATH/TO/KICKSTART_FILE

      硬盘:ks=hd:device:/PATH//KICKSTART_FILE

      HTTP Server:ks=http://host:port/PATH/TO/KICKSTART_FILE

      HTTPS Server:ks=https://host:port/PATH/TO/KICKSTART_FILE

      FTP Server:ks=ftp://host:port/PATH/TO/KICKSTART_FILE

      NFS Server:ks=nfs:host:/PATH/TO/KICKSTART_FILE

2、自动应答文件

2.1、kickstart文件格式说明

kickstart 文件主要包括三部分:命令段、程序包段、脚本段

2.1.1、命令段

指明各种安装前配置,常见命令包括:

  • keyboard:设定键盘类型
  • lang:语言类型
  • zerombr:清除mbr
  • clearpart:清除分区
  • part:创建分区
  • rootpwd:设定root密码
  • timezone:时区
  • text:文本安装界面
  • network:配置网络设置
  • firewall:配置防火墙设置
  • selinux:配置selinux设置
  • reboot:安装完毕自动重启
  • user:安装完成后,创建新用户
  • url:指定安装源

2.1.2、程序包段

指定要安装的程序、程序包或不安装的程序包等

  • %packages:程序包段开始标识

  • 安装包组

    @^environment group:指定环境包组,如:@^minimal-environment

    @group_name

  • package:需要安装的包

  • -package:不需要安装的包

  • %end:程序包段结束标识

2.1.2、脚本段

%pre:安装前脚本

%post:安装后脚本

2.1.3、使用配置工具创建kickstart文件

  • 安装system-config-kickstart

    [root@test-host ~]# yum install system-config-kickstart -y
    
  • 使用system-config-kickstart

    system-config-kickstart 使用图形界面配置,如果系统不带桌面环境,可以配置环境变量 DISPLAY 将显示输出重定向到本地控制电脑上

    [root@test-host ~]# export DISPLAY=192.168.137.1:0.0
    [root@test-host ~]# system-config-kickstart
    

    说明:在选择安装软件包时,如果没有可选项,需要将 /etc/yum.repos.d/CentOS-Base.repo 中的 [base] 标签修改为 [development]

2.2、kickstart示例文件

2.2.1、centos7

[root@test-host ~]# cat /var/www/html/ks_centos7.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 123456
# System language
lang zh_CN
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Firewall configuration
firewall --disabled
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use CDROM installation media
cdrom
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow

%post --interpreter=/usr/bin/bash
useradd user
echo 123456 | passwd --stdin user
sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
%end

%packages
@^infrastructure-server-environment
@base
@core

%end

2.2.2、rocky Linux 9

[root@test-host ksfile]# cat rocky9.cfg
# Generated by Anaconda 34.25.1.14
# Generated by pykickstart v3.32
#version=RHEL9
# Use text mode install
text
# Reboot after installation
reboot
# 本地安装、网络安装二选一
# 本地安装
cdrom
# 网络安装
url --url="http://192.168.137.100/rocky-9.1"

%post --interpreter=/usr/bin/bash
useradd user01
echo 123456 | passwd --stdin user01
sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
%end

%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN

# Firewall configuration
firewall --disabled

%packages
@^minimal-environment
%end

# SELinux configuration
selinux --disabled

# Run the Setup Agent on first boot
firstboot --enable
# Do not configure the X Window System
skipx

# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow

# System timezone
timezone Asia/Shanghai

# Root password
rootpw --plaintext 123456

2.3、使用应答文件安装系统

测试应答文件部署在 192.168.137.100 web服务器上

  • 使用光盘引导系统开机

  • 在引导界面按【esc】,进入自定义启动界面

  • boot: 后面加入 kickstart 文件路径, 使新主机使用指定 kickstart 文件自动部署系统

    # centos7
    linux ks=http://192.168.137.100/ks_centos7.cfg
    
    # rocky Linux 9(RHEL9)
    linux ks=http://192.168.137.100/rocky9.cfg
    
  • 系统进入自动安装阶段