Centos7使用pxe安装KVM虚拟机

1 背景

近期公司在进行迁移本地机房到外地的事宜,一方面是数据的迁移,另一方面便是数据迁移后需要对服务器进行重置装机,由于涉及到的服务器较多,单靠人为靠手动通过光盘或者U盘安装系统的话,显然在人力和物力上都很浪费,因此便使用pxe进行无人值守安装操作系统。

2 关于PXE

Preboot Execution Environment就是预启动执行环境;简称PXE;传统安装操作系统的方法是CDROM或U盘引导,而PXE技术解决的是从网络引导安装系统,当然PXE技术不是什么新技术了,是Intel和Microsoft在1998年定义的更大PC98规范的一部分。

2.1 pxe引导相关角色说明

  • PXE作用:引导装机
  • Kickstart作用:将安装系统的过程中通过ks.cfg文件中的配置信息,实现自动安装配置达到无人值守自动安装
  • TFTP: 支持PXE 引导的网卡中从tftp服务器端下载pxelinux.0文件、vmlinuz、initrd.img等内核引导文件
  • DHCP: PXE引导过程中获首先获取IP地址,并提供pxelinux.0文件位置
  • HTTP: 用于下载ks.cfg以及系统镜像文件

通用的无人值守做法如下:
有一台支持pxe协议的裸机网卡,开机后从dhcp服务器获取ip和pxelinux.0文件并从tftp服务器上加载相关内核引导文件;同时下载 kickstart的cfg配置文件,指定系统镜像文件位置加载安装,从而实现无人值守安装。
在这里插入图片描述

2.2 满足Kickstart安装系统的需求

  1. 网卡必须支持PXE引导;
  2. 客户端需要有与kickstart 通信的IP地址,这里一般都是通过DHCP自动分配IP地址来实现;
  3. 提供引导所需的文件,内核文件,内核镜像文件等,此时一般通过tftp-server等实现;
  4. kickstart文件,它主要实现自动化安装的过程,比如配置主机名、添加用户、安装系统后相关操作;
  5. 提供安装源,一般就是NFS/ftp/httpd等;

pxe工作示意图:
在这里插入图片描述
在这里插入图片描述

  1. Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将pxe环境下的Boot loader文件pxelinux.0的位置信息传送给Client。
  2. Client向PXE Server上的TFTP请求pxelinux.0,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0。
  3. Client执行接收到的pxelinux.0文件。
  4. Client向TFTP请求pxelinux.cfg文件(其实它是目录,里面放置的是是启动菜单,即grub的配置文件),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
  5. Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client。
  6. Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统。
  7. Client加载Linux内核(启动参数已经在4中的配置文件中设置好了)。
  8. Client通过nfs/ftp/http下载系统安装文件进行安装。如果在4中的配置文件指定了kickstart路径,则会根据此文件自动应答安装系统。

3 实战演练

说明:本案例以单网卡机器为例,且已经配置好了yum源,挂载好了镜像

mount /root/CentOS-7-x86_64-DVD-1804.iso /media/

3.1 环境准备

系统版本selinuxfirewalld需要部署的软件系统镜像
CentOS Linux release 7.5.1804 (Core)Disableddisableddhcp、tftp-server、http、xinetd、syslinuxCentOS-7-x86_64-DVD-1804.iso

在这里插入图片描述

3.2 部署软件

yum -y install dhcp tftp-server xinetd  syslinux httpd  #具体的功能无需赘述,详见2 关于PXE

3.3 配置DHCP

备份和拷贝配置文件

cd /etc/dhcp
mv dhcpd.conf  dhcpd.conf .bak
cp  /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example ./dhcpd.conf

编辑配置文件
cat dhcpd.conf

default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 192.168.17.0 netmask 255.255.255.0 {
option routers 192.168.17.254;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.17.115;
option time-offset -18000; # Eastern Standard Time
range dynamic-bootp 192.168.17.80 192.168.17.185;
next-server 192.168.17.115;
filename "pxelinux.0"; 
}
systemctl start dhcpd.service  #启动
systemctl enable dhcpd.servic  #开机自启
ss -tunl | grep "67"           #监听端口

在这里插入图片描述

3.4 配置tftp

1)修改tftp配置文件

cat /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
        disable                 = no      #yes改为no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

在这里插入图片描述

2)拷贝相关文件
cp -rf /usr/share/syslinux/* /var/lib/tftpboot

说明:syslinux是一个功能强大的引导加载程序,而且兼容各种介质。更加确切地说:SYSLINUX是一个小型的Linux操作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。

mkdir /var/lib/tftpboot/centos7
cp /media/isolinux/vmlinuz /var/lib/tftpboot/centos7
cp /media/isolinux/initrd.img /var/lib/tftpboot/centos7
mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/default
chmod 755 /var/lib/tftpboot/pxelinux.cfg/default
3)编写default文件
cd /var/lib/tftpboot/pxelinux.cfg/
cp /media/isolinux/isolinux.cfg ./default  #输入y覆盖

vim default #主要修改的地方如下

...
timeout 30  #倒计时3秒开始执行Install CentOS7

label linux
  menu label ^Install CentOS 7 by wangkx   #可以自定义
  menu default                             #默认安装
  kernel centos7/vmlinuz                   #vmlinuz的文件路径,相对于tftp的根路径而言
  append initrd=centos7/initrd.img  ks=http://192.168.17.115:8088/ks.cfg ksdevice=eth0 quiet   #initrd.img的文件路径,相对于tftp的根路径而言;ks文件的地址;ks绑定的本机网卡;quiet表示静默安装

完整版文件如下
cat default

default vesamenu.c32
timeout 30

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label linux
  menu label ^Install CentOS 7 by wangkx
  menu default
  kernel centos7/vmlinuz
#  kernel vmlinuz
#  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
  append initrd=centos7/initrd.img  ks=http://192.168.17.115:8088/ks.cfg ksdevice=eth0 quiet

label check
  menu label Test this ^media & install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
        Try this option out if you're having trouble installing
        CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
        If the system will not boot, this lets you access files
        and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
        If your system is having issues, a problem with your
        system's memory may be the cause. Use this utility to
        see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end
systemctl status tftp.socket    #重启tftp服务

3.4 配置httpd

1)挂在系统镜像,也可以直接拷贝/media下面的文件
mkdir /var/www/html/CentOS7
mount /root/CentOS-7-x86_64-DVD-1804.iso /var/www/html/CentOS7
2)编辑ks文件
cp /root/anaconda-ks.cfg /var/www/html/ks.cfg

vim ks.cfg #主要修改点如下

#cdrom  #注释点cdrom,换成如下
install
url --url=http://192.168.17.115:8088/CentOS7
ignoredisk --only-use=vda  #如果是安装`虚拟机,则改为vda`;如果是给`物理机安装,则改为sda`
network  --bootproto=dhcp --device=em1 --onboot=off --ipv6=auto --no-activate   #`--device`必须和本机的网卡名一致
bootloader --location=mbr --boot-drive=vda   #如果是安装`虚拟机,则改为vda`;如果是给`物理机安装,则改为sda`
clearpart --all --initlabel --drives=vda    #如果是安装`虚拟机,则改为vda`;如果是给`物理机安装,则改为sda`
#############################添加的分区信息#############################
part /boot --fstype="xfs" --ondisk=vda --size=2048      #boot为2G
part biosboot --fstype="biosboot" --ondisk=vda --size=1
part / --fstype="xfs" --ondisk=vda --size=102400        #/为100G
part /u01 --fstype="xfs" --ondisk=vda --size=1 --grow   #/u01挂载分区
#############################添加的初始化信息#############################
%end
selinux --disabled    #禁用selinux 
firewall --disabled   #禁用firewall 
reboot				  #重启机器 
%post
useradd isi -b /u01   #创建用户
echo isi%2020 | passwd --stdin isi  #设置密码
%end

完整版配置如下

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
install
url --url=http://192.168.17.115:8088/CentOS7
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
#network  --bootproto=dhcp --device=enp2s0f0 --onboot=off --ipv6=auto --no-activate
#network  --bootproto=dhcp --device=enp2s0f1 --onboot=off --ipv6=auto
#network  --bootproto=dhcp --device=enp2s0f2 --onboot=off --ipv6=auto
#network  --bootproto=dhcp --device=enp2s0f3 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
network  --bootproto=dhcp --device=enp2s0f0 --onboot=off --ipv6=auto --no-activate
network  --bootproto=dhcp --device=enp2s0f1 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth1 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=em3 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=em4 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=enp2s0f2 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=enp2s0f3 --onboot=off --ipv6=auto
#network  --bootproto=dhcp --device=enp60s0f0 --onboot=off --ipv6=auto --no-activate
#network  --bootproto=dhcp --device=em1 --onboot=off --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$zgkHrqGl.u3F2Y.i$wTqVu6TeRzjXGszJK/w0m3s.rU27r8.0CPLoKZHVu1PaZroqOT8Km8YTqtKgkXyF15XaJarn2o4QvbDbIwQJe.
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp

# System bootloader configuration
bootloader --location=mbr --boot-drive=vda
# Partition clearing information
clearpart --all --initlabel --drives=vda 
# Disk partitioning information
#part /boot --fstype="xfs" --ondisk=sda --size=1024
part /boot --fstype="xfs" --ondisk=vda --size=2048
part biosboot --fstype="biosboot" --ondisk=vda --size=1
#part /boot/efi --fstype="efi" --ondisk=sda --size=200 --fsoptions="umask=0077,shortname=winnt"
part / --fstype="xfs" --ondisk=vda --size=102400
part /u01 --fstype="xfs" --ondisk=vda --size=1 --grow

#part /boot --fstype="xfs" --ondisk=sda --size=1024
#part / --fstype="xfs" --ondisk=sda --size=102400
#part /u01 --fstype="xfs" --ondisk=sda  --grow
#part swap --fstype="swap" --ondisk=sda --size=8065

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

%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
selinux --disabled
firewall --disabled
reboot
%post
useradd isi -b /u01
echo isi%2020 | passwd --stdin isi
%end

注意:
ks的配置文件中rootpw密码可以使用明文,也可以使用加密过的值。
比如明文密码是:123456,对应加密后的密码是:$1$password$7ZK8mMeLGXxbNPeJEFJOU0
那这个密码是怎么生成的呢,其实和生成linux操作系统的密码shadow一样,命令如下:
perl -e ‘print crypt(“123456”,q( 1 1 1password)),"\n"’
当使用特殊字符时,例如@$符时需要在前面加上\,例:@$,否则加密字符串会错误
其中123456为要给用户设置的密码, 1 1 1password字符串是自定义字符串,shadow里一般用 1 1 1后面跟8个字符这种格式。

3.5 安装虚拟机脚本

cat install-7.sh

#!/bin/bash
machine_name=test_002
vncport=56111
mkdir -p $path

virt-install \
--connect qemu:///system \
--name $machine_name \
--ram 4096 \
--vcpus=2 \
--pxe \
--disk path=/u01/vm/test/${machine_name}.img,size=200,bus=virtio,cache=none \
--network bridge=br1,model=virtio \
--os-variant=rhel6 \
--accelerate \
--vnc --vncport=$vncport \
--vnclisten=192.168.16.4

3.6 安装虚拟机验证

1)启动脚本安装

在这里插入图片描述

2)查看监听端口是否在线

在这里插入图片描述

3)通过本地vnc连接查看安装信息

在这里插入图片描述

4)连接pxe信息

在这里插入图片描述

5)安装信息

在这里插入图片描述

6)安装界面信息

在这里插入图片描述

7)安装过程

在这里插入图片描述

至此安装结束,感谢大家的浏览,因为在这儿踩坑时间比较久,如有问题也可以私信我。

都到这儿了,更多文章,详见个人微信公众号ALL In Linux,来扫一扫吧!
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值