在vmbuilder命令中創建vm時,可以通過參數虛擬機第一次啟動的時候執行的腳本文件。但是由於這個時候虛擬機網絡可能還不通,必須要延遲一會兒,才能保證一些apt-get install命令能夠順利執行。
下面的腳本是我常用的,貢獻出來:
boot.sh文件內容:
- # Set time zone
- cp /usr/share/zoneinfo/Asia/Harbin /etc/localtime
- # Set proxy server
- echo 'Acquire::http::Proxy "http://10.112.18.178:3142";' >> /etc/apt/apt.conf
- while (! ping -c 1 www.baidu.com); do sleep 1; done
- echo 'apt-get install acpid' >> /opt/x
- apt-get install acpid
第一行設置時區
第二行設置代理
第三行等待ping通www.baidu.com
後面安裝acpid
vmbuilder的參數添加:
--firstboot=/var/lib/libvirt/images/$1/boot.sh
我原來測試腳本是bash,但是不能在boot.sh中執行,奇怪,不過先放在這裡,以後還有用。
- # Test Internet connection is ok or not
- # If failed 10 times, exit
- # Return immediately if network is ok
- i=0
- count=10
- while [ $i -lt $count ]
- do
- echo "testing"
- let i++
- ping -c2 www.baidu.com > /dev/null
- r=$?
- echo $r
- if [ $r -ne 0 ]
- then
- echo 'network is down'
- sleep 10
- else
- echo 'network is up'
- let i=count+1
- fi
- done