爲何我們要自動化安裝系統呢?管理一些重複性的操做呢,這樣能夠減輕本身的操做,而且提升了工做效率,假如公司中又有十幾臺甚至更多的服務器,不可能本身一臺一臺的安裝,這樣就須要自動化安裝。html
POST-->boot Sequence-->bootloader(MBR)-->kernel(initramdisk)-->rootfs-->/sbin/init/java
(1)[root@centos7/media/cdrom/isolinux]#ls
linux
內容 | 解釋 |
boot.cat | 至關於啓動菜單顯示內容 |
grub.conf | 啓動系統啓動與boot.cat顯示內容配置文件 |
isolinux.bin | 至關於系統安裝的stage2階段 |
memtest | 內存檢測,這是個獨立程序 |
vmlinuz | 內核映像 |
boot.msg | |
initrd.img | 虛擬文件系統 |
isolinux.cfg | isolinux.cat的配置文件 |
splash.png | 背景圖片 |
vesamenu.c32 | 顯示背景界面 |
(2)isolinux.cfg詳解redis
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 Linux 7 #顯示title menu vshift 8 menu rows 18 menu margin 8 #menu hidden menu helpmsgrow 15 # 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 # 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 tabmsg Press Tab for full configuration options on menu items. #鍵入TAB進入配置模式 menu separator # insert an empty line menu separator # insert an empty line label linux #至關於標籤 menu label ^Install CentOS Linux 7 #安裝linux kernel vmlinuz #內核文件 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet #向內核傳遞參數 label check menu label Test this ^media & install CentOS Linux 7 menu default #默認啓動項 kernel vmlinuz menu separator # insert an empty line # utilities submenu menu begin ^Troubleshooting menu title Troubleshooting label vesa menu indent count 5 menu label Install CentOS Linux 7 in ^basic graphics mode text help Try this option out if you're having trouble installing CentOS Linux 7. endtext kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet label rescue menu indent count 5 menu label ^Rescue a CentOS Linux system text help If the system will not boot, this lets you access files and edit config files to try to get it booting again. endtext kernel vmlinuz append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet label memtest menu label Run a ^memory test text help If your system is having issues, a problem with your system's memory may be the cause. Use this utility to see if the memory is working correctly. endtext kernel memtest menu separator # insert an empty line label local menu label Boot from ^local drive localboot 0xffff menu separator # insert an empty line menu separator # insert an empty line label returntomain menu label Return to ^main menu menu exit menu end
配置文件中有「^」至關於快捷鍵。vim
anaconda是系統安裝程序。啓動系統安裝過程。
anaconda安裝分爲三個階段:centos
(a)安裝前配置階段bash
安裝過程使用的語言 鍵盤類型 安裝目標存儲設備 basic storage 本地磁盤 特殊設備 iscsI 設定主機名 配置網絡接口 時區設置 管理員密碼 設定分區方式及MBR的安裝位置 建立一個普通用戶 選定要安裝的程序包
(b)安裝階段在目標磁盤服務器
建立分區,執行格式化操做等 選定的程序包安裝至目標位置 安裝BootLoader和initramfs
(c)圖形模式首次啓動網絡
iptables selinux core dump
(a)本地光盤 (b)本地硬盤 (c)NFS (d)URL ftp server :yum repository http server:yum repository (e)手動指定安裝源 在系統啓動選項菜單界面進入TAB鍵,進入boot命令行模式輸入`linux askmethod`,若是須要網絡後續會跳出來一個對話框填入IP便可。
(a)交互式配置 (b)經過讀取事先給定的配置文件自動完成配置(kickstart文件)
#version=DEVEL #指明版本 # System authorization information auth --enableshadow --passalgo=sha512 # Install OS instead of upgrade install # Use text install text # Firewall configuration firewall --disabled firstboot --disable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=cn --xlayouts='cn' # System language lang en_US.UTF-8 # Use network installation #url --url="http://172.16.0.1/cblr/links/CentOS-7-1511-x86_64" #repo --name="source-1" --baseurl=http://172.16.0.1/cobbler/ks_mirror/CentOS-7-1511-x86_64 #url --url="http://172.16.0.1/cobbler/ks_mirror/7/" url --url=$tree # Network information network --bootproto=dhcp --device=ens33 --onboot=yes --ipv6=auto network --hostname=localhost.localdomain # Reboot after installation reboot # SELinux configuration selinux --disabled # Do not configure the X Window System skipx # Root password rootpw --iscrypted $6$dGARWYghrvhD9W7P$4af2uw8A4tHvNLe2F6bDrk0J69dt.uYoV4SneKG4kzIsc/nF3JpfnuHg7D5lVE.jxC3p6.K29FCjwtom9VXWf. # System timezone timezone Asia/Shanghai user --groups=wheel --name=wang --password=$6$S0t9SxXkvSI0Npz5$YDVonz69YGGGkGyZDy/KesO4CI1HUBLNThcHj6PXDntQGS0Cl0SNpN6zGASuzYE.z5veu1XAzMvO4xLWbKE.n0 --iscrypted --gecos="123456" # System bootloader configuration bootloader --location=mbr --boot-drive=sda # Partition clearing information clearpart --all --initlabel # Disk partitioning information part /boot --fstype="xfs" --ondisk=sda --size=500 part swap --fstype="swap" --ondisk=sda --size=2049 part / --fstype="xfs" --ondisk=sda --size=40960 part /app --fstype="xfs" --ondisk=sda --size=20480 %post systemctl set-default multi-user.target [ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.ssh cat >> /root/.ssh/authorized_keys << EOF ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlmq1z0G/7wbGuSUewfXlFnwzqCg/myqTi/AwP8LP+JJ49xzIKMzpeWXHD8RWIf5RlDzo+6N7uPK5O22x/QtMosi0egz4shavEJeUkO0EH+KygXXgBIGuMWmAsL+yzbgWXT9H3zdzXi/qWcrBeBv2nYB5mpYSf7o0xqdhCst1MTfcYLD8qxvkwC8RiqBA/1u9N6jeDFbHO+UzZYYCr9zgk9uz4Rrhb9BU7c1GhjUCgRwBDAuo47IHw/OT6KS9lb8lT2R/ujVoDARy/eOhw8cAFXo+QcvzNSW2qKf/Qo21uR/wz2u9SRV0lvUDNSvC2PYtR+iPlDwHY81md430yiNf9w== root@172.16.0.1 EOF ClientName=`ifconfig enoens33 | awk '/inet\>/{print $2}' | awk -F. '{print $NF}'` hostnamectl set-hostname "stu$ClientName.wang.com" #mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak #wget http://172.18.0.1/repos/CentOS-Base.repo -O /etc/yum.repos.d/CentOS-Base.repo %end %packages @base @core @fonts @input-methods @kde-apps @kde-desktop @network-file-system-client @networkmanager-submodules @x11 lftp screen %end %addon com_redhat_kdump --disable --reserve-mb='auto' %end
(a)命令端:指明各類安裝前配置,若是鍵盤類型,時區等 必備命令 authconfig:認證方式配置 authconfig --useshadow --passalgo=sha512 bootloader:bootloader的安裝位置及相關配置 bootloader --location=mbr --driverorder=sda --append=「crashkernel=auto rhgb quiet」 keyboard:設定鍵盤類型 lang:語言類型
part:建立分區 rootpw:指明root的密碼 timezone:時區 可選命令 install OR update text :文本界面安裝 network 設定網絡 firewall 設置防火牆 selinux 設置selinux halt
poweroff reboot repo user:安裝完成後爲系統建立新用戶 url:指明安裝源 key -skip:跳過安裝號碼,適用於rhel版本app
(b)程序包段:指明要安裝的程序包或程序包組,不安裝的包等
%packages #代表開始安裝程序包或包組 @group_name #安裝包組 package #安裝程序包 -package #不安裝程序包 %end 指明安裝結束
(c)腳本段:指明運行的腳本
%pre #安裝前腳本 運行環境:運行於安裝介質上的微型linux環境 %post #安裝後腳本 運行環境:運行於安裝完成後的系統
注意:在每段中必定要有開始有結束%end代表結束安裝。
(a)直接手動編輯,能夠依賴模板進行自行修改
(b)可以使用建立工具:system-config-kickstart命令,依賴某模板修改並生成新的配置文件 注意:在CentOS7中,若是要使用此命令必須把yum倉庫光盤的名稱修改成development。 檢查ks文件的語法錯誤:ksvalidator 格式:ksvalidator /PATH/TO/SOMEFILE
DVD drive:ks=cdrom:/PATH/TO/KICKSTART_FILE Hard drive:ks=hd:/PATH/TO/KICKSTART_FILE FTP server:ks=ftp://host:port/PATH/TO/KICKSTART_FILE HTTP server:ks=http://host:port/PATH/TO/KICKSTART_FILE HTTPS server:ks=https://host:port/PATH/TO/KICKSTART_FILE NFS server:ks=nfs:host:/PATH/TO/KICKSTART_FILE
若是想理解更多,請查考詳細內容《Installation Guide》。
(a)text:文本安裝模式
(b)askmethod:手動指定安裝方法 在系統啓動界面,鍵入ESC進入boot命令行模式:輸入linux askmethod 與網絡相關的引導選項: ip=IPADDR netmask=NETMAKS gateway=GATEWAY dns=DNS_SERVER_IP ifname=NAME:MAC_ADDR (c)與遠程功能相關的引導選項 vns vncpassword=「PASSWORD」
實戰
手動指定kickstart文件安裝系統
方式一:
(1)在CentOS7上搭建ftp服務
[root@centos7~]#cd /var/ftp/pub/
[root@centos7/var/ftp/pub]#cp /root/ks*.cfg .
[root@centos7/var/ftp/pub]#ls
ks6.cfg ks7.cfg
(2)建立一塊虛擬機
(3)手動指定按啓動,選定光盤啓動
當出現上面圖示時,咱們手動指定kickstart啓動。
方法一: 按TAB鍵,輸入ks=ftp://192.168.4.135/pub/ks6.cfg;回車啓動 方法二: 按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks6.cfg
方式二:
在CentOS7搭建http服務
[root@centos7/var/www/html]#mkdir ks
[root@centos7/var/www/html]#mv ks*.cfg ks
其他步驟同上述方法相似。
注意:上述過程必須使ks文件權限爲644.上述方法前提爲橋接模式。
上述方法是沒有辦法安裝包等後續工做的,咱們能夠搭建網絡yum源進行安裝。
方式一:建立網絡yum源安裝
(1)安裝vsftpd服務,並啓動服務
(2)將ks7.cfg文件複製到 /var/ftp/pub
目錄下
(3)在/var/ftp/pub
目錄下建立centos/7
[root@centos7/var/ftp/pub]#mkdir centos/7 -pv
(4)將光盤掛載到centos/7上
[root@centos7/var/ftp/pub]#mount /dev/sr0 /var/ftp/pub/centos/7/
(5)測試是否能在網頁中打開
輸入地址ftp://192.168.4.135/pub
(6)新建一臺虛擬機測試
按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0
或 按TAB鍵在行後追加 ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0
(7)出現下面說明成功
方式二:
(1)在CentOS7上搭建http服務
(2)在/var/www/html
目錄下建立ks目錄和centos/7目錄
[root@centos7/var/www/html]#mkdir testcentos/7 -pv
(3)將光盤掛載至centos/7目錄下[root@centos7/var/www/html]#mount /dev/sr0 testcentos/7/
(4)重啓服務測試 在網站輸入http://192.168.4.135/test (5)新建一臺虛擬機從新測試,出現下面內容說明成功
按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0
或
按TAB鍵在行後追加 ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0
在平常的生產活動中,使用光驅的已經不多了,可是U盤自身的特色獲得了大多數人的使用,這樣讓我掌握如何製做U盤變得特別重要,若是咱們公司有的服務器沒有光盤咋辦,?U盤就很好的幫咱們解決了這個問題。
(1)mkisofs命令有關包genisop_w_picpath 製做光盤須要使用命令才能執行。
選項 | 解釋 |
-o | 指定影響文件的名稱 |
-b | 指定在製做可開機光盤是所須要的開機映像文件 |
-c | 製做可開機光盤時,會將開機映像文件中的no-eltorito-catalog所有內容作成一個文件 |
-no-emul-boot | 非模擬模式啓動 |
-boot-load-size 4 | 設置載入部分的數量 |
-boot-info-table | 在啓動的圖像中顯示信息 |
-R,-rock | 使用Rock RidgeExtensions |
-J,-joliet | 使用Joliet格式的目錄與文件名稱 |
-v,-verbose | 執行時顯示過程 |
-T,-translation-table | 創建文件名的裝換表,適用於不支持Rock RidgeExtensions的系統上 |
(2)製做光盤過程
(a)在CentOS7主機上搭建yum源,並綁定到ftp服務上。
(b)建立光盤引導文件
[root@centos7~]#mkdir /tmp/testiso
[root@centos7~]#mkdir /tmp/testiso/isolinux
[root@centos7~]#cp /media/cdrom/isolinux/* /tmp/testiso/isolinux
[root@centos7/tmp/testiso]#cp /var/ftp/pub/ks7.cfg .
[root@centos7/tmp/testiso]#vim ks7.cfg
#編輯配置文件 [root@centos7/tmp/testiso]#tree
. ├── isolinux │ ├── boot.cat │ ├── boot.msg │ ├── grub.conf │ ├── initrd.img │ ├── isolinux.bin │ ├── isolinux.cfg │ ├── memtest │ ├── splash.png │ ├── TRANS.TBL │ ├── vesamenu.c32 │ └── vmlinuz └── ks7.cfg
注意:此處建立使用的是相對路徑,而和工做目錄無關。必須在isolinux下。
(c)建立光盤[root@centos7/tmp]#mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.9 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/
(d)將光盤下載至本機,安裝到虛擬中嘗試啓動
說明咱們製做的光盤成功
(3)製做U盤
(a)咱們這裏虛擬一塊U盤,添加一塊硬盤
(b)將光盤拷貝至硬盤中
[root@centos7~]#dd if=boot.iso of=/dev/sdb
(c)新建虛擬機掛載此光盤啓動 出現上述狀況說明咱們安裝成功。可是這種方法還須要經過網絡才能安裝,若是咱們去到一個地方沒有網絡,怎麼辦呢?有沒有想過咱們把rpm包到拷貝至一個U盤或光盤上呢?這樣是否是很方便咱們辦公呢?
(a)建立一個臨時目錄並拷貝文件至此目錄下[root@localhost ~]#mkdir /tmp/fulliso
[root@localhost ~]# cp -a /media/cdrom/* /tmp/fulliso/
[root@localhost ~]# cd /tmp/fulliso/
[root@localhost fulliso]# cp /media/cdrom/.treeinfo .
[root@localhost fulliso]# cp /media/cdrom/.discinfo .
(b) 刪除repodate目錄非*comps.xml結尾的文件。[root@localhost fulliso]# find repodata/ -not -name *-comps.xml -delete
[root@localhost fulliso]# find -name TRANS.TBL -delete
(c)以上述那個文件製做倉庫[root@localhost fulliso]# ls repodata/ d918936f5019be3fb66e9981a28cb2a41477a2963d741d454f79377a22214f43-c7-x86_64-comps.xml
(d)編輯kickstart配置文件,並複製fulliso目錄下[root@localhost fulliso]# vim ks7.cfg
auth --enableshadow --passalgo=sha512 # Use graphical install graphical # 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 --onboot=on --ipv6=auto network --hostname=localhost.localdomain # Root password # System services services --disabled="chronyd" # System timezone timezone Asia/Shanghai # X Window System configuration information xconfig --startxonboot # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda zerombr reboot # Partition clearing information clearpart --all part /boot --fstype="xfs" --ondisk=sda --size=1024 part swap --fstype="swap" --ondisk=sda --size=2048 part / --fstype="xfs" --ondisk=sda --size=50000 part /app --fstype="xfs" --ondisk=sda --size=50000 %packages @^gnome-desktop-environment @base @core @desktop-debugging @dial-up @directory-client @fonts @gnome-desktop @guest-agents @guest-desktop-agents @input-methods @internet-browser @java-platform @multimedia @network-file-system-client @networkmanager-submodules @print-client @x11 kexec-tools autofs lftp dhcp vsftpd %end %post #Create repo mkdir /media/cdrom mkdir /etc/yum.repos.d/bak mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak cat > /etc/yum.repos.d/base <<EOF [base] name=centos 7 baseurl=file:///media/cdrom gpgcheck=0 enabled=1 EOF cat >> /etc/fstab << EOF /dev/sr0 /media/cdrom iso9660 defaults 0 0 cat >> /root/.bashrc << EOF alias cdnet="cd /etc/sysconfig/network-scripts/" EOF cat >> /etc/profile.d/env.sh export PS1='\[\e[31m\][\u@\h\w]\$\[\e[0m\]' EOF sed -i 's/quiet/& net.ifnames=0/' /etc/default/grub grub2-config -o /boot/grub2/grub.cfg %end
(e)編輯isolinux.cfg文件[root@localhost fulliso]# vim isolinux/isolinux.cfg
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 Linux 7 menu vshift 8 menu rows 18 menu margin 8 #menu hidden menu helpmsgrow 15 menu tabmsgrow 13 # Border Area # 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 desktop menu label desktop Linux 7 kernel vmlinuz append initrd=initrd.img ks=cdrom:/ks7.cfg label mini menu label mini linux 7 menu default kernel vmlinuz append initrd=initrd.img ks=cdrom:/ks7mini.cfg menu end
(f)製做光盤[root@localhost ~]# mkisofs -R -T -J -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 7 custom" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/centos7.iso /tmp/fulliso/
(g)測試是否可成功,新建一臺虛擬機,測試 出現上述界面,代表製做光盤已經成功。