PXE+KickStart自動安裝linux系統附帶virt-install的使用教程,

來源:互聯網
上載者:User

PXE+KickStart自動安裝linux系統附帶virt-install的使用教程,

環境:Red Hat Enterprise Linux Server release 7.2 (Maipo)

所需安裝軟體:dhcp、tftp-server、system-config-kickstart、syslinux

firewalled、selinux:關閉

1、安裝DHCP服務並配置

[root@foundation76 ~]# cat /etc/dhcp/dhcpd.conf

# option definitions common to all supported networks...option domain-name "example.com";     #網域名稱option domain-name-servers 172.25.254.76;       #指定DNS伺服器default-lease-time 600;          #預設租約期max-lease-time 7200;             #最長租約期log-facility local7;           #記錄層級 A slightly different configuration for an internal subnet.subnet 172.25.254.0 netmask 255.255.255.0 {     #172.25.254.0網段  range 172.25.254.100 172.25.254.130;       #位址集區  option domain-name-servers 172.25.254.76;  option domain-name "example.com";  option routers 172.25.254.255;           #網關  default-lease-time 600;  max-lease-time 7200;  next-server 172.25.254.76;          #tftp服務所在主機IP  filename "pxelinux.0";            #開機檔案名稱} 

[root@foundation76 ~]# systemctl start dhcpd

[root@foundation76 ~]# systemctl enable dhcpd.service

2、安裝tftp-server並配置

依賴包為xinetd,tftp服務為xinetd所管理

[root@foundation76 ~]# vim /etc/xinetd.d/tftp

service tftp{socket_type= dgramprotocol= udpwait= yesuser= rootserver= /usr/sbin/in.tftpdserver_args= -s /var/lib/tftpboot -c  #共用目錄disable= no     #yes-->no啟用tftp服務per_source= 11cps= 100 2flags= IPv4}

[root@foundation76 ~]# systemctl start xinetd

[root@foundation76 ~]# systemctl enable xinetd

3、將client端所需的啟動引導檔案從centos鏡像中複製到tftp共用目錄下

[root@foundation76 ~]# yum install httpd -y#安裝http服務,並掛載centos鏡像

[root@foundation76 ~]# mount /iso/CentOS-7-x86_64-DVD-1611.iso /var/www/html/centos/

[root@foundation76 ~]# cp /var/www/html/centos/isolinux/* /var/lib/tftpboot/

[root@foundation76 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg

[root@foundation76 ~]# cp /var/www/html/centos/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

[root@foundation76 ~]# yum install syslinux#擷取引導檔案pxelinux.0

[root@foundation76 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

[root@foundation76 ~]# systemctl start httpd

修改啟動設定檔

[root@foundation76 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default

timeout 600         #預設逾時時間60s,如果超過該時間使用者不做任何操作,則根據預設引導項啟動對應的labellabel linux1         #label啟動功能表項目  menu label ^Auto Install CentOS Linux 7  menu default          #預設引導項  kernel vmlinuz        #系統核心檔案  append initrd=initrd.img inst.ks=http://172.25.254.76/ks/ks.cfg inst.repo=http://172.25.254.76/centos quiet#inst.ks指定自動應答檔案ks.cfg;inst.repo指定鏡像檔案 

4、使用kickstart製作自動應答檔案

[root@foundation76 ~]# yum install system-config-kickstart -y

[root@foundation76 ~]# system-config-kickstart

指定時區Asia/Shanghai、設定root使用者密碼、指定安裝後重新啟動

指定安裝源http://172.25.254.76/centos

在MBR上安裝新的引導裝載程式

建立必要分區/,/boot,swap,並清除主引導記錄、刪除所有現存分區、初始化磁碟標籤

添加網卡eth0

Authentication不需要修改,禁用firewalld

不安裝圖形介面

Package Selection項會報錯,需要手動在產生的ks.cfg檔案中指定

Pre-Installation Script & Post-Installation Script 為系統安裝前後要指定系統啟動並執行指令碼,沒有可以不填

一般可以在Post-Installation Script中提前配置YUM源或相關服務

配置完成後點擊左上方的File進行儲存

[root@foundation76 ~]# vim /var/www/html/ks/ks.cfg #在末尾指定需要安裝的軟體包

#platform=x86, AMD64, 或 Intel EM64T#version=DEVEL# Install OS instead of upgradeinstall# Keyboard layoutskeyboard 'us'# Root passwordrootpw --iscrypted $1$VDAOIANN$qaQVtk1nl1uZnqFJrJTDK/# System timezonetimezone Asia/Shanghai# Use network installationurl --url="http://172.25.254.76/centos"# System languagelang en_US# Firewall configurationfirewall --disabled# System authorization informationauth  --useshadow  --passalgo=sha512# Use graphical installgraphical# SELinux configurationselinux --disabled# Do not configure the X Window Systemskipx# Network informationnetwork  --bootproto=dhcp --device=eth0# Reboot after installationreboot# System bootloader configurationbootloader --location=mbr# Clear the Master Boot Recordzerombr# Partition clearing informationclearpart --all --initlabel# Disk partitioning informationpart /boot --fstype="xfs" --size=200part swap --fstype="swap" --size=500part / --fstype="xfs" --grow --size=1%packages     #以%packages開頭@base           #base組包lftp        #lftp工具@core           #core組包%end      #以%end結尾

[root@foundation76 ~]# ksvalidator /var/www/html/ks/ks.cfg #檢查ks.cfg檔案文法是否有錯

至此客戶機可通過PXE方式啟動實現自動安裝。

測試:

本次在kvm虛擬機器上通過virt-install命令建立虛擬機器主機測試pxe+kickstart無人值守自動部署linux系統。

virt-install 基本用法:

options:

-h #查看協助

--connect=URL #串連非預設為hypervisor,預設為qemu:///system

-n NAME,--name NAME #指定虛擬機器名稱

--ram #指定虛擬機器記憶體大小(M)

--file #指定虛擬機器磁碟檔案

--file-size #指定虛擬機器磁碟大小(G)

--vcpus #指定虛擬機器虛擬CPU個數

--cdrom #設定光碟片裝置或鏡像路徑

--location #指定其他的安裝源,例如:http://host/path

--extra-args #當執行從"--location"選項指定位置的客戶機安裝時,附加核心命令列參數到安裝程式。例如:"ks=http://172.25.254.116/ks.cfg"指定自動應答檔案

--pxe #使用PXE引導啟動

--import #匯入現有的虛擬機器

--os-type #指定作業系統類型

--network #指定虛擬機器使用的虛擬網路

[root@foundation76 Desktop]# cat pxe_install.sh #建立虛擬機器並以pxe方式引導

#!/bin/bashvirt-install \--name $1 \--ram 1024 \--file /var/lib/libvirt/images/$1.qcow2 \--file-size 10 \--pxe \--network bridge=br0 

[root@foundation76 Desktop]# ./pxe_install.sh world #運行shell指令碼建立world虛擬機器主機

可見PXE+kickstart自動安裝linux系統成功

virt-install其他常見參數配合使用:

1、該指令碼通過指定網路鏡像所在位置,和ks.cfg自動應答指令檔位置實現自動建立及安裝linux系統。

#!/bin/bashvirt-install \--name $1 \--ram 1024 \--file /var/lib/libvirt/images/$1.qcow2 \--file-size 8 \--location http://172.25.254.16/rhel7.2 \--extra-args "ks=http://172.25.254.116/ks.cfg" &

2、該指令碼指定從本地光碟機引導安裝linux作業系統

#!/bin/bashvirt-install \--name $1 \--ram 1024 \--file /var/lib/libvirt/images/$1.qcow2 \--file-size 8 \--cdrom /home/kiosk/Desktop/rhel-server-7.2-x86_64-dvd.iso &  &>/dev/null

3、該指令碼是在已有node.qcow2磁碟檔案基礎上,實現匯入現有的名為node的虛擬機器。

#!/bin/bashvirt-install \--name node \--ram 1024 \--file /var/lib/libvirt/images/node.qcow2 \--import &

4、該指令碼可以實現從本地光碟機啟動安裝linux作業系統,並可使用VNC串連虛擬機器介面。

#!/bin/bashvirt-install \--name $1 \--ram 1024 \--file /var/lib/libvirt/images/$1.qcow2 \--file-size 8 \--cdrom /home/kiosk/Desktop/rhel-server-7.2-x86_64-dvd.iso \--vnc \--vncport=6000 \--vnclisten=172.25.254.16 &

5、該指令碼可以實現reset(重設)某台已有的虛擬機器主機。

#!/bin/bashvirsh destroy $1rm -fr /var/lib/libvirt/images/$1.qcow2qemu-img create -f qcow2 -b /var/lib/libvirt/images/hello.qcow2/var/lib/libvirt/images/$1.qcow2virsh start $1virt-viewer $1 &

完!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.