首頁 > 軟體

在Ubuntu上安裝KVM並搭建虛擬環境

2020-06-16 17:52:00

Ubuntu上安裝KVM並搭建虛擬環境,分為4個章節

1.環境準備
2.安裝KVM軟體
3.設定橋接網絡卡
4.建立虛擬機器(Ubuntu, winxp)

1. 環境準備

1)  兩台計算機,一台是操作計算機Client, 一台是伺服器Server

Client: 筆記型電腦, Win7旗艦版,64位元,內網ip: 192.168.1.101

Server(Host): 伺服器, Ubuntu 12.04LTS 64位元,2核CPU,4G記憶體,內網ip:192.168.1.10,

2) 在server啟動時,進入BIOS中開起CPU虛擬化的支援

進入BIOS中,在config –> CPU選項中
Intel(R) Virtualization Technology (Enabled)
Intel(R) VT-d Feature (Enabled)

不同BIOS選單稍有不同。

3) Server上,先安裝Openssh-server,Client才能連線

~ sudo apt-get install openssh-server

Clinet通過Putty回家連線Server。

註:以下的所有操作都通過SSH進行。

4) Server系統環境

~ uname -a
 Linux conan 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
 ~ cat /etc/issue
 Ubuntu 12.04.2 LTS n l

5) 檢視CPU的虛擬化支援

~ egrep 'svm|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 lm constant_tsc arch_perfmon pebs bts nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority
 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 lm constant_tsc arch_perfmon pebs bts nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority


我機器是intel的CPU,可以檢視vmx的標識

 

6) Server上,設定ubuntu下載源sources.list,我用的是sohu源。

~ sudo vi /etc/apt/sources.list
 deb http://mirrors.sohu.com/ubuntu/ precise main restricted
 deb-src http://mirrors.sohu.com/ubuntu/ precise main restricted
 deb http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
 deb-src http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
 deb http://mirrors.sohu.com/ubuntu/ precise universe
 deb-src http://mirrors.sohu.com/ubuntu/ precise universe
 deb http://mirrors.sohu.com/ubuntu/ precise-updates universe
 deb-src http://mirrors.sohu.com/ubuntu/ precise-updates universe
 deb http://mirrors.sohu.com/ubuntu/ precise multiverse
 deb-src http://mirrors.sohu.com/ubuntu/ precise multiverse
 deb http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
 deb-src http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
 deb http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
 deb-src http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
 deb http://mirrors.sohu.com/ubuntu/ precise-security main restricted
 deb-src http://mirrors.sohu.com/ubuntu/ precise-security main restricted
 deb http://mirrors.sohu.com/ubuntu/ precise-security universe
 deb-src http://mirrors.sohu.com/ubuntu/ precise-security universe
 deb http://mirrors.sohu.com/ubuntu/ precise-security multiverse
 deb-src http://mirrors.sohu.com/ubuntu/ precise-security multiverse
 deb http://extras.ubuntu.com/ubuntu precise main
 deb-src http://extras.ubuntu.com/ubuntu precise main

#更新源
~ sudo apt-get update

2. 安裝KVM軟體

1) Server上,安裝KVM及virt管理軟體

~ sudo apt-get install kvm qemu
 ~ sudo apt-get install virtinst Python-libvirt virt-viewer virt-manager

在ubuntu上安裝,真的很簡單。

3. 設定橋接網絡卡

1) 在這裡我們使用網絡卡橋接的方式

~ sudo apt-get install bridge-utils

2) 檢視網絡卡的設定

~ ifconfig
 eth0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
 inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
 inet6 addr: fe80::21c:25ff:fea1:99fc/64 Scope:Link
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:736674 errors:0 dropped:0 overruns:0 frame:0
 TX packets:1098750 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:63783787 (63.7 MB) TX bytes:1540374899 (1.5 GB)
 Interrupt:20 Memory:fc200000-fc220000

lo Link encap:Local Loopback
 inet addr:127.0.0.1 Mask:255.0.0.0
 inet6 addr: ::1/128 Scope:Host
 UP LOOPBACK RUNNING MTU:16436 Metric:1
 RX packets:1156 errors:0 dropped:0 overruns:0 frame:0
 TX packets:1156 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:114090 (114.0 KB) TX bytes:114090 (114.0 KB)

 virbr0 Link encap:Ethernet HWaddr be:65:ec:88:86:3d
 inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
 UP BROADCAST MULTICAST MTU:1500 Metric:1
 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

3) 已有的網絡卡
eth0是直接插入網線的乙太網卡,
lo是本地網路,本機內部資源
virbr0是剛才我們裝完KVM後,自己生成的虛擬網絡卡

4) 網絡卡橋接
 我們增加一個叫br0的虛擬網絡卡,讓這個網絡卡可以和eth0乙太網的網絡卡做橋接

~ sudo vi /etc/network/interfaces
 auto lo
 iface lo inet loopback
 auto eth0
 iface eth0 inet manual
 auto br0
 iface br0 inet static
 address 192.168.1.10
 netmask 255.255.255.0
 gateway 192.168.1.1
 bridge_ports eth0

儲存,重起網絡卡設定

~ sudo /etc/init.d/networking restart

再檢視網絡卡的設定

~ ifconfig
 br0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
 inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
 inet6 addr: fe80::21c:25ff:fea1:99fc/64 Scope:Link
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:91 errors:0 dropped:0 overruns:0 frame:0
 TX packets:100 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:6260 (6.2 KB) TX bytes:15470 (15.4 KB)

 eth0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:605 errors:0 dropped:0 overruns:0 frame:0
 TX packets:425 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:112441 (112.4 KB) TX bytes:61529 (61.5 KB)
 Interrupt:20 Memory:fc200000-fc220000

lo Link encap:Local Loopback
 inet addr:127.0.0.1 Mask:255.0.0.0
 inet6 addr: ::1/128 Scope:Host
 UP LOOPBACK RUNNING MTU:16436 Metric:1
 RX packets:36 errors:0 dropped:0 overruns:0 frame:0
 TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:2100 (2.1 KB) TX bytes:2100 (2.1 KB)

 virbr0 Link encap:Ethernet HWaddr 2a:b8:36:cf:cc:c6
 inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
 UP BROADCAST MULTICAST MTU:1500 Metric:1
 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

這個時候eth0,已經沒有具體的IP地址。IP地址出在了br0的虛擬網上面。

我們ping一下公網ip,看看是不是通的。

~ ping 8.8.8.8
 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
 64 bytes from 8.8.8.8: icmp_req=1 ttl=46 time=51.7 ms
 64 bytes from 8.8.8.8: icmp_req=2 ttl=46 time=50.8 ms

4. 建立虛擬機器

通過virt虛擬機器管理軟體,建立虛擬機器(Guest)。

上傳2個安裝映象到Server: u1204.iso, xp.iso

放在/home/conan/os目錄下面。

1)建立Guest1, Ubuntu 64位元LTS 12.04版本(命令列,無視覺化介面)

把u1204.iso檔案mount一個目錄

~ sudo mount -o loop /home/conan/os/u1204.iso /home/conan/os/ubuntu
~ ls /home/conan/os/ubuntu
 boot dists EFI isolinux pics preseed ubuntu
 cdromupgrade doc install md5sum.txt pool README.diskdefines

建立虛擬機器

~ sudo virt-install --connect=qemu:///system
 --name g1
 --ram 2048
 --vcpus=2
 --os-type=linux
 --os-variant=ubuntuprecise
 --accelerate
 --hvm
 --disk path=/home/conan/os/g1.img,size=6,bus=virtio
 --location /home/conan/os/ubuntu
 --extra-args='console=tty0 console=ttyS0'
 --network bridge=br0,model=virtio
 --graphics none

進入安裝介面

lqqqqqqqqqqqqqqqqqqqqqqqu [!!] Select a language tqqqqqqqqqqqqqqqqqqqqqqqqk
 x x
 x Choose the language to be used for the installation process. The x
 x selected language will also be the default language for the installed x
 x system. x
 x x
 x Language: x
 x x
 x C x
 x English x
 x x
 x <Go Back> x
 x x
 mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
 <Tab> moves; <Space> selects; <Enter> activates buttons

安裝系統

lqqqqqqqqqqqqqqqqqqqqqu [!] Configure the network tqqqqqqqqqqqqqqqqqqqqqk
 x x
 x Please enter the hostname for this system. x
 x x
 x The hostname is a single word that identifies your system to the x
 x network. If you don't know what your hostname should be, consult your x
 x network administrator. If you are setting up your own home network, x
 x you can make something up here. x
 x x
 x Hostname: x
 x x
 x localhost____________________________________________________________ x
 x x
 x <Go Back> <Continue> x
 x x
 mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
 <Tab> moves; <Space> selects; <Enter> activates buttons

安裝完成

2). 建立Guest2, Ubuntu 64位元LTS 12.04版本(通過VNC,有視覺化介面)

在server端,我要開啟vnc_listen的埠支援

~ vi /etc/libvirt/qemu.conf
 #開啟註釋
vnc_listen="0.0.0.0"

重新啟動virt軟體

~ sudo /etc/init.d/libvirt-bin restart
 ~ netstat -nlt|grep 5900
 tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN


我們需要使用到Xming和putty配合,可以讓putty遠端傳輸的VNC信號通過Xming顯示出來。

在putty裡面,我們要修改一下設定。

啟動Xming軟體

執行命令

~ sudo virt-install --connect=qemu:///system
 --name g2
 --ram 2048
 --vcpus=2
 --os-type=linux
 --os-variant=ubuntuprecise
 --accelerate
 --hvm
 --disk path=/home/conan/os/g2.img,size=6,bus=virtio
 --cdrom /home/conan/os/u1204.iso
 --network bridge=br0,model=virtio
 --graphics vnc

通過Xming顯示了VNC的介面,ubuntu安裝畫面

安裝完成

3. 建立Guest3, Winxp(通過VNC,有視覺化介面)

註1:這裡的winxp必須安裝版,ghost是不行的。

註2:windows的安裝,必須要使用VNC介面方式,沒有命令直接安裝的方法

執行命令

sudo virt-install --connect=qemu:///system
 --name g3
 --ram 1024
 --vcpus=1
 --os-type=windows
 --os-variant=winxp
 --accelerate
 --hvm
 --disk path=/home/conan/os/g3.img,size=4
 --cdrom /home/conan/os/xp.iso
 --network bridge=br0,model=ne2k_pci
 --graphics vnc

通過Xming顯示了VNC的介面,WinXP安裝畫面

安裝完成

虛擬機器互相通訊

我們啟動Guest1(ubuntu), Guest3(winxp).
Client: ip: 192.168.1.101
Server: ip: 192.168.1.10
Guest1: ip: 192.168.1.11
Guest3: ip: 192.168.1.12

他們都在同一網段,相互可以ping通,也可以相互呼叫。

如果你實驗結果和我一樣,那麼恭喜你,已經實現了通過KVM搭建自己的VPS過程。

 

錯誤解決方案

1. 錯誤程式碼:

libvirtError: internal error Process exited while reading console log output: char device redirected to /dev/pts/4
 Could not access KVM kernel module: Permission denied
 failed to initialize KVM: Permission denied
 No accelerator found!


解決辦法:

~ sudo rmmod kvm_intel
~ sudo modprobe -r kvm_intel
~ sudo modprobe kvm_intel nested=1

--------------------------------------分割線 --------------------------------------


IT145.com E-mail:sddin#qq.com