QEMU-KVM虛擬化


以下命令行親自執行有效,執行環境:

Compiled against library: libvirt 4.5.0
Using library: libvirt 4.5.0
Using API: QEMU 4.5.0
Running hypervisor: QEMU 1.5.3

虛擬化的分類

  1. 服務器虛擬化:虛擬服務器
  2. 桌面虛擬化:瘦客戶機連接 win7 實現普通辦公
  3. 存儲虛擬化:
    1.   SAN ( 基於磁盤 )
    2.   NAS ( NFS / Samba )
    3.   GlusterFS
  4. 應用虛擬化:將辦公軟件虛擬化,最典型的就是 office
  5. 網絡虛擬化:SDN
Kernel-based Virtual Machine 的簡稱,是一個開源的系統虛擬化模塊,自 Linux 2.6.20 之后集成在 Linux 的各個主要發行版本中。它使用 Linux 自身的調度器進行管理,所以相對於 Xen,其核心源碼很少。KVM 目前已成為學術界的主流 VMM 之一。
KVM 的虛擬化需要硬件支持(如 Intel VT 技術或者 AMD V 技術)。是基於硬件的完全虛擬化。而 Xen 早期則是基於軟件模擬的 Para-Virtualization,新版本則是基於硬件支持的完全虛擬化。但 Xen 本身有自己的進程調度器,存儲管理模塊等,所以代碼較為龐大。廣為流傳的商業系統虛擬化軟件 VMware ESX 系列是基於軟件模擬的 Full-Virtualization。

hypervisor

Hypervisor——一種運行在基礎物理服務器和操作系統之間的中間軟件層,可允許多個操作系統和應用共享硬件。也可叫做VMM( virtual machine monitor ),即虛擬機監視器。

Hypervisors是一種在虛擬環境中的“元”操作系統。他們可以訪問服務器上包括磁盤和內存在內的所有物理設備。Hypervisors不但協調着這些硬件資源的訪問,而且在各個虛擬機之間施加防護。當服務器啟動並執行Hypervisor時,它會加載所有虛擬機客戶端的操作系統同時會分配給每一台虛擬機適量的內存,CPU,網絡和磁盤。

目前市場上各種x86 管理程序(hypervisor)的架構存在差異,三個最主要的架構類別包括:

  • I型:虛擬機直接運行在系統硬件上,創建硬件全仿真實例,被稱為“裸機”型。裸機型在虛擬化中Hypervisor直接管理調用硬件資源,不需要底層操作系統,也可以將Hypervisor看作一個很薄的操作系統。這種方案的性能處於主機虛擬化與操作系統虛擬化之間。
  • II型:虛擬機運行在傳統操作系統上,同樣創建的是硬件全仿真實例,被稱為“托管(宿主)”型。托管型/主機型Hypervisor運行在基礎操作系統上,構建出一整套虛擬硬件平台(CPU/Memory/Storage/Adapter),使用者根據需要安裝新的操作系統和應用軟件,底層和上層的操作系統可以完全無關化,如Windows運行Linux操作系統。主機虛擬化中VM的應用程序調用硬件資源時需要經過:VM內核->Hypervisor->主機內核,因此相對來說,性能是三種虛擬化技術中最差的。
  • Ⅲ型:虛擬機運行在傳統操作系統上,創建一個獨立的虛擬化實例(容器),指向底層托管操作系統,被稱為“操作系統虛擬化”。操作系統虛擬化是在操作系統中模擬出運行應用程序的容器,所有虛擬機共享內核空間,性能最好,耗費資源最少。但是缺點是底層和上層必須使用同一種操作系統,如底層操作系統運行的是Windows系統,則VPS/VE就必須運行Windows。
 主要廠商及產品:VMware vSphere、微軟Hyper-V、Citrix XenServer 、IBM PowerVM、Red Hat Enterprise Virtulization、Huawei FusionSphere、開源的KVM、Xen、VirtualBSD等。服務器虛擬化需要評估、選擇和部署hypervisor,組織通常會選擇一種主流的hypervisor:VMware的ESXi、微軟的Hyper-V或者思傑的XenServer。然而,對很多組織來說,單獨的hypervisor已經不能滿足所有的虛擬化需求。這時候可以選擇采用第二類hypervisor產品。隨着服務器虛擬化技術的成熟,多hypervisor環境已經變得常見。

全虛擬化

不需要對GuestOS操作系統軟件的源代碼做任何的修改,就可以運行在這樣的VMM中

在全虛擬化的虛擬平台中,GuestOS並不知道自己是一台虛擬機,它會認為自己就是運行在計算機物理硬件設備上的HostOS。因為全虛擬化的VMM會將一個OS所能夠操作的CPU、內存、外設等物理設備邏輯抽象成為虛擬CPU、虛擬內存、虛擬外設等虛擬設備后,再交由GuestOS來操作使用。這樣的GuestOS會將底層硬件平台視為自己所有的,但是實際上,這些都是VMM為GuestOS制造了這種假象。

全虛擬化又分為:軟件輔助的全虛擬化 & 硬件輔助的全虛擬化。

軟件輔助全虛擬化架構圖:

特權解除(優先級壓縮):從上述的軟件輔助全虛擬化架構圖中可以看出,VMM、GuestOS、GuestApplications都是運行在Ring 1-3用戶態中的應用程序代碼。當在GuestOS中執行系統內核的特權指令時,一般都會觸發異常。這是因為用戶態代碼不能直接運行在核心態中,而且系統內核的特權指令大多都只能運行在Ring 0核心態中。在觸發了異常之后,這些異常就會被VMM捕獲,再由VMM將這些特權指令進行虛擬化成為只針對虛擬CPU起作用的虛擬特權指令。其本質就是使用若干能運行在用戶態中的非特權指令來模擬出只針對GuestOS有效的虛擬特權指令,從而將特權指令的特權解除掉。

虛擬化在這里就遇到了一個難題,因為宿主操作系統是工作在ring0的,客戶操作系統就不能也在ring0了,但是它不知道這一點,以前執行什么指令,現在還是執行什么指令,那肯定不行啊,沒權限啊,玩不轉啊。所以這時候虛擬機管理程序(VMM)就要避免這件事情發生。 (VMM在ring0上,一般以驅動程序的形式體現,驅動程序都是工作在ring0上,否則驅動不了設備) 
一般是這樣做,客戶操作系統執行特權指令時,會觸發異常(CPU機制,沒權限的指令,觸發異常),然后VMM捕獲這個異常,在異常里面做翻譯,模擬,最后返回到客戶操作系統內,客戶操作系統認為自己的特權指令工作正常,繼續運行。但是這個性能損耗,就非常的大,你想想原來,簡單的一條指令,執行完,了事,現在卻要通過復雜的異常處理過程。

這時候半虛擬化就來了,半虛擬化的思想就是,讓客戶操作系統知道自己是在虛擬機上跑的,工作在非ring0狀態,那么它原先在物理機上執行的一些特權指令,就會修改成其他方式,這種方式是可以和VMM約定好的,這就相當於,我通過修改代碼把操作系統移植到一種新的架構上來,就是定制化。所以像XEN這種半虛擬化技術,客戶機操作系統都是有一個專門的定制內核版本,和x86、mips、arm這些內核版本等價。這樣以來,就不會有捕獲異常、翻譯、模擬的過程了,性能損耗非常低。這就是XEN這種半虛擬化架構的優勢。這也是為什么XEN只支持虛擬化Linux,無法虛擬化windows原因,微軟不改代碼啊。

半虛擬化 Paravirtualization

半虛擬化是需要GuestOS協助的虛擬化。因為在半虛擬化VVMM中運行的GuestOS,都需要將其內核源碼進行都進過了特別的修改。半虛擬化VMM在處理敏感指令和內核態指令的流程上相對更簡單一些。在半虛擬化VMM上運行的GuestOS都需要修改內核代碼,主要是修改GuestOS指令集中的敏感指令和核心態指令。讓HostOS在捕抓到沒有經過半虛擬化VMM模擬和翻譯處理的GuestOS內核態指令或敏感指令時,HostOS也能夠准確的判斷出該指令是否屬於GuestOS(GuestOS知道自己是虛擬機)。這樣就可以高效的避免了上述問題。典型的半虛擬化軟件有——Xen、KVM-PowerPC(簡易指令集).

可以后來,CPU廠商,開始支持虛擬化了,情況有發生變化,拿X86 CPU來說,引入了Intel-VT 技術,支持Intel-VT 的CPU,有VMX root operation 和 VMX non-root operation兩種模式,兩種模式都支持Ring 0 ~ Ring 3 這 4 個運行級別。這下好了,VMM可以運行在VMX root operation模式下,客戶OS運行在VMX non-root operation模式下。也就說,硬件這層做了些區分,這樣全虛擬化下,有些靠“捕獲異常-翻譯-模擬”的實現就不需要了。而且CPU廠商,支持虛擬化的力度越來越大,靠硬件輔助的全虛擬化技術的性能逐漸逼近半虛擬化,再加上全虛擬化不需要修改客戶操作系統這一優勢,全虛擬化技術應該是未來的發展趨勢。

XEN是最典型的半虛擬化,不過現在XEN也支持硬件輔助的全虛擬化,大趨勢,拗不過啊。。。 
KVM、VMARE這些一直都是全虛擬化。

硬件輔助虛擬化 HVM

005年 — Intel提出並開發了由CPU直接支持的虛擬化技術。這種虛擬化技術引入新的CPU運行模式和新的指令集,使得VMM和GuestOS運行於不同的模式下(VMM=Root Mode;GuestOS=Non-Root Mode),GuestOS運行於受控模式,原來的一些敏感指令在受控模式下會全部陷入VMM,由VMM來實現模擬,這樣就解決了部分非內核態敏感指令的陷入——模擬難題,而且模式切換時上下文的保存恢復由硬件來完成,這樣就大大提高了陷入——模擬時上下文切換的效率 。該技術的引入使x86 CPU可以很容易地實現完全虛擬化。故皆被幾乎所有之前分歧的各大流派所采用,包括KVM-x86,VMWare ESX Server 3,Xen 3.0 。

HVM的分類:
1). Intel –> VT-X
2). AMD –> AMD-V

內存虛擬化

原來的GuestOS使用的是虛擬內存,不可以缺少虛擬內存到物理內存的翻譯,影響了虛擬機的效率。后來Intel EPT AMD RVI表示支持內存虛擬化。

內存虛擬化的映射實現

  • A –> 虛擬地址(VA),指GuestOS提供給其應用程序使用的線性地址空間。
  • B –> 物理地址(PA),經VMM抽象的,虛擬機看到的偽物理地址
  • C –> 機器地址(MA),真是的機器物理地址,即地址總線上出現的地址信號

內存地址的映射關系::
GuestOS:PA = f(VA) #GuestOS維護着一套頁表,負責VA到PA的映射
VMM:MA = g(PA) #VMM維護着一套頁表,負責PA到MA的映射
通過轉換方法實現了從虛擬地址到機器地址的映射。實際運行時,用戶程序訪問VA1,經過GuestOS的頁表轉換得到PA1,再由VMM介入並使用VMM的頁表將PA1轉換為MA1 。

總線虛擬化

分類:

  • 1). Intel –> VT/d
  • 2). AMD –> iommu

總線虛擬化可以實現將一塊網卡分給若干個GuestOS使用,每個虛擬機1/N,性能高,接近真機。
從軟件的角度出發,IO設備就是一堆狀態寄存器,控制寄存器,中斷並與其交互.

主要的虛擬化方式:設備接口完全模擬、前端-后端模擬(Xen) 
直接划分:直接把物理設備划分給Guest OS,無須經過VMM。Intel VT-d

內存虛擬化和總線虛擬化進一步的拉近了GuestOS和HostOS的運行性能。

安裝KVM

安裝步驟

  確認 CPU 已經支持虛擬化,intel 的 CPU 虛擬化技術叫 vmx,AMD 的 CPU 叫 svm

grep -E   "vmx|svm" /proc/cpuinfo
 安裝kvm-qemu平台及工具包
yum install qemu-kvm qemu-kvm-tools libvirt virt-manager virt-install

  啟動libvirtsd並設置為開機啟動,librirtd會創建一個橋接的網卡virbr0而且IP地址是192.168.122.1

systemctl  start libvirtd &  systemctl  enable  libvirtd

  查看網卡virbr0, ifconfig virbr0

  創建一個格式為raw大小為10G的裸磁盤

mkdir /data
qemu-img create -f raw /data/centos7.raw 10G
 成功后顯示:Formatting '/data/centos7.raw', fmt=raw size=10737418240

  使用本地 iso鏡像進行安裝

# 默認網絡
virt-install --virt-type kvm --name centos7 --ram 1024 --cdrom=/home/CentOS-7-x86_64-Minimal-2003.iso --disk path=/data/centos7.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
# 橋接網絡:
virt-install --virt-type kvm --name openstack-middleware1 --ram 4096 --vcpus 4 --cdrom=/usr/local/src/CentOS-7-x86_64-Minimal-1511.iso --disk path=/var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud-1511-ok.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

  鏈接VNS

  開始你的虛擬機中嵌套虛擬機的旅程吧。虛機安裝過程略,過程中給內核傳遞 net.ifnames=0 以及 biosdevname=0 使網卡的命名為 eth*

 查看正在運行的虛擬機,查看virsh幫助

[root@hadoop001 ~]# virsh -help

virsh [options]... [<command_string>]
virsh [options]... <command> [args...]

  options:
    -c | --connect=URI      hypervisor connection URI
    -d | --debug=NUM        debug level [0-4]
    -e | --escape <char>    set escape sequence for console
    -h | --help             this help
    -k | --keepalive-interval=NUM
                            keepalive interval in seconds, 0 for disable
    -K | --keepalive-count=NUM
                            number of possible missed keepalive messages
    -l | --log=FILE         output logging to file
    -q | --quiet            quiet mode
    -r | --readonly         connect readonly
    -t | --timing           print timing information
    -v                      short version
    -V                      long version
         --version[=TYPE]   version, TYPE is short or long (default short)
  commands (non interactive mode):

 Domain Management (help keyword 'domain')
    attach-device                  attach device from an XML file
    attach-disk                    attach disk device
    attach-interface               attach network interface
    autostart                      autostart a domain
    blkdeviotune                   Set or query a block device I/O tuning parameters.
    blkiotune                      Get or set blkio parameters
    blockcommit                    Start a block commit operation.
    blockcopy                      Start a block copy operation.
    blockjob                       Manage active block operations
    blockpull                      Populate a disk from its backing image.
    blockresize                    Resize block device of domain.
    change-media                   Change media of CD or floppy drive
    console                        connect to the guest console
    cpu-stats                      show domain cpu statistics
    create                         create a domain from an XML file
    define                         define (but don't start) a domain from an XML file
    desc                           show or set domain's description or title
    destroy                        destroy (stop) a domain
    detach-device                  detach device from an XML file
    detach-device-alias            detach device from an alias
    detach-disk                    detach disk device
    detach-interface               detach network interface
    domdisplay                     domain display connection URI
    domfsfreeze                    Freeze domain's mounted filesystems.
    domfsthaw                      Thaw domain's mounted filesystems.
    domfsinfo                      Get information of domain's mounted filesystems.
    domfstrim                      Invoke fstrim on domain's mounted filesystems.
    domhostname                    print the domain's hostname
    domid                          convert a domain name or UUID to domain id
    domif-setlink                  set link state of a virtual interface
    domiftune                      get/set parameters of a virtual interface
    domjobabort                    abort active domain job
    domjobinfo                     domain job information
    domname                        convert a domain id or UUID to domain name
    domrename                      rename a domain
    dompmsuspend                   suspend a domain gracefully using power management functions
    dompmwakeup                    wakeup a domain from pmsuspended state
    domuuid                        convert a domain name or id to domain UUID
    domxml-from-native             Convert native config to domain XML
    domxml-to-native               Convert domain XML to native config
    dump                           dump the core of a domain to a file for analysis
    dumpxml                        domain information in XML
    edit                           edit XML configuration for a domain
    event                          Domain Events
    inject-nmi                     Inject NMI to the guest
    iothreadinfo                   view domain IOThreads
    iothreadpin                    control domain IOThread affinity
    iothreadadd                    add an IOThread to the guest domain
    iothreaddel                    delete an IOThread from the guest domain
    send-key                       Send keycodes to the guest
    send-process-signal            Send signals to processes
    lxc-enter-namespace            LXC Guest Enter Namespace
    managedsave                    managed save of a domain state
    managedsave-remove             Remove managed save of a domain
    managedsave-edit               edit XML for a domain's managed save state file
    managedsave-dumpxml            Domain information of managed save state file in XML
    managedsave-define             redefine the XML for a domain's managed save state file
    memtune                        Get or set memory parameters
    perf                           Get or set perf event
    metadata                       show or set domain's custom XML metadata
    migrate                        migrate domain to another host
    migrate-setmaxdowntime         set maximum tolerable downtime
    migrate-getmaxdowntime         get maximum tolerable downtime
    migrate-compcache              get/set compression cache size
    migrate-setspeed               Set the maximum migration bandwidth
    migrate-getspeed               Get the maximum migration bandwidth
    migrate-postcopy               Switch running migration from pre-copy to post-copy
    numatune                       Get or set numa parameters
    qemu-attach                    QEMU Attach
    qemu-monitor-command           QEMU Monitor Command
    qemu-monitor-event             QEMU Monitor Events
    qemu-agent-command             QEMU Guest Agent Command
    reboot                         reboot a domain
    reset                          reset a domain
    restore                        restore a domain from a saved state in a file
    resume                         resume a domain
    save                           save a domain state to a file
    save-image-define              redefine the XML for a domain's saved state file
    save-image-dumpxml             saved state domain information in XML
    save-image-edit                edit XML for a domain's saved state file
    schedinfo                      show/set scheduler parameters
    screenshot                     take a screenshot of a current domain console and store it into a file
    set-lifecycle-action           change lifecycle actions
    set-user-password              set the user password inside the domain
    setmaxmem                      change maximum memory limit
    setmem                         change memory allocation
    setvcpus                       change number of virtual CPUs
    shutdown                       gracefully shutdown a domain
    start                          start a (previously defined) inactive domain
    suspend                        suspend a domain
    ttyconsole                     tty console
    undefine                       undefine a domain
    update-device                  update device from an XML file
    vcpucount                      domain vcpu counts
    vcpuinfo                       detailed domain vcpu information
    vcpupin                        control or query domain vcpu affinity
    emulatorpin                    control or query domain emulator affinity
    vncdisplay                     vnc display
    guestvcpus                     query or modify state of vcpu in the guest (via agent)
    setvcpu                        attach/detach vcpu or groups of threads
    domblkthreshold                set the threshold for block-threshold event for a given block device or it's backing chain element

 Domain Monitoring (help keyword 'monitor')
    domblkerror                    Show errors on block devices
    domblkinfo                     domain block device size information
    domblklist                     list all domain blocks
    domblkstat                     get device block stats for a domain
    domcontrol                     domain control interface state
    domif-getlink                  get link state of a virtual interface
    domifaddr                      Get network interfaces' addresses for a running domain
    domiflist                      list all domain virtual interfaces
    domifstat                      get network interface stats for a domain
    dominfo                        domain information
    dommemstat                     get memory statistics for a domain
    domstate                       domain state
    domstats                       get statistics about one or multiple domains
    domtime                        domain time
    list                           list domains

 Host and Hypervisor (help keyword 'host')
    allocpages                     Manipulate pages pool size
    capabilities                   capabilities
    cpu-baseline                   compute baseline CPU
    cpu-compare                    compare host CPU with a CPU described by an XML file
    cpu-models                     CPU models
    domcapabilities                domain capabilities
    freecell                       NUMA free memory
    freepages                      NUMA free pages
    hostname                       print the hypervisor hostname
    hypervisor-cpu-baseline        compute baseline CPU usable by a specific hypervisor
    hypervisor-cpu-compare         compare a CPU with the CPU created by a hypervisor on the host
    maxvcpus                       connection vcpu maximum
    node-memory-tune               Get or set node memory parameters
    nodecpumap                     node cpu map
    nodecpustats                   Prints cpu stats of the node.
    nodeinfo                       node information
    nodememstats                   Prints memory stats of the node.
    nodesuspend                    suspend the host node for a given time duration
    sysinfo                        print the hypervisor sysinfo
    uri                            print the hypervisor canonical URI
    version                        show version

 Interface (help keyword 'interface')
    iface-begin                    create a snapshot of current interfaces settings, which can be later committed (iface-commit) or restored (iface-rollback)
    iface-bridge                   create a bridge device and attach an existing network device to it
    iface-commit                   commit changes made since iface-begin and free restore point
    iface-define                   define an inactive persistent physical host interface or modify an existing persistent one from an XML file
    iface-destroy                  destroy a physical host interface (disable it / "if-down")
    iface-dumpxml                  interface information in XML
    iface-edit                     edit XML configuration for a physical host interface
    iface-list                     list physical host interfaces
    iface-mac                      convert an interface name to interface MAC address
    iface-name                     convert an interface MAC address to interface name
    iface-rollback                 rollback to previous saved configuration created via iface-begin
    iface-start                    start a physical host interface (enable it / "if-up")
    iface-unbridge                 undefine a bridge device after detaching its slave device
    iface-undefine                 undefine a physical host interface (remove it from configuration)

 Network Filter (help keyword 'filter')
    nwfilter-define                define or update a network filter from an XML file
    nwfilter-dumpxml               network filter information in XML
    nwfilter-edit                  edit XML configuration for a network filter
    nwfilter-list                  list network filters
    nwfilter-undefine              undefine a network filter
    nwfilter-binding-create        create a network filter binding from an XML file
    nwfilter-binding-delete        delete a network filter binding
    nwfilter-binding-dumpxml       network filter information in XML
    nwfilter-binding-list          list network filter bindings

 Networking (help keyword 'network')
    net-autostart                  autostart a network
    net-create                     create a network from an XML file
    net-define                     define an inactive persistent virtual network or modify an existing persistent one from an XML file
    net-destroy                    destroy (stop) a network
    net-dhcp-leases                print lease info for a given network
    net-dumpxml                    network information in XML
    net-edit                       edit XML configuration for a network
    net-event                      Network Events
    net-info                       network information
    net-list                       list networks
    net-name                       convert a network UUID to network name
    net-start                      start a (previously defined) inactive network
    net-undefine                   undefine a persistent network
    net-update                     update parts of an existing network's configuration
    net-uuid                       convert a network name to network UUID

 Node Device (help keyword 'nodedev')
    nodedev-create                 create a device defined by an XML file on the node
    nodedev-destroy                destroy (stop) a device on the node
    nodedev-detach                 detach node device from its device driver
    nodedev-dumpxml                node device details in XML
    nodedev-list                   enumerate devices on this host
    nodedev-reattach               reattach node device to its device driver
    nodedev-reset                  reset node device
    nodedev-event                  Node Device Events

 Secret (help keyword 'secret')
    secret-define                  define or modify a secret from an XML file
    secret-dumpxml                 secret attributes in XML
    secret-event                   Secret Events
    secret-get-value               Output a secret value
    secret-list                    list secrets
    secret-set-value               set a secret value
    secret-undefine                undefine a secret

 Snapshot (help keyword 'snapshot')
    snapshot-create                Create a snapshot from XML
    snapshot-create-as             Create a snapshot from a set of args
    snapshot-current               Get or set the current snapshot
    snapshot-delete                Delete a domain snapshot
    snapshot-dumpxml               Dump XML for a domain snapshot
    snapshot-edit                  edit XML for a snapshot
    snapshot-info                  snapshot information
    snapshot-list                  List snapshots for a domain
    snapshot-parent                Get the name of the parent of a snapshot
    snapshot-revert                Revert a domain to a snapshot

 Storage Pool (help keyword 'pool')
    find-storage-pool-sources-as   find potential storage pool sources
    find-storage-pool-sources      discover potential storage pool sources
    pool-autostart                 autostart a pool
    pool-build                     build a pool
    pool-create-as                 create a pool from a set of args
    pool-create                    create a pool from an XML file
    pool-define-as                 define a pool from a set of args
    pool-define                    define an inactive persistent storage pool or modify an existing persistent one from an XML file
    pool-delete                    delete a pool
    pool-destroy                   destroy (stop) a pool
    pool-dumpxml                   pool information in XML
    pool-edit                      edit XML configuration for a storage pool
    pool-info                      storage pool information
    pool-list                      list pools
    pool-name                      convert a pool UUID to pool name
    pool-refresh                   refresh a pool
    pool-start                     start a (previously defined) inactive pool
    pool-undefine                  undefine an inactive pool
    pool-uuid                      convert a pool name to pool UUID
    pool-event                     Storage Pool Events

 Storage Volume (help keyword 'volume')
    vol-clone                      clone a volume.
    vol-create-as                  create a volume from a set of args
    vol-create                     create a vol from an XML file
    vol-create-from                create a vol, using another volume as input
    vol-delete                     delete a vol
    vol-download                   download volume contents to a file
    vol-dumpxml                    vol information in XML
    vol-info                       storage vol information
    vol-key                        returns the volume key for a given volume name or path
    vol-list                       list vols
    vol-name                       returns the volume name for a given volume key or path
    vol-path                       returns the volume path for a given volume name or key
    vol-pool                       returns the storage pool for a given volume key or path
    vol-resize                     resize a vol
    vol-upload                     upload file contents to a volume
    vol-wipe                       wipe a vol

 Virsh itself (help keyword 'virsh')
    cd                             change the current directory
    echo                           echo arguments
    exit                           quit this interactive terminal
    help                           print help
    pwd                            print the current directory
    quit                           quit this interactive terminal
    connect                        (re)connect to hypervisor
View Code
# 當前正在運行的所有虛擬機:virsh list --all

# 命令幫助:virsh list --help
# 列出關閉的虛擬機:virsh list --inactive

  可以看到狀態是關閉的。

  啟動虛擬機

virsh start centos7

  關閉虛擬機

  virsh shutdown centos7

  查看配置文件:/etc/libvirt/qemu/這個目錄下保存已經安裝虛擬機的信息

      查看centos7.xml

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit centos7
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>centos7</name>
  <uuid>6f0b80d9-4e1a-4d19-97d4-79459e5985d8</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-noTSX-IBRS</model>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='spec-ctrl'/>
    <feature policy='require' name='ssbd'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/data/centos7.raw'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:93:61:2e'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </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='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <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>

  備份虛擬機 

virsh dumpxml CentOS7 > CentOS7.xml.bak

  刪除一個虛擬機

virsh undefine centos7

  從備份的 xml 文件中恢復

virsh define /root/CentOS7.xml.bak

到這里,一個完整的安裝、刪除、備份流程完成了,接下來開始進行CPU、內存、網絡的配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM