首頁 > 軟體

QEMU-KVM安裝部署虛擬機器

2020-06-16 17:15:48

QEMU-KVM安裝KVM

作為虛擬化來用時,與KVM結合,稱之為QEMU-KVM。QEMU-KVM是基於硬體虛擬化技術(例如Intel VT ,AMD SVM),並結合了QEMU提供裝置虛擬化功能,來實現整個系統虛擬化。

從QEMU-KVM定義來看好像物理計算機系統只能是x86體系結構硬體平台,執行Linux作業系統計算機。

但是任何事物都發展的,將來當QEMU-KVM採用IPS , PPC等硬體虛擬化技術,並且KVM移植到其他作業系統上(FreeBSD等),也是有可能。

1.BIOS開啟虛擬化
2.啟用kvm模組
#modprobe kvm

關閉防火牆
# systemctl stop firewalld.service

3.功能區分intel/amd的啟用
#modprobe kvm-intel
# lsmod | grep kvm
kvm_intel            170181  3
kvm                  554609  1 kvm_intel
irqbypass              13503  1 kvm

4.安裝kvm
#yum install -y qemu-kvm libvirt virt-manager qemu-kvm-tools

5.安裝網橋管理工具tunctl
下載:http://www.rpmfind.net/linux/rpm2html/search.php?query=tunctl
# yum install tunctl-1.5-3.el6.x86_64.rpm

6.掛載U盤,拷貝映象檔案
首先要進入官網下載NTFS-3G工具
http://www.tuxera.com/community/ntfs-3g-download/
6.1.下載之後進行解壓
wget http://tuxera.com/opensource/ntfs-3g_ntfsprogs-2014.2.15.tgz
tar -xvf ntfs-3g_ntfsprogs-2014.2.15.tgz

6.2.編譯並進行安裝
cd ntfs-3g_ntfsprogs-2014.2.15
./configure
make
make install

6.3.掛載NTFS
mount -t ntfs-3g /dev/sda2  /mnt/ckl

6.5.拷貝
cp /mnt/ckl/CentOS-7-x86_64-DVD-1611.iso /root/CentOS-7-x86_64-DVD-1611.iso

7.設定網絡卡
7.1.會自動啟動一個橋裝置,這相當於VMware Workstation中的host-only僅主機的網路裝置;
# ifconfig 
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.3  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ec4:17fa:8756:5a8f  prefixlen 64  scopeid 0x20<link>
        ether 00:1d:72:58:6f:93  txqueuelen 1000  (Ethernet)
        RX packets 2606  bytes 162013 (158.2 KiB)
        RX errors 0  dropped 2269  overruns 0  frame 0
        TX packets 177  bytes 16706 (16.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17 
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 96  bytes 8308 (8.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 96  bytes 8308 (8.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:7e:e7:ca  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

7.2.使用網橋管理命令檢視
# brctl show
bridge name    bridge id              STP enabled    interfaces
virbr0          8000.5254007ee7ca      yes            virbr0-nic

7.4.需要將NetworkManager服務關閉,開機啟動也關閉
# chkconfig NetworkManager off
# service NetworkManager stop

7.5.建立橋接裝置及關聯網絡卡到橋接裝置上
# virsh iface-bridge ens3 br0
Created bridge br0 with attached device ens3
Bridge interface br0 started

7.6.檢視網絡卡情況
# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.3  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::21d:72ff:fe58:6f93  prefixlen 64  scopeid 0x20<link>
        ether 00:1d:72:58:6f:93  txqueuelen 1000  (Ethernet)
        RX packets 22  bytes 1636 (1.5 KiB)
        RX errors 0  dropped 15  overruns 0  frame 0
        TX packets 22  bytes 4045 (3.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:1d:72:58:6f:93  txqueuelen 1000  (Ethernet)
        RX packets 522  bytes 34644 (33.8 KiB)
        RX errors 0  dropped 341  overruns 0  frame 0
        TX packets 148  bytes 16251 (15.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17 
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 14  bytes 770 (770.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 770 (770.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:7e:e7:ca  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

7.7.為GUEST新增虛擬網絡卡tap10,並加入網橋tap10
# tunctl -t tap10
Set 'tap10' persistent and owned by uid 0
# brctl addif br0 tap10
# ifconfig tap10 up
# brctl show
bridge name    bridge id              STP enabled    interfaces
br0            8000.001d72586f93      yes            ens3
                                                        tap10
virbr0          8000.5254007ee7ca      yes            virbr0-nic

檢視多出tap10 
# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.3  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::21d:72ff:fe58:6f93  prefixlen 64  scopeid 0x20<link>
        ether 00:1d:72:58:6f:93  txqueuelen 1000  (Ethernet)
        RX packets 13615  bytes 6924507 (6.6 MiB)
        RX errors 0  dropped 3445  overruns 0  frame 0
        TX packets 5742  bytes 7811746 (7.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:1d:72:58:6f:93  txqueuelen 1000  (Ethernet)
        RX packets 14517  bytes 7182587 (6.8 MiB)
        RX errors 0  dropped 341  overruns 0  frame 0
        TX packets 6733  bytes 7868932 (7.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17 
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 14  bytes 770 (770.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 770 (770.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
tap10: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 5a:c7:ec:11:0b:b8  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:7e:e7:ca  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

7.8.為虛擬橋接網絡卡tap10建立組態檔,重新啟動系統時能隨系統啟動,並加入網橋 br0

# vim /etc/sysconfig/network-scripts/ifcfg-tap10
DEVICE=tap10
TYPE=Tap
ONBOOT=yes
BRIDGE=br0

8.安裝vnc
yum install vnc-server tigervnc -y

設定vnc密碼
# vncpasswd

9.開始安裝kvm
9.1.建立虛擬磁碟目錄
# mkdir -pv /home/kvm/image/

9.2.建立虛擬磁碟檔案
# qemu-img create -o preallocation=metadata -f qcow2 /home/kvm/image/cent7_1.qcow2 20G

9.3.vnc連線

9.4.安裝虛擬機器
# /usr/libexec/qemu-kvm  -m 512 -smp 1
-drive file=/home/kvm/image/cent7_1.qcow2
-net nic,vlan=2,model=e1000,macaddr=52:54:00:12:34:56
-net tap,vlan=2,ifname=tap10,script=no -boot d
-cdrom /root/CentOS-7-x86_64-DVD-1611.iso  -vnc 192.168.1.3:1  --daemonize

檢視進程:
# ps aux | grep kvm
root      605  0.0  0.0      0    0 ?        S<  10:26  0:00 [kvm-irqfd-clean]
root      5168  2.8  1.8 941392 34832 ?        Sl  11:03  0:00 /usr/libexec/qemu-kvm -m 512 -smp 1 -drive file=/home/kvm/image/cent7_1.qcow2 -net nic,vlan=2,model=e1000,macaddr=52:54:00:12:34:56 -net tap,vlan=2,ifname=tap10,script=no -boot d -cdrom /root/CentOS-7-x86_64-DVD-1611.iso -vnc 192.168.1.3:1 --daemonize
root      5173  0.0  0.0      0    0 ?        S    11:03  0:00 [kvm-pit/5168]
root      5234  0.0  0.0 112652  944 pts/1    S+  11:04  0:00 grep --color=auto kvm

9.5.安裝過程

9.6.安裝完畢後結束掉kvm進程
# kill -15 5168

9.7.重新啟動,啟動Guest虛擬機器,修改啟動方式為硬碟 -boot c ,去除光碟資源
# /usr/libexec/qemu-kvm  -m 512 -smp 1
-drive file=/home/kvm/image/cent7_1.qcow2
-net nic,vlan=2,model=e1000,macaddr=52:54:00:12:34:56
-net tap,vlan=2,ifname=tap10,script=no -boot c
-vnc 192.168.1.3:1  --daemonize

10.安裝完成

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-05/143292.htm


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