1. 程式人生 > 其它 >介面和抽象類有什麼區別?

介面和抽象類有什麼區別?

KVM虛擬化

KVM(Kernel-Based Virtual Machines)是一個基於Linux核心的虛擬化技術, 可以直接將Linux核心轉換為Hypervisor(系統管理程式)從而使得Linux核心能夠直接管理虛擬機器, 直接呼叫Linux核心中的記憶體管理、程序管理子系統來管理虛擬機器。

KVM的虛擬化需要硬體支援(如Intel VT技術或者AMD V技術)。是基於硬體的完全虛擬化。而Xen早期則是基於軟體模擬的Para-Virtualization,新版本則是基於硬體支援的完全虛擬化。但Xen本身有自己的程序排程器、儲存管理模組等,所以程式碼較為龐大。廣為流傳的商業系統

虛擬化軟體VMware ESX系列是基於軟體模擬的Full-Virtualization。

任務一、安裝KVM

1.建立虛擬機器

虛擬機器記憶體至少設定成 2 GB,在“硬體”選項卡中單擊“處理器”, 在虛擬化引擎中勾選如紅框內所示選項

接下來,我們新增硬碟,新增一個 50GB 左右大小的就可以

虛擬機器設定完成之後如下圖:

配置完成之後,啟動虛擬機器(配置網絡卡、關閉防火牆並設定開機不自啟、掛載yum源)

配置網絡卡:    
    # ip addr
    # vi /etc/sysconfig/network-scripts/ifcfg-ens33
    # systemctl restart network
# ip addr 關閉防火牆,並設定開機不自啟: # systemctl stop firewalld # systemctl disable firewalld # setenforce 0 # vi /etc/selinux/config //換成disabled 掛載yum源# mkdir /opt/centos # mount /dev/sr0 /opt/centos # mv /etc/yum.repos.d/* /media/ # vi /etc/yum.repos.d/local.repo [centos] name=centos
baseurl=file:///opt/centos gpgcheck=0 enabled=1 # yum clean all # yum repolist

2.檢查CPU引數

檢視以下記憶體是否更改成功:
    # free
       total      used     free      shared    buff/cache   available
Mem:  1868688  125360  1582372     8752     160956     1584208
Swap:  097148     0     2097148
檢查CPU是否開啟虛擬化支援:
    # grep -Ei 'vmx|svm' /proc/cpuinfo
       //vmx為英特爾的cpu;svm為AMD的cpu。
       flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr 
pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb
rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc
eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe
popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm
3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2
smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca
cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm
constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu
pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt
tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm
3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2
smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves 如果顯示為空,就要檢查虛擬機器設定是否打“√”

3.掛載新磁碟

使用lsblk命令,檢查虛擬機器是否新增一塊50 GB的磁碟:
    # lsblk
       NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
       sda               8:0    0   20G  0 disk 
      ├─sda1            8:1    0  500M  0 part /boot
      └─sda2            8:2    0 19.5G  0 part 
        ├─centos-root 253:0    0 17.5G  0 lvm  /
        └─centos-swap 253:1    0    2G  0 lvm  [ SWAP]
      sdb               8:16   0   50G  0 disk 
      sr0              11:0    1    4G  0 rom 
我們可以看到確實新增了一塊磁碟名為sdb。
接下來格式化這塊磁碟,格式化成ext4格式# mkfs.ext4 /dev/sdb 
       mke2fs 1.42.9 (28-Dec-2013)
       /dev/sdb is entire device, not just one partition!
       Proceed anyway? (y,n) y
       Filesystem label=
       OS type: Linux
       Block size=4096 (log=2)
       Fragment size=4096 (log=2)
       Stride=0 blocks, Stripe width=0 blocks
       3276800 inodes, 13107200 blocks
       655360 blocks (5.00%) reserved for the super user
       First data block=0   
       Maximum filesystem blocks=2162163712
       400 block groups
       32768 blocks per group, 32768 fragments per group
       8192 inodes per group
       Superblock backups stored on blocks: 
       32768, 98304, 163840, 229376, 294912, 819200, 884736, 
       1605632, 2654208, 
       4096000, 7962624, 11239424

       Allocating group tables: done                            
       Writing inode tables: done                            
       Creating journal (32768 blocks): done
       Writing superblocks and filesystem accounting information: 
       done   

    # blkid /dev/sdb
       //blkid+磁碟:檢視磁碟資訊
       /dev/sdb: UUID="dab6d659-e334-41ed-a15a-9 
       6ce05b48c1a" TYPE="ext4" 
格式化完成後,我們掛載磁碟,先建立掛載目錄/kvm_data,然後進行掛載:
    # mkdir /kvm_data
    # mount /dev/sdb /kvm_data/
這種方式掛載,重啟虛擬機器後,需要再次重新掛載,我們為了方便開機後可以自動掛載,編寫/etc/fstab檔案,新增檔案最末一行:
    # vi /etc/fstab 
    # cat /etc/fstab 

     #
     # /etc/fstab
     # Created by anaconda on Thu Aug 22 22:51:46 2019
     #
     # Accessible filesystems, by reference, are maintained under '/dev/disk'
     # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     #
     /dev/mapper/centos-root /                       xfs     defaults        0 0
     UUID=34f3cd91-b7f0-44dd-9334-2bb66e939898 /boot                   xfs     defaults        0 0
     /dev/mapper/centos-swap swap                    swap    defaults        0 0
     /dev/sdb     /kvm_data           ext4      defaults    0 0

4.安裝KVM

使用Yum進行安裝KVM:
    # yum install -y  virt-*  libvirt  bridge-utils qemu-img
      …………
      Complete!

任務二、啟動KVM

1.配置網絡卡

增加橋接網絡卡ifcfg-br0,命令如下:
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
//ifcfg-ens33為配置IP的網絡卡,網絡卡可能不同
修改橋接網絡卡ifcfg-br0的內容:
[root@kvm network-scripts]# vi ifcfg-br0 
[root@kvm network-scripts]# cat ifcfg-br0 
    TYPE=Bridge
    BOOTPROTO=none
    NAME=br0
    DEVICE=br0
    ONBOOT=yes
    IPADDR=192.168.233.5
    NETMASK=255.255.255.0
    GATEWAY=192.168.233.2
    DNS1=114.114.114.114
    DNS2=8.8.8.8
修改NAT網絡卡ifcfg-ens33:
[root@kvm network-scripts]# vi ifcfg-ens33
[root@kvm network-scripts]# cat ifcfg-ens33 
    TYPE=Ethernet
    BOOTPROTO=none
    NAME=ens33
    DEVICE=ens33
    ONBOOT=yes
    BRIDGE=br0
修改完成網絡卡內容後,重新啟動網絡卡服務並檢視網絡卡資訊:
[root@kvm network-scripts]# service network restart
    Restarting network (via systemctl):  [  OK  ]     
[root@kvm network-scripts]# ifconfig 
    br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.16.5  netmask 255.255.255.0  broadcast 
    192.168.16.255
            inet6 fe80::20c:29ff:fe8d:1f90  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:8d:1f:90  txqueuelen 0  (Ethernet)
            RX packets 90  bytes 7092 (6.9 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 38  bytes 3604 (3.5 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  
     mtu 1500
            ether 00:0c:29:8d:1f:90  txqueuelen 1000  (Ethernet)
            RX packets 126287  bytes 182444678 (173.9 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 32532  bytes 2953477 (2.8 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    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 0  (Local Loopback)
            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
可以清楚的看到br0網絡卡出現並且帶有IP,而以前的ens33網絡卡則沒有IP,網絡卡配置完成

2.啟動libvirtd服務

首先檢查KVM模組是否載入:    
[root@kvm network-scripts]# lsmod|grep kvm
    kvm_intel             162153  0 
    kvm                   525259  1 kvm_intel
啟動libvirtd並檢查是否成功啟動:
[root@kvm network-scripts]# systemctl start libvirtd
[root@kvm network-scripts]# ps -ef |grep libvirt
    root      17270      1  1 04:36 ?        00:00:00 /usr/sbin/libvirtd
    nobody    17383      1  0 04:36 ?        00:00:00 
    /usr/sbin/dnsmasq --conf- 
    file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp- 
    script=/usr/libexec/libvirt_leaseshelper
    root      17384  17383  0 04:36 ?        00:00:00 
    /usr/sbin/dnsmasq --conf- 
    file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp- 
    script=/usr/libexec/libvirt_leaseshelper
    root      17416   2264  0 04:36 pts/0    00:00:00 grep -- 
    color=auto libvirt
啟動成功後,使用brctl命令可以看到兩個網絡卡,如下程式碼所示:
[root@kvm ~]# cd
[root@kvm ~]# brctl show
    bridge name     bridge id               STP enabled     interfaces
    br0            8000.000c298d1f90       no          ens33
    virbr0          8000.525400c7e229       yes           virbr0-nic

3.安裝CentOS 7

CentOS7映象下載地址:

http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2003.iso

首先,通過SCRT上傳CentOS 7映象到/tmp目錄下,如果沒有映象的可以自行下載。上傳後,使用ll命令檢視/tmp目錄下是否存在CentOS 7映象檔案:
[root@kvm ~]# ll /tmp/
    total 4228096
    -rw-r--r--  1 root root 4329570304 Apr 15  2016 CentOS-7- x86_64-DVD-1511.iso

命令解釋:

l --name:指定虛擬機器的名稱。

l --memory:指定分配給虛擬機器的記憶體資源大小。

l maxmemory:指定可調節的最大記憶體資源大小,因為KVM支援熱調整虛擬機器的資源。

l --vcpus:指定分配給虛擬機器的CPU核心數量。

l maxvcpus:指定可調節的最大CPU核心數量。

l --os-type:指定虛擬機器安裝的作業系統型別。

l --os-variant:指定系統的發行版本。

l --location:指定ISO映象檔案所在的路徑,支援使用網路資源路徑,也就是說可以使用URL。

l --disk path:指定虛擬硬碟所存放的路徑及名稱,size則是指定該硬碟的可用大小,單位是G。

l --bridge:指定使用哪一個橋接網絡卡,也就是說使用橋接的網路模式。

l --graphics:指定是否開啟圖形。

l --console:定義終端的屬性,target_type 則是定義終端的型別。

l --extra-args:定義終端額外的引數。

映象存在,接下來使用virt-install進行CentOS 7的安裝:
[root@kvm ~]# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10  --bridge=br0 --graphics=none --console=pty,target_type=serial  --extra-args="console=tty0 console=ttyS0"
打完上面那條命令後,等一會,就可以看見下面這段命令,這是系統基礎設定,帶[!]基本都是需要配置的,接下來我們開始配置“Timezone settings”,輸入“2”,按Enter鍵
Starting installer, one moment...
anaconda 21.48.22.56-1 for CentOS 7 started.
 * installation log files are stored in /tmp during the installation
 * shell is available on TTY2
 * when reporting a bug add logs from /tmp as separate text/plain attachments
17:01:51 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [!] Timezone settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 2
=====================================================================
“Timezone settings”時區設定選擇 5) Asia亞洲,再選擇城市 62) Shanghai上海,命令如下:
Timezone settings

Available regions
 1)  Africa                 6)  Atlantic              10)  Pacific
 2)  America                7)  Australia             11)  US
 3)  Antarctica             8)  Europe                12)  Etc
 4)  Arctic                 9)  Indian
 5)  Asia
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 5
================================================================================
================================================================================
Timezone settings

Available timezones in region Asia
 1)  Aden                  28)  Irkutsk               54)  Pyongyang
 2)  Almaty                29)  Jakarta               55)  Qatar
 3)  Amman                 30)  Jayapura              56)  Qyzylorda
 4)  Anadyr                31)  Jerusalem             57)  Rangoon
 5)  Aqtau                 32)  Kabul                 58)  Riyadh
 6)  Aqtobe                33)  Kamchatka             59)  Sakhalin
 7)  Ashgabat              34)  Karachi               60)  Samarkand
 8)  Baghdad               35)  Kathmandu             61)  Seoul
 9)  Bahrain               36)  Khandyga              62)  Shanghai
10)  Baku                  37)  Kolkata               63)  Singapore
11)  Bangkok             38)Krasnoyarsk             64) Srednekolymsk
12)  Beirut                39)  Kuala_Lumpur          65)  Taipei
13)  Bishkek               40)  Kuching               66)  Tashkent
14)  Brunei                41)  Kuwait                67)  Tbilisi
15)  Chita                 42)  Macau                 68)  Tehran
16)  Choibalsan            43)  Magadan               69)  Thimphu
17)  Colombo               44)  Makassar              70)  Tokyo
18)  Damascus             45)  Manila                71)  Ulaanbaatar
19)  Dhaka                 46)  Muscat                72)  Urumqi
20)  Dili                  47)  Nicosia               73)  Ust-Nera
21)  Dubai                 48)  Novokuznetsk          74)  Vientiane
22)  Dushanbe             49)  Novosibirsk           75)  Vladivostok
Press ENTER to continue
23)  Gaza                  50)  Omsk                  76)  Yakutsk
24)  Hebron             51)  Oral                  77)  Yekaterinburg
25)  Ho_Chi_Minh           52)  Phnom_Penh            78)  Yerevan
26)  Hong_Kong             53)  Pontianak
27)  Hovd
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 62
================================================================================
輸入完畢後,可以發現2的[!]變成了[x],證明配置完畢。
接下來我們配置“Software selection”,選擇“4”,在選擇“c”,因為預設就是minimalinstall,按Enter鍵,命令如下:
Installation

 1) [x] Language settings                 2) [x] Timezone settings
        (English (United States))                (Asia/Shanghai timezone)
 3) [x] Installation source               4) [!] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 4
================================================================================
================================================================================
Base environment
Software selection

Base environment

 1)  [x] Minimal Install                 7)  [ ] Server with GUI
 2)  [ ] Compute Node                    8)  [ ] GNOME Desktop
 3)  [ ] Infrastructure Server           9)  [ ] KDE Plasma Workspaces
 4)  [ ] File and Print Server          10)  [ ] Development and Creative Work
 5)  [ ] Basic Web Server station
 6)  [ ] Virtualization Host
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
接下來我們配置“Installation Destination”,選擇“5”,其餘的依次選擇“c”,按Enter鍵,命令如下:
Installation

 1) [x] Language settings                 2) [x] Timezone settings
        (English (United States))           (Asia/Shanghai timezone)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
 Please make your choice from above ['q' to quit | 'b' to begin installation |
 'r' to refresh]: 5
================================================================================
================================================================================
Probing storage...
Installation Destination

[x] 1) : 10 GiB (vda)

1 disk selected; 10 GiB capacity; 10 GiB free ...

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
================================================================================
================================================================================
Autopartitioning Options

[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 3) Use Free Space

Installation requires partitioning of your hard drive. Select what space to use 
for the install target.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
================================================================================
================================================================================
Partition Scheme Options

[ ] 1) Standard Partition

[ ] 2) Btrfs

[x] 3) LVM

[ ] 4) LVM Thin Provisioning

Select a partition scheme configuration.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: c
Generating updated storage configuration
Checking storage configuration...
================================================================================
================================================================================
接下來配置“Root password”,選擇8,按Enter鍵,命令如下:
Installation

 1) [x] Language settings                 2) [x] Timezone settings
        (English (United States))                (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning selecte          (Kdump is enabled)
        d)                                8) [!] Root password
 7) [ ] Network configuration                    (Password is not set.)
        (Not connected)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 8
================================================================================
================================================================================
Please select new root password. You will have to type it twice.
依次輸入兩次密碼,密碼相同,我設定的為123456:
Password: 123456 
//密碼為密文,不顯示。
Password (confirm): 123456
================================================================================
================================================================================
Question

You have provided a weak password: The password fails the dictionary check - it 
is too simplistic/systematic
Would you like to use it anyway?
//這句話的意思:你的密碼太過簡單,是否使用它,輸入yes即可。
Please respond 'yes' or 'no': yes
=========================================================
=========================================================
配置完成,選擇“b”,按Enter鍵後,開始安裝:
Installation

 1) [x] Language settings                 2) [x] Timezone settings
        (English (United States))                (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning selecte          (Kdump is enabled)
        d)                                8) [x] Root password
 7) [ ] Network configuration                    (Password is set.)
        (Not connected)
 9) [ ] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: b
================================================================================
================================================================================
Progress
Setting up the installation environment
.
Creating disklabel on /dev/vda
.
Creating xfs on /dev/vda1
.
Creating lvmpv on /dev/vda2
.
Creating swap on /dev/mapper/centos-swap
.
……………………
Installing readline (34/297)
Installing gawk (35/297)
Installing elfutils-libelf (36/297)
Installing libgpg-error (37/297)
Installing libffi (38/297)
Installing libattr (39/297)
Installing libacl (40/297)
Installing libcap (41/297)
Installing libgcrypt (42/297)
Installing cpio (43/297)
Installing libxml2 (44/297)
Installing libnl3 (45/297)
Installing expat (46/297)
Installing p11-kit (47/297)
……………………
.
Performing post-installation setup tasks
.
Configuring installed system
.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs
.
Running post-installation scripts
.
        Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA

        Installation complete.  Press return to quit 
//按回車
……………………
CentOS Linux 7 (Core)
Kernel 3.10.0-327.el7.x86_64 on an x86_64
localhost login: root
Password: 123456
[root@localhost ~]# 
//按“Ctrl+]”鍵,退出終端,回到宿主機。
[root@kvm ~]# 

CentOS安裝完成

任務三、虛擬機器管理

1.KVM基本管理

完成虛擬機器安裝,已經退回到宿主機,接下來我們來通過宿主機virsh命令進行管理剛才安裝的CentOS 7虛擬機器

檢視虛擬機器列表:
[root@kvm ~]# virsh list
 //檢視虛擬機器列表,只能看到正在執行的虛擬機器
     Id    Name                           State
    ----------------------------------------------------
     2     test                           running
[root@kvm ~]# virsh list --all
//檢視虛擬機器列表,包括未執行的虛擬機器
     Id    Name                           State
    ----------------------------------------------------
     2     test                           running
進入指定的虛擬,命令如下:
[root@kvm ~]# virsh console test
    Connected to domain test
    Escape character is ^]
    //如果遲遲未動敲下回車就ok,下面就是正常登入時需要輸入的使用者名稱密碼
    CentOS Linux 7 (Core)
    Kernel 3.10.0-327.el7.x86_64 on an x86_64

    localhost login: 
若發生以下報錯資訊,命令如下:
[root@kvm ~]# virsh console test
    Connected to domain test
    Escape character is ^]
    error: operation failed: Active console session exists for this domain
解決方式,命令如下:
[root@kvm ~]# ps -ef |grep console
    root      11167  10987  0 01:55 tty1     00:00:00 virsh console 
    test
    root      16017  15750  0 10:01 pts/1    00:00:00 grep -- 
    color=auto console
[root@kvm ~]# kill -9 11167

vrish常用命令,命令如下:

virsh shutdown test

//關閉虛擬機器

virsh start test

//開啟虛擬機器

virsh destroy test

//類似stop,這個是強制停止

virsh undefine test

//徹底銷燬虛擬機器,會刪除虛擬機器配置檔案,virsh list --all就看不到

virsh autostart test

//宿主機開機該虛擬機器也開機

virsh autostart --disable test

//解除開機啟動

virsh suspend test

//掛起

virsh resume test

2.克隆虛擬機器

克隆虛擬機器之前,要先關閉虛擬機器,不然會提示這種錯誤:
[root@kvm ~]# virt-clone --original test --name test02 --file /kvm_data/test02.img
    ERROR    Domain with devices to clone must be paused or shutoff.
關閉虛擬機器後,再次進行克隆:
[root@kvm ~]# virsh shutdown test
    Domain test is being shutdown
[root@kvm ~]# virt-clone  --original test --name test02 --file /kvm_data/test02.img
    Allocating 'test02.img'                        3% 
    [=                    ]  20 MB/s | 365 MB  00:08:23 ETA
    //開始克隆
[root@kvm ~]# virt-clone  --original test --name test02 --file /kvm_data/test02.img
    Allocating     'test02.img'         |  10 GB  00:00:35     
    Clone 'test02' created successfully.
    //克隆完成且成功
命令解釋:
 --original:指定克隆源虛擬機器。
 --name:指定克隆後的虛擬機器名字。
 --file:指定目標虛擬機器的虛擬磁碟檔案。
檢視虛擬機器配置檔案/etc/libvirt/qemu/,看是否增加test02.xml檔案:
[root@kvm ~]# ls /etc/libvirt/qemu/
    networks  test02.xml  test.xml
啟動剛剛克隆的虛擬機器test02,首先檢視虛擬機器列表,然後啟動克隆的虛擬機器test02:
[root@kvm ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     test                           shut off
     -     test02                         shut off

[root@kvm ~]# virsh start test02
    Domain test02 started

3.快照管理

快照是在我們使用Vmware的時候,很常用的一個功能,它可以回到之前的某一狀態,在KVM中RAW格式的虛擬磁碟不支援做快照,qcow2支援

建立快照,命令如下:
[root@kvm ~]# virsh snapshot-create test
    Domain snapshot 1588485687 created
檢視test.img資訊,同時會檢視到快照列表:
[root@kvm ~]# qemu-img info /kvm_data/test.img 
    image: /kvm_data/test.img
    file format: qcow2
    virtual size: 10G (10737418240 bytes)
    disk size: 1.1G
    cluster_size: 65536
    Snapshot list:
    ID    TAG       VM SIZE   DATE              VM CLOCK
    1     1588485687    0       2020-05-03 14:01:27   
    00:00:00.000
    Format specific information:
        compat: 1.1
    lazy refcounts: true
列出所有快照,命令如下:
[root@kvm ~]# virsh snapshot-list test
     Name                 Creation Time             State
    ------------------------------------------------------------
     1588485687           2020-05-03 14:01:27 +0800 shutoff
檢視當前快照版本,命令如下:
[root@kvm ~]# virsh snapshot-current test
<domainsnapshot>
  <name>1588485687</name>
  <state>shutoff</state>
  <creationTime>1588485687</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='vda' snapshot='internal'/>
    <disk name='hda' snapshot='no'/>
  </disks>
  <domain type='kvm'>
    <name>test</name>
    <uuid>49d7cb9c-20dc-42dd-a260-01532b5132e5</uuid>
    <memory unit='KiB'>1048576</memory>
    <currentMemory unit='KiB'>524288</currentMemory>
    <vcpu placement='static' current='1'>2</vcpu>
    <os>

     ..........
     ..........

      </serial>
      <console type='pty'>
        <target type='serial' port='0'/>
      </console>
      <channel type='unix'>
        <target type='virtio' name='org.qemu.guest_agent.0'/>
        <address type='virtio-serial' controller='0' bus='0' port='1'/>
      </channel>
      <input type='mouse' bus='ps2'/>
      <input type='keyboard' bus='ps2'/>
      <memballoon model='virtio'>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
      </memballoon>
      <rng model='virtio'>
        <backend model='random'>/dev/urandom</backend>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
      </rng>
    </devices>
  </domain>
</domainsnapshot>
檢視所有快照配置檔案,命令如下:
[root@kvm ~]# ls /var/lib/libvirt/qemu/snapshot/test/
    1588485687.xml
恢復指定快照,命令如下:
[root@kvm ~]# virsh snapshot-revert test 1588485687
刪除指定快照,命令如下:
[root@kvm ~]# virsh snapshot-delete test 1588485687
    Domain snapshot 1588485687 deleted

4.磁碟格式

檢視虛擬磁碟格式,命令如下:
[root@kvm ~]# qemu-img info /kvm_data/test.img
    image: /kvm_data/test.img
    file format: qcow2
    virtual size: 10G (10737418240 bytes)
    disk size: 1.1G
    cluster_size: 65536
    Format specific information:
        compat: 1.1
    lazy refcounts: true
建立2 GB的RAW格式磁碟,命令如下:
[root@kvm ~]# qemu-img create -f raw /kvm_data/test_1.img 2G
    Formatting '/kvm_data/test_1.img', fmt=raw 
    size=2147483648 
RAW格式的磁碟轉換為qcow2格式:
[root@kvm ~]# qemu-img convert -O qcow2 /kvm_data/test_1.img /kvm_data/test_1.qcow2
檢視test1.img大小,命令如下:
[root@kvm ~]# ls -lh /kvm_data/test_1.img 
    -rw-r--r-- 1 root root 2.0G May  3 15:11 /kvm_data/test_1.img
[root@kvm ~]# ls -lh /kvm_data/test_1.qcow2 
    -rw-r--r-- 1 root root 193K May  3 15:38 
    /kvm_data/test_1.qcow2
    //可以看到qcow2檔案比較小,raw檔案大小和我們指定空間大小一樣是2G
    //raw格式的磁碟效能比qcow2要好,但是raw格式的磁碟無法做快照
將test02轉成raw格式,命令如下:
[root@kvm ~]# qemu-img convert -O raw /kvm_data/test02.img /kvm_data/test02_2.raw
更改磁碟格式和檔案路徑,然後啟動test02虛擬機器:
[root@kvm ~]# virsh edit test02
…………
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow'/>
      <source file='/kvm_data/test02.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
//找到上面這部分將qcow修改為raw,/kvm_data/test02.img改為/kvm_data/test02_2.raw
<disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_data/test02_2.raw'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
………………
檢視磁碟所屬使用者和組,命令如下:
[root@kvm ~]# ls -l /kvm_data/
    total 3310628
    drwx------ 2 root root       16384 Apr 29 00:20 lost+found
    -rw-r--r-- 1 root root 10737418240 May  3 16:14 test02_2.raw
    -rw------- 1 root root  1133772800 May  3 16:02 test02.img
    -rw-r--r-- 1 root root  2147483648 May  3 15:11 test_1.img
    -rw-r--r-- 1 root root      197120 May  3 16:05 test_1.qcow2
    -rw------- 1 root root 10739384832 May  3 14:14 test.img
啟動虛擬機器test02,然後再次檢視磁碟所屬使用者和組:    
[root@kvm ~]# virsh start test02
    Domain test02 started
[root@kvm ~]# ls -l /kvm_data/
    total 3310628
    drwx------ 2 root root       16384 Apr 29 00:20 lost+found
    -rw-r--r-- 1 qemu qemu 10737418240 May  3 16:14 test02_2.raw
    -rw------- 1 root root  1133772800 May  3 16:02 test02.img
    -rw-r--r-- 1 root root  2147483648 May  3 15:11 test_1.img
    -rw-r--r-- 1 root root      197120 May  3 16:05 test_1.qcow2
    -rw------- 1 root root 10739384832 May  3 14:14 test.img
    //啟動後所屬組變化成qemu的是test02_2.raw,證明這個磁碟正在被使用

5.磁碟擴容

在這裡磁碟一共有兩種格式,一種是RAW格式,一種是qcow2格式,接下來我們分別給這兩種磁碟格式進行擴容

首先擴容RAW格式,命令如下:
[root@kvm ~]# qemu-img resize /kvm_data/test02_2.raw +2G
    Image resized.
檢視test02_2.raw資訊,命令如下:
[root@kvm ~]# qemu-img info /kvm_data/test02_2.raw 
    image: /kvm_data/test02_2.raw
    file format: raw
    virtual size: 12G (12884901888 bytes)
    disk size: 1.0G
進入虛擬機器test02,使用fdisk -l檢視磁碟,命令如下:
[root@kvm ~]# virsh console test02
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865
Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM

Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
//可以看到磁碟還是10 GB
退出終端,關閉test02虛擬機器,重新啟動,然後再次進入虛擬機器test02,使用fdisk -l檢視磁碟:
[root@kvm ~]# virsh destroy test02
    Domain test02 destroyed

[root@kvm ~]# virsh start test02
    Domain test02 started

[root@kvm ~]# virsh console test02
[root@localhost ~]# fdisk -l
Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865
Device Boot       Start       End          Blocks   Id  System
/dev/vda1   *     2048       1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736  8e  Linux LVM

Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
可以看到已經變成12 GB了
接下來分割槽:
[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
//新建分割槽
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
//按Enter鍵 
First sector (20971520-25165823, default 20971520): 
//按Enter鍵 
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823):
//按Enter鍵
Using default value 25165823
Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): p
//檢視分割槽列表
Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865

Device Boot      Start       End          Blocks   Id  System
/dev/vda1   *    2048       1026047      512000   83  Linux
/dev/vda2        1026048    20971519     9972736  8e  Linux LVM
/dev/vda3        20971520   25165823     2097152  83  Linux

Command (m for help): w
//儲存退出並退出終端
除了對已有磁碟擴容外,還可以額外增加磁碟:
[root@kvm ~]# qemu-img create -f raw /kvm_data/test02_3.raw 5G
    Formatting '/kvm_data/test02_3.raw', fmt=raw size=5368709120 
使用virsh edit編輯test02虛擬機器,將新磁碟增加到test02虛擬機器:
[root@kvm ~]# virsh edit test02
………………
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_data/test02_2.raw'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' 
function='0x0'/>
</disk>
//找到這一部分內容,在下面增加以下內容
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_data/test02_3.raw'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' 
function='0x0'/>
</disk>
………………

關閉test02虛擬機器,重新啟動,然後進入該虛擬機器,使用fdisk -l檢視磁碟:
[root@kvm ~]# virsh destroy test02
Domain test02 destroyed

[root@kvm ~]# virsh start test02
Domain test02 started
[root@kvm ~]# virsh console test02
[root@localhost ~]# fdisk -l

Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865

   Device Boot      Start      End         Blocks   Id  System
/dev/vda1   *        2048     1026047     512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM
/dev/vda3        20971520    25165823     2097152   83  Linux

Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
可以看到多了一塊5GB的磁碟,退出終端。
接下來,我們擴容qcow2格式的磁碟,命令如下:
[root@kvm ~]# qemu-img resize /kvm_data/test.img +2G
    Image resized.
    //若提示qemu-img: Can't resize an image which has snapshots,需要刪除快照
檢視test.img資訊,命令如下:
[root@kvm ~]# qemu-img info /kvm_data/test.img 
    image: /kvm_data/test.img
    file format: qcow2
    virtual size: 12G (12884901888 bytes)
    disk size: 1.1G
    cluster_size: 65536
    Format specific information:
        compat: 1.1
    lazy refcounts: true
關閉test虛擬機器,重新啟動,然後再次進入虛擬機器test02,使用fdisk -l檢視磁碟:
[root@kvm ~]# virsh destroy test
    Domain test destroyed

[root@kvm ~]# virsh start test
    Domain test started

[root@kvm ~]# virsh console test
[root@localhost ~]# fdisk -l
Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865

Device Boot      Start      End          Blocks   Id  System
/dev/vda1   *     2048     1026047      512000   83  Linux
/dev/vda2         1026048  20971519     9972736  8e  Linux LVM

Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
分割槽新增加的2 GB磁碟,命令如下:
[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (20971520-25165823, default 20971520): 
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-25165823, default 25165823): 
Using default value 25165823
Partition 3 of type Linux and of size 2 GiB is set

Command (m for help): w
儲存並退出終端
新增一塊qcow2格式的磁碟,命令如下:
[root@kvm ~]# qemu-img create -f qcow2 /kvm_data/test_2.img 5G
    Formatting '/kvm_data/test_2.img', fmt=qcow2 
    size=5368709120 encryption=off cluster_size=65536 
    lazy_refcounts=off
使用virsh edit編輯test虛擬機器,將新磁碟增加到test虛擬機器,命令如下:
[root@kvm ~]# virsh edit test
………………
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm_data/test.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
//找到這一部分內容,在下面增加以下內容
       <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm_data/test_2.img'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</disk>
………………
關閉test虛擬機器,重新啟動,然後進入該虛擬機器,使用fdisk -l檢視磁碟:
[root@kvm ~]# virsh destroy test
    Domain test destroyed

[root@kvm ~]# virsh start test
    Domain test started

[root@kvm ~]# virsh console test
[root@localhost ~]# fdisk -l

Disk /dev/vda: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0005b865

Device Boot      Start       End          Blocks   Id  System
/dev/vda1   *    2048       1026047      512000    83  Linux
/dev/vda2        1026048    20971519     9972736   8e  Linux LVM
/dev/vda3        20971520   25165823     2097152   83  Linux

Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-root: 9093 MB, 9093251072 bytes, 17760256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
退出終端

6.調整 CPU 記憶體、網絡卡

檢視虛擬機器配置資訊,命令如下:
[root@kvm ~]# virsh dominfo test
Id:             8
Name:           test
UUID:           49d7cb9c-20dc-42dd-a260-01532b5132e5
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       20.8s
Max memory:     1048576 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0
編輯虛擬機器記憶體,命令如下:
[root@kvm ~]# virsh edit test
…………
<memory unit='KiB'>1048576</memory>
//最大記憶體
  <currentMemory unit='KiB'>524288</currentMemory>
//可用記憶體
  <vcpu placement='static' current='1'>2</vcpu>
//最大cpu
………………
//將以上內容修改為如下內容
…………
<memory unit='KiB'>1048576</memory>  
  <currentMemory unit='KiB'>624288</currentMemory>
  <vcpu placement='static' current='1'>2</vcpu>
…………
關閉虛擬機器,重啟動虛擬機器,命令如下:
[root@kvm ~]# virsh destroy test
Domain test destroyed
[root@kvm ~]# virsh start test
Domain test started
檢視虛擬機器配置資訊,看是否修改成功,命令如下:
[root@kvm ~]# virsh dominfo test
Id:             10
Name:           test
UUID:           49d7cb9c-20dc-42dd-a260-01532b5132e5
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       21.4s
Max memory:     1048576 KiB
Used memory:    624288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0
除了這種修改方式之外,還有一種動態修改,命令如下:
[root@kvm ~]# virsh setmem test 800m
//動態修改記憶體
[root@kvm ~]# virsh setvcpus test 2
//動態修改cpu,只可以增加不可以減少
檢視配置資訊,看是否修改成功,命令如下:
[root@kvm ~]# virsh dominfo test
Id:             10
Name:           test
UUID:           49d7cb9c-20dc-42dd-a260-01532b5132e5
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       22.8s
Max memory:     1048576 KiB
Used memory:    819200 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

[root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml
//需要把配置寫入到配置檔案裡
檢視網絡卡列表,命令如下:
[root@kvm ~]# virsh domiflist test
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet1      bridge     br0        virtio      52:54:00:93:bf:07
增加一塊新的網絡卡,並設定為NAT網路模式(virbr0類似VMware的VMnet8),這裡如果寫--source br0,則網路模式為橋接:
[root@kvm ~]# virsh attach-interface test --type bridge --source virbr0
Interface attached successfully
[root@kvm ~]# virsh domiflist test
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet1      bridge     br0        virtio      52:54:00:93:bf:07
vnet2      bridge     virbr0     rtl8139     52:54:00:29:ed:8b
[root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test.xml
進入test虛擬機器,檢視網絡卡資訊,命令如下:
[root@kvm ~]# virsh console test
[root@localhost ~]# ifconfig
ens10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.8  netmask 255.255.255.0  broadcast 192.168.122.255
        inet6 fe80::5054:ff:fe29:ed8b  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:29:ed:8b  txqueuelen 1000  (Ethernet)
        RX packets 5186  bytes 7413267 (7.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1480  bytes 84889 (82.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.16.129  netmask 255.255.255.0  broadcast 192.168.16.255
        inet6 fe80::5054:ff:fe15:abbd  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:15:ab:bd  txqueuelen 1000  (Ethernet)
        RX packets 13  bytes 1328 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1668 (1.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:93:bf:07  txqueuelen 1000  (Ethernet)
        RX packets 55  bytes 8520 (8.3 KiB)
        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

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 0  (Local Loopback)
        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.遷移虛擬機器

首先關閉虛擬機器,命令如下:
[root@kvm ~]# virsh shutdown test
    Domain test is being shutdown
檢視虛擬機器磁碟所在目錄,命令如下:
[root@kvm ~]# virsh domblklist test
Target     Source
------------------------------------------------
vda        /kvm_data/test.img
vdb        /kvm_data/test_2.img
hda        -
[root@kvm ~]# virsh dumpxml test > /etc/libvirt/qemu/test03.xml 
//如果是遠端機器,需要把該配置檔案拷貝到遠端機器上
[root@kvm ~]# rsync -av /kvm_data/test.img  /kvm_data/test03.img
//-bash: rsync: command not found則yum install -y rsync
//如果是遷移到遠端,則需要把該磁碟檔案拷貝到遠端機器上
sending incremental file list
test.img
sent 10,742,006,844 bytes  received 35 bytes  60,179,310.25 bytes/sec
total size is 10,739,384,832  speedup is 1.00
因為是遷移到本機,配置檔案用的是test子機的配置,不改會有衝突,所以需要修改該檔案,如果是遠端機器不用修改:
[root@kvm ~]# vi /etc/libvirt/qemu/test03.xml 
 <name>test03</name>
//修改domname: 
//修改uuid(隨便改一下數字,位數不要變)
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm_data/test03.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
//修改磁碟路徑
定義新虛擬機器,命令如下:
[root@kvm ~]# virsh define /etc/libvirt/qemu/test03.xml  
    Domain test03 defined from /etc/libvirt/qemu/test03.xml
檢視虛擬機器列表,會發現新遷移的虛擬機器test03:
[root@kvm ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     6     test02                         running
     -     test                           shut off
     -     test03                         shut off