引用:① https://www.jianshu.com/p/added2e6faf6
②https://blog.csdn.net/liang_operations/article/details/80610123
kickstart
一、PXE解释与工作流程图:
1.什么是pxe
PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的技术,
工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,
并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,
再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)
下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件设置,
从而引导预先安装在服务器中的终端操作系统。
2.流程图
原理介绍:
1.Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件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 Server发送针对本机的配置信息文件(在TFTP服务的pxelinux.cfg目录下,这是系统菜单文件,格式和isolinux.cfg格式一样,功能也是类似),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
5.Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client
6.Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统
7.Client启动Linux内核
8.Client下载安装源文件,读取自动化安装脚本
二、安装步骤:
1.需要安装的服务
①.DHCP
②.TFTP
③.HTTP
④.PXE
⑤.syslinux
2.系统环境
2.1系统版本 VirtualBox +Centos7.2
[root@liang ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
2.2系统ip
[root@liang ~]# ifconfig
enp0s3:192.168.8.101 mask 255.255.255.0 #此为实际网络ip为防止冲突,增加10.0.0.X网段作为pxe的dhcp服务器的网络
enp0s3:1 10.0.0.1 mask 255.255.255.0
2.3系统内核版本
[root@liang ~]# uname -r
3.10.0-327.el7.x86_64
3.关闭防火墙与selinux
[root@liang ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@liang ~]# setenforce 0
4.安装服务,我这里使用的是阿里云的ym源。
[root@liang ~]# yum install dhcp httpd tftp-server httpd xinetd -y
5.创建目录并挂载镜像
[root@liang ~]# mkdir /var/www/html/Centos-7.2
[root@liang ~]# mount /dev/cdrom /var/www/html/Centos-7.2/
mount: /dev/sr0 is write-protected, mounting read-only
mount -t cifs -o username=Everyone //192.168.8.78/shareos /var/www/html/Centos-7.2/
找不到pxelinux.0,则安装
yum install syslinux -ycentos7安装桌面环境
# yum -y groups install "GNOME Desktop"
安装完成后,用此命令启动
# startx
6.启动http服务
[root@liang ~]# rm -f /etc/httpd/conf.d/welcome.conf
[root@liang ~]# systemctl start httpd
[root@liang ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
7.配置tftp
[root@liang ~]# 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
disable = no(yes改为no)
per_source = 11
cps = 100 2
flags = IPv4
}
7.1启动tftp服务
[root@liang ~]# systemctl enable xinetd
[root@liang ~]# systemctl start xinetd
7.2将pxelinux.0 复制到tftpboot/目录下
[root@liang ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
7.3创建pxelinux.cfg目录,将镜像文件拷到/var/lib/tftpboot/目录下
[root@liang ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@liang ~]# cp /var/www/html/Centos-7.2/isolinux/* /var/lib/tftpboot/
[root@liang ~]# cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
8.配置dhcp
[root@liang ~]# cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example >>/etc/dhcp/dhcpd.conf
[root@liang ~]# vim /etc/dhcp/dhcpd.conf
45 subnet 10.0.0.0 netmask 255.255.255.0{
46 range dynamic-bootp 10.0.0.100 10.0.0.200;
47 option subnet-mask 255.255.255.0;
48 next-server 10.0.0.134;
49 filename "pxelinux.0";
50 }
8.1启动dhcp服务
[root@liang ~]# systemctl start dhcpd
[root@liang ~]# systemctl enable dhcpd
9.配置ks.cfg文件
[root@liang ~]# yum install system-config-kickstart -y
9.1安装完成后,在桌面环境下,
#startx
如果没有图形界面,请参照此链接安装GNOME-Desktop https://www.cnblogs.com/u-drive/p/9832356.html
执行system-config-kickstart开始配置ks.cfg文件
# system-config-kickstart #设置自动安装的选择
4.下面对图形界面进行设置
这个ks.cfg就是你在图形界面上设置参数的配置文件。通过这个配置我们可以实现自动化安装。
5.vim ks.cfg #编辑文件,指定安装过程中需要安装的软件,在文件末尾添加
%packages
@base
%end
ksvalidator ks.cfg #检测语法是否正确
通过网址验证:
10.修改default文件(修改大概第64行,并删除大概第69行的“menu default”)
[root@liang ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
61 label linux
62 menu label ^Install CentOS 7
63 kernel vmlinuz
64 append initrd=initrd.img repo=http://10.0.0.134/Centos-7.2/ ks=http://10.0.0.134/ks2.cfg
65
66 label check
67 menu label Test this ^media & install CentOS 7
68 kernel vmlinuz
69 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
11.重启服务
[root@liang ~]# systemctl restart xinetd dhcpd httpd
↓↓ 以下未验证。
6.转到真机
vim /mnt/xxx.sh
chmod /mnt/xxx.sh #加可执行权限
/mnt/xxx.sh 虚拟机名字 #执行脚本,生成虚拟机
#!/bin/bash
virt-install \
--name $1 \
--ram 1024 \
--cpus 1 \
--disk /var/lib/libvirt/images/$1.qcow2,bus=virtio,size=9 \
--network bridge=br0,model=virtio \
--location http://172.25.254.36/rhel7.0 \ #真机
--extra-args ks=http://172.25.254.100/ks.cfg & #虚拟机下ks.cfg的地址
[root@foundation36 Desktop]# bash kickstart.sh vm
直接在运行kickstart
只有两个区别
1.在配置图形界面的第二项http地址中,HTTP Server应为http://你主机的ip,HTTP Directory为你主机挂载镜像的位置。(我的是/var/www/html/rhel7.0,所以填写rhel7.0)
2.vim kickstart.sh脚本,修改extra-args ks=http://172.25.254.100/ks.cfg & 的ip为你主机的ip,并且要保证你的ks.cfg脚本要在/var/www/html目录下。
如果提示
[root@foundation36 Desktop]# ERROR Error validating install location: Could not find an installable distribution at 'http://172.25.254.36/rhel7.0'
那么说明kickstart没有找到http://172.25.254.36/rhel7.0这个目录或者你的镜像没有挂载到/var/www/html/rhel7.0下面,那么就先新建一个/var/www/html/rhel7.0目录,然后将对应的镜像挂载到/var/www/html/rhel7.0。
[root@foundation36 Desktop]# mount /home/kiosk/Desktop/software/rhel-server-7.0-x86_64-dvd.iso /var/www/html/rhel7.0/ #挂载
mount: /dev/loop1 is write-protected, mounting read-only
[root@foundation36 Desktop]# bash kickstart.sh sd #执行脚本
[root@foundation36 Desktop]#
Starting install... #开始安装
...