我需要做一個一鍵安裝centos系統的程序,并且pxe無法使用,因為dhcp服務器不支持pxe指令集,所以只是使用kickstart,但又想智能自動一點,所以使用了網絡安裝方式,有一個好處就是,現在我不使用cdrom了,使用u盤,但是u盤的在linux下的device標簽會有時候變,會在sda和sdb中變化,所以最后還是決定使用網絡安裝系統,由u盤引導,最后就有了這樣一篇文章。
簡述
先說說linux的系統啟動
1. 載入 BIOS 的硬體資訊與進行自我測試,並依據設定取得第一個可開機的裝置;
2. 讀取並執行第一個開機裝置內 MBR 的 boot Loader (亦即是 grub, spfdisk 等程式);
3. 依據 boot loader 的設定載入 Kernel ,Kernel 會開始偵測硬體與載入驅動程式;
4. 在硬體驅動成功後,Kernel 會主動呼叫 init 程式,而 init 會取得 run-level 資訊;
5. init 執行 /etc/rc.d/rc.sysinit 檔案來準備軟體執行的作業環境 (如網路、時區等);
6. init 執行 run-level 的各個服務之啟動 (script 方式);
7. init 執行 /etc/rc.d/rc.local 檔案;
8. init 執行終端機模擬程式 mingetty 來啟動 login 程式,最後就等待使用者登入啦;
引用參考:http://linux.vbird.org/linux_basic/0510osloader.php#startup_intro
然后說說Anaconda
Anaconda是RedHat、CentOS、Fedora等Linux的安裝管理程序。它可以提供文本、圖形等安裝管理方式,并支持Kickstart等腳本提供自動安裝的功能。此外,其還支持許多啟動參數,熟悉這些參數可為安裝帶來很多方便。該程序的功能是把位于光盤或其他源上的數據包,根據設置安裝到主機上。為實現該定制安裝,它提供一個定制界面,可以實現交互式界面供用戶選擇配置(如選擇語言,鍵盤,時區等信息)。Anaconda的大部分模塊用Python編寫,有少許的載入模塊用C編寫。
Anaconda支持的管理模式:
(1)Kickstart提供的自動化安裝;
(2)對一個RedHat實施upgrade;
(3)Rescuse模式對不能啟動的系統進行故障排除。
要進入安裝步驟,需要先有一個引導程序引導啟動一個特殊的Linux安裝環境系統;引導有多種方式:
(1)基于網絡方式的小型引導鏡像,需要提供小型的引導鏡像;
(2)U盤引導,通過可引導存儲介質中的小型引導鏡像啟動安裝過程;
(3)基于PXE的網絡安裝方式,要提供PXE的完整安裝環境;
(4)其他bootloder引導(如GRUB)。
可用的安裝方式:本地CDROM、硬盤驅動器、網絡方式(NFS、FTP、HTTP)。
對于Kickstart,它是一個利用Anconda工具實現服務器自動化安裝的方法。通過生成的kickstart配置文件ks.cfg,服務器安裝可以實現從裸機到全功能服務的的非交互式(無人值守式)安裝配置;ks.cfg是一個簡單的文本文件,文件包含Anconda在安裝系統及安裝后配置服務時所需要獲取的一些必要配置信息(如鍵盤設置,語言設置,分區設置等)。Anconda直接從該文件中讀取必要的配置,只要該文件信息配置正確無誤且滿足所有系統需求,就不再需要同用戶進行交互獲取信息,從而實現安裝的自動化。但是配置中如果忽略任何必需的項目,安裝程序會提示用戶輸入相關的項目的選擇,就象用戶在典型的安裝過程中所遇到的一樣。一旦用戶進行了選擇,安裝會以非交互的方式(unattended)繼續。使用kickstart可以實現流線化自動化的安裝、快速大量的裸機部署、強制建立的一致性(軟件包,分區,配置,監控,安全性)、以及減少人為的部署失誤。
(1)第一階段:加載安裝樹的isolinux目錄下的內核映像vmlinuz和初始RAM磁盤initrd.img,建立安裝環境。initrd.img中的/init程序調用/sbin/loader程序,loader加載kickstart文件,最后運行/usr/bin/anaconda主程序,進入第二階段。
(2)第二階段:anaconda程序加載各python和bash模塊,執行各個安裝步驟
引用參考:http://blog.csdn.net/zhoudaxia/article/details/7172020
再說說一下pxe+dhcp+kickstart+ftp(http,nfs)
他是由dhcp中pxe的指令集分配pxe指令到客戶機,并且客戶機本身網卡支持pxe網絡啟動,這樣的話就可以在分配ip的時候告知客戶機kickstart配置和系統iso資源的位置,這樣就能完成了無人值守的一鍵安裝

最后說說我的dhcp+kickstart+http

第一步是啟動盤引導
第二步是獲取dhcp分配的ip
第三步是訪問web服務器拿到ks.cfg文件
第四步是通過第三步的ks.cfg來訪問web服務器的系統盤iso資源進行安裝
第一次格式化磁盤的時候kickstart并不能識別,所以有warning,按照指示點擊確認即可
配置
主要是這2步。
1.創建一個啟動iso鏡像,并且寫入到光盤或者u盤里面,這個叫光盤引導驅動盤或者u盤引導驅動盤
1.下載原始光盤(centos官網下載,32位,64位,mini32),我這邊選用32位mini
下載完成后掛載到/mnt/下
mount -o loop -t iso9660 /root/CentOS-6.5-i386-minimal.iso /mnt
mkdir /tmp/isomake_dir
cp -arp /mnt/* /tmp/isomake_dir/
2.修改iso啟動配置文件isolinux.cfg(vi /tmp/isomake_dir/isolinux/isolinux.cfg)
default vesamenu.c32
#prompt 1
timeout 600
display boot.msg
menu background splash.jpg
menu title Welcome to CentOS 6.5!
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 xdriver=vesa 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 http
menu http
kernel vmlinuz
append initrd=initrd.img append initrd=initrd.img ks=http://192.168.6.127/centos/isolinux/ks.cfg
主要是增加了
label http #啟動界面的名字,寫http是為了方便識別
menu http
kernel vmlinuz
append initrd=initrd.img append initrd=initrd.img ks=http://192.168.6.127/centos/isolinux/ks.cfg
ks.cfg是后面創建,這里先寫好,這里的意義是在啟動盤啟動引導的時候指定使用的kickstart文件,(isolinux.cfg是centos6系統盤iso的啟動引導菜單,就是安裝彈出的第一個選擇的菜單,增加這個是指定了哪個啟動項對于的啟動動作)然后交給這個kickstart文件進行引導安裝

3.制作啟動iso鏡像
yum -y install mkisofs
cd /tmp/isomake_dir
mkisofs -R -J -T -v -no-emul-boot -boot-load-size 4 -boot-info-table -V "test centos install iso" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /tmp/test.iso
I: -input-charset not specified, using utf-8 (detected in locale settings)
genisoimage 1.1.9 (Linux)
Scanning .
Scanning ./isolinux
Excluded by match: ./isolinux/boot.cat
Scanning ./Packages
Scanning ./repodata
Scanning ./images
Excluded: ./TRANS.TBL
Using RPM_G000.;1 for /RPM-GPG-KEY-CentOS-Debug-6 (RPM-GPG-KEY-CentOS-6)
Using RPM_G001.;1 for /RPM-GPG-KEY-CentOS-6 (RPM-GPG-KEY-CentOS-Security-6)
Using RPM_G002.;1 for /RPM-GPG-KEY-CentOS-Security-6 (RPM-GPG-KEY-CentOS-Testing-6)
Using DEVIC000.RPM;1 for ./Packages/device-mapper-multipath-libs-0.4.9-72.el6.i686.rpm (device-mapper-event-libs-1.02.79-8.el6.i686.rpm)
Using DEVIC001.RPM;1 for ./Packages/device-mapper-event-libs-1.02.79-8.el6.i686.rpm (device-mapper-multipath-0.4.9-72.el6.i686.rpm)
Using DEVIC002.RPM;1 for ./Packages/device-mapper-multipath-0.4.9-72.el6.i686.rpm (device-mapper-1.02.79-8.el6.i686.rpm)
Using LIBSE000.RPM;1 for ./Packages/libselinux-2.0.94-5.3.el6_4.1.i686.rpm (libselinux-utils-2.0.94-5.3.el6_4.1.i686.rpm)
Using FIPSC000.RPM;1 for ./Packages/fipscheck-1.2.0-7.el6.i686.rpm (fipscheck-lib-1.2.0-7.el6.i686.rpm)
Using OPENS000.RPM;1 for ./Packages/openssh-server-5.3p1-94.el6.i686.rpm (openssh-5.3p1-94.el6.i686.rpm)
Using SELIN000.RPM;1 for ./Packages/selinux-policy-3.7.19-231.el6.noarch.rpm (selinux-policy-targeted-3.7.19-231.el6.noarch.rpm)
Using CRACK000.RPM;1 for ./Packages/cracklib-2.8.16-4.el6.i686.rpm (cracklib-dicts-2.8.16-4.el6.i686.rpm)
Using IPTAB000.RPM;1 for ./Packages/iptables-1.4.7-11.el6.i686.rpm (iptables-ipv6-1.4.7-11.el6.i686.rpm)
Using DEVIC003.RPM;1 for ./Packages/device-mapper-1.02.79-8.el6.i686.rpm (device-mapper-libs-1.02.79-8.el6.i686.rpm)
Using E2FSP000.RPM;1 for ./Packages/e2fsprogs-1.41.12-18.el6.i686.rpm (e2fsprogs-libs-1.41.12-18.el6.i686.rpm)
Using CYRUS000.RPM;1 for ./Packages/cyrus-sasl-lib-2.1.23-13.el6_3.1.i686.rpm (cyrus-sasl-2.1.23-13.el6_3.1.i686.rpm)
Using DEVIC004.RPM;1 for ./Packages/device-mapper-libs-1.02.79-8.el6.i686.rpm (device-mapper-persistent-data-0.2.8-2.el6.i686.rpm)
Using KEYUT000.RPM;1 for ./Packages/keyutils-libs-1.4-4.el6.i686.rpm (keyutils-1.4-4.el6.i686.rpm)
Using OPENS001.RPM;1 for ./Packages/openssh-5.3p1-94.el6.i686.rpm (openssh-clients-5.3p1-94.el6.i686.rpm)
Using PLYMO000.RPM;1 for ./Packages/plymouth-0.8.3-27.el6.centos.i686.rpm (plymouth-scripts-0.8.3-27.el6.centos.i686.rpm)
Using PLYMO001.RPM;1 for ./Packages/plymouth-scripts-0.8.3-27.el6.centos.i686.rpm (plymouth-core-libs-0.8.3-27.el6.centos.i686.rpm)
Using CRYPT000.RPM;1 for ./Packages/cryptsetup-luks-1.2.0-7.el6.i686.rpm (cryptsetup-luks-libs-1.2.0-7.el6.i686.rpm)
Using NSS_S000.RPM;1 for ./Packages/nss-softokn-3.14.3-9.el6.i686.rpm (nss-softokn-freebl-3.14.3-9.el6.i686.rpm)
Using P11_K000.RPM;1 for ./Packages/p11-kit-0.18.5-2.el6.i686.rpm (p11-kit-trust-0.18.5-2.el6.i686.rpm)
Using NFS_U000.RPM;1 for ./Packages/nfs-utils-1.2.3-39.el6.i686.rpm (nfs-utils-lib-1.1.5-6.el6.i686.rpm)
Using DEVIC005.RPM;1 for ./Packages/device-mapper-persistent-data-0.2.8-2.el6.i686.rpm (device-mapper-event-1.02.79-8.el6.i686.rpm)
Using NCURS000.RPM;1 for ./Packages/ncurses-libs-5.7-3.20090208.el6.i686.rpm (ncurses-base-5.7-3.20090208.el6.i686.rpm)
Using NCURS001.RPM;1 for ./Packages/ncurses-base-5.7-3.20090208.el6.i686.rpm (ncurses-5.7-3.20090208.el6.i686.rpm)
Using COREU000.RPM;1 for ./Packages/coreutils-libs-8.4-31.el6.i686.rpm (coreutils-8.4-31.el6.i686.rpm)
Writing: Initial PadblockStart Block 0
Done with: Initial PadblockBlock(s)16
Writing: Primary Volume Descriptor Start Block 16
Done with: Primary Volume Descriptor Block(s)1
Writing: Eltorito Volume Descriptor Start Block 17
Size of boot image is 4 sectors -No emulation
Done with: Eltorito Volume Descriptor Block(s)1
Writing: Joliet Volume DescriptorStart Block 18
Done with: Joliet Volume DescriptorBlock(s)1
Writing: End Volume Descriptor Start Block 19
Done with: End Volume Descriptor Block(s)1
Writing: Version block Start Block 20
Done with: Version block Block(s)1
Writing: Path table Start Block 21
Done with: Path table Block(s)4
Writing: Joliet path table Start Block 25
Done with: Joliet path table Block(s)4
Writing: Directory tree Start Block 29
Done with: Directory tree Block(s)27
Writing: Joliet directory tree Start Block 56
Done with: Joliet directory tree Block(s)17
Writing: Directory tree cleanup Start Block 73
Done with: Directory tree cleanup Block(s)0
Writing: Extension recordStart Block 73
Done with: Extension recordBlock(s)1
Writing: The File(s) Start Block 74
3.02% done, estimate finish Sun Jan 18 05:17:14 2015
6.04% done, estimate finish Sun Jan 18 05:17:14 2015
9.06% done, estimate finish Sun Jan 18 05:17:14 2015
12.08% done, estimate finish Sun Jan 18 05:17:14 2015
15.09% done, estimate finish Sun Jan 18 05:17:14 2015
18.11% done, estimate finish Sun Jan 18 05:17:14 2015
21.13% done, estimate finish Sun Jan 18 05:17:14 2015
24.15% done, estimate finish Sun Jan 18 05:17:14 2015
27.16% done, estimate finish Sun Jan 18 05:17:14 2015
30.18% done, estimate finish Sun Jan 18 05:17:14 2015
33.20% done, estimate finish Sun Jan 18 05:17:14 2015
36.22% done, estimate finish Sun Jan 18 05:17:14 2015
39.24% done, estimate finish Sun Jan 18 05:17:14 2015
42.25% done, estimate finish Sun Jan 18 05:17:14 2015
45.28% done, estimate finish Sun Jan 18 05:17:14 2015
48.29% done, estimate finish Sun Jan 18 05:17:14 2015
51.31% done, estimate finish Sun Jan 18 05:17:14 2015
54.33% done, estimate finish Sun Jan 18 05:17:14 2015
57.34% done, estimate finish Sun Jan 18 05:17:14 2015
60.36% done, estimate finish Sun Jan 18 05:17:14 2015
63.38% done, estimate finish Sun Jan 18 05:17:14 2015
66.39% done, estimate finish Sun Jan 18 05:17:14 2015
69.42% done, estimate finish Sun Jan 18 05:17:14 2015
72.43% done, estimate finish Sun Jan 18 05:17:14 2015
75.45% done, estimate finish Sun Jan 18 05:17:14 2015
78.47% done, estimate finish Sun Jan 18 05:17:14 2015
81.49% done, estimate finish Sun Jan 18 05:17:14 2015
84.50% done, estimate finish Sun Jan 18 05:17:14 2015
87.52% done, estimate finish Sun Jan 18 05:17:14 2015
90.54% done, estimate finish Sun Jan 18 05:17:14 2015
93.56% done, estimate finish Sun Jan 18 05:17:14 2015
96.57% done, estimate finish Sun Jan 18 05:17:14 2015
99.60% done, estimate finish Sun Jan 18 05:17:14 2015
Total translation table size: 68712
Total rockridge attributes bytes: 30785
Total directory bytes: 49152
Path table size(bytes): 72
Done with: The File(s) Block(s)165455
Writing: Ending Padblock Start Block 165529
Done with: Ending Padblock Block(s)150
Max brk space used 61000
165679 extents written (323 MB)
這里做的比較簡便,將安裝包也一起打包到iso了,其實我們使用了網絡安裝,安裝包不需要也可以的,只要有linux引導三部曲(vmlinux,initrd,root分區信息)就可以了,不過現在的新系統都比較豐滿了,包裝了很多東西進去,也懶得刪減了,畢竟本身也就幾百M。
2.安裝web 服務器 apache(可以是ftp,nfs)
yum -y install httpd
默認安裝后配置文件在/etc/httpd/conf/httpd.conf,默認使用apache 用戶運行,80端口監聽,網頁存放目錄/var/www/html/
啟動apache(root用戶運行)
service httpd start
創建一個ks.cfg的kickstart配置(demo模板在/root/anaconda-ks.cfg),并放到web服務器apache 的網頁目錄,例如默認的目錄就是/var/www/html/
install
url --url http://192.168.6.127/centos/
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6 --hostname=XXXX
rootpw XXXX
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone --utc Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
clearpart --all --drives=sda
part / --fstype=ext4 --size=7000
part /var/log --fstype=ext4 --size=1024
part swap --size=1024
part /workerfree --fstype=ext4 --size=30 --grow
#repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100
%packages --nobase
@core
%post --interpreter=/bin/bash
(
cd /mnt
/usr/bin/curl -O http://192.168.6.127/XXXX.tar.gz
/bin/tar -zxpf XXXX.tar.gz
cd XXXX
/bin/sh install.sh
)2>&1 >> /root/post-install.log
%end
reboot
注意修改權限755,起碼可以讓apache服務器訪問這個文件
mkdir /var/www/html/centos
chmod 755 /var/www/html/centos/isolinux/ks.cfg

這個位置就是ks=http://192.168.6.127/centos/isolinux/ks.cfg訪問的位置,遙相呼應的
將安裝光盤鏡像iso的掛載的目錄復制到web服務器apache的網頁目錄,達到可以網頁訪問的效果
mkdir /var/www/html/centos
cp -arp /tmp/isomake_dir/* /var/www/html/centos
這里做的目的是網絡安裝,啟動盤讀到kickstart文件后轉到訪問網絡安裝鏡像的位置進行安裝
至此完成配置,即可試驗。
試驗
開機啟動選擇http
然后就一鍵安裝了
科普時間
- mkisofs的參數意義
-r -rational-rock 使用Rock Ridge Extensions,并開放全部文件的讀取權限。
-J -joliet -J或-joliet 使用Joliet格式的目錄與文件名稱。
-b:啟動image
-c boot_catalog #指定啟動的boot catalog
Specifies the path and filename of the boot catalog, which is required for an El Torito bootable CD. The pathname must be relative to the source path specified to genisoimage. This file will be
inserted into the output tree and not created in the source filesystem, so be sure the specified filename does not conflict with an existing file, or it will be excluded. Usually a name like
boot.catalog is chosen.
If -sort has not been specified, the boot catalog sorted with low priority (+1) to the beginning of the medium. If you don’t like this, you need to specify a sort weight of 0 for the boot catalog.
-no-emul-boot Boot image is 'no emulation' image即非模擬模式啟動
-boot-load-size 4 #指定啟動的分區,設置為4是為了兼容某些bios
Specifies the number of "virtual" (512-byte) sectors to load in no-emulation mode. The default is to load the entire boot file. Some BIOSes may have problems if this is not a multiple of 4.
-boot-info-table #指定啟動信息
Specifies that a 56-byte table with information of the CD-ROM layout will be patched in at offset 8 in the boot file. If this option is given, the boot file is modified in the source filesystem, so
make a copy of this file if it cannot be easily regenerated! See the EL TORITO BOOT INFO TABLE section for a description of this table.
有興趣可以自行man mkisofs
- kickstart文件配置
這里我說說我的配置文件
install #安裝模式
url --url http://192.168.6.127/centos/ #使用url模式,也支持nfs,harddisk,cdrom,這里就是說安裝方式
lang en_US.UTF-8 #我這里不需要考慮中文
keyboard us #鍵盤制式
network --onboot yes --device eth0 --bootproto dhcp --noipv6 --hostname=XXXX #配置網絡相關信息
rootpw XXXXX #配置root密碼
firewall --disabled #默認關閉iptables
authconfig --enableshadow --passalgo=sha512 #配置驗證方式,使用sha512加密密碼
selinux --disabled #默認關閉selinux
timezone --utc Asia/Shanghai #時區
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" #引導內容,安裝引導程序grub到mbr,磁盤識別的順序是sda優先,使用auto rhgb quiet的內核啟動參數,該參數rhgb表示redhat graphics boot,就是會看到圖片來代替啟動過程中顯示的文本信息,這些信息在啟動后用dmesg也可以看到,quiet表示在啟動過程中只有重要信息顯示,類似硬件自檢的消息不回顯示
clearpart --all --drives=sda #清理磁盤所有內容,清理的磁盤是sda盤,關于sda和hda,sdb的區別hda一般是指IDE接口的硬盤,hda一般指第一塊硬盤,類似的有hdb,hdc等,sda一般是指SATA接口的硬盤,sda一般指第一塊硬盤,類似的有sdb,sdc等
part / --fstype=ext4 --size=7000 #分區設置
part /var/log --fstype=ext4 --size=1024
part swap --size=1024 #swap配置
part /workerfree --fstype=ext4 --size=30 --grow #--grow代表增長到可以用的最大
#repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100
%packages --nobase #nobase是為了防止彈出的警告說沒有basegroup的組件
@core #mini的iso只有core的group包可以安裝
%post --interpreter=/bin/bash #配置安裝后的動作,這里指定bash作為環境變量和腳本執行工具
(
cd /mnt
/usr/bin/curl -O http://192.168.6.127/XXXX.tar.gz
/bin/tar -zxpf XXXX.tar.gz
cd XXXX
/bin/sh install.sh
)2>&1 >> /root/post-install.log #自定義腳本命令或者命令合集,例如可以下載文件,配置網絡等等,并且將內容的執行日志輸出到指定的日志
%end #結束%post的動作
reboot #安裝完成后自動重啟
~
詳細可以參考官方:
這里也有幾個可以看的翻譯文章,但是怎么說呢,翻譯總是滯后,并且不一定翻譯對,最重要還是要理解原文作者的意思,這樣才能搞懂,以不變應萬變,當然實操試驗才是王道。
參考:http://www.cnblogs.com/xiaOt119/p/3143462.html
參考:http://bbs.linuxtone.org/thread-7539-1-1.html
troubleshooting
Unable to read package metadata. This may be due to missing repodata directory. Please ensure that your install tree has been correctly generated.
Failare:
repodata/743fec56b2af0ce8d6ec82c47a4efafc2a4d18cddfa9683f29611cb18d1a33de-primary.sqlite.bz2 from anaconda-CentOS-201311271240.i386: [Ermo 256] No more mirrors to try.
這里是因為iso經過解壓后將里面的bz或者gz壓縮文件解壓了,導致安裝程序的安裝過程處理失敗,解決辦法就是找一個完整的安裝iso,不要解壓,用iso的掛載方式重新讀取repodata下的文件,然后覆蓋你的系統安裝資源即可
原文鏈接:http://www.godblessyuan.com/2015/01/24/kickstart_http_install_centos_no_pxe/