自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

請雙擊關注!!!

序:

本文主要介紹自動化部署操作系統,主要藉助的工具包含(Kickstart+DHCP+NFS(http)+TFTP+PXE),至於每種工具用途及作用請自行補腦,本文主要基於實戰演練。

本實驗所用系統均為Centos 6.8

注:新部署主機必須和dhcp及tftp等服務器相通且必須與企業生產主機隔離,以免造成不必要的麻煩,具體網絡配置此文不贅述,有任何網絡問題可與我取得聯繫,後續會撰寫網絡博文!

一、部署基本環境

1.1 自動化部署流程圖

自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

本實驗中使用兩臺操作系統,一臺部署所用的所有環境,另一臺為無系統虛擬機,用來自動安裝。

1.2 部署DHCP、TFTP、PEX/KICKSTART等服務(亦可用Apache,只要能發佈目錄即可,主要用於預裝系統的主機下載ISO文件)

[root@localhost ~]# yum install dhcp* tftp* -y

注:使用yum安裝dhcp及tftp服務

1.2.1 配置tftp服務

1.2.1.1 配置tftp主配置文件

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

注:進入tftp主配置文件

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

# default: off

# description: The tftp server serves files using the trivial file transfer \\

# protocol. The tftp protocol is often used to boot diskless \\

# workstations, download configuration files to network-aware printers, \\

# and to start the installation process for some operating systems.

service tftp

{

socket_type = dgram

protocol = udp

wait = yes

user = root

server = /usr/sbin/in.tftpd

server_args = -s /var/lib/tftpboot

# disable = no #這裡修改為no

per_source = 11

cps = 100 2

flags = IPv4

}

~

注:此文件只需把disable=yes 後面的yes修改為no即可

1.2.1.2 配置TFTP目錄

要實現遠程系統安裝,必須在tftpboot目錄中指定相應的pxe內核模塊及相關參數,此兩者配合才能達到目的。

首先需要掛載本地光盤

[root@localhost ~]# mount /dev/cdrom /mnt #掛載本地光盤到mnt目錄

mount: block device /dev/sr0 is write-protected, mounting read-only #此報錯無需解決

[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root

19G 797M 17G 5% /

tmpfs 238M 0 238M 0% /dev/shm

/dev/sda1 477M 33M 419M 8% /boot

/dev/sr0 3.7G 3.7G 0 100% /mnt

[root@localhost ~]#

注:掛載本地光盤到mnt目錄

若系統是5.x,默認tftpboot目錄會自動創建到/根目錄下

若系統是6.x,默認tftpboot目錄創建在/var/lib/下,需要做軟連接到/跟目錄下

[root@localhost /]# ll /var/lib/ -d tftpboot #查看文件
ls: cannot access tftpboot: No such file or directory
drwxr-xr-x. 16 root root 4096 Aug 12 18:23 /var/lib/
[root@localhost /]# ll / -d tftpboot #查看文件
ls: cannot access tftpboot: No such file or directory
dr-xr-xr-x. 22 root root 4096 Aug 12 17:59 /
[root@localhost /]#
[root@localhost /]# ln -s /var/lib/tftpboot/ / #做軟連接到根下

注:做軟連接到根下

1.2.2 配置PEX

需要安裝syslinux來啟用PEX

說明:

syslinux是一個功能強大的引導加載程序且兼容各種介質。更加確切地說:SYSLINUX是一個小型的Linux操作系統,它的目的是簡化首次安裝Linux的時間,並建立修護或其它特殊用途的啟動盤。

[root@localhost doc]# yum install syslinux syslinux-devel -y

注:安裝syslinux

[root@localhost syslinux]# cp /usr/share/syslinux/pxelinux.0 /tftpboot/

注:拷貝pxelinux.0文件到tftpboot目錄中

[root@localhost syslinux]# cp /mnt/images/pxeboot/{vmlinuz,initrd.img} /tftpboot/

注:將iso鏡像中的initrd.img和vmlinux拷貝到tftpboot中

[root@localhost tftpboot]# cp /mnt/isolinux/*.msg /tftpboot/

注:將boot.msg拷貝到tftpboot中

[root@localhost tftpboot]# cp /mnt/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default

注:拷貝iso鏡像中的isolinux.cfg到pexlinux.cfg中並重命名

[root@localhost tftpboot]# cp /mnt/isolinux/vesamenu.c32 /var/lib/tftpboot/ 

注:將vesamenu.c32拷貝到tftpboot下


[root@localhost pxelinux.cfg]# vim default #修改default文件
[root@localhost pxelinux.cfg]# chmod 777 default #給default賦權
default vesamenu.c32
#prompt 1
timeout 10
display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.8!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
label linux
menu label ^Install or upgrade an existing system
kernel vmlinuz
append ks=nfs:192.168.8.136:/centosinstall/ks.cfg ksdevice=eth0 initrd=initrd.img
label vesa
menu label Install system with ^basic video driver
kernel vmlinuz
append initrd=initrd.img nomodeset
label rescue
menu label ^Rescue installed system
kernel vmlinuz
append initrd=initrd.img rescue
label local
menu label Boot from ^local drive
localboot 0xffff
label memtest86
menu label ^Memory test
kernel memtest
append -
~
~
自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

注:修改default文件其中192.168.8.136是kickstart服務器,nfs:192.168.8.136:/centosinstall/ks.cfg 是nfs共享Linux鏡像的目錄,也是Linux存放安裝文件的路徑,ks.cfg是kickstart的主配置文件

設置timeout 10 超時間為10s

ksdevice=eth0 代表如果服務器有多張網卡,就從eth0安裝系統

1.2.3 配置NFS

遠程安裝系統,客戶端需要下載安裝所需的軟件包,所以需要使用相關軟件把鏡像給共享出來,可以使用httpd或者nfs,此實驗使用nfs,httpd同理。

[root@localhost /]# yum install nfs-utils rpcbind -y

注:安裝nfs

[root@localhost /]# mkdir centosinstall

[root@localhost /]# echo "

/centosinstall/ *(rw,no_root_squash,no_all_squash,sync)" >> /etc/exports

[root@localhost /]# cp -rf /mnt/* /centosinstall/ #此步時間稍久,可以後臺執行

注:創建centosinstall目錄;

配置nfs共享目錄;

拷貝鏡像到centosinstall目錄下

1.2.4 配置kickstart

Kickstart可以有兩種方式來獲得,第一種是使用system-kickstart軟件包來配置,第二種是直接拷貝/root/目錄下anaconda-ks.cfg重命名為ks.cfg,並吧ks.cfg拷貝到共享目錄且賦權為777

[root@localhost /]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# cp anaconda-ks.cfg /centosinstall/ks.cfg
[root@localhost ~]# chmod 777 /centosinstall/ks.cfg

注:拷貝文件並賦權

[root@localhost centosinstall]# vim ks.cfg
# Kickstart file automatically generated by anaconda.
#version=DEVEL
install
cdrom #此處修改為text
nfs --server=192.168.8.136 --dir=/centosinstall #添加此條
key --skip #添加此條
lang en_US.UTF-8 #此處可修改為 lang zh_CN.UTF-8,也可不修改
keyboard us
network --onboot no --device eth0 --bootproto dhcp --noipv6
rootpw fengchenglangzi #輸入root密碼,刪除一下三行#開頭的密碼信息
#--iscrypted #123456$6$xt0XKz9HloO1YiVa$p9QeUKOYXBkDsNzu96AKMJOD.ZNbrEXVyFSYWtIYKH/J
#A27xX4kvhpGnRLCK8iGJjH9MHTq/Dz23Ma6mYnOGA1
firewall --service=ssh #此處修改為firewall --disabled
authconfig --enableshadow --passalgo=sha512 #此處最後一句可修改為—enablemd5,也可不用修改
selinux --enforcing #此處修改問色linux --disabled
timezone --utc Asia/Shanghai #此處要刪掉--utc,否則時間不生效
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" # #auto rhgb quiet參數設置了系統的啟動方式,去掉rhgb則在開機的過程中不顯示開機動畫,而是以文本的形式打印出系統的啟動過程,此處可不用修改

zerombr #初始化磁盤,備註在後
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
#clearpart --linux --drives=sda
#volgroup VolGroup --pesize=4096 pv.008002
#logvol / --fstype=ext4 --name=lv_root --vgname=VolGroup --grow --size=1024 --maxsiz
e=51200
#logvol swap --name=lv_swap --vgname=VolGroup --grow --size=960 --maxsize=960
#part /boot --fstype=ext4 --size=500
#part pv.008002 --grow --size=1
clearpart --all --initlabel #加入此條
# Disk partitioning information #加入此條
part / --fstype="ext4" --size=8192 #加入此條
part swap --fstype="swap" --size=1024 #加入此條
part /home --fstype="ext4" --size=2048 #加入此條
reboot
repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100 #註釋掉此行
%packages --nobase #刪除後面的--nobase,這裡的意思是不安裝基本包
@base #添加此條
@core
@chinese-support #若之前選的中文,就添加此項
%end

注:以上是配置ks.cfg文件,其中zerombr(可選)如果指定了zerombr且yes是它的唯一參數,任何磁盤上的無效分區表都將被初始化.這會毀壞有無效分區表的磁盤上的所有內容.

自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

1.2.5 配置DHCP

在配置DHCP服務器之前需要先查看本機網卡配置,因為本機網卡ip及信息都要反應到dhcp服務器上,其他設備才能正確獲取地址

[root@localhost /]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:7A:25:4F
TYPE=Ethernet
UUID=85917203-4340-4457-8b2e-ae09506ce18c
ONBOOT=yes
IPADDR=192.168.8.136
NETMASK=255.255.255.0
GATEWAY=192.168.8.1
DNS1=61.139.2.69
NM_CONTROLLED=yes
BOOTPROTO=static
[root@localhost /]#

注:如果不是靜態地址,請使用靜態地址

下面開始修改dhcp主配置文件

[root@localhost dhcp]# vim /etc/dhcp/dhcpd.conf

#

# DHCP Server Configuration file.

# see /usr/share/doc/dhcp*/dhcpd.conf.sample

# see 'man 5 dhcpd.conf'

#

ddns-update-style interim;

ignore client-updates;

filename "pxelinux.0";

next-server 192.168.8.136; #配置tftp主機地址

allow booting;

allow bootp;

subnet 192.168.8.0 netmask 255.255.255.0 {

# --- default gateway

option routers 192.168.8.1;

option subnet-mask 255.255.255.0;

range dynamic-bootp 192.168.8.100 192.168.8.200;

#host ns { #一下三行是為某個mac分配固定ip,在pxe中可以不用設置

#hardware ethernet xx:xx:xx:xx:xx:xx;

#fixed-address 192.168.8.193;}

}

注:配置dhcp主配置文件

自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

1.2.6 重啟服務並測試

[root@localhost ~]# service xinetd restart

[root@localhost ~]# service rpcbind restart

[root@localhost ~]# service nfs restart

[root@localhost ~]# service dhcpd restart

注:啟動tftp、nfs、dhcp服務

自動化批量部署Linux系統(Kickstart+DHCP+NFS(http)+TFTP+PXE)

最後把相應的新主機與dhcp服務器放到一個網絡,網絡互通即可,然後開機新主機就會自動安裝系統,本人已經測試可行!!!


分享到:


相關文章: