kickstart全自動安裝Linux

    

    今天晚上在這裏和大家分享一下如何利用kickstart全自動安裝Linux,當然還有別的工具可以全自動的安裝Linux,每個工具都有它自己的優點和缺點,選擇一個適合你的就好了。


    需要的軟件包:nfs*   dhcp*   tftp*

    注:爲了防止意外發生,我們這裏把以nfs dhcp tftp 開頭的所有軟件都裝上。


一、安裝軟件

    先把上面的三個軟件包安裝好

yum install nfs* -y
yum install dhcp* -y
yum install tftp* -y

二、拷貝光盤內容

    在根分區中建立一個目錄,這個目錄是用來存儲從光盤拷貝來的文件。

mkdir /redhatinstall

    掛載光盤,拷貝光盤內容到這個目錄下,其中的 -r ro 表示是以只讀的方式進行掛載。

mount -o ro /dev/cdrom /media
cp -r /media/* /redhatinstall


三、配置nfs服務器

    編輯 nfs 的共享配置文件

echo "/redhatinstall *(rw,sync)" >> /etc/exports

把我們剛剛拷貝進光盤的那個目錄共享出去,*表示任何ip都可以mount這個目錄。


四、配置dhcp服務器

[root@server ~]# cat /etc/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  
#[root@server ~]# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf 
cp: overwrite `/etc/dhcpd.conf'? y
[root@server ~]#

    通過命令我們可以看到,dhcp的默認配置文件中沒有內容,有一個提示說,我們可以參考 /usr/share/doc/dhcp*/dhcpd.conf.sample 這個示例文件。

    所以我們把這個文件拷貝過來,直接覆蓋。

下面我們打開這個文件進行配置

      1 ddns-update-style interim;
      2 ignore client-updates;
      3 
      4 subnet 192.168.21.0 netmask 255.255.255.0 {
      5 
      6 option routers    192.168.21.1;
      7  option subnet-mask   255.255.255.0;
      8 range dynamic-bootp 192.168.21.100 192.168.21.200;
      9 default-lease-time 21600;
     10 
     11  next-server 192.168.21.10;
     12  filename "pxelinux.0";
     13 }

這是我的配置文件,其中 subnet 代表要分配ip所處的網段,option routers 表示默認網關,range dynamic-bootp 表示,分配ip的範圍,next-server 表示要去那個ip服務器讀取文件,filename表示讀取哪個文件。


五、配置TFTP

    修改文件 /etc/xinetd.d/tftp

      1 # default: off
      2 # description: The tftp server serves files using the trivial file transfer \
      3 # protocol.  The tftp protocol is often used to boot diskless \
      4 # workstations, download configuration files to network-aware printers, \
      5 # and to start the installation process for some operating systems.
      6 service tftp
      7 {
      8         socket_type     = dgram
      9         protocol      = udp
     10         wait         = yes
     11         user         = root
     12         server        = /usr/sbin/in.tftpd
     13         server_args     = -s /tftpboot
     14         disable       = yes
     15         per_source      = 11
     16         cps         = 100 2
     17         flags        = IPv4
     18 }

把14行的disable的值改爲 no。

    然後進入  /tftpboot 目錄

[root@server ~]# cd /tftpboot/
[root@server tftpboot]# ls
linux-install
[root@server tftpboot]#

只有一個目錄,這時候我們需要copy幾個文件到這個 tftpboot目錄下面

[root@server tftpboot]# cp /redhatinstall/images/pxeboot/initrd.img .
[root@server tftpboot]# cp /redhatinstall/images/pxeboot/vmlinuz .
[root@server tftpboot]# ls
initrd.img  linux-install  vmlinuz
[root@server tftpboot]#

接着再拷貝一個文件,就是剛剛我們在dhcpd.conf配置文件中配置的  pxelinux.0 文件。

如果不知道  pxelinux.0 這個文件在哪裏,可以用find命令進行查找

[root@server tftpboot]# find / -name "pxelinux.0"
/usr/lib/syslinux/pxelinux.0
[root@server tftpboot]# cp /usr/lib/syslinux/pxelinux.0 .
[root@server tftpboot]# ls
initrd.img  linux-install  pxelinux.0  vmlinuz
[root@server tftpboot]#

我們再建立一個目錄,拷貝一個文件到這個目錄下面:

[root@server tftpboot]# pwd
/tftpboot
[root@server tftpboot]# mkdir pxelinux.cfg
[root@server pxelinux.cfg]# pwd
/tftpboot/pxelinux.cfg
[root@server pxelinux.cfg]# cp /redhatinstall/isolinux/isolinux.cfg default
[root@server pxelinux.cfg]# ls
default
[root@server pxelinux.cfg]# ll
total 4
-r-xr-xr-x 1 root root 366 Mar 29 22:39 default
[root@server pxelinux.cfg]# chmod 777 default 
[root@server pxelinux.cfg]# ll
total 4
-rwxrwxrwx 1 root root 366 Mar 29 22:39 default
[root@server pxelinux.cfg]#

記得修改權限,要不然我們沒法編輯。接下來編輯這個文件 default

      1 default linux
      2 prompt 0
      3 timeout 600
      4 display boot.msg
      5 F1 boot.msg
      6 F2 options.msg
      7 F3 general.msg
      8 F4 param.msg
      9 F5 rescue.msg
     10 label linux
     11    kernel linuz
     12    append ks=nfs:192.168.21.10:/redhatinstall/ks.cfg initrd=initrd.img
     13 
     14 
     15 #label linux
     16 #  kernel vmlinuz
     17 #  append initrd=initrd.img
     18 label text
     19   kernel vmlinuz
     20   append initrd=initrd.img text
     21 label ks
     22   kernel vmlinuz
     23   append ks initrd=initrd.img
     24 label local
     25   localboot 1
     26 label memtest86
     27   kernel memtest
     28   append -
     29

第10-12行是我自己添加進去的。ks=nfs:192.168.21.10:/redhatinstall/ks.cfg 表示我們讀取nfs服務器上的ks.cfg 文件的路徑。


六、拷貝ks.cfg文件

    Linux系統安裝完後我們可以在root的家目錄下看到有一個 anaconda-ks.cfg 文件,我們把這個文件拷貝到 /redhatinstall 目錄下面,並改名爲 ks.cfg。

[root@server ~]# cp anaconda-ks.cfg /redhatinstall/ks.cfg
[root@server ~]# cd /redhatinstall/
[root@server redhatinstall]# ll ks.cfg 
-rw------- 1 root root 1107 Mar 29 22:44 ks.cfg
[root@server redhatinstall]# chmod 777 ks.cfg

把這個問價的權限改爲 777。然後編輯這個文件,下面是我的配置信息,想要看看每個選項是什麼意思的話,可以去這裏看http://www.linuxidc.com/Linux/2013-07/87299.htm

      # Kickstart file automatically generated by anaconda.
      
      install
      text #表示用字符界面進行安裝
      key --skip
      lang en_US.UTF-8
      keyboard us
      xconfig --startxonboot
      network --device eth0 --bootproto dhcp #設置以dhcp的方式獲取ip
      rootpw oracle #密碼設置爲oracle
      firewall --diabled  # 禁用防火牆
      authconfig --enableshadow --enablemd5
      nfs --server=192.168.21.10 --dir=/redhatinstall  #設置nfs方式
     selinux --diabled  #禁用SELinux
     timezone --utc Asia/Shanghai
     bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
     clearpart --linux   #設置分區表
     part /boot --fstype ext3 --size=200
     part swap --size=3000
     part / --fstype ext3 --size=100 --grow
     %packages
     @admin-tools
     @base
     @core
     @dialup
     @editors
     @gnome-desktop
     @graphical-internet
     @graphics
     @legacy-software-support
     @printing
     @system-tools
     @text-internet
     @base-x
     kexec-tools
     fipscheck
     device-mapper-multipath
     sgpio
     emacs
     libsane-hpaio
     festival
     audit
     xorg-x11-utils
     xorg-x11-server-Xnest

配置好這個文件後,我們把需要的服務都啓動起來

[root@server redhatinstall]# 
[root@server redhatinstall]# service dhcpd restart
Starting dhcpd:                                            [  OK  ]
[root@server redhatinstall]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@server redhatinstall]# service nfs restart
Shutting down NFS mountd:                                  [FAILED]
Shutting down NFS daemon:                                  [FAILED]
Shutting down NFS quotas:                                  [FAILED]
Shutting down NFS services:                                [FAILED]
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
[root@server redhatinstall]#

    全部OK,記得啓動dhcp服務的時候,一定保證本機的ip是static的。

這個時候,我們新建一個虛擬機,讓這個虛擬機和剛纔我們配置好的服務器都用橋接模式,我們不掛載光盤,直接啓動。就開始自動安裝了。





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