無人值守安裝Linux系統
隨著互聯網技術的不斷壯大,服務器的數量也在不斷的增加,從初期的幾臺服務器發展到龐大的數據中心,單靠人工已經無法滿足在技術、業務、管理等方面的要求,那麽標準化、自動化、架構優化、過程優化等降低IT服務成本的因素越來越被人們所重視。而如何自動化批量部署安裝一個穩定的系統是實現自動化的第一步。
二、PXE簡介
PXE,就是預啟動執行環境,是一種引導啟動的方式。這種協議一般由兩部分構成,一部分是服務器端,一個是客戶端。簡單來說,我們通過這種方式可以自己創建一個“安裝源”,在安裝系統的時候只要能找到這個“源”便可以實現系統的安裝。在實現無人值守的安裝前,我們必須要搭建一些服務,來實現“安裝源”的建立,例如ftp、http、tftp、dhcp等。當一臺主機啟動時,標準輸入輸出會將PXE客戶端調入我們的內存中進行相關的操作,並提示相關的選項,在這裏我們可以進行選擇。PXE的客戶端通過網絡下載(download)啟動文件到本地運行。具體過程是,PXE客戶端通過網卡向局域網內發送ip請求,然後DHCP服務器會提供給給它一個ip地址和系統安裝所需要的文件,接下使用接收到的文件進行系統安裝。而安裝的過程又需要其他服務器提供的資源,例如:yum源,內核文件等,當主機拿到這些資源,便可以順利的安裝了。最終結果是:任意一臺主機在選著網絡啟動時會獲取DHCP服務器分發的ip,通過通過獲取到的ip地址與局域網內的TFTP服務器通信並獲取啟動文件,與FTP或者HTTP通信並獲取yum源文件及內核文件等。之後開始自動安裝,而這個過程不需要人在做任何操作。
PXE安裝優點,這種安裝系統的方式可以不受光驅,光盤以及一些外部設備的限制,還可以做到無人值守,大大減輕了運維人員的工作負荷,像在一些主機數量龐大的機房進行批量安裝,PXE將是你不二的選擇。
三、服務器搭建
1.安裝前準備
由於涉及到主機間不同基於不同協議的通信,所以為了避免不必要的麻煩,我們選擇關閉防火墻以及selinux。
2.配置靜態IP地址
修改完配置後記得重啟網卡使配置生效
3.安裝服務[root@db ~]# yum -y install dhcp tftp-server httpd syslimux xinetd
啟動服務並設為開機自啟
[root@db ~]# systemctl enable dhcpd [root@db ~]# systemctl enable tftp [root@db ~]# systemctl enable httpd [root@db ~]# systemctl start httpd [root@db ~]# systemctl start tftp [root@db ~]# vim /etc/xinetd.d/tftp { ... disable =yes # 修改這一項為"disable =no" ... } [root@db ~]# systemctl restart xinetd
4.配置DHCP服務
[root@db ~]# cd /etc/dhcp/
[root@db dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example dhcpd.conf #復制板配置文件
[root@db ~]# vim dhcpd.conf # 打開模板文件並添加一下內容 subnet 192.168.92.0 netmask 255.255.255.0{ #這裏寫的ip地址為子網的地址 range 192.168.92.200 192.168.92.224; #子網的範圍,之後主機請求獲取的ip地址就是這其中的一個 next-server 192.168.92.33; # 指明tftp服務器的地址 option routers 192.168.92.33; #網關地址也就是服務器地址 filename "pxelinux.0"; # 指明PXE文件位置,這個在申請ip的時候會發送給安裝主機 } [root@db dhcp]# systemctl restart dhcpd #重啟服務
5.準備yum源文件以及kickstart文件
[root@db dhcp]# cd /var/www/html/
[root@db html]# mkdir -p centos/{6,7}
[root@db html]# mkdir ksdir
關於Yum源文件,我們可以從光盤中復制,也可以直接把光盤掛載到指定目錄,這裏我選擇把光盤掛載到指定目錄
[root@db dhcp]# mount /dev/sr0 centos/6 # 掛載CentOS 6光盤值6目錄下
[root@db dhcp]# mount /dev/sr1 centos/7 # 掛載CentOS 7光盤值7目錄下
[root@db dhcp]# yum install -y system-config-kickstart
[root@db dhcp]# system-config-kickstart
可以按照順序自定義配置,之後保存到/var/www/html/ksdir/目錄下
或者復制/root目錄下的anaconda-ks.cfg文件進行修改
[root@db ~]# cp anaconda-ks.cfg /var/www/html/ksdir/ks7.cfg
[root@db ~]# vim /var/www/html/ksdir/ks7.cfg
[root@db ~]# chmod +r /var/www/html/ksdir/ks7.cfg
[root@db ~]# cat /var/www/html/ksdir/ks7.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url="http://192.168.92.33/centos/7" #指明yum源路徑
# Use graphical install
text # 將cdrom修改為text,我們不是基於光盤安裝的,我們是基於字符界面安裝
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network --hostname=centos7.cyz
# Root password
rootpw --iscrypted $6$cvwgC8f2Z/MY2UC9$MhmVS1.BVZmfMZAnM9LeYhW19587XIJ9d4cE9mG0GLAt4ETX/HNjBxUoOkhMRLcH2gY2mOT361w0CrFaangje0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai
# X Window System configuration information
xconfig --startxonboot
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /app --fstype="xfs" --ondisk=sda --size=19073
part swap --fstype="swap" --ondisk=sda --size=4096
part /boot --fstype="xfs" --ondisk=sda --size=190
part / --fstype="xfs" --ondisk=sda --size=19073
%packages #安裝軟件包,我這個是mini安裝所以安裝包比較少,可以自定義安裝。
@^minimal
@core
vim
%end
%addon com_redhat_kdump --disable --reserve-mb=‘auto‘
%end
centos6的kickstart文件
#version=DEVEL
install
text # 基於字符界面安裝
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$hfb25YOYZDU3YZTl$VxTkHGGJGGBbr59OPnY5kTJzvJ9hb9NRwrh5FMHLIAlXh9VQ74PYoK7QzPWYN0zaJrm3mv/IP0fDkHxFglNi6/
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
url --url="http://192.168.92.33/centos/6" # yum源的路徑
selinux --disabled # 關閉selinux
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# 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
zerombr # 清除mbr
clearpart --all # 清除分區信息
reboot # 安裝完成後重啟
part /boot --fstype=ext4 --size=1000 # 分區信息
part / --fstype=ext4 --size=50000
part /app --fstype=ext4 --size=40000
part swap --size=2048
#repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100 # 註釋掉此行
%packages
@base
@core
@debugging
@basic-desktop
@desktop-debugging
@desktop-platform
@directory-client
@fonts
@general-desktop
@graphical-admin-tools
@input-methods
@internet-applications
@internet-browser
@java-platform
@kde-desktop
@legacy-x
@network-file-system-client
@office-suite
@print-client
@remote-desktop-clients
@server-platform
@server-policy
@workstation-policy
@x11
mtools
pax
python-dmidecode
oddjob
wodim
sgpio
genisoimage
device-mapper-persistent-data
abrt-gui
qt-mysql
samba-winbind
certmonger
pam_krb5
krb5-workstation
xterm
xorg-x11-xdm
libXmu
rdesktop
%end
%post # 安裝後腳本,安裝完系統後向做什麽配置,都可以寫在%post...%end中間
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end
6.準備內核文件、菜單文件
[root@db ~]# cd /var/lib/tftpboot/
[root@db tftpboot]# mkdir -p centos{6,7} # 創建目錄文件
[root@db tftpboot]# cp /var/www/html/centos/6/{vmlinuz,initrd.img} centos6/ # 復制內核,虛擬根文件
[root@db tftpboot]# cp /var/www/html/centos/7/{vmlinuz,initrd.img} centos7/ # 由於之前掛載了不同系統,直接復制對應文件即可
[root@db tftpboot]# cp /usr/share/syslinux/pxelinux.0 . # pxelinux文件來自syslinux包,直接復制即可
[root@db tftpboot]# cp /usr/share/syslinux/vesamenu.c32 . #復制菜單文件
[root@db tftpboot]# mkdir pxelinux.cfg
[root@db tftpboot]# cp /var/www/html/centos/6/isolinux/isolinux.cfg pxelinux.cfg/default # 復制並改名
[root@db tftpboot]# vim pxelinux.cfg/default
[root@db tftpboot]# cat pxelinux.cfg/default # 查看修改過後的菜單文件內容
default vesamenu.c32
timeout 600
display boot.msg
# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13
# Border Area
menu color border * #00000000 #00000000 none
# Selected item
menu color sel 0 #ffffffff #00000000 none
# Title bar
menu color title 0 #ff7ba3d0 #00000000 none
# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none
# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none
# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none
# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none
# Help text
menu color help 0 #ffffffff #00000000 none
# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none
# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
menu label ^Install CentOS 7
menu default #光標默認在這一行
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://192.168.92.33/ksdir/ks7.cfg
label centos6
menu label Auto Install CentOS Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://192.168.92.33/ksdir/ks6.cfg
label linux
menu label Boot from ^local drive
localboot 0xffff
menu end
四、調試並安裝
新創建一臺虛擬機,設置網絡段在上述DHCP服務所在的網絡段,選擇使用物理驅動器自動檢測。
系統已經裝好了。
無人值守安裝Linux系統