歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> KVM創建虛擬機後指定啟動腳本

KVM創建虛擬機後指定啟動腳本

日期:2017/2/28 15:29:03   编辑:Linux教程

在vmbuilder命令中創建vm時,可以通過參數虛擬機第一次啟動的時候執行的腳本文件。但是由於這個時候虛擬機網絡可能還不通,必須要延遲一會兒,才能保證一些apt-get install命令能夠順利執行。

下面的腳本是我常用的,貢獻出來:

boot.sh文件內容:

  1. # Set time zone
  2. cp /usr/share/zoneinfo/Asia/Harbin /etc/localtime
  3. # Set proxy server
  4. echo 'Acquire::http::Proxy "http://10.112.18.178:3142";' >> /etc/apt/apt.conf
  5. while (! ping -c 1 www.baidu.com); do sleep 1; done
  6. echo 'apt-get install acpid' >> /opt/x
  7. apt-get install acpid

第一行設置時區

第二行設置代理

第三行等待ping通www.baidu.com

後面安裝acpid

vmbuilder的參數添加:

--firstboot=/var/lib/libvirt/images/$1/boot.sh

我原來測試腳本是bash,但是不能在boot.sh中執行,奇怪,不過先放在這裡,以後還有用。

  1. # Test Internet connection is ok or not
  2. # If failed 10 times, exit
  3. # Return immediately if network is ok
  4. i=0
  5. count=10
  6. while [ $i -lt $count ]
  7. do
  8. echo "testing"
  9. let i++
  10. ping -c2 www.baidu.com > /dev/null
  11. r=$?
  12. echo $r
  13. if [ $r -ne 0 ]
  14. then
  15. echo 'network is down'
  16. sleep 10
  17. else
  18. echo 'network is up'
  19. let i=count+1
  20. fi
  21. done
Copyright © Linux教程網 All Rights Reserved