PXE+KickStart自动安装linux系统附带virt-install的使用教程,


环境:Red Hat Enterprise Linux Server release 7.2 (Maipo)

所需安装软件:dhcp、tftp-server、system-config-kickstart、syslinux

firewalled、selinux:关闭

1、安装DHCP服务并配置

[root@foundation76 ~]# cat /etc/dhcp/dhcpd.conf

# option definitions common to all supported networks...
option domain-name "example.com";     #域名
option domain-name-servers 172.25.254.76;       #指定DNS服务器

default-lease-time 600;          #默认租约期
max-lease-time 7200;             #最长租约期

log-facility local7;           #日志级别

 A slightly different configuration for an internal subnet.
subnet 172.25.254.0 netmask 255.255.255.0 {     #172.25.254.0网段
  range 172.25.254.100 172.25.254.130;       #地址池
  option domain-name-servers 172.25.254.76;
  option domain-name "example.com";
  option routers 172.25.254.255;           #网关
  default-lease-time 600;
  max-lease-time 7200;
  next-server 172.25.254.76;          #tftp服务所在主机IP
  filename "pxelinux.0";            #启动文件名称
} 

[root@foundation76 ~]# systemctl start dhcpd

[root@foundation76 ~]# systemctl enable dhcpd.service

2、安装tftp-server并配置

依赖包为xinetd,tftp服务为xinetd所管理

[root@foundation76 ~]# vim /etc/xinetd.d/tftp

service tftp
{
	socket_type		= dgram
	protocol		= udp
	wait			= yes
	user			= root
	server			= /usr/sbin/in.tftpd
	server_args		= -s /var/lib/tftpboot -c  #共享目录
	disable			= no     #yes-->no启用tftp服务
	per_source		= 11
	cps			= 100 2
	flags			= IPv4
}

[root@foundation76 ~]# systemctl start xinetd

[root@foundation76 ~]# systemctl enable xinetd

3、将client端所需的启动引导文件从centos镜像中复制到tftp共享目录下

[root@foundation76 ~]# yum install httpd -y#安装http服务,并挂载centos镜像

[root@foundation76 ~]# mount /iso/CentOS-7-x86_64-DVD-1611.iso /var/www/html/centos/

[root@foundation76 ~]# cp /var/www/html/centos/isolinux/* /var/lib/tftpboot/

[root@foundation76 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg

[root@foundation76 ~]# cp /var/www/html/centos/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

[root@foundation76 ~]# yum install syslinux#获取引导文件pxelinux.0

[root@foundation76 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

[root@foundation76 ~]# systemctl start httpd

修改启动配置文件

[root@foundation76 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default

timeout 600         #默认超时时间60s,如果超过该时间用户不做任何操作,则根据默认引导项启动对应的label

label linux1         #label启动菜单项
  menu label ^Auto Install CentOS Linux 7
  menu default          #默认引导项
  kernel vmlinuz        #系统内核文件
  append initrd=initrd.img inst.ks=http://172.25.254.76/ks/ks.cfg inst.repo=http://172.25.254.76/centos quiet
#inst.ks指定自动应答文件ks.cfg;inst.repo指定镜像文件 

4、使用kickstart制作自动应答文件

[root@foundation76 ~]# yum install system-config-kickstart -y

[root@foundation76 ~]# system-config-kickstart

\

指定时区Asia/Shanghai、设置root用户密码、指定安装后重新启动

\

指定安装源http://172.25.254.76/centos

\

在MBR上安装新的引导装载程序

\

\

创建必要分区/,/boot,swap,并清除主引导记录、删除所有现存分区、初始化磁盘标签

\

添加网卡eth0

\

Authentication不需要修改,禁用firewalld

\

不安装图形界面

Package Selection项会报错,需要手动在生成的ks.cfg文件中指定

Pre-Installation Script & Post-Installation Script 为系统安装前后要指定系统运行的脚本,没有可以不填

一般可以在Post-Installation Script中提前配置YUM源或相关服务

配置完成后点击左上角的File进行保存

[root@foundation76 ~]# vim /var/www/html/ks/ks.cfg #在末尾指定需要安装的软件包

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$VDAOIANN$qaQVtk1nl1uZnqFJrJTDK/
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://172.25.254.76/centos"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=200
part swap --fstype="swap" --size=500
part / --fstype="xfs" --grow --size=1
%packages     #以%packages开头
@base           #base组包
lftp        #lftp工具
@core           #core组包
%end      #以%end结尾

[root@foundation76 ~]# ksvalidator /var/www/html/ks/ks.cfg #检查ks.cfg文件语法是否有错

至此客户机可通过PXE方式启动实现自动安装。

测试:

本次在kvm虚拟机上通过virt-install命令创建虚拟主机测试pxe+kickstart无人值守自动部署linux系统。

virt-install 基本用法:

options:

-h #查看帮助

--connect=URL #连接非默认为hypervisor,默认为qemu:///system

-n NAME,--name NAME #指定虚拟机名称

--ram #指定虚拟机内存大小(M)

--file #指定虚拟机磁盘文件

--file-size #指定虚拟机磁盘大小(G)

--vcpus #指定虚拟机虚拟CPU个数

--cdrom #设置光盘设备或镜像路径

--location #指定其他的安装源,例如:http://host/path

--extra-args #当执行从"--location"选项指定位置的客户机安装时,附加内核命令行参数到安装程序。例如:"ks=http://172.25.254.116/ks.cfg"指定自动应答文件

--pxe #使用PXE引导启动

--import #导入现有的虚拟机

--os-type #指定操作系统类型

--network #指定虚拟机使用的虚拟网络

[root@foundation76 Desktop]# cat pxe_install.sh #创建虚拟机并以pxe方式引导

#!/bin/bash
virt-install \
--name $1 \
--ram 1024 \
--file /var/lib/libvirt/images/$1.qcow2 \
--file-size 10 \
--pxe \
--network bridge=br0 





[root@foundation76 Desktop]# ./pxe_install.sh world #运行shell脚本创建world虚拟主机

\

可见PXE+kickstart自动安装linux系统成功

virt-install其他常见参数配合使用:

1、该脚本通过指定网络镜像所在位置,和ks.cfg自动应答脚本文件位置实现自动创建及安装linux系统。

#!/bin/bash
virt-install \
--name $1 \
--ram 1024 \
--file /var/lib/libvirt/images/$1.qcow2 \
--file-size 8 \
--location http://172.25.254.16/rhel7.2 \
--extra-args "ks=http://172.25.254.116/ks.cfg" &

2、该脚本指定从本地光驱引导安装linux操作系统

#!/bin/bash
virt-install \
--name $1 \
--ram 1024 \
--file /var/lib/libvirt/images/$1.qcow2 \
--file-size 8 \
--cdrom /home/kiosk/Desktop/rhel-server-7.2-x86_64-dvd.iso &  &>/dev/null

3、该脚本是在已有node.qcow2磁盘文件基础上,实现导入现有的名为node的虚拟机。

#!/bin/bash
virt-install \
--name node \
--ram 1024 \
--file /var/lib/libvirt/images/node.qcow2 \
--import &

4、该脚本可以实现从本地光驱启动安装linux操作系统,并可使用VNC连接虚拟机界面。

#!/bin/bash
virt-install \
--name $1 \
--ram 1024 \
--file /var/lib/libvirt/images/$1.qcow2 \
--file-size 8 \
--cdrom /home/kiosk/Desktop/rhel-server-7.2-x86_64-dvd.iso \
--vnc \
--vncport=6000 \
--vnclisten=172.25.254.16 &

5、该脚本可以实现reset(重置)某台已有的虚拟主机。

#!/bin/bash
virsh destroy $1
rm -fr /var/lib/libvirt/images/$1.qcow2
qemu-img create -f qcow2 -b /var/lib/libvirt/images/hello.qcow2	/var/lib/libvirt/images/$1.qcow2
virsh start $1
virt-viewer $1 &

完!!!

相关内容