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安裝腳本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM