對虛擬化的支持一般在BIOS中是禁掉的,必須開啓才能夠。es6
對於Intel CPU,咱們能夠經過下面的命令查看是否支持虛擬化。ubuntu
# grep "vmx" /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 pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca 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 ermscentos
對於AMD CPU,則執行下面的命令api
grep "svm" /proc/cpuinfo網絡
KSM: Kernel Same Page Mergingapp
Kernel SamePage Merging (KSM) (also: Kernel Shared Memory, Memory Merging) lets the hypervisor system share identical memory pages amongst different processes or virtualized guests.ssh
This is done by scanning through the memory finding duplicate pages. The duplicate pair is then merged into a single page, and mapped into both original locations. The page is also marked as "copy-on-write", so the kernel will automatically separate them again should one process modify its data.ide
KSM was originally intended to run more virtual machines on one host by sharing memory between processes as well as virtual machines.oop
查看是否支持flex
# cat /boot/config-3.13.0-27-generic | grep KSM
CONFIG_KSM=y
查看是否enable
# cat /sys/kernel/mm/ksm/run
0
這是disable,設爲1則爲enable
在ubuntu下面安裝
apt-get install kvm qemu-kvm
KVM kernel modules
# lsmod | grep kvm
kvm_intel 143060 27
kvm 451511 1 kvm_intel
能夠經過以下的命令加載內核模塊
modprobe kvm
modprobe kvm_intel
dmesg命令用於打印Linux系統開機啓動信息,kernel會將開機信息存儲在ring buffer中。您如果開機時來不及查看信息,可利用dmesg來查看(print or control the kernel ring buffer)。開機信息亦保存在/var/log/dmesg的文件裏。某些硬件設備(好比七號信令卡、語音卡之類)在安裝的時候,一般會安裝驅動程序(內核模塊),會打印一些信息,就能夠經過dmesg命令來查看。
查看啓動的時候是否Load了kvm
dmesg | grep kvm
要建立一個虛擬機須要下面的過程:
qemu-img create -f qcow2 /tmp/centos5.8.img 10G
virt-install --virt-type qemu --name centos-5.8 --ram 2048 --noreboot --disk path=/tmp/centos5.8.img,format=qcow2,bus=virtio,device=disk --cdrom=/tmp/CentOS-5.8-x86_64-bin-DVD-1of2.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
qemu-img convert -O qcow2 -c centos-5.8.img centos-5.8.1.qcow2
使用qemu
qemu-img create -f qcow2 ubuntutest.img 5G
# qemu-system-x86_64 -enable-kvm -m 2048 -hda ubuntutest.img -cdrom ubuntu-14.04-server-amd64.iso -boot d
Could not initialize SDL(No available video device) - exiting
SDL(Simple DirectMedia Layer)是一個用C語言編寫的、跨平臺的、免費和開源的多媒體程序庫,它提供了一個簡單的接口用於操做硬件平臺的圖形顯示、聲音、輸入設備等。
在QEMU模擬器中的圖形顯示默認就是使用SDL的。
SDL的功能很好用,也比較強大,不過它也有一個侷限性,就是在建立客戶機並以SDL方式顯示時,它會直接彈出一個窗口,因此SDL方式只能在圖形界面中使用。若是在非圖形界面中(如ssh鏈接到宿主機中),使用SDL時會出現以下的錯誤信息。
因爲咱們是在ssh模式下,因此要關閉SDL,使用vnc
# qemu-system-x86_64 -enable-kvm -name ubuntutest -m 2048 -hda ubuntutest.img -cdrom ubuntu-14.04-server-amd64.iso -boot d -vnc :19
安裝完了後,就能夠從硬盤啓動了
固然上面的啓動是沒法聯網的。因此咱們還須要配置網絡。
qemu的網絡配置有如下幾種:
咱們一般能夠經過下面的方法定義一張虛擬網卡
-net nic,vlan=1,macaddr=00:16:35:AF:94:4B,model=virtio,name=ncard1
vlan=1是虛擬網絡的id,若是不指定則默認爲0,這個虛擬網絡和IEEE 802.1Q徹底兩回事。是指qemu本身定義的網絡而已。
能夠指定mac地址
model能夠查看
# qemu-system-x86_64 -enable-kvm -net nic,model=?
qemu: Supported NIC models: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio
配置User Networking (SLIRP)
This is the default networking backend and generally is the easiest to use. It does not require root / Administrator privileges. It has the following limitations:
qemu-system-x86_64 -enable-kvm -name ubuntutest -m 2048 -hda ubuntu-14.04.img -boot c -vnc :19 -net user -net nic,model=virtio
The VM Guest allocates an IP address from a virtual DHCP server. VM Host Server (the DHCP server) is reachable at 10.0.2.2, while the IP address range for allocation starts from 10.0.2.15. You can use ssh to connect to VM Host Server at 10.0.2.2, and scp to copy files back and forth.
配置Bridge Networking
With the -net tap option, QEMU creates a network bridge by connecting the host TAP network device to a specified VLAN of VM Guest. Its network interface is then visible to the rest of the network.
bridge=br0
tap=$(sudo tunctl -u $(whoami) -b)
sudo ip link set $tap up
sudo brctl addif $bridge $tap
qemu-kvm -m 512 -hda /images/sles11sp1_base.raw
-net nic,vlan=0,model=virtio,macaddr=00:16:35:AF:94:4B
-net tap,vlan=0,ifname=$tap,script=no,downscript=no
sudo brctl delif $bridge $tap
sudo ip link set $tap down
sudo tunctl -d $tap
(1) 在Host機器上建立bridge br0
brctl addbr br0
(2) 將br0設爲up
ip link set br0 up
(3) 建立tap device
# tunctl -b
tap0
(4) 將tap0設爲up
ip link set tap0 up
(5) 將tap0加入到br0上
brctl addif br0 tap0
(6) 啓動虛擬機
qemu-system-x86_64 -enable-kvm -name ubuntutest -m 2048 -hda ubuntu-14.04.img -boot c -vnc :19 -net nic,model=virtio -net tap,ifname=tap0,script=no,downscript=no
虛擬機鏈接tap0,tap0鏈接br0
(7) 虛擬機啓動後,網卡沒有配置,因此沒法鏈接外網,先給br0設置一個ip
ifconfig br0 192.168.57.1/24
(8) 在虛擬機裏面,給網卡設置地址
ifconfig eth0 192.168.57.2/24
這個時候在虛擬機裏面能夠ping的通192.168.57.1了,可是仍是沒法訪問外網
(9) 在Host上設置NAT,而且enable ip forwarding
# sysctl -p
net.ipv4.ip_forward = 1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
(10) 在虛擬機裏面設置默認網關
route add –net default gw 192.168.57.1
這個時候,能夠ping的通外網網關了
ping 16.158.164.1
(11) 而後在虛擬機裏面設置dns,則能夠進行apt-get
# cat /etc/resolv.conf # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTENnameserver 16.110.135.52nameserver 16.110.135.51