制作kickstart文件实现半自动化及PXE自动化安装Linux系统
实验一:使用 kickstart 半自动化安装CentOS系统
实验选择用kickstart安装centos7
1.实验前准备:
从阿里云镜像站下载CentOS-7-x86_64-NetInstall-2003镜像包:https://mirrors.aliyun.com/centos/7/isos/x86_64/
VMware网络模式选择NAT模式并进行以下设置
2.在centos7上关闭防火墙和SELINUX,安装并启动httpd
#把/etc/selinux/config中SELINUX设为disabled并重启
[root@centos7 ~]#cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
#重启后
[root@centos7 ~]#systemctl stop firewalld
[root@centos7 ~]#systemctl start httpd
#搭建安装系统所需的yum仓库
[root@centos7 ~]#mkdir -p /var/www/html/centos/7/os/x86_64/
[root@centos7 ~]#mount /dev/sr1 /var/www/html/centos/7/os/x86_64/
mount: /dev/sr1 is write-protected, mounting read-only
把作为httpd服务的centos7的yum源配置文件标题改为development,否则用图形工具制作kickstart文件时加载不出相关包
3.制作kickstart文件
在现有的centos7上安装图形工具:system-config-kickstart
[root@centos7 ~]#yum -y install system-config-kickstart
借助Xmanager工具,显示图形,打开Xmanager中的Xmanager-Passive
鼠标指向桌面右下角的Xmanager-Passive查看编号,如下0.0
执行以下命令
#10.0.0.1为虚拟机ip地址
[root@centos7 ~]#export DISPLAY=10.0.0.1:0.0
[root@centos7 ~]#system-config-kickstart
/usr/share/system-config-kickstart/kickstartGui.py:104: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page size is deprecated
xml = gtk.glade.XML ("/usr/share/system-config-kickstart/system-config-kickstart.glade", domain="system-config-kickstart")
Loaded plugins: fastestmirror
接下来在弹出的图形界面配置kickstart文件
后两项为安装前后脚本,根据自身需求配置,可以省略
把root家目录下生成的kickstart文件放到httpd服务器上
[root@centos7 ~]#mkdir /var/www/html/ksdir
[root@centos7 ~]#cp ks7.cfg /var/www/html/ksdir/
#centos7 kickstart文件内容如下
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$J5tDeo1a$DwwEJdVpS7xyZm8npCng4/
# System language
lang en_US
# 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
# Network information
network --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://10.0.0.7/centos/7/os/x86_64/"
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="xfs" --size=50000
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="xfs" --size=50000
%post
yum -y install vim tree wget
%end
4.准备安装系统
创建一个centos7的虚拟机,并把光驱指定为下载好的CentOS-7-x86_64-NetInstall-2003包,内存设为2G,以免内存不足导致安装失败
开机在菜单界面按Esc键进入boot,添加参数,准备安装系统
参数为:linux ks=http://10.0.0.7/ksdir/ks7.cfg ip=dhcp 然后键入回车,系统将自动安装
静静等待,系统将自动安装完成。到此,半自动化安装Linux系统就实现了。
实验二:实现pxe安装双系统centos6、centos7
1.实验准备
关闭虚拟机的dhcp功能
本实验需要提前搭建3个服务器,再次选择在centos7上搭建,分别是HTTP、TFTP、DHCP服务器,需要提前关闭SELINUX和防火墙,方法同实验一。
准备好centos6和centos7的完整镜像文件
2.安装相关服务包并启动
[root@centos7 ~]#yum -y install tftp-server dhcp syslinux
[root@centos7 ~]#systemctl start httpd tftp
dhcp服务启动需要配置相关文件
[root@centos7 ~]#vim /etc/dhcp/dhcpd.conf
option domain-name " ";
default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.50 10.0.0.200;
option routers 10.0.0.2;
next-server 10.0.0.7; #TFTP服务器地址
filename "pxelinux.0"; #bootloader启动文件的名称
}
[root@centos7 ~]#systemctl start dhcpd
3.准备yum源和相关目录
[root@centos7 ~]#mkdir -p /var/www/html/centos/{6,7}/os/x86_64
[root@centos7 ~]#mount /dev/sr0 /var/www/html/centos/6/os/x86_64/
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos7 ~]#mount /dev/sr1 /var/www/html/centos/7/os/x86_64/
mount: /dev/sr1 is write-protected, mounting read-only
4.准备kickstart文件
centos6和centos7的kickstart文件参考实验一用工具生成
centos7的kickstart文件同实验一
centos6的kickstart文件内容如下
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="http://10.0.0.7/centos/6/os/x86_64/"
# Root password
rootpw --iscrypted $1$g2QA8hUE$A0cTZI0ddvWDRs9vYgGx81
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Network information
network --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="ext4" --size=50000
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="ext4" --size=50000
%post
yum -y install vim tree wget
%end
把生成的kick start文件放到centos7的http服务器上
[root@centos ~]#scp ks6.cfg 10.0.0.7:/var/www/html/ksdir/
5.准备PXE启动相关文件
[root@centos7 ~]#cd /var/lib/tftpboot
[root@centos7 tftpboot]#mkdir -p /var/lib/tftpboot/{centos{6,7},pxelinux.cfg}
[root@centos7 tftpboot]#cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} centos6
[root@centos7 tftpboot]#cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} centos7
#pxelinux.0相当于bootloader,menu.32为启动菜单背景图,两个文件均在下载syslinux时生成
[root@centos7 tftpboot]#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
#复制光盘镜像中的isolinux.cfg菜单文件到pxelinux.cfg下,并改名为default,稍作修改,制作成自己想要的菜单
[root@centos7 tftpboot]#cp /var/www/html/centos/7/os/x86_64/isolinux/isolinux.cfg pxelinux.cfg/default
#最终所需目录文件如下
[root@centos7 tftpboot]#tree
.
├── centos6
│ ├── initrd.img
│ └── vmlinuz
├── centos7
│ ├── initrd.img
│ └── vmlinuz
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
└── default
3 directories, 7 files
准备启动菜单,修改default中内容,菜单内容如下,包括centos6、7版本的安装及救援模式和本地启动选项,默认设为本地启动防止把已经装好的系统重装
default menu.c32
timeout 600
menu title PXE INSTALL SYSTEM
label linux
menu label Install CentOS ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.0.0.7/ksdir/ks6.cfg
label linux
menu label Install CentOS ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://10.0.0.7/ksdir/ks7.cfg
label rescue
menu label Rescue a CentOS6 system
kernel centos6/vmlinuz
append initrd=centos6/initrd.img
inst.repo=http://10.0.0.7/centos/6/os/x86_64/ rescue
label rescue
menu label Rescue a CentOS7 system
kernel centos7/vmlinuz
append initrd=centos7/initrd.img
inst.repo=http://10.0.0.7/centos/7/os/x86_64/ rescue
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
6.准备安装
创建centos6和centos7新的虚拟机,内存分配至少2G以免安装时内存不足,准备安装
centos6安装
成功获取ip后进入菜单,选择系统,等待安装完成
centos7安装
成功获取ip后进入菜单,选择系统,等待安装完成
最终,成功实现了PXE自动化安装各个版本Linux系统。