管理虛擬存儲(二)

管理虛擬存儲(二)

五.管理虛擬存儲

5.1 虛擬磁盤概述

5.1.1 虛擬化項目中存儲的注意事項

  • 存儲的性能幾乎老是虛擬化的瓶頸
  • 經過多個硬盤驅動以分佈磁盤I/O來實現存儲解決方案
  • 考慮部署集中化的SAN/NFS來實現高可用性和實時遷移

image.png-159.1kB

5.1.2 KVM存儲模式

基於文件系統的存儲linux

  • dir:Filesystem Directory 虛擬文件系統目錄
  • fs:Pre-Formatted Block Device
  • netfs:Network Exported Directory

基於設備的存儲ios

  • Disk:Physical Disk Device
  • Iscsi:iSCSI Target
  • logical:LVM Volume Group

image.png-27.2kB

5.1.3 虛擬磁盤類型

固定 Fixedvim

  • 在配置時,指定磁盤大小
  • 無論在虛擬磁盤上實際存儲多少數據,都將佔用相同大小主機磁盤空間

動態 Dynamic後端

  • 增加到最大容量,可是隻根據需求使用更多的空間

差別 Differencingcentos

  • 由於建立是差別磁盤,因此只保存變動的數據
  • 例如,將操做系統安裝在父盤,而後建立差別化磁盤來執行進一步配置

5.1.4 KVM支持的虛擬磁盤類型

  • raw
    這並不是是一種真正的磁盤格式,而是表明虛擬機所使用的原始鏡像
    它並不存儲元數據,所以能夠做爲保證虛擬機兼容性的候選方案。然而,也正由於它不存儲元數據,所以不能自持某些高級特性,好比快照和壓縮等。
    格式簡單,容易轉換爲其餘的格式。須要文件系統的支持才能支持sparse file
  • cow:copy-on-write格式,曇花一現
  • qcow:QEMU早期的copy-on-write格式,過渡性方案
  • qcow2
    按需進行分配磁盤空間,無論文件系統是否支持
    支持快照
    支持zlib的磁盤壓縮
    支持AES的加密
  • vmdk(Virtual Machine Disk)
    VMware環境當中默認使用的磁盤格式
  • vhd  vhdx(Virtual Hard Disk)
    微軟默認採用的文件格式
  • vdi(VirtualBox)
  • 能夠經過qemu-img --help查看支持的格式
[root@KVM ~]# qemu-img --help | grep Supported
Supported formats: vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug

5.2 使用qemu-img管理虛擬磁盤

5.2.1 qemu-img概述

qemu-img 是一個功能強制磁盤鏡像管理工具
qemu-img --help 包括如下功能網絡

  • check 檢查完整性
  • create 建立鏡像
  • commit 提交更改
  • compare 比較
  • convert 轉換
  • info 得到信息
  • map 映射
  • snapshot 快照管理
  • rebase 在已有的鏡像的基礎上建立新的鏡像
  • resize 調整大小
  • amend 修訂鏡像格式選項

5.2.2 建立虛擬磁盤

[root@KVM ~]# qemu-img | grep create         #建立磁盤的命令格式
  create [-q] [-f fmt] [-o options] filename [size]
       contain only zeros for qemu-img to create a sparse image during
  '-n' skips the target volume creation (useful if the volume is created
  'snapshot' is the name of the snapshot to create, apply or delete
  '-c' creates a snapshot
[root@KVM ~]# qemu-img create t1.img 1g         #只輸入磁盤名和大小建立
Formatting 't1.img', fmt=raw size=1073741824     #默認的磁盤格式fmt=raw
[root@KVM ~]# qemu-img info t1.img               #查看虛擬磁盤的信息
image: t1.img                        #文件名稱
file format: raw                     #文件格式
virtual size: 1.0G (1073741824 bytes)
disk size: 0                         #虛擬磁盤大小
[root@KVM ~]# ll -h t1.img           #磁盤空間尺寸是0????
-rw-r--r--. 1 root root 1.0G 12月 30 14:49 t1.img    #ll查看磁盤是1G沒錯
[root@KVM ~]# du -sh t1.img    #但咱們用du查看一下,發現磁盤真實空間的佔用還真是0
0   t1.img

根據以上測試,咱們發現默認狀況下qemu-img建立的磁盤文件的類型是動態的(Dynamic 空洞)會根據真實存放數據的大小進行動態擴容直到磁盤空間設定值的大小。app

#假如咱們想看一下各類磁盤格式所附帶的-o option都有什麼,咱們能夠這麼作
[root@KVM ~]# qemu-img create -f raw -o ?       #raw格式磁盤只有一個尺寸大小選項 
Supported options:
size             Virtual disk size

[root@KVM ~]# qemu-img create -f qcow2 -o?      #qcow2則有不少選項
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image          #用於指定後端鏡像文件
backing_fmt      Image format of the base image     #設置後端鏡像的鏡像格式  
encryption       Encrypt the image                  #用於設置加密
cluster_size     qcow2 cluster size      #設置鏡像中的簇大小,取值在512到2M之間,默認值64K
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)             #設置鏡像文件空間的預分配模式
lazy_refcounts   Postpone refcount updates

利用dd命令模擬建立一個沒有空洞的文件dom

[root@KVM ~]# dd if=/dev/zero of=flat1.img bs=1024k count=1000
記錄了1000+0 的讀入
記錄了1000+0 的寫出
1048576000字節(1.0 GB)已複製,32.3206 秒,32.4 MB/秒

[root@KVM ~]# qemu-img info flat1.img 
image: flat1.img
file format: raw
virtual size: 1.0G (1048576000 bytes)
disk size: 1.0G        #磁盤佔用也是1G,沒有空洞
#ext4文件格式是支持空洞文件的建立的,例如dd命令咱們也能夠實現空洞文件的建立
[root@KVM ~]# dd if=/dev/zero of=flat2.img bs=1024k count=0 seek=1024
記錄了0+0 的讀入
記錄了0+0 的寫出
0字節(0 B)已複製,0.00039378 秒,0.0 kB/秒

[root@KVM ~]# qemu-img info flat2.img 
image: flat2.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0           #實際磁盤佔用0

空洞文件被複制之後,那麼仍是空洞文件嗎?ssh

[root@KVM ~]# du -sh flat*
1000M   flat1.img
0   flat2.img
[root@KVM ~]# cp flat1.img flat1a.img
[root@KVM ~]# cp flat2.img flat2a.img
[root@KVM ~]# du -sh flat*
1000M   flat1a.img        #非空洞文件複製後仍是非空洞
1000M   flat1.img
0   flat2a.img            #空洞文件複製後仍是空洞文件
0   flat2.img
#利用--sparse=always將非空洞文件複製成空洞文件
[root@KVM ~]# du -sh flat*
1000M   flat1a.img
1000M   flat1.img
0   flat2a.img
0   flat2.img
[root@KVM ~]# cp flat1.img flat1b.img --sparse=always
[root@KVM ~]# du -sh flat*
1000M   flat1a.img
0   flat1b.img            #加了參數的cp複製出來的是空洞文件
1000M   flat1.img
0   flat2a.img
0   flat2.img
#利用--sparse=never將空洞文件複製成非空洞文件
[root@KVM ~]# du -sh flat*
1000M   flat1a.img
0   flat1b.img
1000M   flat1.img
0   flat2a.img
0   flat2.img
[root@KVM ~]# cp flat2.img flat2b.img --sparse=never
[root@KVM ~]# du -sh flat*
1000M   flat1a.img
0   flat1b.img
1000M   flat1.img
0   flat2a.img
1.0G    flat2b.img          #加了參數的cp複製出來的是非空洞文件
0   flat2.img

5.2.3 檢查虛擬磁盤

[root@KVM vm]# pwd
/vm
[root@KVM vm]# du -sh ce*
1.1G    centos6.5.qcow2

[root@KVM vm]# qemu-img info centos6.5.qcow2
image: centos6.5.qcow2
file format: qcow2                           #文件格式
virtual size: 8.0G (8589934592 bytes)        #虛擬磁盤尺寸大小
disk size: 1.1G                              #磁盤真實佔用大小
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    
[root@KVM vm]# qemu-img check centos6.5.qcow2
No errors were found on the image.           #檢查結果沒有錯誤
131072/131072 = 100.00% allocated, 0.00% fragmented, 0.00% compressed clusters
Image end offset: 8591507456

5.2.4 預分配磁盤策略(qcow2)

  • off :缺省策略,即不使用預分配策略
  • metadata :分配元數據(metadata),預分配後的虛擬磁盤仍然屬於稀疏映像類型(空洞文件)
  • full :分配全部磁盤空間並置零,預分配後的虛擬磁盤屬於非稀疏映像類型
  • falloc :分配文件的塊並標示它們的狀態爲未初始化,相對full模式來講,建立虛擬磁盤的速度要快不少。
[root@KVM vm]# qemu-img create -f qcow2 test2.qcow2 -o ?
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image
backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)     #預分配策略
lazy_refcounts   Postpone refcount updates
#preallocation=off建立磁盤
[root@KVM vm]# qemu-img create -f qcow2 test1.qcow2 1g -o preallocation=off 
Formatting 'test1.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='off' lazy_refcounts=off 

[root@KVM vm]# qemu-img info test1.qcow2 
image: test1.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K                #咱們發現磁盤實際佔用196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
#preallocation=metadata建立磁盤
[root@KVM vm]# qemu-img create -f qcow2 test2.qcow2 1g -o preallocation=metadata
Formatting 'test2.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off 

[root@KVM vm]# qemu-img info test2.qcow2
image: test2.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 516K                #咱們發現磁盤的實際佔用變成了516K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
#preallocation=falloc建立磁盤
[root@KVM vm]# qemu-img create -f qcow2 test3.qcow2 1g -o preallocation=falloc
Formatting 'test3.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='falloc' lazy_refcounts=off 

[root@KVM vm]# qemu-img info test3.qcow2
image: test3.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0G                #咱們發現磁盤真實佔用也是1G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
#preallocation=full建立磁盤
[root@KVM vm]# qemu-img create -f qcow2 test4.qcow2 1g -o preallocation=full
Formatting 'test4.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='full' lazy_refcounts=off 

[root@KVM vm]# qemu-img info test4.qcow2
image: test4.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0G        #咱們發現falloc和full建立的磁盤的真實佔用都是1G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
#四種預分配策略進行對比
[root@KVM vm]# ll -h test*
-rw-r--r--. 1 root root 193K 12月 30 17:10 test1.qcow2
-rw-r--r--. 1 root root 1.1G 12月 30 18:04 test2.qcow2
-rw-r--r--. 1 root root 1.1G 12月 30 18:05 test3.qcow2
-rw-r--r--. 1 root root 1.1G 12月 30 18:08 test4.qcow2

[root@KVM vm]# du -sh test*
196K    test1.qcow2
516K    test2.qcow2
1.1G    test3.qcow2
1.1G    test4.qcow2

經過對比咱們發現預分配策略裏,off和metadata預分配策略都屬於空洞文件,而falloc和full屬於非空洞文件。tcp

5.2.5 後備差別虛擬磁盤

  • 存儲與基礎鏡像(父)磁盤的變化
    基礎鏡像(父)磁盤不會改變
    差別磁盤隔離變化
    多個差別磁盤可使用相同的基礎鏡像(父)磁盤
  • 優勢:標準化基礎鏡像,節省空間
  • 缺點:增長了開銷,較差的性能

image.png-24.3kB

image.png-69.7kB

image.png-35.8kB

#建立父磁盤並安裝父Centos6.5操做系統
[root@KVM vm]# qemu-img create -f qcow2 Base_CentOS6.5.qcow2
qemu-img: Base_CentOS6.5.qcow2: Image creation needs a size parameter

[root@KVM vm]# qemu-img create -f qcow2 Base_CentOS6.5.qcow2 10G
Formatting 'Base_CentOS6.5.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off 

[root@KVM vm]# virt-install --name=Base_CentOS7 --disk path=/vm/Base_CentOS6.5.qcow2 --vcpu=1 --ram=1024 --cdrom=/iso/CentOS-6.5-x86_64-bin-DVD1.iso --network network=default --graphics vnc,listen=0.0.0.0 --os-type=linux --os-variant=rhel6

[root@KVM ~]# virsh list
 Id    名稱                         狀態
----------------------------------------------------
 3     centos6.5                      running
 6     vm1                            running
 8     Base_CentOS7                   running
#建立OA後備差別虛擬磁盤,OA子系統盤
[root@KVM ~]# qemu-img create -f qcow2 \
> -o backing_file=Base_CentOS6.5.qcow2 \       #指定父盤
> OA-disk0.qcow2                               #子盤名稱
Formatting 'OA-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

[root@KVM vm]# qemu-img info OA-disk0.qcow2
image: OA-disk0.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: Base_CentOS6.5.qcow2             #父盤名稱
Format specific information:
    compat: 1.1
    lazy refcounts: false
#建立ERP,HR,CRM子系統盤
[root@KVM vm]# qemu-img create -f qcow2 -o backing_file=Base_CentOS6.5.qcow2 ERP-disk0.qcow2Formatting 'ERP-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

[root@KVM vm]# qemu-img create -f qcow2 -o backing_file=Base_CentOS6.5.qcow2 HR-disk0.qcow2
Formatting 'HR-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 

[root@KVM vm]# qemu-img create -f qcow2 -o backing_file=Base_CentOS6.5.qcow2 CRM-disk0.qcow2Formatting 'CRM-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 


[root@KVM vm]# ll -h *-disk0.qcow2
-rw-r--r--. 1 root root 193K 12月 30 21:29 CRM-disk0.qcow2
-rw-r--r--. 1 root root 193K 12月 30 21:28 ERP-disk0.qcow2
-rw-r--r--. 1 root root 193K 12月 30 21:29 HR-disk0.qcow2
-rw-r--r--. 1 root root 193K 12月 30 21:27 OA-disk0.qcow2
#建立四種子系統虛擬機
[root@KVM vm]# virt-install --import \
> --name=oa \
> --vcpus=1 --ram=512 \
> --disk path=/vm/OA-disk0.qcow2 \
> --network network=default \
> --graphics vnc,listen=0.0.0.0 \
> --os-variant=rhel6 \
> --os-type=linux
開始安裝......
建立域......                   
##中間省略....
域建立完成。


[root@KVM vm]# virt-install --import --name=erp --vcpus=1 --ram=512 --disk path=/vm/ERP-disk0.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-variant=rhel6 --os-type=linux
開始安裝......
建立域......                   
##中間省略....
域建立完成。


[root@KVM vm]# virt-install --import --name=hr --vcpus=1 --ram=512 --disk path=/vm/HR-disk0.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-variant=rhel6 --os-type=linux 
開始安裝......
建立域......                   
##中間省略....
域建立完成。


[root@KVM vm]# virt-install --import --name=crm --vcpus=1 --ram=512 --disk path=/vm/CRM-disk0.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-variant=rhel6 --os-type=linux
開始安裝......
建立域......                   
##中間省略....
域建立完成。
[root@KVM vm]# virsh list --all
 Id    名稱                         狀態
----------------------------------------------------
 3     centos6.5                      running
 6     vm1                            running
 8     Base_CentOS7                   running
 9     oa                             running     #子系統建立完畢
 10    erp                            running     #子系統建立完畢
 11    hr                             running     #子系統建立完畢
 12    crm                            running     #子系統建立完畢

父子系統的方式,其實就是克隆的方式,咱們若是要經過命令來實現就是如此操做。
若是在圖形界面下,相似的方式以下圖

image.png-24.9kB

5.2.6 虛擬磁盤格式轉換(虛擬機遷移案例)
#語法格式
[root@KVM vm]# qemu-img --help | grep convert
  convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename

案例:不一樣格式虛擬機的遷移
(1)咱們在VMware Workstation虛擬機中找一個關閉狀態的虛擬機進行導出
image.png-77.1kB

image.png-22.6kB
(2)先將.vmdk虛擬磁盤文件拷入到KVM虛擬機中

[root@KVM vm]# ll -h *.vmdk
-rw-r--r--. 1 root root 924M 12月 31 20:56 LNMP-disk1.vmdk

[root@KVM vm]# qemu-img info LNMP-disk1.vmdk
image: LNMP-disk1.vmdk
file format: vmdk
virtual size: 20G (21474836480 bytes)
disk size: 923M
cluster_size: 65536
Format specific information:
    cid: 3915664802
    parent cid: 4294967295
    create type: streamOptimized
    extents:
        [0]:
            compressed: true
            virtual size: 21474836480
            filename: LNMP-disk1.vmdk
            cluster size: 65536
            format:

(3)而後進行磁盤文件的格式轉換

[root@KVM vm]# du -sh LNMP-disk1.vmdk
924M    LNMP-disk1.vmdk
[root@KVM vm]# qemu-img convert -O qcow2 LNMP-disk1.vmdk LNMP-disk1.qcow2
[root@KVM vm]# du -sh LNMP*
2.5G    LNMP-disk1.qcow2
924M    LNMP-disk1.vmdk

(4)而後根據虛擬磁盤遷移前的VMware配置,導入到新的KVM虛擬機

  • 1個vcpu
  • 1GB內存
  • 兩個網卡(橋接+NAT)
  • 磁盤類型:scsi
  • VNC顯示卡
  • OS類型爲RHEL6
#導入新的KVM虛擬機
[root@KVM vm]# virt-install --import --name=LNMP --vcpus=1 --ram=1024 --disk bus=scsi,path=/vm/LNMP-disk1.qcow2 --network type=bridge,source=virbr0 --network network=default --graphics vnc,listen=0.0.0.0 --os-type=linux  --os-variant=rhel6 --noautoconsole

開始安裝......
域建立完成。
[root@KVM vm]# virsh list
 Id    名稱                         狀態
----------------------------------------------------
 13    LNMP                           running

5.2.7 調整虛擬磁盤大小

語法格式

[root@KVM vm]# qemu-img --help | grep resize
  resize [-q] filename [+ | -]size
  • 操做以前,必定要作好數據備份
  • 增長文件大小後,須要在客戶機中使用fdisk,parted等分區工具進行相應操做才能真正讓客戶機使用到增長後的鏡像空間。
  • 縮小鏡像以前,要在客戶機中保證裏面的文件系統有空餘空間,不然會數據丟失
  • qcow2不支持縮小鏡像的操做
#實操
[root@KVM vm]# qemu-img info LNMP-disk1.qcow2 
image: LNMP-disk1.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)          #磁盤原始尺寸
disk size: 2.4G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

[root@KVM vm]# qemu-img resize LNMP-disk1.qcow2 +10G    #增長10G尺寸
Image resized.

[root@KVM vm]# qemu-img info LNMP-disk1.qcow2 
image: LNMP-disk1.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)          #擴大了10G
disk size: 2.4G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

5.3快照管理

5.3.1 快照/檢查點概述

磁盤快照

  • 對磁盤數據進行快照
  • 主要用於虛擬機備份等場合

內存快照

  • 對虛擬機的內存/設備信息進行保存
  • 該機制同時用於休眠恢復,遷移等場景
  • 主要使用virsh save(qemu migrate to file)實現,只能對運行的虛擬機進行

檢查點快照

  • 同時保存虛擬機的磁盤快照和內存快照
  • 用於將虛擬機恢復到某個時間點
  • 能夠保證數據的一致性

image.png-15.6kB

5.3.2 磁盤快照分類

按快照信息保存分爲:

  • 內置快照:快照數據和base磁盤數據放在一個qcow2文件中
  • 外置快照:快照數據單獨的qcow2文件存放

按虛擬機狀態能夠分爲:

  • 關機態快照:數據能夠保持一致性
  • 運行態快照:數據沒法保持一致性,相似與系統crash後的磁盤數據。使用是可能須要fsck等操做。

按磁盤數量能夠分爲:

  • 單盤:單盤快照不涉及原子性
  • 多盤:涉及原子性。主要分兩個方面:1,是全部盤快照點相同 2,全部盤要麼都快照成功,要麼都快照失敗。主要依賴於qemu的transaction實現

5.3.3 管理磁盤快照

語法格式:

[root@KVM vm]# qemu-img --help | grep snapshot
  snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
Parameters to snapshot subcommand:
  'snapshot' is the name of the snapshot to create, apply or delete
  '-a' applies a snapshot (revert disk to saved state)  #快照回滾
  '-c' creates a snapshot        #建立快照
  '-d' deletes a snapshot        #刪除快照
  '-l' lists all snapshots in the given image           #顯示快照列表

快照管理之磁盤快照實操:

#建立磁盤快照
[root@KVM vm]# qemu-img snapshot -l /vm/Base_CentOS6.5.qcow2 
[root@KVM vm]# qemu-img snapshot -c s1 /vm/Base_CentOS6.5.qcow2 #建立磁盤快照
[root@KVM vm]# qemu-img snapshot -l /vm/Base_CentOS6.5.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         s1                        0 2018-05-06 13:33:16   00:00:00.000


#關閉虛擬機回滾磁盤快照
#在虛擬機裏刪除一個文件而後進行磁盤迴滾
[root@KVM vm]# qemu-img snapshot -a s1 /vm/Base_CentOS6.5.qcow2

須要注意的是,進行磁盤快照回滾時須要關閉虛擬機,回滾後,咱們發現數據已經恢復
qemu-img方式的磁盤快照只能支持原生態的qcow2格式,不支持從raw格式轉換而來的qcow2否則會出現問題。

5.4 存儲池

5.4.1 存儲池的基礎概念

Libvirt能夠以存儲池的形式對存儲進行統一管理,簡化操做
對於虛擬機操做來講,存儲池和卷並非必須的
支持如下存儲池

  • dir:Filesystem Directory
  • disk:Physical Disk Device
  • fs:Pre-Formatted Block Device
  • gluster:Gluster FileSystem
  • iscsi:iSCSI Target
  • logical:LVM Volume Group
  • mpath:Multipath Device Enumerator
  • netfs:Network Export Directory
  • rbd:RADOS Block Device/Ceph
  • scsi:SCSI Host Adapter
  • sheepdog:Sheepdog Filesystem

image.png-40.4kB

[root@KVM ~]# cd /etc/libvirt/storage/
[root@KVM storage]# ll
總用量 12
drwxr-xr-x. 2 root root  54 12月 29 13:27 autostart
-rw-------. 1 root root 538 12月 28 17:38 default.xml   #對應存儲池的xml文件
-rw-------. 1 root root 511 12月 28 21:35 iso.xml       #對應存儲池的xml文件
-rw-------. 1 root root 508 12月 29 13:27 vm.xml        #對應存儲池的xml文件
[root@KVM storage]# cat iso.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 pool-edit iso        #警告這是一個自動文件若須要編輯請用virsh pool-edit iso
or other application using the libvirt API.
-->

<pool type='dir'>            #池類型
  <name>iso</name>           #池名稱
  <uuid>bfcd14e0-d6b2-448d-8b29-bbfa5b5e97d0</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
  </source>
  <target>
    <path>/iso</path>        #源是在/iso這個目錄裏
  </target>
</pool>
#咱們發如今/etc/libvirt/storage/路徑下還有一個目錄autostart
[root@KVM storage]# ll autostart/
總用量 0
lrwxrwxrwx. 1 root root 32 12月 28 17:38 default.xml -> /etc/libvirt/storage/default.xml
lrwxrwxrwx. 1 root root 28 12月 28 21:35 iso.xml -> /etc/libvirt/storage/iso.xml
lrwxrwxrwx. 1 root root 27 12月 29 13:27 vm.xml -> /etc/libvirt/storage/vm.xml
#若是咱們用virsh pool-edit iso來打開iso.xml文件的話,會是以下內容
[root@KVM storage]# virsh pool-edit iso
<pool type='dir'>
  <name>iso</name>
  <uuid>bfcd14e0-d6b2-448d-8b29-bbfa5b5e97d0</uuid>
  <capacity unit='bytes'>18238930944</capacity>         #內容出現了變化
  <allocation unit='bytes'>13504528384</allocation>     #內容出現了變化
  <available unit='bytes'>4734402560</available>        #內容出現了變化
  <source>
  </source>
  <target>
    <path>/iso</path>
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
      <label>unconfined_u:object_r:default_t:s0</label>
    </permissions>
  </target>
</pool>

virsh中的存儲池相關命令

image.png-254kB

virsh中的存儲卷相關命令

image.png-181.1kB

5.4.2 顯示池與卷的信息

#pool-list 幫助
[root@KVM storage]# virsh pool-list --help
  NAME
    pool-list - 列出池

  SYNOPSIS
    pool-list [--inactive] [--all] [--transient] [--persistent] [--autostart] [--no-autostart] [--type <string>] [--details] [--uuid] [--name]

  DESCRIPTION
    返回池列表

  OPTIONS
    --inactive       列出不活躍的池
    --all            不活躍和活躍的池
    --transient      列出臨時池
    --persistent     列出持久池
    --autostart      列出啓用 autostart 的池
    --no-autostart   列出禁用 autostart 的池
    --type <string>  只列出指定類型的池(若是支持)
    --details        爲池顯示擴展的詳情
    --uuid           list UUID of active pools only
    --name           list name of active pools only
#查看全部的存儲池
[root@KVM storage]# virsh pool-list
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 iso                  活動     是       
 vm                   活動     是
#查看某個存儲池的詳細信息
[root@KVM storage]# virsh pool-info vm
名稱:       vm
UUID:           b1a25def-9ecf-46d7-93d7-2c59c5594e5c
狀態:       running            #存儲池狀態
持久:       是                 #是不是永久性存儲池
自動啓動:   是                 #是否隨系統開機啓動
容量:       16.99 GiB          #總容量大小
分配:       12.58 GiB          #已經分配容量
可用:       4.41 GiB           #可用容量
#查看某個存儲池中的全部存儲卷
[root@KVM storage]# virsh vol-list vm
 名稱               路徑                                  
------------------------------------------------------------------------------
 Base_CentOS6.5.qcow2 /vm/Base_CentOS6.5.qcow2                
 centos6.5.qcow2      /vm/centos6.5.qcow2                     
 CRM-disk0.qcow2      /vm/CRM-disk0.qcow2                     
 ERP-disk0.qcow2      /vm/ERP-disk0.qcow2                     
 HR-disk0.qcow2       /vm/HR-disk0.qcow2                      
 OA-disk0.qcow2       /vm/OA-disk0.qcow2                      
 test1.qcow2          /vm/test1.qcow2                         
 test2.qcow2          /vm/test2.qcow2                         
 test3.qcow2          /vm/test3.qcow2                         
 test4.qcow2          /vm/test4.qcow2                         
 yangwenbo.qcow2      /vm/yangwenbo.qcow2

5.4.3 基於目錄的存儲池

準備目錄

  • 設置目錄權限

# chown root:root /guest_images/
# chmod 700 /guest_images

  • 經過virt-manager建立
  • 經過virsh建立

# virsh pool-define-as guest_images dir --target "/guest_images2"

image.png-17.1kB

存儲池實操:

#建立存儲池目錄
[root@KVM ~]# mkdir /guest_images
[root@KVM ~]# chown root:root /guest_images
[root@KVM ~]# chmod 700 /guest_images/

(1)經過virt-manager建立存儲池

image.png-28.6kB

image.png-18kB

image.png-18.9kB

image.png-51.3kB

image.png-18.1kB

image.png-29.6kB

#用命令查看存儲池狀態
[root@KVM ~]# virsh pool-list 
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_dir     活動     是           #建立完畢   
 iso                  活動     是       
 vm                   活動     是
#查看新建立的存儲池的配置文件
[root@KVM ~]# cat /etc/libvirt/storage/guest_images_dir.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 pool-edit guest_images_dir
or other application using the libvirt API.
-->

<pool type='dir'>
  <name>guest_images_dir</name>
  <uuid>36a61077-c4db-40fe-8745-34e49d48506d</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
  </source>
  <target>
    <path>/guest_images</path>
  </target>
</pool>

經過virt-manager刪除存儲池

image.png-32.7kB

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動       是       
 guest_images_dir     不活動     否       
 iso                  活動       是       
 vm                   活動       是

image.png-32kB

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 iso                  活動     是       
 vm                   活動     是 
 
[root@KVM ~]# ls /etc/libvirt/storage/
autostart  default.xml  iso.xml  vm.xml

(2)經過virsh建立一個自定義存儲池

#定義一個存儲池
[root@KVM ~]# virsh pool-define --help
  NAME
    pool-define - define an inactive persistent storage pool or modify an existing persistent one from an XML file

  SYNOPSIS
    pool-define <file>    #若是直接去定義存儲池參數比較多,咱們能夠選擇直接經過存儲池的配置文件去定義存儲池

  DESCRIPTION
    Define or modify a persistent storage pool.

  OPTIONS
    [--file] <string>  包含 XML 池描述的文件
[root@KVM ~]# virsh pool-define-as guest_images dir --target "/guest_images2"      #guest_images存儲池名字,target目標路徑
定義池 guest_images

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images         不活躍   否          #新定義的存儲池並無啓動 
 iso                  活動     是       
 vm                   活動     是 

[root@KVM ~]# virsh pool-start guest_images
錯誤:啓動池 guest_images 失敗
錯誤:cannot open directory '/guest_images2': 沒有那個文件或目錄
#咱們不要用手動去建立存儲池目錄,由於可能會有權限問題。
#建立一個定義好的存儲池的存儲目錄
[root@KVM ~]# virsh pool-build guest_images
構建池 guest_images

[root@KVM ~]# ll -d /gue*
drwx------. 2 root root 6 1月   1 17:01 /guest_images
drwx--x--x. 2 root root 6 1月   1 17:07 /guest_images2

[root@KVM ~]# virsh pool-start guest_images          #啓動存儲池
池 guest_images 已啓動

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images         活動     否                 #存儲池處於啓動狀態
 iso                  活動     是       
 vm                   活動     是       

[root@KVM ~]# virsh pool-autostart guest_images   #標記存儲池自動啓動
池 guest_images 標記爲自動啓動

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images         活動     是       
 iso                  活動     是       
 vm                   活動     是

刪除自定義的存儲池

[root@KVM ~]# virsh pool-destroy guest_images     #中止存儲池的活動
銷燬池 guest_images

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images         不活躍   是       
 iso                  活動     是       
 vm                   活動     是       

[root@KVM ~]# virsh pool-delete guest_images       #刪除存儲池目錄
池 guest_images 被刪除

[root@KVM ~]# ll -d /gue*         #存儲池的存儲目錄已經被刪除了
drwx------. 2 root root 6 1月   1 17:01 /guest_images
[root@KVM ~]# virsh pool-info guest_images      #但存儲池配置文件還在
名稱:       guest_images
UUID:           e850074b-232a-457f-b7dd-06dd45a72410
狀態:       不活躍
持久:       是
自動啓動:   是

[root@KVM ~]# ll /etc/libvirt/storage/
總用量 16
drwxr-xr-x. 2 root root  78 1月   1 17:08 autostart
-rw-------. 1 root root 538 12月 28 17:38 default.xml
-rw-------. 1 root root 539 1月   1 17:04 guest_images.xml    #在這裏
-rw-------. 1 root root 511 12月 28 21:35 iso.xml
-rw-------. 1 root root 508 12月 29 13:27 vm.xml

[root@KVM ~]# virsh pool-undefine guest_images    #清除存儲池配置文件
池 guest_images 已經被取消定義

[root@KVM ~]# ll /etc/libvirt/storage/             #已經沒了
總用量 12
drwxr-xr-x. 2 root root  54 1月   1 17:17 autostart
-rw-------. 1 root root 538 12月 28 17:38 default.xml
-rw-------. 1 root root 511 12月 28 21:35 iso.xml
-rw-------. 1 root root 508 12月 29 13:27 vm.xml

[root@KVM ~]# virsh pool-info guest_images          #配置文件已經沒了
錯誤:得到池 'guest_images' 失敗
錯誤:未找到存儲池: 沒有與名稱 'guest_images' 匹配的存儲池

5.4.4 基於分區的存儲池

  • libvirtd會自動mount分區
  • 準備分區並建立文件系統

# fdisk /dev/sdc
# mkfs.ext4 /dev/sdc1

建立:

  • Source Path:塊設備名
  • Target Path:mount到的目錄名

# virsh pool-define-as guest_images_fs fs --source-dev "/dev/sdc1" --target "/guest_images2"

操做準備
給虛擬機再加入一塊磁盤,分區並進行格式化來繼續咱們的實驗

#將新磁盤sdc分兩個分區,並進行格式化。
[root@KVM ~]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 1月   4 08:37 /dev/sdc
[root@KVM ~]# mkfs.ext4 /dev/sdc     #磁盤格式化
[root@KVM ~]# fdisk /dev/sdc
#如下省略若干。。。
[root@KVM ~]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 1月   4 09:02 /dev/sdc
brw-rw----. 1 root disk 8, 33 1月   4 09:02 /dev/sdc1
brw-rw----. 1 root disk 8, 34 1月   4 09:02 /dev/sdc2

[root@KVM /]# mkfs.ext4 /dev/sdc1           #使用前把磁盤格式化一下

[root@KVM ~]# rm -rf /guest_images/         #清除舊存儲池存放目錄

(1)經過virt-manager建立一個基於分區的存儲池

image.png-33.9kB

image.png-36.2kB

image.png-18.4kB

image.png-24.9kB

#驗證建立的存儲池
[root@KVM /]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_fs      活動     是          #有了並處於活動狀態
 
[root@KVM /]# virsh pool-info guest_images_fs
名稱:       guest_images_fs
UUID:           542f3884-b320-4da5-84f3-7a85e9af8e50
狀態:       running
持久:       是
自動啓動: 是
容量:       19.56 GiB
分配:       44.02 MiB
可用:       19.52 GiB

[root@KVM /]# ll -d /guest_images/
drwxr-xr-x. 3 root root 4096 1月   5 16:17 /guest_images/

[root@KVM /]# df -h
文件系統                     容量  已用  可用 已用% 掛載點
/dev/mapper/centos_kvm-root   17G  3.9G   14G   23% /
devtmpfs                     898M     0  898M    0% /dev
tmpfs                        910M     0  910M    0% /dev/shm
tmpfs                        910M   11M  900M    2% /run
tmpfs                        910M     0  910M    0% /sys/fs/cgroup
/dev/sda1                   1014M  201M  814M   20% /boot
tmpfs                        182M   44K  182M    1% /run/user/0
/dev/sr0                     4.2G  4.2G     0  100% /run/media/root/CentOS 7 x86_64
/dev/sdc1                     20G   45M   19G    1% /guest_images   #磁盤/dev/sdc1被自動掛載了
[root@KVM /]# cat /etc/libvirt/storage/guest_images_fs.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 pool-edit guest_images_fs
or other application using the libvirt API.
-->

<pool type='fs'>             #磁盤類型爲fs
  <name>guest_images_fs</name>
  <uuid>542f3884-b320-4da5-84f3-7a85e9af8e50</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
    <device path='/dev/sdc1'/>     #源磁盤路徑
    <format type='auto'/>
  </source>
  <target>
    <path>/guest_images</path>      #目標目錄路徑
  </target>
</pool>

[root@KVM /]# mount | grep sdc1       #查看mount掛載的信息
/dev/sdc1 on /guest_images type ext4 (rw,relatime,seclabel,data=ordered)
[root@KVM /]# reboot    #重啓檢查一下是否能夠自動掛載
[root@KVM ~]# df -h
文件系統                     容量  已用  可用 已用% 掛載點
/dev/mapper/centos_kvm-root   17G  3.9G   14G   23% /
devtmpfs                     898M     0  898M    0% /dev
tmpfs                        910M     0  910M    0% /dev/shm
tmpfs                        910M   11M  900M    2% /run
tmpfs                        910M     0  910M    0% /sys/fs/cgroup   #成功
/dev/sda1                   1014M  201M  814M   20% /boot
/dev/sdc1                     20G   45M   19G    1% /guest_images
tmpfs                        182M   32K  182M    1% /run/user/0
/dev/sr0                     4.2G  4.2G     0  100% /run/media/root/CentOS 7 x86_64

這裏咱們成功後,而後咱們清理掉guest_images_fs存儲池信息,繼續實驗

(2)經過virsh命令建立一個基於分區的存儲池

[root@KVM ~]# virsh pool-define-as guest_images_fs fs --source-dev "/dev/sdc1" --target "/guest_images2"
定義池 guest_images_fs

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_fs      不活躍   否      #新定義的存儲池未啓動
[root@KVM ~]# ll -d /gue*         #沒有目標掛載目錄
drwxr-xr-x. 2 root root 6 1月   5 16:14 /guest_images

[root@KVM ~]# virsh pool-build guest_images_fs    #建立存儲池存儲目錄
構建池 guest_images_fs

[root@KVM ~]# ll -d /guest*
drwxr-xr-x. 2 root root 6 1月   5 16:14 /guest_images
drwx--x--x. 2 root root 6 1月   5 18:50 /guest_images2    #存在了
[root@KVM ~]# virsh pool-start guest_images_fs     #啓動存儲池
池 guest_images_fs 已啓動

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_fs      活動     否          #啓動成功 

[root@KVM ~]# virsh pool-autostart guest_images_fs   #標記存儲池開機自啓動
池 guest_images_fs 標記爲自動啓動

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_fs      活動     是       

[root@KVM ~]# mount | grep /dev/sdc1     #已經自動掛載
/dev/sdc1 on /guest_images2 type ext4 (rw,relatime,seclabel,data=ordered)

5.4.5 基於LVM的存儲池

基於LVM的存儲池要求使用所有磁盤分區
建立時存儲池,有兩種方法

  • 使用現有的VG
  • 建立新的VG

建立新的VG

  • Target Path:新的卷組名
  • Source Path:存儲設備的位置
  • Build Pool:會建立新的VG

建立
# virsh pool-define-as guest_images_lvm3 logical \ --source-dev=/dev/sdc --source-name=libvirt_lvm \ --target=/dev/libvirt_vg

(1)以手動的方式建立VG並經過virt-manager建立基於lvm的存儲池

#fdisk分出一個所有容量的分區
[root@KVM ~]# mkfs.ext4 /dev/sdc     #磁盤格式化
[root@KVM ~]# fdisk /dev/sdc
#如下省略若干。。。

[root@KVM ~]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 1月   5 19:14 /dev/sdc
brw-rw----. 1 root disk 8, 33 1月   5 19:14 /dev/sdc1    #這就是咱們實驗用的分區

#建立一個卷組
[root@KVM ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.

[root@KVM ~]# vgcreate guest_images_lvm /dev/sdc1
  Volume group "guest_images_lvm" successfully created

image.png-19.1kB

image.png-23.6kB

image.png-34.4kB

#驗證存儲池建立狀態
[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_lvm     活動     是  
 
[root@KVM ~]# cat /etc/libvirt/storage/guest_images_lvm.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 pool-edit guest_images_lvm
or other application using the libvirt API.
-->

<pool type='logical'>
  <name>guest_images_lvm</name>
  <uuid>5a11064e-2e9b-47d2-aa1d-10896f25bdd5</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
    <name>guest_images_lvm</name>
    <format type='lvm2'/>
  </source>
  <target>
    <path>/dev/guest_images_lvm</path>
  </target>
</pool>

圖形界面建立成功

(2)經過virt-manager建立vg並建立LVM存儲池

先清除以前建立的存儲池

#清除以前的vg卷組
[root@KVM ~]# vgs
  VG               #PV #LV #SN Attr   VSize   VFree  
  centos_kvm         1   2   0 wz--n- <19.00g      0 
  guest_images_lvm   1   0   0 wz--n- <40.00g <40.00g
  vmvg               1   1   0 wz--n- <30.00g      0 
[root@KVM ~]# vgremove guest_images_lvm
  Volume group "guest_images_lvm" successfully removed
[root@KVM ~]# vgs
  VG         #PV #LV #SN Attr   VSize   VFree
  centos_kvm   1   2   0 wz--n- <19.00g    0 
  vmvg         1   1   0 wz--n- <30.00g    0
#清除以前的pv狀態
[root@KVM ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree  
  /dev/sda2  centos_kvm lvm2 a--  <19.00g      0 
  /dev/sdb1  vmvg       lvm2 a--  <30.00g      0 
  /dev/sdc1             lvm2 ---  <40.00g <40.00g
[root@KVM ~]# pvremove /dev/sdc1
  Labels on physical volume "/dev/sdc1" successfully wiped.
[root@KVM ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree
  /dev/sda2  centos_kvm lvm2 a--  <19.00g    0 
  /dev/sdb1  vmvg       lvm2 a--  <30.00g    0
#經過fdisk清除/dev/sdc1的磁盤分區,不要經過mkfs.ext4方式來清除/dev/sdc1,否則圖形軟件沒法自動pv化磁盤
[root@KVM ~]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 1月   5 19:33 /dev/sdc

image.png-19.6kB

image.png-23.2kB

#驗證存儲池建立狀態
[root@KVM ~]# vgs
  VG               #PV #LV #SN Attr   VSize   VFree  
  centos_kvm         1   2   0 wz--n- <19.00g      0 
  guest_images_lvm2   1   0   0 wz--n- <40.00g <40.00g
  vmvg               1   1   0 wz--n- <30.00g      0 
[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_lvm2     活動     是
[root@KVM ~]# cat /etc/libvirt/storage/guest_images_lvm2.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 pool-edit guest_images_lvm2
or other application using the libvirt API.
-->
<pool type='logical'>
  <name>guest_images_lvm2</name>
  <uuid>a66a085c-d017-409d-8925-4f508956e2b0</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
    <device path='/dev/sdc'/>
    <name>guest_images_lvm2</name>
    <format type='lvm2'/>
  </source>
  <target>
    <path>/dev/guest_images_lvm2</path>
  </target>
</pool>

(3)經過virsh命令建立vg並建立基於LVM的存儲池

#清除以前建立的存儲池並抹除vg痕跡
[root@KVM ~]# vgs
  VG                #PV #LV #SN Attr   VSize  VFree 
  cl                  1   2   0 wz--n- 19.00g     0 
  guest_images_lvm2   1   0   0 wz--n- 20.00g 20.00g
  vmvg                1   1   0 wz--n- 40.00g     0 
[root@KVM ~]# vgremove guest_images_lvm2
  Volume group "guest_images_lvm2" successfully removed

[root@KVM ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sda2  cl   lvm2 a--  19.00g     0 
  /dev/sdb1  vmvg lvm2 a--  40.00g     0 
  /dev/sdc        lvm2 ---  20.00g 20.00g
[root@KVM ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped.
#virsh命令建立vg並建立基於LVM的存儲池
[root@KVM ~]# virsh pool-define-as guest_images_lvm3 logical --source-dev=/dev/sdc --source-name=libvirt_lvm --target=/dev/libvirt_vg
定義池 guest_images_lvm3

[root@KVM ~]# vgs       #咱們發現virsh命令沒有自動建立vg
  VG         #PV #LV #SN Attr   VSize   VFree
  centos_kvm   1   2   0 wz--n- <19.00g    0 
  
[root@KVM ~]# pvs       #咱們發現virsh命令沒有自動建立pv
  PV         VG         Fmt  Attr PSize   PFree
  /dev/sda2  centos_kvm lvm2 a--  <19.00g    0
#virsh命令建立卷組libvirt_lvm
[root@KVM ~]# virsh pool-build guest_images_lvm3
構建池 guest_images_lvm3

[root@KVM ~]# pvs
  PV         VG          Fmt  Attr PSize   PFree  
  /dev/sda2  centos_kvm  lvm2 a--  <19.00g      0 
  /dev/sdc   libvirt_lvm lvm2 a--  <40.00g <40.00g
[root@KVM ~]# vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  centos_kvm    1   2   0 wz--n- <19.00g      0 
  libvirt_lvm   1   0   0 wz--n- <40.00g <40.00g
[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 guest_images_lvm3    不活躍  否       

[root@KVM ~]# virsh pool-start guest_images_lvm3
池 guest_images_lvm3 已啓動

[root@KVM ~]# virsh pool-autostart guest_images_lvm3
池 guest_images_lvm3 標記爲自動啓動

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 guest_images_lvm3    活動     是
5.4.6 基於NFS的存儲池
# virsh pool-define-as --name nfstrial2 --type netfs --source-host 192.168.200.26 --source-path /nfsshare --target /nfstrial2

環境準備

  • 在一臺KVM虛擬機中建立NFS共享存儲文件系統
  • 建立基於NFS共享存儲的存儲池

第一步:在一臺虛擬機中yum安裝nfs服務端

#這是一臺NAT連接的虛擬機,yum安裝nfs
[root@KVM ~]# yum -y install rpcbind nfs-utils
[root@KVM ~]# rpm -qa rpcbind nfs-utils
nfs-utils-1.3.0-0.61.el7.x86_64
rpcbind-0.2.0-47.el7.x86_64
[root@KVM ~]# mkdir /nfs1
[root@KVM ~]# mkdir /nfsshare
[root@KVM ~]# useradd nfsnobody
[root@KVM ~]# chown nfsnobody.nfsnobody /nfsshare
[root@KVM ~]# cat /etc/exports    #由於虛擬機是NAT模式,若要宿主機可以掛載須要開通兩個網段權限
/nfsshare   192.168.122.0/24(rw,sync) 192.168.200.0/24(rw,sync)
#啓動rpc與nfs
[root@KVM ~]# systemctl restart rpcbind.service

[root@KVM ~]# systemctl start nfs.service

第二步:virt-manager建立基於NFS的存儲池

image.png-18.8kB

image.png-24.3kB

image.png-30.2kB

#在宿主機中測試共享目錄
[root@KVM ~]# df -h | grep nfsshare       #已經自動掛載
192.168.200.26:/nfsshare      17G  3.9G   14G   23% /nfs1
[root@KVM ~]# mount | grep nfsshare       #已經自動掛載
192.168.200.26:/nfsshare on /nfs1 type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.200.26,local_lock=none,addr=192.168.200.26)

[root@KVM ~]# virsh pool-list --all 
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_lvm3    活動     是       
 nfs1                 活動     是
[root@KVM ~]# cat /etc/libvirt/storage/nfs1.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 pool-edit nfs1
or other application using the libvirt API.
-->

<pool type='netfs'>            #類型爲網絡文件系統network file system
  <name>nfs1</name>
  <uuid>c056492d-178a-42e1-9b8d-7302db333352</uuid>
  <capacity unit='bytes'>0</capacity>
  <allocation unit='bytes'>0</allocation>
  <available unit='bytes'>0</available>
  <source>
    <host name='192.168.200.26'/>      #源共享主機IP
    <dir path='/nfsshare'/>            #源共享主機目錄
    <format type='auto'/>
  </source>
  <target>
    <path>/nfs1</path>                  #目標掛載目錄
  </target>
</pool>

5.5 存儲卷

5.5.1 存儲卷概述

存儲池被分割爲存儲卷(Storage Volume)
存儲卷

  • 文件
  • 塊設備(如物理分區,LVM邏輯卷等)
  • libvirt管理的其餘類型存儲的抽象

image.png-279.4kB

[root@KVM ~]# virsh help volume
 Storage Volume (help keyword 'volume'):
    vol-clone                      克隆卷。
    vol-create-as                  從一組變量中建立卷
    vol-create                     從一個 XML 文件建立一個卷
    vol-create-from                生成卷,使用另外一個卷做爲輸入。
    vol-delete                     刪除卷
    vol-download                   將卷內容下載到文件中
    vol-dumpxml                    XML 中的卷信息
    vol-info                       存儲卷信息
    vol-key                        爲給定密鑰或者路徑返回卷密鑰
    vol-list                       列出卷
    vol-name                       爲給定密鑰或者路徑返回卷名
    vol-path                       爲給定密鑰或者路徑返回卷路徑
    vol-pool                       爲給定密鑰或者路徑返回存儲池
    vol-resize                     創新定義卷大小
    vol-upload                     將文件內容上傳到卷中
    vol-wipe                       擦除卷

5.5.2 存儲卷管理

  • 建立
  • 克隆
  • 刪除

(1) 演示:存儲卷的建立

[root@KVM ~]# virsh vol-create-as --help
  NAME
    vol-create-as - 從一組變量中建立卷

  SYNOPSIS
    vol-create-as <pool> <name> <capacity> [--allocation <string>] [--format <string>] [--backing-vol <string>] [--backing-vol-format <string>] [--prealloc-metadata] [--print-xml]

  DESCRIPTION
    建立一個卷。

  OPTIONS
    [--pool] <string>  卷名稱
    [--name] <string>  卷的名稱
    [--capacity] <string>  卷大小,以整數計(默認爲字節)
    --allocation <string>  初始化分配大小,以整數計(默認爲 KiB)
    --format <string>  文件格式類型:raw、bochs、qcow、qcow二、qed、vmdk
    --backing-vol <string>  提取快照時的後端卷
    --backing-vol-format <string>  提取快照時的後端卷格式
    --prealloc-metadata  預先分配的元數據(用於 qcow2 而不是整個分配)
    --print-xml      打印 XML 文檔,但不能定義/建立

1)基於目錄的存儲池中的存儲卷管理

#查看全部的存儲池
[root@KVM /]# virsh pool-list 
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 ios                  活動     是       
 nfs1                 活動     是       
 VM                   活動     是    #目標存儲池
 
#查看VM存儲的xml文檔
[root@KVM /]# virsh pool-dumpxml VM
<pool type='dir'>         #基於目錄的存儲池
  <name>VM</name>
  <uuid>8567eb78-049e-42cb-b1ca-8f832e4995e5</uuid>
  <capacity unit='bytes'>31568334848</capacity>
  <allocation unit='bytes'>1207033856</allocation>
  <available unit='bytes'>30361300992</available>
  <source>
  </source>
  <target>
    <path>/VM</path>       #存儲池位置
    <permissions>
      <mode>0755</mode>
      <owner>0</owner>
      <group>0</group>
      <label>system_u:object_r:unlabeled_t:s0</label>
    </permissions>
  </target>
</pool>
#查看VM存儲池的全部存儲卷
[root@KVM /]# virsh vol-list VM
 名稱               路徑                                  
------------------------------------------------------------------------------
 centos6.5-2.qcow2    /VM/centos6.5-2.qcow2 

#向VM存儲池裏建立一個存儲卷
[root@KVM /]# virsh vol-create-as VM test1.qcow2 1G --format qcow2
建立卷 test1.qcow2 

#查看test1.qcow2的卷信息的兩種方式
[root@KVM /]# virsh vol-info /VM/test1.qcow2 
名稱:       test1.qcow2
類型:       文件
容量:       1.00 GiB
分配:       196.00 KiB

[root@KVM /]# virsh vol-info test1.qcow2 --pool VM
名稱:       test1.qcow2
類型:       文件
容量:       1.00 GiB
分配:       196.00 KiB

2)基於LVM的存儲池中的存儲卷管理

#定義一個基於LVM的存儲池
[root@KVM /]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 1月  10 22:46 /dev/sdc

[root@KVM /]# virsh pool-define-as guest_images_lvm logical --source-dev=/dev/sdc --target=/dev/vg_libvirt
定義池 guest_images_lvm

#構建LVM存儲池(若是構建失敗,可手動pvcreate -y /dev/sdc後再執行)
[root@KVM /]# pvcreate -y /dev/sdc
  Wiping ext4 signature on /dev/sdc.
  Physical volume "/dev/sdc" successfully created.
[root@KVM /]# virsh pool-build guest_images_lvm 
構建池 guest_images_lvm


#啓動基於LVM的存儲池
[root@KVM /]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_lvm     不活躍   否       
 iso                  活動     是       
 nfs1                 活動     是       
 VM                   活動     是       
[root@KVM /]# virsh pool-start guest_images_lvm
池 guest_images_lvm 已啓動
[root@KVM /]# virsh pool-autostart guest_images_lvm
池 guest_images_lvm 標記爲自動啓動
[root@KVM /]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 guest_images_lvm     活動     是       
 iso                  活動     是       
 nfs1                 活動     是       
 VM                   活動     是   

#向LVM存儲池中建立一個存儲卷
[root@KVM /]# virsh vol-create-as guest_images_lvm lvvoll 1G
建立卷 lvvoll 
[root@KVM /]# virsh vol-create-as guest_images_lvm lvvol2 2G
建立卷 lvvol2 
[root@KVM /]# virsh vol-create-as guest_images_lvm lvvol3 3G
建立卷 lvvol3 
[root@KVM /]# virsh vol-list guest_images_lvm
 名稱               路徑                                  
------------------------------------------------------------------------------
 lvvol2               /dev/guest_images_lvm/lvvol2            
 lvvol3               /dev/guest_images_lvm/lvvol3            
 lvvoll               /dev/guest_images_lvm/lvvoll

(2) 演示:存儲卷的克隆

[root@KVM /]# virsh vol-clone --help
  NAME
    vol-clone - 克隆卷。

  SYNOPSIS
    vol-clone <vol> <newname> [--pool <string>] [--prealloc-metadata] [--reflink]

  DESCRIPTION
    Clone an existing volume within the parent pool.

  OPTIONS
    [--vol] <string>  卷名稱、密鑰或者路徑
    [--newname] <string>  克隆名稱
    --pool <string>  池名或 uuid
    --prealloc-metadata  預先分配的元數據(用於 qcow2 而不是整個分配)
    --reflink        use btrfs COW lightweight copy
#克隆基於目錄的存儲池中的存儲卷test1.qcow2
[root@KVM /]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM
使用 test2.qcow2 克隆的卷 test1.qcow2 

[root@KVM /]# virsh vol-list VM
 名稱               路徑                                  
------------------------------------------------------------------------------
 centos6.5-2.qcow2    /VM/centos6.5-2.qcow2                   
 test1.qcow2          /VM/test1.qcow2                         
 test2.qcow2          /VM/test2.qcow2     #克隆後的盤                      
[root@KVM /]# virsh vol-info /VM/test2.qcow2 
名稱:       test2.qcow2
類型:       文件
容量:       1.00 GiB
分配:       196.00 KiB
#克隆基於LVM的存儲池中的存儲卷
[root@KVM /]# virsh vol-clone lvvoll lvvol4 --pool guest_images_lvm
使用 lvvol4 克隆的卷 lvvoll 

[root@KVM /]# virsh vol-list guest_images_lvm
 名稱               路徑                                  
------------------------------------------------------------------------------
 lvvol2               /dev/guest_images_lvm/lvvol2            
 lvvol3               /dev/guest_images_lvm/lvvol3            
 lvvol4               /dev/guest_images_lvm/lvvol4   #克隆後的卷    
 lvvoll               /dev/guest_images_lvm/lvvoll            

[root@KVM /]# virsh vol-info /dev/guest_images_lvm/lvvol4 
名稱:       lvvol4
類型:       塊
容量:       1.00 GiB
分配:       1.00 GiB

(3) 演示:存儲卷的刪除

#查看命令幫助
[root@KVM ~]# virsh vol-delete --help
  NAME
    vol-delete - 刪除卷

  SYNOPSIS
    vol-delete <vol> [--pool <string>] [--delete-snapshots]

  DESCRIPTION
    刪除一個給定的卷。

  OPTIONS
    [--vol] <string>  卷名稱、密鑰或者路徑
    --pool <string>  池名或 uuid
    --delete-snapshots  delete snapshots associated with volume (must be supported by storage driver)
#刪除基於LVM存儲池中的存儲卷
[root@KVM ~]# virsh vol-delete lvvol4 guest_images_lvm
卷 lvvol4 被刪除
[root@KVM ~]# virsh vol-delete lvvol3 guest_images_lvm
卷 lvvol3 被刪除
[root@KVM ~]# virsh vol-delete lvvol2 guest_images_lvm
卷 lvvol2 被刪除
[root@KVM ~]# virsh vol-delete lvvoll guest_images_lvm
卷 lvvoll 被刪除
[root@KVM ~]# virsh vol-list guest_images_lvm
 名稱               路徑                                  
------------------------------------------------------------------------------
#刪除基於LVM的存儲池
[root@KVM ~]# virsh pool-destroy guest_images_lvm
銷燬池 guest_images_lvm
[root@KVM ~]# virsh pool-undefine guest_images_lvm
池 guest_images_lvm 已經被取消定義

[root@KVM ~]# virsh pool-list --all
 名稱               狀態     自動開始
-------------------------------------------
 default              活動     是       
 ios                  活動     是       
 nfs1                 活動     是       
 VM                   活動     是  
 
#刪除基於目錄的存儲池中的存儲卷
[root@KVM ~]#  virsh vol-delete /VM/test2.qcow2 
卷 /VM/test2.qcow2 被刪除

5.5.3 向虛擬機添加捲

  • 經過virt-manager添加新設備 :經過圖形管理程序添加新設備
  • attach-device :經過XML添加新的設備
  • attach-disk :經過參數添加新的磁盤設備

(1)經過virt-manager添加新設備
假如咱們想要向一個虛擬機中添加一塊虛擬磁盤

image.png-40.8kB

點開一個虛擬機,運行狀態的也能夠

image.png-24.2kB

選擇硬件添加

image.png-72.7kB

添加虛擬磁盤

image.png-53.6kB

進入運行中的虛擬機的交互界面

image.png-25.8kB

image.png-24.2kB

(2)經過XML文件添加新的設備

#建立一個disk類型的xml文檔
[root@KVM ~]# vim /tmp/disks.xml 
[root@KVM ~]# cat /tmp/disks.xml 
<disk type='file' device='disk'>          #文件類型
    <driver name='qemu' type='qcow2' cache='none'/>    #磁盤類型
    <source file='/VM/test1.qcow2'/>      #虛擬卷位置
    <target dev='vdb'/>                   #虛擬卷掛載名稱
</disk>
#查看virsh domblklist命令的幫助
[root@KVM ~]#  virsh domblklist --help
  NAME
    domblklist - 列出全部域塊

  SYNOPSIS
    domblklist <domain> [--inactive] [--details]

  DESCRIPTION
    獲取域塊設備小結

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    --inactive       獲取不活躍而不是運行的配置
    --details        type 和 device 值的附加顯示
#查看虛擬機的磁盤掛載狀況
[root@KVM ~]# virsh list --all
 Id    名稱                         狀態
----------------------------------------------------
 4     centos6.5                      running     #一個正在運行的虛擬機
 -     centos6.5-2                    關閉

[root@KVM ~]# virsh domblklist centos6.5   #查看虛擬機的磁盤設備掛載狀況
目標     源
------------------------------------------------
vda        /var/lib/libvirt/images/centos6.5.qcow2
hda        -
#將磁盤類型的xml文檔加入到虛擬機
[root@KVM ~]# virsh attach-device --help      #查看命令幫助
  NAME
    attach-device - 從一個XML文件附加裝置

  SYNOPSIS
    attach-device <domain> <file> [--persistent] [--config] [--live] [--current]

  DESCRIPTION
    從一個XML文件附加裝置.

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    [--file] <string>  XML 文件
    --persistent     讓實時更改持久
    --config         影響下一次引導
    --live           影響運行的域
    --current        影響當前域
[root@KVM ~]# virsh attach-device centos6.5 /tmp/disks.xml --persistent   #將xml文件添加進虛擬機
成功附加設備
[root@KVM ~]# virsh domblklist centos6.5
目標     源
------------------------------------------------
vda        /var/lib/libvirt/images/centos6.5.qcow2
vdb        /VM/test1.qcow2
hda        -

經過virt-manager查看磁盤已經添加成功,以下圖:

image.png-59.7kB
(3)經過參數添加新的磁盤設備

#克隆一個存儲卷
[root@KVM ~]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM
使用 test2.qcow2 克隆的卷 test1.qcow2 

[root@KVM ~]# ll /VM/test2.qcow2 
-rw-------. 1 qemu qemu 1245184 1月  11 00:17 /VM/test2.qcow2
#掛載一個存儲捲到虛擬機
[root@KVM VM]# virsh attach-disk centos6.5 --source=/VM/test2.qcow2 --target=vdc
成功附加磁盤

[root@KVM VM]# virsh domblklist centos6.5
目標     源
------------------------------------------------
vda        /var/lib/libvirt/images/centos6.5.qcow2
vdb        /VM/test1.qcow2
vdc        /VM/test2.qcow2
hda        -
相關文章
相關標籤/搜索