如何通過PXE遠程安裝linux

操作步驟
 
1 安裝tftp和dhcp服務器
# rpm -ivh tftp-server-0.39-0.EL3.1.c0.i386.rpm
# rpm -ivh dhcp-3.0.1-10_EL3.i386.rpm
 
2 配置tftp和dhcp服務器
# vi /etc/xinetd.d/tftp
=============+==========+===========+============+============
service tftp
{
        disable = no
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -u nobody -s /tftpboot
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
=============+==========+===========+============+============
 
# cp /usr/share/doc/dhcp-3.0.1/dhcpd.conf.sample /etc/dhcpd.conf
# vi /etc/dhcpd.conf
=============+==========+===========+============+============
option domain-name "systeng.net";
default-lease-time 6000;
max-lease-time 11400;
authourtative;
ddns-update-style ad-hoc;
log-facility local7;
 
subnet 192.168.10.0 netmask 255.255.255.0{
range 192.168.10.150 192.168.10.155;
option domain-name-servers 202.99.160.68;
option domain-name "systeng.net";
option netbios-name-servers 192.168.10.1;
option routers 192.168.10.1;
option broadcast-address 192.168.10.255;
default-lease-time 6000;
max-lease-time 11400;
filename "/pxelinux.0";
}
=============+==========+===========+============+============
 
3 配置支持PXE
# mkdir /tftpboot
# cp /usr/lib/syslinux/pxelinux.0 /tftpboot
把LINUX第一張安裝光盤上/image/pxeboot/initr.img和vmlinux 以及isolinux/*.msg考到/tftpboot目錄下
# cd /tftpboot
# mkdir pxelinux.cfg
# vi default
=============+==========+===========+============+============
default
prompt 1
timeout 30
display boot.msg
F1 boot.msg
F2 options.msg
F3 general.msg
F4 param.msg
F5 rescue.msg
F7 snake.msg
label local
  localboot 0
label linux
  kernel vmlinuz
  append initrd=initrd.img devfs=nomount ramdisk_size=9216
label text
  kernel vmlinuz
  append initrd=initrd.img text devfs=nomount ramdisk_size=9216
label expert
  kernel vmlinuz
  append expert initrd=initrd.img devfs=nomount ramdisk_size=9216
label ks
  kernel vmlinuz
  append ks initrd=initrd.img devfs=nomount ramdisk_size=9216
label nofb
  kernel vmlinuz
  append initrd=initrd.img devfs=nomount nofb ramdisk_size=9216
label lowres
  kernel vmlinuz
  append initrd=initrd.img lowres devfs=nomount ramdisk_size=9216
kernel vmlinuz
=============+==========+===========+============+============
 
4 配置NFS服務
# vi /etc/exports
/home/iso/      192.168.10.0/24(ro,sync)
# mkdir /home/iso
拷貝安裝LINUX的ISO文件到這個目錄
 
5 需要開啓的系統服務
/dhcpd/network/nfs/portmap/syslog/tftp/xinetd/
 
6 硬件需求
網卡上需要添加PXE遠程啓動芯片
 
 
 
附: 如何遠程安裝Linux
作者:湯凱   
本文選自:IBM DW中國  2002年12月06日
 
一般情況下,我們都是利用軟驅或光驅引導Linux後,再通過本地的光驅安裝Linux。但是,這種安裝方法在以下的幾種情況下就不能適用:
 
· 無軟驅和光驅:很多公司爲了節省成本,計算機一般都不帶光驅或軟驅,這樣就無法通過本地安裝Linux;
 
· 非標準的軟驅和光驅:雖然筆記本都會配置光驅,但是並不一定都是標準的IDE設備,有些是通過USB接口,有些是通過1394接口(例如Samsung的 Q10)。在Linux安裝時所引導的Linux內核一般都不會帶這些接口的驅動,所以也無法通過本地安裝Linux;
 
另外,在一些場合,如機房中,有大量的計算機需要同時安裝Linux,如果通過光驅的方式一個個安裝,不僅效率低,也不利於維護。
 
筆者在工作過程中,就遇到過第二種情況。一臺Samsung的Q10筆記本需要安裝Redhat Linux 8.0,但是通過光驅引導後發現,安裝程序無法訪問光盤。針對這個問題,筆者經過查閱資料和摸索,找到了在Q10上安裝Linux的方法。在下面的討論中,如不做特別聲明,都將以Q10爲例,介紹如何通過PXE Bootrom來遠程安裝Linux。
 
基本原理
 
什麼是PXE
 
PXE(Pre-boot Execution Environment)是由Intel設計的協議,它可以使計算機通過網絡啓動。協議分爲client和server兩端,PXE client在網卡的ROM中,當計算機引導時,BIOS把PXE client調入內存執行,並顯示出命令菜單,經用戶選擇後,PXE client將放置在遠端的操作系統通過網絡下載到本地運行。
 
PXE協議的成功運行需要解決以下兩個問題:
 
既然是通過網絡傳輸,那麼計算機在啓動時,它的IP地址由誰來配置;
 
通過什麼協議下載Linux內核和根文件系統
 
對於第一個問題,可以通過DHCP Server解決,由DHCP server來給PXE client分配一個IP地址,DHCP Server是用來給DHCP Client動態分配IP地址的協議,不過由於這裏是給PXE Client分配IP地址,所以在配置DHCP Server時,需要增加相應的PXE特有配置。
 
至於第二個問題,在PXE client所在的ROM中,已經存在了TFTP Client。PXE Client使用TFTP Client,通過TFTP協議到TFTP Server上下載所需的文件。
 
這樣,PXE協議運行的條件就具備了,下面我們就來看看PXE協議的工作過程。
 
工作過程
 
在上圖中,PXE client是需要安裝Linux的計算機,TFTP Server和DHCP Server運行在另外一臺Linux Server上。Bootstrap文件、配置文件、Linux內核以及Linux根文件系統都放置在Linux Server上TFTP服務器的根目錄下。
 
PXE client在工作過程中,需要三個二進制文件:bootstrap、Linux 內核和Linux根文件系統。Bootstrap文件是可執行程序,它向用戶提供簡單的控制界面,並根據用戶的選擇,下載合適的Linux內核以及Linux根文件系統。
 
步驟
 
有了前面的背景知識,接下來就可以正式操作了,下面按照順序給出了操作步驟:
 
配置DHCP Server
 
選用ISC dhcp-3.0,DHCP Server的配置文件是/etc/dhcpd.conf,配置文件的內容如下:
 
option space PXE;
option PXE.mtftp-ip               code 1 = ip-address;
option PXE.mtftp-cport            code 2 = unsigned integer 16;
option PXE.mtftp-sport            code 3 = unsigned integer 16;
option PXE.mtftp-tmout            code 4 = unsigned integer 8;
option PXE.mtftp-delay            code 5 = unsigned integer 8;
option PXE.discovery-control      code 6 = unsigned integer 8;
option PXE.discovery-mcast-addr   code 7 = ip-address;
class “pxeclients” {
match if substring (option vendor-class-identifier, 0, 9) = “PXEClient”;
option vendor-class-identifier “PXEClient”;
vendor-option-space PXE;
# At least one of the vendor-specific PXE options must be set in
# order for the client boot ROMs to realize that we are a PXE-compliant
# server.  We set the MCAST IP address to 0.0.0.0 to tell the boot ROM
# that we can’t provide multicast TFTP (address 0.0.0.0 means no
# address).
option PXE.mtftp-ip 0.0.0.0;
# This is the name of the file the boot ROMs should download.
filename “pxelinux.0″;
# This is the name of the server they should get it from.
next-server 192.168.0.1;
}
ddns-update-style interim;
ignore client-updates;
default-lease-time 1200;
max-lease-time 9200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.0.255;
option routers 192.168.0.254;
option domain-name-servers 192.168.0.1,192.168.0.2;
option domain-name “mydomain.org”;
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.100;
}
host q10 {
hardware ethernet 00:00:F0:6B:38:5B;
fixed-address 192.168.0.22;
}
 
dhcpd.conf配置文件中幾個關鍵部分說明如下:host q10{…}定義了筆記本Q10網卡的MAC地址與IP地址的對應關係,表明DHCP Server爲Q10分配一個固定的IP:192.168.0.22;filename “”指定bootstrap的文件名;netx-server指定TFTP Server的地址。其它的配置請讀者參考DHCP Server的手冊。
 
配置TFTP server
 
選用tftp-hpa,TFTP Server的配置文件是/etc/xinetd.d/tftp,配置文件的內容如下:
 
service tftp
{
socket_type             = dgram
protocol                = udp
wait                    = yes
user                    = root
server                  = /usr/sbin/in.tftpd
server_args             = -u nobody -s /tftpboot
disable                 = no
per_source              = 11
cps                     = 100 2
}
 
這裏制定了/tftpboot爲TFTP Server的根目錄位置。
 
配置bootstrap
 
bootstrap 文件在dhcpd.conf中被指定爲pxelinux.0文件,放置在/tftpboot。Linux內核以及Linux根文件系統也放置在 /tftpboot。pxelinux.0在執行過程中,要讀配置文件,所有的配置文件都放在/tftpboot/pxelinux.cfg/目錄下。由於PXElinux具有爲不同的PXE Client提供不同的Linux內核以及根文件系統的功能,所以要通過不同的配置文件名來區分出不同的PXE Client的需求。比如一個PXE Client由DHCP Server分配的IP地址爲192.168.0.22,那麼相對應的配置文件名爲/tftpboot/pxelinux.cfg/C0A80016 (注:C0A80016爲IP地址192.168.0.22的十六進制表示)。如果找不到,就按照順序C0A80016->; C0A8001->; C0A800->; C0A80->; C0A8->; C0A->; C0->; C->;default查找配置文件。
 
/tftpboot/pxelinux.cft/C0A80001配置文件的具體內容如下:
 
DEFAULT install
PROMPT 1
LABEL install
KERNEL vmlinuz
APPEND initrd=initrd.img devfs=nomount ramdisk_size=16384
 
此配置文件指定了Linux內核以及根文件系統的名稱,並給內核傳遞了一些參數,其中ramdisk_size參數要非常注意,它指定Linux內核啓動後建立ramdisk的大小,如果設置太小,Linux的安裝過程就可能無法進行。
 
製作Linux內核/根文件系統
 
因 爲需要通過網絡安裝,所有選擇Redhat Linux 8.0安裝盤(disk #1)中E:/images/bootnet.img(光驅的盤符爲E:)。bootnet.img包括Linux內核、Linux根文件系統(有安裝程序在內)。用bootnet.img製作一張引導軟盤,方法在DOS命令行運行E:/dosutils/rawrite E:/images/bootnet.img,根據提示製作。製作完畢後,將引導軟盤中的vmlinuz(Linux內核)和initrd.img (Linux根文件系統)拷貝到Linux Server的/tftpboot下。
 
啓動DHCP Server/TFTP Server
 
在Linux Server上,運行service dhcpd start和service xinetd restart。
 
啓動Q10
 
Q10加電後,在出現Samsung公司徽標時,在左下角會提示用戶按F12進入網絡引導。按F12後,Q10進入網絡引導過程。首先通過DHCP Server獲得了IP地址,然後下載並執行bootstrap文件pxelinux.0;在執行中,讀入配置文件 /tftpboot/pxelinux.cfg/C0A80016。此時屏幕上出現boot:,敲入install,就進入了Redhat Linux 8.0的網絡安裝界面,一切OK!
 
結束語
 
雖然以上的討論是針對Samsung Q10筆記本,但是工作原理和安裝方法對於其它類型的計算機也是適用的,不過要記住,計算機的網卡中必須要有BootROM–PXE Client。同時,PXE協議也可作爲無盤Linux技術,用於引導放置在遠程的Linux。實際上,只要根據需要,在Pxelinux的配置文件的 APPEND選項中,爲Linux內核傳進需要mount的文件系統的位置參數即可。
 
關於作者
 
湯凱,解放軍理工大學指揮自動教研室講師。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章