2013年1月11日

Ubuntu 12.04 安裝 KVM 虛擬化筆記

最近打算在 Ubuntu Server 上增加幾個 Virtual Machine,用於 Jenkins 持續整合(CI, Continuous Integration)測試環境的佈署。因為 CI 在獨立的虛擬機器下運作,比較容易分別設定不同的開發軟體版本,避免太多衝突帶來困擾。

安裝套件

sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils virt-viewer virt-manager bridge-utils

設定 bridged network(讓虛擬機器可以取得對外 IP)

sudo vim /etc/network/interfaces

將對外連線的網路裝置 eth0(或 eth1)改成 br0:

# auto eth0
auto br0

# iface eth0 inet static
iface br0 inet static

保留 eth* 原有的 address、netmask、gateway 等設定,在下方加上 bridge 相關設定:

bridge_ports eth1
bridge_stp off
bridge_fd 0
bridge_maxwait 0

重新開機或重新啟動網路介面:

sudo reboot


sudo /etc/init.d/networking restart

檢查是否有 br0

ifconfig | grep br0

在 Ubuntu Desktop 或 Mac OS X (加裝 XQuartz)使用 ssh -X 連線到 server。

ssh -X user@hostname

第一次使用時,需要先將使用者帳號加入 libvirtd 這個群組,編輯 /etc/group 設定如下:

libvirtd:x:117:使用者帳號

修改群組儲存後,需要重新登入才會生效。

檢查硬體是否支援虛擬化技術

cat /proc/cpuinfo

flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms

如果處理器支援虛擬化,flags 項目會包含 vmx(Intel)或 svm(AMD)標示。

但是處理器支援,並不代表 BIOS 選項有開啟。可以使用 kvm-ok 指令,查詢是否正確啟用。

sudo kvm-ok

如果

sudo kvm-ok

如果 CPU 支援虛擬化,但 BIOS 未啟用相關設定(如 Intel(R) Virtual Technology 設定),則會顯示以下訊息。

INFO: /dev/kvm does not exist
HINT:   sudo modprobe kvm_intel
INFO: Your CPU supports KVM extensions
INFO: KVM (vmx) is disabled by your BIOS
HINT: Enter your BIOS setup and enable Virtualization Technology (VT),
      and then hard poweroff/poweron your system
KVM acceleration can NOT be used

如果 BIOS 已正確啟用硬體支援虛擬化技術,則會顯示以下 ok 訊息。

INFO: /dev/kvm exists
KVM acceleration can be used

使用 virt-manager 管理虛擬機器

透過 X11 forwarding 的 SSH 連線到 server 執行 virt-manager,或是在 Ubuntu Desktop 安裝 virt-manager 並設定 remote connection。


預設的儲存方式是 Image-based,也就是在現有的磁碟系統上增加磁碟映像檔;如果需要更好的磁碟效能,則可以用 LVM。

利用 system-config-lvm 簡化 LVM 設定。

sudo apt-get install system-config-lvm


在 virt-manager 的 Edit / Connection Details / Storage 可以增加 LVM Storage 設定。

使用 virt-manager 建立新的虛擬機器,開始安裝作業系統。


接下來,Linux Server 一台可以當多台用了 : )

參考資料

http://sealmemory.blogspot.tw/2012/03/ubuntu-linux-kvm.html
http://www.howtoforge.com/virtualization-with-kvm-on-ubuntu-12.04-lts
http://vitobotta.com/setting-up-kvm-lvm-virtual-machine-host/
http://www.linux-kvm.org/page/Networking

沒有留言:

張貼留言

lyhcode by lyhcode
歡迎轉載,請務必註明出處!