天天看點

KICKSTART無人值守安裝

KICKSTART無人值守安裝

1.1 環境說明

[root@test ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)

[root@test ~]# uname -r
2.6.32-696.el6.x86_64

[root@test ~]# getenforce
Disabled

[root@test ~]# /etc/init.d/iptables status
iptables: Firewall is not running.

[root@test ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $4}'
10.0.0.250

[root@test ~]# hostname
test      

1.2 配置DHCP

1.2.1 安裝dhcp

yum -y install dhcp
rpm -ql dhcp |grep "dhcpd.conf"      

1.2.2 編寫配置檔案

[root@test ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see 'man 5 dhcpd.conf'
#
subnet 10.0.0.0 netmask 255.255.255.0 {

        range 10.0.0.100 10.0.0.200;

        option subnet-mask 255.255.255.0;

        default-lease-time 21600;

        max-lease-time 43200;

        next-server 10.0.0.250;

        filename "/pxelinux.0";

}      

----------------------------------------------------------------

 # 注釋

range 10.0.0.100 10.0.0.200;         # 可配置設定的起始IP-結束IP
option subnet-mask 255.255.255.0;    # 設定netmask
default-lease-time 21600;            # 設定預設的IP租用期限
max-lease-time 43200;                # 設定最大的IP租用期限
next-server 10.0.0.250;                # 告知用戶端TFTP伺服器的ip
filename "/pxelinux.0";              # 告知用戶端從TFTP根目錄下載下傳pxelinux.0檔案      

1.2.3 啟動服務

[root@test ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                            [  OK  ]
[root@test ~]# netstat -tunlp|grep dhcp
udp        0      0 0.0.0.0:67                  0.0.0.0:*                               4578/dhcpd         

1.3 安裝TFTP服務

1.3.1 安裝tftp服務

[root@linux-node1 ~]# yum -y install tftp-server      

1.3.2 編寫xindetd下的配置檔案

[root@linux-node1 ~]# 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 # 由原來的yes改為no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}      

1.3.3 啟動服務,讓xinetd 管理

[root@linux-node1 ~]# /etc/init.d/xinetd restart
Stopping xinetd:                                           [FAILED]
Starting xinetd:                                           [  OK  ]      

1.3.4 檢查端口

[root@linux-node1 ~]# netstat -tunlp|grep 69
udp        0      0 0.0.0.0:69                  0.0.0.0:*                               1106/xinetd      

1.4 配置HTTP服務

1.4.1 安裝nginx的依賴包(pcre-devel openssl-devel)

yum install -y pcre-devel openssl-devel      

1.4.2 下載下傳nginx軟體

wget http://nginx.org/download/nginx-1.10.3.tar.gz      

解壓軟體

tar xf nginx-1.10.3.tar.gz      

1.4.3 建立管理使用者 www

useradd -M -s /sbin/nologin www      

1.4.4  nginx軟體編譯安裝過程

1、配置軟體,在軟體的解壓目錄中

[root@web01 nginx-1.10.3]# ./configure --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module      

   通過軟體編譯過程中的傳回值是否正确,确認配置是否正确

[root@web01 nginx-1.10.3]# echo $?
0      

   2、編譯軟體

[root@web01 nginx-1.10.3]# make      

   3、編譯安裝

[root@web01 nginx-1.10.3]# make install      

1.4.5 建立軟連接配接

[root@web01 application]# ln -s /application/nginx-1.10.3/ /application/nginx      

1.4.6 修改nginx配置檔案

添加一行配置,作用是顯示目錄裡的所檔案

[root@test html]# vim ../conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            autoindex on;
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}      

1.4.7 啟動程式

[root@web01 application]# /application/nginx/sbin/nginx
[root@web01 application]#      

檢查是否啟動

[root@web01 application]# ps -ef |grep nginx
root      26548      1  0 20:13 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
www       26549  26548  0 20:13 ?        00:00:00 nginx: worker process        
root      26551  23431  3 20:13 pts/0    00:00:00 grep --color=auto nginx      

檢查端口資訊

[root@web01 application]# netstat -lntup |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      26548/nginx        

1.5 挂載CD光牒

1.5.1 删除預設的首頁檔案,建立挂載目錄

cd /application/nginx-1.10.3/html && \rm *.html
mkdir -p /application/nginx-1.10.3/html/ios      

1.5.2 挂載CD光牒

mount /dev/cdrom /application/nginx-1.10.3/html/ios/      

1.5.3 檢查挂載資訊

[root@test html]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  1.8G   16G  10% /
tmpfs           238M     0  238M   0% /dev/shm
/dev/sda1       190M   40M  141M  22% /boot
/dev/sr0        3.7G  3.7G     0 100% /application/nginx-1.10.3/html/ios/      

1.6 配置支援PXE的啟動程式

安裝syslinux

yum -y install syslinux      

複制啟動菜單程式檔案

[root@test ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@test ~]# cp -a /application/nginx-1.10.3/html/isolinux/* /var/lib/tftpboot/
[root@test ~]#  ls /var/lib/tftpboot/
boot.cat  grub.conf   isolinux.bin  memtest     splash.jpg  vesamenu.c32
boot.msg  initrd.img  isolinux.cfg  pxelinux.0  TRANS.TBL   vmlinuz      

建立一個pxelinux.cfg目錄,存放用戶端的配置檔案。

mkdir -p /var/lib/tftpboot/pxelinux.cfg
cp -a /application/nginx-1.10.3/html/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default      

1.7 建立一個新的虛拟機

    不要使用CD光牒,然後開機

KICKSTART無人值守安裝

  出現此界面說明上面的步驟都配置正确

[root@test ~]# cat  /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32    # 預設加載一個菜單
#prompt 1     # 開啟會顯示指令行'boot: '提示符。prompt值為0時則不提示,将會直接啟動'default'參數中指定的内容。
timeout 600   # timeout時間是引導時等待使用者手動選擇的時間,設為1可直接引導,機關為1/10秒。

display boot.msg
# 菜單背景圖檔、标題、顔色。
menu background splash.jpg
menu title Welcome to CentOS 6.9!
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指定在boot:提示符下輸入的關鍵字,比如boot:linux[ENTER],這個會啟動label linux下标記的kernel和initrd.img檔案。
label linux    # 一個标簽就是前面圖檔的一行選項
  menu label ^Install or upgrade an existing system
  menu default
  kernel vmlinuz   # 指定要啟動的核心。同樣要注意路徑,預設是/tftpboot目錄
  append initrd=initrd.img   # 指定追加給核心的參數,initrd.img是一個最小的linux系統
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 -      

1.8 建立ks.cfg檔案

通常,我們在安裝作業系統的過程中,需要大量的和伺服器互動操作,為了減少這個互動過程,kickstart就誕生了。使用這種kickstart,隻需事先定義好一個Kickstart自動應答配置檔案ks.cfg(通常存放在安裝伺服器上),并讓安裝程式知道該配置檔案的位置,在安裝過程中安裝程式就可以自己從該檔案中讀取安裝配置,這樣就避免了在安裝過程中多次的人機互動,進而實作無人值守的自動化安裝。

生成kickstart配置檔案的三種方法:

方法1、

   每安裝好一台Centos機器,Centos安裝程式都會建立一個kickstart配置檔案,記錄你的真實安裝配置。

  如果你希望實作和某系統類似的安裝,可以基于該系統的kickstart配置檔案來生成你自己的kickstart配置檔案。(生成的檔案名字叫anaconda-ks.cfg位于/root/anaconda-ks.cfg)

方法2、

  Centos提供了一個圖形化的kickstart配置工具。在任何一個安裝好的Linux系統上運作該工具,就可以很容易地建立你自己的kickstart配置檔案。

  kickstart配置工具指令為redhat-config-kickstart(RHEL3)或system-config-kickstart(RHEL4,RHEL5).網上有很多用CentOS桌面版生成ks檔案的文章,如果有現成的系統就沒什麼可說。但沒有現成的,也沒有必要去用桌面版,指令行也很簡單。

方法3、

  閱讀kickstart配置檔案的手冊。用任何一個文本編輯器都可以建立你自己的kickstart配置檔案。

ks.cfg檔案詳解

官網文檔
CentOS5 : http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-kickstart2-options.html
CentOS6 : https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/s1-kickstart2-options.html      

官網自帶中文版,選一下語言即可

ks.cfg

檔案組成大緻分為3段

  • 指令段

    鍵盤類型,語言,安裝方式等系統的配置,有必選項和可選項,如果缺少某項必選項,安裝時會中斷并提示使用者選擇此項的選項

  • 軟體包段
    1. %packages

    2. @groupname:指定安裝的包組

    3. package_name:指定安裝的包

    4. -package_name:指定不安裝的包

在安裝過程中預設安裝的軟體包,安裝軟體時會自動分析依賴關系。

  • 腳本段(可選)
    1. %pre:安裝系統前執行的指令或腳本(由于隻依賴于啟動鏡像,支援的指令很少)

    2. %post:安裝系統後執行的指令或腳本(基本支援所有指令)

關鍵字 含義

install

告知安裝程式,這是一次全新安裝,而不是更新

upgrade

url --url=" "

通過

FTP

HTTP

從遠端伺服器上的安裝樹中安裝。

url --url="http://10.0.0.7/CentOS-6.7/"

url --url ftp://<username>:<password>@<server>/<dir>

nfs

從指定的

NFS

伺服器安裝。

nfs --server=nfsserver.example.com --dir=/tmp/install-tree

text

使用文本模式安裝。

lang

設定在安裝過程中使用的語言以及系統的預設語言。

lang en_US.UTF-8

keyboard

設定系統鍵盤類型。

keyboard us

zerombr

清除

mbr

引導資訊。

bootloader

系統引導相關配置。

bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"

--location=

,指定引導記錄被寫入的位置.有效的值如下:

mbr

(預設),

partition

(在包含核心的分區的第一個扇區安裝引導裝載程式)或

none

(不安裝引導裝載程式)。

--driveorder

,指定在

BIOS

引導順序中居首的驅動器。

--append=

,指定核心參數.要指定多個參數,使用空格分隔它們。

network

為通過網絡的

kickstart

安裝以及所安裝的系統配置聯網資訊。

network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6

--bootproto=[dhcp/bootp/static]

中的一種,預設值是

dhcp

bootp

dhcp

被認為是相同的。

static

方法要求在

kickstart

檔案裡輸入所有的網絡資訊。

network --bootproto=static --ip=10.0.0.100 --netmask=255.255.255.0 --gateway=10.0.0.2 --nameserver=10.0.0.2

請注意所有配置資訊都必須在一行上指定,不能使用反斜線來換行。

--ip=

,要安裝的機器的

IP

位址.

--gateway=

,IP位址格式的預設網關.

--netmask=

,安裝的系統的子網路遮罩.

--hostname=

,安裝的系統的主機名.

--onboot=

,是否在引導時啟用該裝置.

--noipv6=

,禁用此裝置的

IPv6

.

--nameserver=

,配置

dns

解析.

timezone

設定系統時區。

timezone --utc Asia/Shanghai

authconfig

系統認證資訊。

authconfig --enableshadow --passalgo=sha512

設定密碼加密方式為

sha512

 啟用

shadow

檔案。

rootpw

root

密碼

clearpart

清空分區。

clearpart --all --initlabel

--all

 從系統中清除所有分區,

--initlable

 初始化磁盤标簽

part

磁盤分區。

part /boot --fstype=ext4 --asprimary --size=200

part swap --size=1024

part / --fstype=ext4 --grow --asprimary --size=200

--fstype=

,為分區設定檔案系統類型.有效的類型為

ext2

,

ext3

swap

vfat

--asprimary

,強迫把分區配置設定為主分區,否則提示分區失敗。

--size=

,以

MB

為機關的分區最小值.在此處指定一個整數值,如

500

.不要在數字後面加

MB

--grow

,告訴分區使用所有可用空間(若有),或使用設定的最大值。

firstboot

負責協助配置redhat一些重要的資訊。

firstboot --disable

selinux

關閉

selinux

selinux --disabled

firewall

關閉防火牆。

firewall --disabled

logging

設定日志級别。

logging --level=info

reboot

設定安裝完成後重新開機,此選項必須存在,不然kickstart顯示一條消息,并等待使用者按任意鍵後才重新開機,也可以選擇

halt

關機。

1.9 檢視系統安裝完成的anaconda-ks.cfg

[root@test ~]#  cat anaconda-ks.cfg
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
cdrom
lang en_US.UTF-8
keyboard us
network --onboot no --device eth0 --bootproto dhcp --noipv6
rootpw  --iscrypted $6$.8PjXDBfzrUEFZte$IfTqwmdHXTJ6HD5/mYOuhuNMhVWaJI0xwyRMvOIrYkaEZduHVYuTEfjbgAqEuEsJii0wkBQvCVgF.KRG9ikwu0
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
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
#clearpart --none

#part /boot --fstype=ext4 --asprimary --size=200
#part swap --asprimary --size=768
#part / --fstype=ext4 --grow --asprimary --size=200


repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100

%packages
@base
@compat-libraries
@core
@debugging
@development
@server-policy
@workstation-policy
python-dmidecode
sgpio
device-mapper-persistent-data
systemtap-client      

1.9.1 編寫ks檔案

[root@test ~]# grub-crypt
Password:  123456
Retype password:  123465
$6$OH3zrKw7ruG5mtIh$8bV2RhvoB72VCIXYY.2ROFi8AOLdI3lHGB.rkGDEhlqxTZduPE3VoJW2OIZRA1y9Gw4Zka461IBZ9VuIIaNqK.      

建立ks檔案存放目錄

[root@test ~]# mkdir /application/nginx-1.10.3/html/ks_config -p      

ks檔案内容

[root@test ks_config]# cat /application/nginx-1.10.3/html/ks_config/CentOS-6.9-ks.cfg
# Kickstart Configurator for CentOS 6.9 by hou zhaoshun
install
url --url="http://10.0.0.250/ios/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=768
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post
wget -O /tmp/optimization.sh http://10.0.0.250/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end      

1.10 編寫開機優化腳本

[root@test ks_config]# cat /application/nginx-1.10.3/html/ks_config/optimization.sh 
#!/bin/bash
##############################################################
# File Name: /var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: houzhaoshun
# Organization: blog.znix.top
# Created Time : 2017-10-23 
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=10.0.0.250
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip  -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
        echo "Http server is bad!"
        exit 1
}
# Defined result function
function Msg(){
        if [ $? -eq 0 ];then
          action "$1" /bin/true
        else
          action "$1" /bin/false
        fi
}
# Defined IP function
function ConfigIP(){
Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print $6}'`
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.0.0.$Suffix
PREFIX=24
GATEWAY=10.0.0.254
DNS1=223.5.5.5
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
END
Msg "config eth0"
}
# Defined Yum source Functions
function yum(){
        YumDir=/etc/yum.repos.d
        [ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori} 
        wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
        wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
        Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
        [ -f "/etc/issue" ] && >/etc/issue
        Msg "Hide issue" 
        [ -f "/etc/issue.net" ] && > /etc/issue.net
        Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
        [ -f "/etc/security/limits.conf" ] && {
        echo '*  -  nofile  65535' >> /etc/security/limits.conf
        Msg "open files"
        }
}
# Defined Kernel parameters Functions
function kernel(){
        KernelDir=/etc
        [ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
        wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
        Msg "Kernel config"
}
# Defined System Startup Services Functions
function boot(){
        for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"` 
          do 
           chkconfig $oldboy off
        done
        Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
        echo "#time sync by houzhaoshun at $(date +%F)" >>/var/spool/cron/root
        echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
        Msg "Time Synchronization"
}
# Defined main Functions
function main(){
        ConfigIP
        yum
        HideVersion
        openfiles
        kernel
        boot
        Time
}
main
# rz上傳CentOS-Base.repo、epel.repo、sysctl.conf      

1.11 整合編輯default配置檔案

[root@test ks_config]# cat  /var/lib/tftpboot/pxelinux.cfg/default
default ks
prompt 0
timeout 600

display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.9!
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
  menu default
  kernel vmlinuz
  append 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 -
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.250/ks_config/CentOS-6.9-ks.cfg      

1.11 以上操作完成後将之前建立的虛拟機重新開機

    然後你就可以取喝杯茶,等他一會就ok了

作者:慘綠少年

出處:http://clsn.io

本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

繼續閱讀