advertisement

2016年2月19日

QEMU Installation

QEMU Installation

Step1.檢查是否支援硬體虛擬化

$ grep "vmx" /proc/cpuinfo
如果有輸出表示處理器型態為Intel VT
$ grep "SVM" /proc/cpuinfo
如果有輸出表示處理器型態為AMD-V


如果都沒有輸出表示不支援硬體虛擬化

Step2.檢查Linux kernel KSM是否有開啟

$ grep KSM /boot/config-`uname -r`
CONFIG_KSM=y


沒有請重編Linux kernel

Step3.檢查ksm是否有啟動

$ cat /sys/kernel/mm/ksm/run  (ksm可能為大小或小寫)
0
輸出0代表沒啟動,為了讓重開機能自動啟動ksm,我們將下令指令輸入至/etc/rc.local
echo 1 > /sys/kernel/mm/ksm/run
# vi /etc/rc.local


A fine tuning is done by modifying the files in the / sys / kernel / mm / KSM /.
On Debian / Ubuntu, this configuration is done in the / etc / default / qemu-kvm file.
 # / Etc / default / qemu-kvm
# To disable qemu-kvm's page merging feature, set KSM_ENABLED = 0 and
# Sudo restart qemu-kvm
KSM_ENABLED = 1
# SLEEP_MILLISECS = 2000

NOTE:搜尋套件方式

  1. dpkg -S file_name # 搜尋file_name所屬 package
  2. apt-cache search package_name # 從套件資訊中查詢包含 package_name的套件

Step4.安裝qemu所需要的套件

安裝額外的套件

apt-get install git libglib2.0-dev libfdt-dev libpixman-1-dev

安裝必要的套件

apt-get install build-essential gcc make wget libdirectfb-dev libjpeg-dev zlib1g-dev libsdl-gfx1.2-dev gcc libsdl1.2-dev libasound2-dev pkg-config libpci-dev dh-autoreconf

安裝qemu官方建議的套件

依自己需求選擇所要的套件,可透過./configure的訊息得知哪些功能被開啟或關閉(yes/no),若未開啟請安裝該功能所需要的套件,底下是官方建議的套件,非必要性。
apt-get install git-email libaio-dev libbluetooth-dev libbrlapi-dev libbz2-dev libcap-dev libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev libibverbs-dev libjpeg62-turbo-dev libncurses5-dev libnuma-dev librbd-dev librdmacm-dev libsasl2-dev libsdl1.2-dev libseccomp-dev libsnappy-dev libssh2-1-dev libvde-dev libvdeplug-dev libvte-2.90-dev libxen-dev liblzo2-dev valgrind xfslibs-dev libnfs-dev libiscsi-dev
--------------------------------------------------------------------------------------
apt-get build-dep qemu-kvm kvm
Linux 核心虛擬機器,使Linux cpu可以虛擬化

Step5.下載source code

# cd /usr/local/
# git clone git://git.qemu-project.org/qemu.git
Cloning into 'qemu'...
remote: Counting objects: 232086, done.
remote: Compressing objects: 100% (46740/46740), done.
remote: Total 232086 (delta 186161), reused 230268 (delta 184698)
Receiving objects: 100% (232086/232086), 75.15 MiB | 6.11 MiB/s, done.
Resolving deltas: 100% (186161/186161), done.
Checking connectivity... done.

Step6.設定與編譯QEMU

# cd qemu
# mkdir -p bin/debug/native
# cd bin/debug/native
# ../../../configure --enable-debug
# ./configure --enable-debug
# make -j8

Step7.安裝QEMU與確認版本

# make install
$ qemu-system-x86_64 --version
QEMU emulator version 2.3.90, Copyright (c) 2003-2008 Fabrice Bellard


QEMU SETUP

建立虛擬映像檔

$ qemu-img create win7.img 120G
Formatting 'win7.img', fmt=raw size=128849018880
$ qemu-img create -f raw win7.raw 120G
Formatting 'win7.raw', fmt=raw size=128849018880
-f : 檔案格式


$ qemu-system-x86_64 -m 1024 -cpu host -smp cores=8,threads=8 -boot d -drive file=win7.raw,if=virtio,index=0,media=disk,format=raw -drive file=/opt/win7.iso,index=1,media=cdrom -net nic,model=virtio,macaddr=52:54:00:05:11:11 -net user


#!/bin/bash
qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp cores=8 -boot d \
-drive file=/home/clementyan/qemu/win7.raw,index=0,media=disk,format=raw \
-drive file=/opt/win7.iso,index=2,media=cdrom \
-net nic,model=virtio,macaddr=52:54:00:05:11:11 -net user -vga qxl

寫成script執行檔

(bash語法暫且不在此討論)


$ vi buildwin7.sh
將下列內容寫進去
#!/bin/bash
qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp cores=8 -boot c \
-drive file=/home/clementyan/qemu/win7.raw,index=1,media=disk,format=raw \
-drive file=/opt/win7.iso,index=3,media=cdrom \
-net user -net nic,macaddr=52:54:00:05:11:11 \
-vga qxl


QEMU參數說明:
-enable-kvm 開啟KVM才可使用-cpu host
-m 記憶體大小
-cpu
host 用實體cpu來虛擬化
-smp
coures 核心數量
-boot
d 用光碟開機
c 用硬碟開機
n 用網路開機
-drive
file 檔案路徑
index 設定硬碟或光碟機順序
media 設定光碟或硬碟(cdrom/disk)
format 檔案格式
-net nic 設定網路卡資訊
model 選擇網路卡型號
macaddr設定網卡的mac
user 硬體網路的pass through
-net user 給予權限連至實體網路

執行虛擬機

# vi /etc/group
kvm:x:121:username
$ sh buildwin7.sh

CPU PASSTHROUGH

  1. 裝kvm套件
  2. -enable-kvm加入此參數
  3. 確認執行使用者有加入kvm群組
  4. ./configure時KVM support=yes
  5. -cpu host加入此參數
擷取選取區域_004.png

USB PASSTHROUGH

觀察電腦USB硬體資訊

我們要讀取我的Seagate外接硬碟
$ lsusb
Bus 004 Device 003: ID 0461:4de2 Primax Electronics, Ltd
Bus 004 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
[Sakar]
Bus 003 Device 004: ID 0bc2:2320 Seagate RSS LLC USB 3.0 bridge [Portable Expansion Drive]
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub


0bc2:2320
Vendor ID=0x0bc2
Product ID=0x2320

加入下列參數

-usb -device usb-host,hostbus=3,hostaddr=4
-usb -usbdevice host:3.4


但此參數可能只支援USB 1.0(待查),在windows xp guset o.s.可以正常驅動,而在windows7 guset o.s.雖然可以偵測到usb,但無法正常驅動。

設定USB2.0



QEMU參數說明:
-usb
-device
usb-uhci usb 1.0
usb-ehci usb 2.0
usb-xhci usb 3.0
usb-host,vendorid=0x0bc2,productid=0x2320
-usbdevice
tablet 使滑鼠可以直接穿過虛擬機

修改script檔案

#!/bin/bash
qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp cores=8 -boot c \
-drive file=/home/clementyan/qemu/win7.raw,index=1,media=disk,format=raw \
-drive file=/opt/win7.iso,index=3,media=cdrom \
-net user -net nic,macaddr=52:54:00:05:11:11 \
-vga qxl \
-usb \
-device usb-ehci \
-device usb-host,vendorid=0x0951,productid=0x1624 \
-device usb-host,vendorid=0x0bc2,productid=0x2320
#-usbdevice tablet \


擷取選取區域_006.png

libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/003/004: 拒絕不符權限的操作
因為我只是要暫時讀取隨身碟,所以這邊暫時用最高權限執行QEMU


Note
error message:指-cpu host用硬體CPU來虛擬化
qemu-system-x86_64: CPU model 'host' requires KVM
1.configure時KVM support=no
2.少了-enable--kvm的參數
3.使用者的group裡面沒有kvm
4.沒裝kvm套件


測試階段
VGA PASSTHROUGH
$ egrep -q '^flags.*(svm|vmx)' /proc/cpuinfo && echo virtualization extensions available
virtualization extensions available
$ cat /boot/config-`uname -r` |grep CONFIG_VFIO_PCI_VGA
CONFIG_VFIO_PCI_VGA=y


SPICE PORTOCOL REMOTE
在build.sh加入
-spice port=5930,password=0104


並執行build.sh


在要操作的pc上輸入透過spice potocol去遠端控制虛擬機
$ spicec -h 140.118.18.36 -p 5930 -w 0104

沒有留言:

張貼留言

文章有誤或有問題麻煩您留言告知! 謝謝您~~