Centos7使用pxe安装KVM虚拟机

        Centos7使用pxe安装KVM虚拟机

一、安装服务所需的软件

[root@localhost ~]yum install nginx  dhcp  vsftpd  syslinux -y
[root@localhost ~]mount -p loop -t iso9660 images/CentOS-7-x86_64-DVD-1804.iso /mnt  #提前上传镜像
[root@localhost ~]systemctl start  vsftpd   
[root@localhost ~]systemctl enable vsftpd    

二、安装DHCP服务器

[root@localhost ~]cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/  #复制示例配置文件
[root@localhost ~]cd /etc/dhcp/
[root@localhost dhcp]#rm -f dhcpd.conf
[root@localhost dhcp]#mv dhcpd.conf.example dhcpd.conf
[root@localhost dhcp]# egrep -v "^#|^$" dhcpd.conf 
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 172.16.150.0 netmask 255.255.255.0 {  #网段
  range 172.16.150.230 172.16.150.232 ; #地址池
  option routers 172.16.150.254;  #网关
  option domain-name-servers 172.16.160.20,172.16.160.21; #dns服务器
}
[root@localhost ~]cp /usr/lib/systemd/system/dhcpd.service /etc/systemd/system/
[root@localhost ~]vi /etc/systemd/system/dhcpd.service
ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid <your_interface_name(s)>
[root@localhost ~]systemctl --system daemon-reload
[root@localhost ~]systemctl restart dhcpd.service #启动服务 

三、创建tftp相关目录和拷贝内核镜像及文件

[root@localhost ~]mkdir /var/tftp
[root@localhost ~]cp /mnt/images/pxeboot/initrd.img /var/tftp/  
[root@localhost ~]cp /mnt/images/pxeboot/vmlinuz    /var/tftp/
[root@localhost ~]rpm -ql syslinux | grep "pxelinux.0"  
[root@localhost ~]cp /usr/share/syslinux/pxelinux.0 /var/tftp/
[root@localhost ~]mkdir /var/tftp/pxelinux.cfg/ 

四、创建菜单配置文件

[root@localhost ~]# vim /var/tftp/pxelinux.cfg/default
default linux
prompt 1
timeout 60
display boot.msg
label linux
  kernel vmlinuz
  append initrd=initrd.img text ks=http://172.16.150.32/ks.cfg net.ifnames=0 biosdevname=0  ksdevice=eth0  
#172.16.150.32是http服务器地址,我们使用http的方式下载ks.cfg及软件包,因为7的网卡名称不在是eth0,1,2而改成enoxx的格式,新的方式不但长,而且难记。ksdevice=eth0表示所有网卡名称从eth0开始,如果有多快,依次增加

五、配置nginx下载、安装配置

[root@localhost ~]#cd /usr/share/nginx/html/
[root@localhost html]#mkdir Centos7_4 #创建软件包下载目录
[root@localhost html]#umont /mnt
[root@localhost html]#mount -p loop -t iso9660 /root/images/CentOS-7-x86_64-DVD-1804.iso /usr/share/nginx/html/Centos7_4/ 
  
[root@localhost html]# vim /usr/share/nginx/html/ks.cfg  #具体自定义,文章结尾有份示例文件,也可以使用system-config-kickstart命令生成,命令可以通过yum安装
.....
url --url="http://172.16.150.32/Centos7_4"  #在ks文件中指定下载的地址
.....

#如果想打开nginx目录浏览功能,可以添加如下配置: [root@localhost html]# cat /etc/nginx/nginx.conf ..... server { autoindex on; autoindex_exact_size on; autoindex_localtime on; .......
#如果想在线浏览ks文件,则添加如下配置 vim /etc/nginx/mime.types text/plain cfg; #添加该行

六、注意事项

1.安装的系统内容不得小于2G,否则报如下错误:
    dracut-initqueue[552]: /sbin/dmsquash-live-root: line 273: printf: write error: No space left on device
2.boot分区不得小于200M,否则或导致kickstart进程暂停,需要手动确认才会继续

 #示例文件

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $6$pTG9CQO9$MmB9KurbyQtyAeqJm7Hrbh.kl1IGUhQK35Ce027/lGYq6TkNYHyUd6WYqluhUVnEQoPzruTbqm7HFO8CyZB0r/  #root密码,加密方法参考http://clavinli.github.io/2014/11/14/linux-command-hash-root-password-in-kickstart/
user --groups=admin --name=admin
user --groups=log --name=log

url --url="http://172.16.150.32/Centos7_4/"  #软件包下载地址
# System language
lang zh_CN
# Firewall configuration
firewall --enabled --http --ftp --ssh --port=8080:tcp,20881:tcp,8000:tcp  #可以禁用,或者开启需要放行的端口或者服务
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Network information
network  --bootproto=dhcp --device=eth0 --gateway=172.16.150.254 --onboot=on
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# 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=300 #不得少于200M
part swap --fstype="swap" --size=3000
part / --fstype="xfs" --grow --size=1  #1代表使用所有的剩余磁盘空间
%packages #这段表示你要安装的软件包,@表示选择的包组,最前面使用横杠表示取反,即不选择的包或包组。@base和@core两个包组总是被默认选择,可以不在%packages中指定它们
@Base 
curl 
lrzsz 
%end 
%post --interpreter /bin/bash 
/bin/curl http://172.16.150.32/System_initialize.sh -o /root/System_initialize.sh  #安装后需要做的操作,也可以是命令或者打包到脚本中执行
/bin/bash /root/System_initialize.sh
%end
ks.cfg文件
virt-install --virt-type=kvm   --name "zest_machine"  --location=http://172.16.150.32/Centos7_4/  --memory=4096  --vcpus=1 --disk path=/var/lib/libvirt/images/zest_machine.img,size=60  --network bridge=br0  --os-type linux --os-variant rhel7 --extra-args="ks=http://172.16.150.32/ks.cfg --autostart  console=tty0 " --force &
kvm安装脚本
原文地址:https://www.cnblogs.com/panwenbin-logs/p/9237630.html