自動化安裝操作系統

一、準備階段

(一)開機啓動進入救援模式另一種方法:

1.開機選擇CD-ROM 啓動
2.按ESC鍵進入命令行模式
3.輸入rescue並回車,進入救援模式
配置文件路徑:[root@CentOS6 isolinux]#cat /misc/cd/isolinux/isolinux.cfg
在這裏插入圖片描述
cat /misc/cd/isolinux/isolinux.cfg
在這裏插入圖片描述

Centos6最小化安裝217個包

製作應答文件:
圖形化軟件自動幫忙安裝
1.安裝圖形化軟件:[root@CentOS6 isolinux]#yum install system-config-kickstart
2.運行軟件:[root@CentOS6 isolinux]#system-config-kickstart

運行system-config-kickstart如報錯誤:Could not open display because no X server is running;則需要安裝xmanager並做設置,我這裏只安裝了Xshell才報此錯誤。
在這裏插入圖片描述

(二)ks應答文件製作

1.kickstart文件格式
可參考已經通過手動安裝好的系統,家目錄下的anaconda-ks.cfg文件進行修改

Centos7:

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade   1.安裝或更新系統
install
# Keyboard layouts	2.鍵盤佈局
keyboard 'us'
# Root password  3.超級管理員密碼
rootpw --iscrypted $1$1RvN.G3m$HYDaYiQwPVls0U143IY3J/
# Use network installation	4.安裝源
url --url="http://192.168.37.100/centos/os/7/x86_64/"
# System language	5.系統語言
lang en_US
# System authorization information    6.加密方式
auth  --useshadow  --passalgo=sha512
# Use text mode install    7.字符安裝模式,非圖形化安裝
text
# SELinux configuration  8.關閉selinux
selinux --disabled
# Do not configure the X Window System 9.配置圖形界面
skipx

# Firewall configuration 10.關閉防火牆
firewall --disabled
# Network information 11.網絡設置
network  --bootproto=dhcp --device=eth0
# Reboot after installation 12.安裝後重啓
reboot
# System timezone	13.設置時區
timezone Asia/Shanghai
# System bootloader configuration 14.內核參數設定,更改網卡名
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record  15.清除磁盤原有MBR
zerombr
# Partition clearing information  16.清除原有分區
clearpart --all --initlabel
# Disk partitioning information   17.創建分區
part /boot --fstype="xfs" --size=1000
part / --fstype="xfs" --size=20000
part swap --fstype="swap" --size=4000

Centos6:

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration   1.關閉系統自帶防火牆
firewall --disabled
# Install OS instead of upgrade  2.安裝或升級系統
install
# Use network installation	3.系統安裝源
url --url="http://192.168.37.100/centos/os/6/x86_64/"
# Root password	4.超級管理員密碼
rootpw --plaintext 123456
# System authorization information	5.系統加密方式
auth  --useshadow  --passalgo=sha512
# Use text mode install 6.安裝系統方式,字符安裝
text
# System keyboard 8.鍵盤佈局
keyboard us
# System language	9.系統語言
lang en_US
# SELinux configuration 10.關閉selinux
selinux --disabled
# Do not configure the X Window System  11.關閉圖形界面
skipx
# Installation logging level 12.日誌格式
logging --level=info
# Reboot after installation 13.安裝後重啓
reboot
# System timezone	14.設置時區
timezone  Africa/Abidjan
# Network information	15.網卡信息
#network  --bootproto=dhcp --device=eth0 --onboot=on   Centos6的system-config-kickstart 設置網絡有問題,使用root家目錄中anaconda-ks.cfg網絡配置做參考,或者不設置網絡均可
network --onboot yes --device eth0 --bootproto dhcp --noipv6  # anaconda-ks.cfg網絡配置參考
# System bootloader configuration 16.清除MBR
bootloader --location=mbr
# Clear the Master Boot Record	17.清除原來MBR
zerombr
# Partition clearing information  18.清除原來分區
clearpart --all --initlabel 
# Disk partitioning information  19.創建分區
part /boot --fstype="ext4" --size=1000
part / --fstype="ext4" --size=20000
part swap --fstype="swap" --size=2000

2.基於圖形界面定製kickstart文件
注:Centos7 安裝時需要將yum源名稱改爲[development]纔可以選擇Package Selection

安裝system-config-kickstart包
[root@CentOS6 ~]#yum install system-config-kickstart
[root@CentOS6 ~]#system-config-kickstart

(1)基本語言時區設置
在這裏插入圖片描述
(2)安裝方式:基於HTTP網絡安裝
在這裏插入圖片描述
(3)Boot Loader設置,centos7 內核參數中可設置:net.ifnames=0
在這裏插入圖片描述
(4)創建分區
在這裏插入圖片描述
(5)設置網絡:修改設備名稱
在這裏插入圖片描述
(6)認證:默認即可
在這裏插入圖片描述
(7)防火牆設置:禁用防火牆
在這裏插入圖片描述
(8)顯示設置:不需要圖形界面,需將install a graphical environment 選項 取消
在這裏插入圖片描述
(9)安裝包選擇:選擇需要安裝的軟件包
在這裏插入圖片描述
(10)安裝前腳本:系統安裝之前執行的腳本
在這裏插入圖片描述
(11)安裝後腳本:系統安裝後執行的腳本,可設置如系統的初始化配置等
在這裏插入圖片描述
(12)保存kickstart文件:點擊File->save選擇保存路徑,進行保存
在這裏插入圖片描述

二、PXE方式安裝

1.安裝相關包

yum install dhcp tftp-server httpd syslinux -y

2.準備yum安裝源

[root@CentOS7 ~]lsblk
[root@CentOS7 ~]#echo '- - -' > /sys/class/scsi_host/host0/scan 
[root@CentOS7 ~]#echo '- - -' > /sys/class/scsi_host/host1/scan 
[root@CentOS7 ~]#echo '- - -' > /sys/class/scsi_host/host2/scan
[root@CentOS7 ~]#mkdir -p /var/www/html/centos/{6,7}/x86_64
[root@CentOS7 ~]#mount /dev/sr0 /var/www/html/centos/7/x86_64/
[root@CentOS7 ~]#mount /dev/sr1 /var/www/html/centos/6/x86_64/

在這裏插入圖片描述
3.準備kickstart文件

[root@CentOS7 data]#cp /data/ks_centos6.10_mini.cfg /var/www/html/ksdir/
[root@CentOS7 data]#cp ks_centos7_mini.cfg /var/www/html/ksdir/
[root@CentOS7 data]#vim /var/www/html/ksdir/ks_centos6.10_mini.cfg
# Use network installation
url --url="http://192.168.37.100/centos/6/x86_64/"  

在這裏插入圖片描述

4.配置dhcp服務:
如果是虛擬機環境,需要設置關閉自動dhcp功能!
找到編輯->虛擬網絡編輯器->更改設置->取消勾選“使用本地DHCP將IP分配給虛擬機”->確定

[root@CentOS7 cd]#rpm -ql dhcp
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
[root@CentOS7 cd]#cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/dhcpd.conf 

[root@CentOS7 cd]#vim /etc/dhcp/dhcpd.conf 
option domain-name "example.org";
option domain-name-servers 114.114.114.114,8.8.8.8;
default-lease-time 6000;
max-lease-time 72000; 
subnet 192.168.37.0 netmask 255.255.255.0 {
   range 192.168.37.10 192.168.37.200;
   option routers 192.168.37.2;
   next-server 192.168.37.100;
   filename "pxelinux.0";
}

5.配置TFTP服務器
[root@CentOS7 cd]#rpm -ql tftp-server
/var/lib/tftpboot
[root@CentOS7 cd]#rpm -ql syslinux
/usr/share/syslinux/menu.c32
/usr/share/syslinux/pxelinux.0
[root@CentOS7 cd]#cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

6.自定義啓動菜單

[root@CentOS7 cd]#cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
[root@CentOS7 cd]#cp /misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/
[root@CentOS7 cd]#vim /var/lib/tftpboot/isolinux.cfg
default menu.c32
timeout 60
menu title CentOS Auto Install

label mini
  menu label Install CentOS ^Mini 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.37.100/ksdir/ks_centos7_mini.cfg
label desktop
  menu label Install CentOS M^ini 6
    kernel vmlinuz
    append initrd=initrd.img ks=http://192.168.37.100/ksdir/ks_centos6.10_mini.cfg
label local
  default menu                                                                                                              
  menu label Boot from ^local drive
  localboot 0xffff
  
  [root@CentOS7 tftpboot]#mkdir pxelinux.cfg
  [root@CentOS7 tftpboot]#mv isolinux.cfg pxelinux.cfg/default

在這裏插入圖片描述
在這裏插入圖片描述

7.拷貝內核文件

[root@CentOS7 ~]#cp /misc/cd/isolinux/vmlinuz /misc/cd/isolinux/initrd.img /var/lib/tftpboot/

在這裏插入圖片描述
8.啓動服務

[root@CentOS7 data]#systemctl start tftp
[root@CentOS7 data]#systemctl start dhcpd

9.創建新虛擬機
在這裏插入圖片描述
在這裏插入圖片描述

三、基於cobbler自動化安裝系統

(一)安裝軟件包:yum install cobbler dhcp -y
(二)啓動服務:

[root@CentOS7 ~]#systemctl start cobblerd httpd tftpd  #dhcp 目前起不來
[root@CentOS7 ~]#ss -nlut  #通過查看服務端口查看服務起來沒有

(三)運行cobbler check 進行配置

[root@CentOS7 ~]#cobbler check
The following are potential configuration items that you may want to fix:

1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work.  This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.         # 指定cobbler服務器IP
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.   指定TFTP服務器IP
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp   #centos7 不需要處理此項
4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely.  Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.  #下載bootloader內核等相關文件
5 : enable and start rsyncd.service with systemctl  #啓用此服務爲了同步速度快,此項不用做
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories  #此項關於deb系統,centos無需處理此項
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one    #修改默認root用戶密碼
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them   #集羣相關,非集羣環境無需考慮此項

Restart cobblerd and then run 'cobbler sync' to apply changes.

在這裏插入圖片描述
cobbler安裝後,tftp服務共享目錄中的文件夾已經創建
在這裏插入圖片描述

1.根據提示,先解決cobbler serverIP設置

vim /etc/cobbler/settings
# this is the address of the cobbler server
server: 192.168.37.100 

2.根據提示解決next-server指定tftp服務器IP地址

#if using cobbler with manage_dhcp, put the IP address
# of the cobbler server here so that PXE booting guests can find it
# if you do not set this correctly, this will be manifested in TFTP open timeouts.
next_server: 192.168.37.100

3.根據提示,修改root密碼,默認密碼是:cobbler

# cobbler has various sample kickstart templates stored
# in /var/lib/cobbler/kickstarts/.  This controls
# what install (root) password is set up for those
# systems that reference this variable.  The factory
# default is "cobbler" and cobbler check will warn if
# this is not changed.
# The simplest way to change the password is to run 
# openssl passwd -1
# and put the output between the "" below.
default_password_crypted: "$1$bdKI8ODk$6InoOSz5QxW0PFmLyBhrc/"   #通過openssl passwd -1 生成加密密碼粘貼上去

4.設置提示以外的dhcp

# set to 1 to enable Cobbler's DHCP management features.
# the choice of DHCP management engine is in /etc/cobbler/modules.conf
manage_dhcp: 1   #1表示根據 /etc/cobbler/modules.conf模板自動生成dhcp配置文件

5.根據提示,下載內核等文件

[root@CentOS7 ~]#cobbler get-loaders

在這裏插入圖片描述
6.修改dhcp模板

[root@CentOS7 ~]#vim /etc/cobbler/dhcp.template
    ddns-update-style interim;
    allow booting;
    allow bootp;
    ignore client-updates;
    set vendorclass = option vendor-class-identifier;
    option pxe-system-type code 93 = unsigned integer 16;
    subnet 192.168.37.0 netmask 255.255.255.0 {   #設置dhcp分配網段
         option routers             192.168.37.2;   #設置路由網關
         option domain-name-servers 192.168.37.2;	#設置dns服務器IP
         option subnet-mask         255.255.255.0;	#設置子網掩碼
         range dynamic-bootp        192.168.37.10 192.168.37.200;  #設置IP地址池範圍
         default-lease-time         21600;
         max-lease-time             43200;
         next-server                $next_server;
         class "pxeclients" {
              match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
              if option pxe-system-type = 00:02 {
                      filename "ia64/elilo.efi";
              } else if option pxe-system-type = 00:06 {
                      filename "grub/grub-x86.efi";
              } else if option pxe-system-type = 00:07 {
                      filename "grub/grub-x86_64.efi";
              } else if option pxe-system-type = 00:09 {
                      filename "grub/grub-x86_64.efi";
              } else {
                      filename "pxelinux.0";
              }
         }
    
    }

7.配置完的內容還沒有生效,需要重啓服務,執行cobbler同步命令生效;
注:這裏需要先重啓在執行同步命令,如果先執行同步,dhcp配置文件不會生效

[root@CentOS7 ~]#systemctl restart cobblerd
[root@CentOS7 ~]#cobbler sync 

在這裏插入圖片描述

8.導入光盤系統文件生成啓動菜單項

[root@CentOS7 ~]#mount /dev/sr1 /mnt    #掛載光盤
[root@CentOS7 ~]#cobbler import --path=/mnt --name=CentOS6.10-x86_64 --arch=x86_64  #導入光盤-name 指定cobbler生成yum安裝源名稱  --arch指定架構:因爲centos CPU支持的架構很多
[root@CentOS7 ~]#du -sh /var/www/cobbler/ks_mirror/
[root@CentOS7 ~]#cat /var/lib/tftpboot/pxelinux.cfg/default  #導入光盤命令執行完畢後,菜單就自動生成了

在這裏插入圖片描述
9.自定義ks應答文件
9.1 修改自己的ks文件,指定cobbler的yum源

[root@CentOS7 data]#vim ks_centos6_mini.cfg 
# Use network installation
url --url="$tree"     #設定爲cobbler的變量

9.2 拷貝自己的ks文件到cobbler放應答文件的目錄

[root@CentOS7 data]#cp /data/ks_centos6_mini.cfg /var/lib/cobbler/kickstarts/

9.3 查看cobbler的yum安裝源

[root@CentOS7 data]#cobbler distro list

9.4 查看cobbler自帶的ks應答文件

[root@CentOS7 data]#cobbler profile list

9.5添加自定義ks文件

注:指定ks文件路徑,即使是在cobbler的kickstart目錄中也要指定絕對路徑,否則cobbler無法找到
[root@CentOS7 kickstarts]#cobbler profile add --name Centos6.1-_mini --distro=CentOS6.10-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks_centos6_mini.cfg
[root@CentOS7 kickstarts]#cat /var/lib/tftpboot/pxelinux.cfg/default  #查看啓動菜單配置文件
[root@CentOS7 kickstarts]#cobbler profile list	#查看ks列表

9.6刪除系統自帶ks文件

[root@CentOS7 kickstarts]#cobbler profile remove --name=CentOS6.10-x86_64
[root@CentOS7 kickstarts]#cobbler profile list	#查看ks列表
[root@CentOS7 kickstarts]#cat /var/lib/tftpboot/pxelinux.cfg/default	#查看啓動菜單配置文件

10.創建新虛擬機
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章