Centos7使用pxe安裝KVM虛擬機

        Centos7使用pxe安裝KVM虛擬機

1、安裝服務所需的軟件

[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    

2、安裝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 #啓動服務 

3、建立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/ 

4、建立菜單配置文件

[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開始,若是有多快,依次增長

5、配置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; #添加該行

6、注意事項html

1.安裝的系統內容不得小於2G,不然報以下錯誤:
    dracut-initqueue[552]: /sbin/dmsquash-live-root: line 273: printf: write error: No space left on device
2.boot分區不得小於200M,不然或致使kickstart進程暫停,須要手動確認纔會繼續

 #示例文件linux

#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安裝腳本
相關文章
相關標籤/搜索