Ubuntu-server Network Setting Of The Automated Installation

在RHEL/CentOS、VMWare ESXI與SLES的自動安裝系統流程中,都有pre與post階段。Ubuntu的kickstart檔案,也提供了pre與post區塊;但Ubuntu與其它distribution最大差異在於: pre階段並沒載入網卡驅動。它的流程像這樣:

  1. Run pre script。
  2. Detect HW。
  3. Install OS。
  4. Run post script。

在Detect HW階段,會去偵測並設定網卡。

當你有兩張以上網卡且都可以連線的時候,安裝就會停住,並要求你選一張:(圖片來自於link)

原先我是在preseed檔案中宣告了:

d-i netcfg/choose_interface select auto
但它卻很惱殘的選第一張且無法連線的網卡。後來爬文發現這是一個bug,所以改在isolinux.cfg或grub.cfg中串入以下參數:
netcfg/choose_interface=auto
以isolinux.cfg為例:
append file=/cdrom/example.seed  initrd=/install/initrd.gz ks=cdrom:/ks.cfg netcfg/choose_interface=auto
如此就會自動挑一張可以用的或者是你在kickstart檔案中所宣告的device名稱。

在RHEL/CentOS中,面對多張需要被設定的網卡,你可以在kickstart檔案中,透過多個network指令與–device參數去完成設定。而在Ubuntu上,即使你設定了多個network指令,也只有最後一個會生效。這意味著以下的內容只有em2會在第二階段被設定:

network --bootproto=dhcp --device=em1
network --bootproto=dhcp --device=em2
這個問題我目前的解決方式是在%post的區塊去設定網卡。我提供設定dhcp的方式給大家參考:
%post --interpreter=/bin/bash 
FILE_NET_CONFIG=/etc/network/interfaces
 
network_devices=($(ls -I lo /sys/class/net))
for device in ${network_devices[@]};
do
	check_config=`cat $FILE_NET_CONFIG | grep $device`
	if [ ! "$check_config" ]; then
		echo auto $device >> $FILE_NET_CONFIG
		echo iface $device inet dhcp >> $FILE_NET_CONFIG
 
		dhclient -r $device
		dhclient $device
 
		ifconfig $device down
 
		status="up";
		until [ -z "$status" ]
		do
			status=$(ifconfig |grep -e '^$device ')
			sleep 1
		done
 
		ifconfig $device up
	fi
done
設定static ip的方式等到我有做再分享。