五,管理虛擬存儲
5.1 虛擬磁盤概述
5.1.1 虛擬化項目中存儲的注意事項
- [x] 存儲的性能幾乎總是虛擬化的瓶頸
- [x] 通過多個硬盤驅動以分布磁盤I/O來實現存儲解決方案
- [x] 考慮部署集中化的SAN/NFS來實現高可用性和實時遷移
5.1.2 KVM存儲模式
- [x] 基於文件系統的存儲
- dir:Filesystem Directory 虛擬文件系統目錄
- fs:Pre-Formatted Block Device
- netfs:Network Exported Directory
- [x] 基於設備的存儲
- Disk:Physical Disk Device
- Iscsi:iSCSI Target
- logical:LVM Volume Group
5.1.3 虛擬磁盤類型
- [x] 固定 Fixed
- 在配置時,指定磁盤大小
- 不管在虛擬磁盤上實際存儲多少數據,都將占用相同大小主機磁盤空間
- [x] 動態 Dynamic
- 增長到最大容量,但是只根據需求使用更多的空間
- [x] 差異 Differencing
- 因為創建是差異磁盤,所以只保存變更的數據
- 例如,將操作系統安裝在父盤,然后創建差異化磁盤來執行進一步配置
5.1.4 KVM支持的虛擬磁盤類型
- [x] raw
- 這並非是一種真正的磁盤格式,而是代表虛擬機所使用的原始鏡像
- 它並不存儲元數據,因此可以作為保證虛擬機兼容性的候選方案。然而,也正因為它不存儲元數據,因此不能自持某些高級特性,比如快照和壓縮等。
- 格式簡單,容易轉換為其他的格式。需要文件系統的支持才能支持sparse file
- [x] cow:copy-on-write格式,曇花一現
- [x] qcow:QEMU早期的copy-on-write格式,過渡性方案
- [x] qcow2
- 按需進行分配磁盤空間,不管文件系統是否支持
- 支持快照
- 支持zlib的磁盤壓縮
- 支持AES的加密
- [x] vmdk(Virtual Machine Disk)
- VMware環境當中默認使用的磁盤格式
- [x] vhd \ vhdx(Virtual Hard Disk)
- 微軟默認采用的文件格式
- [x] vdi(VirtualBox)
- [x] 可以通過qemu-img --help查看支持的格式
[root@localhost ~]# 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概述
- [x] qemu-img 是一個功能強制磁盤鏡像管理工具
- [x] qemu-img --help 包括以下功能
- check 檢查完整性
- create 創建鏡像
- commit 提交更改
- compare 比較
- convert 轉換
- info 獲得信息
- map 映射
- snapshot 快照管理
- rebase 在已有的鏡像的基礎上創建新的鏡像
- resize 調整大小
- amend 修訂鏡像格式選項
5.2.2 創建虛擬磁盤
[root@localhost ~]# 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@localhost ~]# qemu-img create t1.img 1g #只輸入磁盤名和大小創建
Formatting 't1.img', fmt=raw size=1073741824 #默認的磁盤格式fmt=raw
[root@localhost ~]# qemu-img info t1.img #查看虛擬磁盤的信息
image: t1.img #文件名稱
file format: raw #文件格式
virtual size: 1.0G (1073741824 bytes) #虛擬磁盤大小
disk size: 0 #磁盤空間尺寸是0????
[root@localhost ~]# ll -h t1.img
-rw-r--r-- 1 root root 1.0G 5月 2 11:24 t1.img #ll查看磁盤是1G沒錯
[root@localhost ~]# du -sh t1.img #但我們用du查看一下,發現磁盤真實空間的占用還真是0
0 t1.img
根據以上測試,我們發現默認情況下qemu-img創建的磁盤文件的類型是動態的(Dynamic 空洞)會根據真實存放數據的大小進行動態擴容直到磁盤空間設定值的大小。
#假如我們想看一下各種磁盤格式所附帶的-o option都有什么,我們可以這么做
[root@localhost ~]# qemu-img create -f raw -o ? #raw格式磁盤只有一個尺寸大小選項
Supported options:
size Virtual disk size
[root@localhost ~]# 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命令模擬創建一個沒有空洞的文件
[root@localhost vm]# dd if=/dev/zero of=flat1.img bs=1024k count=1000
記錄了1000+0 的讀入
記錄了1000+0 的寫出
1048576000字節(1.0 GB)已復制,2.2495 秒,466 MB/秒
[root@localhost vm]# 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@localhost vm]# dd if=/dev/zero of=flat2.img bs=1024k count=0 seek=1024
記錄了0+0 的讀入
記錄了0+0 的寫出
0字節(0 B)已復制,0.000114639 秒,0.0 kB/秒
[root@localhost vm]# qemu-img info flat2.img
image: flat2.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0 #實際磁盤占用0
空洞文件被復制以后,那么還是空洞文件嗎?
[root@localhost vm]# du -sh flat*
1001M flat1.img
0 flat2.img
[root@localhost vm]# cp flat1.img flat1a.img
[root@localhost vm]# cp flat2.img flat2a.img
[root@localhost vm]# du -sh flat*
1001M flat1a.img #非空洞文件復制后還是非空洞
1001M flat1.img
0 flat2a.img #空洞文件復制后還是空洞文件
0 flat2.img
#利用--sparse=always將非空洞文件復制成空洞文件
[root@localhost vm]# cp flat1.img flat1b.img --sparse=always
[root@localhost vm]# du -sh flat*
1001M flat1a.img
0 flat1b.img #加了參數的cp復制出來的是空洞文件。
1001M flat1.img
0 flat2a.img
0 flat2.img
#利用--sparse=never將空洞文件復制成非空洞文件
[root@localhost vm]# cp flat2.img flat2b.img --sparse=never
[root@localhost vm]# du -sh flat*
1001M flat1a.img
0 flat1b.img
1001M flat1.img
0 flat2a.img
1.1G flat2b.img #加了參數的cp復制出來的是非空洞文件
0 flat2.img
5.2.3 檢查虛擬磁盤
[root@localhost vm]# du -sh ce*
1.1G centos6.5-2.qcow2
[root@localhost vm]# qemu-img info centos6.5-2.qcow2
image: centos6.5-2.qcow2
file format: qcow2 #文件格式
virtual size: 8.0G (8589934592 bytes) #虛擬磁盤尺寸大小
disk size: 1.0G #磁盤真實占用大小
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
[root@localhost vm]# qemu-img check centos6.5-2.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)
- [x] off
- 缺省策略,即不使用預分配策略
- [x] metadata
- 分配元數據(metadata),預分配后的虛擬磁盤仍然屬於稀疏映像類型(空洞文件)
- [x] full
- 分配所有磁盤空間並置零,預分配后的虛擬磁盤屬於非稀疏映像類型
- [x] falloc
- 分配文件的塊並標示它們的狀態為未初始化,相對full模式來說,創建虛擬磁盤的速度要快很多。
[root@localhost 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@localhost 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@localhost 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@localhost 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@localhost vm]# qemu-img info test2.qcow2
image: test2.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 328K #我們發現磁盤的實際占用變成了328K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
#preallocation=falloc創建磁盤
[root@localhost 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@localhost 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@localhost 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@localhost 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@localhost vm]# ll -h test*
-rw-r--r-- 1 root root 193K 5月 4 09:45 test1.qcow2 #off
-rw-r--r-- 1 root root 1.1G 5月 4 09:49 test2.qcow2 #metadata
-rw-r--r-- 1 root root 1.1G 5月 4 09:51 test3.qcow2 #falloc
-rw-r--r-- 1 root root 1.1G 5月 4 09:54 test4.qcow2 #full
[root@localhost vm]# du -sh test*
196K test1.qcow2 #off
332K test2.qcow2 #metadata
1.1G test3.qcow2 #falloc
1.1G test4.qcow2 #full
通過對比我們發現預分配策略里,off和metadata預分配策略都屬於空洞文件,而falloc和full屬於非空洞文件。
5.2.5 后備差異虛擬磁盤
- [x] 存儲與基礎鏡像(父)磁盤的變化
- 基礎鏡像(父)磁盤不會改變
- 差異磁盤隔離變化
- 多個差異磁盤可以使用相同的基礎鏡像(父)磁盤
- [x] 優點:標准化基礎鏡像,節省空間
- [x] 缺點:增加了開銷,較差的性能
演示后備差異虛擬磁盤(克隆)
#創建父磁盤並安裝父Centos6.5操作系統
[root@localhost vm]# qemu-img create -f qcow2 Base_CentOS6.5.qcow2
qemu-img: Base_CentOS6.5.qcow2: Image creation needs a size parameter
[root@localhost 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@localhost 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@localhost vm]# virsh list
Id 名稱 狀態
----------------------------------------------------
1 vm2 running
9 Base_CentOS7 running #父操作系統
#創建OA后備差異虛擬磁盤,OA子系統盤
[root@localhost vm]# 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@localhost 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@localhost vm]# qemu-img create -f qcow2 -o backing_file=Base_CentOS6.5.qcow2 ERP-disk0.qcow2
Formatting 'ERP-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@localhost 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@localhost vm]# qemu-img create -f qcow2 -o backing_file=Base_CentOS6.5.qcow2 CRM-disk0.qcow2
Formatting 'CRM-disk0.qcow2', fmt=qcow2 size=10737418240 backing_file='Base_CentOS6.5.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@localhost vm]# ll -h *-disk0.qcow2
-rw-r--r-- 1 root root 193K 5月 4 11:11 CRM-disk0.qcow2
-rw-r--r-- 1 root root 193K 5月 4 11:11 ERP-disk0.qcow2
-rw-r--r-- 1 root root 193K 5月 4 11:11 HR-disk0.qcow2
-rw-r--r-- 1 root root 193K 5月 4 11:07 OA-disk0.qcow2
#創建四種子系統虛擬機
[root@localhost 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@localhost 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@localhost 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@localhost 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@localhost vm]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
1 vm2 running
10 oa running #子系統創建完畢
11 erp running #子系統創建完畢
12 hr running #子系統創建完畢
13 crm running #子系統創建完畢
- Base_CentOS7 關閉
- centos6.5 關閉
- centos6.5-2 關閉
- centos6.5-3 關閉
父子系統的方式,其實就是克隆的方式,我們如果要通過命令來實現就是如此操作。
如果在圖形界面下,類似的方式如下圖
5.2.6 虛擬磁盤格式轉換(虛擬機遷移案例)
#語法格式
[root@localhost ~]# 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虛擬機中找一個關閉狀態的虛擬機進行導出
(2)先將.vmdk虛擬磁盤文件拷入到KVM虛擬機中
[root@localhost vm]# ll -h *.vmdk
-rw-r--r-- 1 root root 956M 5月 4 22:05 LNMP-disk1.vmdk
[root@localhost vm]# qemu-img info LNMP-disk1.vmdk
image: LNMP-disk1.vmdk
file format: vmdk
virtual size: 20G (21474836480 bytes)
disk size: 956M
cluster_size: 65536
Format specific information:
cid: 1935336885
parent cid: 4294967295
create type: streamOptimized
extents:
[0]:
compressed: true
virtual size: 21474836480
filename: LNMP-disk1.vmdk
cluster size: 65536
format:
(3)然后進行磁盤文件的格式轉換
[root@localhost vm]# qemu-img convert -O qcow2 LNMP-disk1.vmdk \
> LNMP-disk1.qcow2
[root@localhost vm]# ll -h LNMP*
-rw-r--r-- 1 root root 2.5G 5月 4 22:12 LNMP-disk1.qcow2 #轉換后文件變大
-rw-r--r-- 1 root root 956M 5月 4 22:05 LNMP-disk1.vmdk
[root@localhost vm]# du -sh LNMP*
2.5G LNMP-disk1.qcow2 #轉換后文件變大
956M LNMP-disk1.vmdk
(4)然后根據虛擬磁盤遷移前的VMware配置,導入到新的KVM虛擬機
1個vcpu
1GB內存
兩個網卡(橋接+NAT)
磁盤類型:scsi
VNC顯示卡
OS類型為RHEL6
#導入新的KVM虛擬機
[root@localhost 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@localhost vm]# virsh list
Id 名稱 狀態
----------------------------------------------------
17 LNMP running #虛擬機遷移成功
5.2.7 調整虛擬磁盤大小
- [x] 語法格式
[root@localhost vm]# qemu-img --help | grep resize
resize [-q] filename [+ | -]size
- [x] 操作之前,一定要做好數據備份
- [x] 增加文件大小后,需要在客戶機中使用fdisk,parted等分區工具進行相應操作才能真正讓客戶機使用到增加后的鏡像空間。
- [x] 縮小鏡像之前,要在客戶機中保證里面的文件系統有空余空間,否則會數據丟失
- [x] qcow2不支持縮小鏡像的操作
#實操
[root@localhost vm]# qemu-img info LNMP-disk1.qcow2
image: LNMP-disk1.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes) #磁盤原始尺寸
disk size: 2.5G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@localhost vm]# qemu-img resize LNMP-disk1.qcow2 +10G #增加10G尺寸
Image resized.
[root@localhost vm]# qemu-img info LNMP-disk1.qcow2
image: LNMP-disk1.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes) #擴大了10G
disk size: 2.5G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
5.3 快照管理
5.3.1 快照/檢查點概述
- [x] 磁盤快照
- 對磁盤數據進行快照
- 主要用於虛擬機備份等場合
- [x] 內存快照
- 對虛擬機的內存/設備信息進行保存
- 該機制同時用於休眠恢復,遷移等場景
- 主要使用virsh save(qemu migrate to file)實現,只能對運行的虛擬機進行
- [x] 檢查點快照
- 同時保存虛擬機的磁盤快照和內存快照
- 用於將虛擬機恢復到某個時間點
- 可以保證數據的一致性
5.3.2 磁盤快照分類
- [x] 按快照信息保存分為:
- 內置快照:快照數據和base磁盤數據放在一個qcow2文件中
- 外置快照:快照數據單獨的qcow2文件存放
- [x] 按虛擬機狀態可以分為:
- 關機態快照:數據可以保持一致性
- 運行態快照:數據無法保持一致性,類似與系統crash后的磁盤數據。使用是可能需要fsck等操作。
- [x] 按磁盤數量可以分為:
- 單盤:單盤快照不涉及原子性
- 多盤:涉及原子性。主要分兩個方面:1,是所有盤快照點相同 2,所有盤要么都快照成功,要么都快照失敗。主要依賴於qemu的transaction實現
5.3.3 管理磁盤快照
語法格式:
[root@localhost ~]# 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@localhost vm]# qemu-img snapshot -l /vm/Base_CentOS6.5.qcow2
[root@localhost vm]# qemu-img snapshot -c s1 /vm/Base_CentOS6.5.qcow2 #創建磁盤快照
[root@localhost 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@localhost vm]# qemu-img snapshot -a s1 /vm/Base_CentOS6.5.qcow2
需要注意的是,進行磁盤快照回滾時需要關閉虛擬機,回滾后,我們發現數據已經恢復
qemu-img方式的磁盤快照只能支持原生態的qcow2格式,不支持從raw格式轉換而來的qcow2不然會出現問題。
5.4 存儲池
5.4.1 存儲池的基礎概念
- [x] Libvirt可以以存儲池的形式對存儲進行統一管理,簡化操作
- [x] 對於虛擬機操作來說,存儲池和卷並不是必須的
- [x] 支持以下存儲池
- 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
[root@localhost vm]# cd /etc/libvirt/storage/
[root@localhost storage]# ll
總用量 12
drwxr-xr-x. 2 root root 54 4月 4 20:39 autostart
-rw-------. 1 root root 538 3月 30 12:12 default.xml #對應存儲池的xml文件
-rw-------. 1 root root 511 4月 4 10:16 iso.xml #對應存儲池的xml文件
-rw-------. 1 root root 508 4月 4 20:39 VM.xml #對應存儲池的xml文件
[root@localhost 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>b985a9b1-730b-43c7-96b1-00c494276117</uuid> #唯一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@localhost storage]# ll autostart/
總用量 0
lrwxrwxrwx. 1 root root 32 3月 30 12:12 default.xml -> /etc/libvirt/storage/default.xml
lrwxrwxrwx. 1 root root 28 4月 4 10:16 iso.xml -> /etc/libvirt/storage/iso.xml
lrwxrwxrwx. 1 root root 27 4月 4 20:39 VM.xml -> /etc/libvirt/storage/VM.xml
#如果我們用virsh pool-edit iso來打開iso.xml文件的話,會是如下內容
[root@localhost storage]# virsh pool-edit iso
<pool type='dir'>
<name>iso</name>
<uuid>b985a9b1-730b-43c7-96b1-00c494276117</uuid>
<capacity unit='bytes'>18238930944</capacity> #內容出現了變化
<allocation unit='bytes'>9555505152</allocation> #內容出現了變化
<available unit='bytes'>8683425792</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中的存儲池相關命令
virsh中的存儲卷相關命令
5.4.2 顯示池與卷的信息
#pool-list 幫助
[root@localhost storage]# virsh pool-list --help
NAME
pool-list - 列出池
SYNOPSIS
pool-list [--inactive] [--all] [--transient] [--persistent] [--autostart] [--no-autostart] [--type <string>] [--details]
DESCRIPTION
返回池列表
OPTIONS
--inactive 列出不活躍的池
--all 不活躍和活躍的池
--transient 列出臨時池
--persistent 列出持久池
--autostart 列出啟用 autostart 的池
--no-autostart 列出禁用 autostart 的池
--type <string> 只列出指定類型的池(如果支持)
--details 為池顯示擴展的詳情
#查看所有的存儲池
[root@localhost storage]# virsh pool-list
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
iso active yes
VM active yes
#查看某個存儲池的詳細信息
[root@localhost storage]# virsh pool-info VM
setlocale: No such file or directory
Name: VM
UUID: 8594b419-685a-477d-88bd-c8f1b057073e
State: running #存儲池狀態
Persistent: yes #是否是永久性存儲池
Autostart: yes #是否隨系統開機啟動
Capacity: 39.24 GiB #總容量大小
Allocation: 6.97 GiB #已經分配容量
Available: 32.27 GiB #可用容量
#查看某個存儲池中的所有存儲卷
[root@localhost vm]# virsh vol-list VM
setlocale: No such file or directory
Name Path
------------------------------------------------------------------------------
Base_CentOS6.5.qcow2 /vm/Base_CentOS6.5.qcow2
centos6.5-2.qcow2 /vm/centos6.5-2.qcow2
chensiqi.qcow2 /vm/chensiqi.qcow2
CRM-disk0.qcow2 /vm/CRM-disk0.qcow2
ERP-disk0.qcow2 /vm/ERP-disk0.qcow2
HR-disk0.qcow2 /vm/HR-disk0.qcow2
LNMP-disk1.qcow2 /vm/LNMP-disk1.qcow2
LNMP-disk1.vmdk /vm/LNMP-disk1.vmdk
lost+found /vm/lost+found
OA-disk0.qcow2 /vm/OA-disk0.qcow2
5.4.3 基於目錄的存儲池
- [x] 准備目錄
- 設置目錄權限
# chown root:root /guest_images/
# chmod 700 /guest_images
- 設置目錄權限
- [x] 通過virt-manager創建
- [x] 通過virsh創建
# virsh pool-define-as guest_images dir --target "/guest_images2"
存儲池實操:
#創建存儲池目錄
root@localhost ~]# mkdir /guest_images
[root@localhost ~]# chown root:root /guest_images
[root@localhost ~]# chmod 700 /guest_images/
(1)通過virt-manager創建存儲池
#用命令查看存儲池狀態
[root@localhost ~]# virsh pool-list
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_dir 活動 是 #創建完畢
iso 活動 是
VM 活動 是
#查看新創建的存儲池的配置文件
[root@localhost ~]# 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>1a0b0e7f-8f40-4b59-96d0-42d8eb4ad709</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刪除存儲池
[root@localhost ~]# virsh pool-list --all
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
guest_images_dir inactive yes #狀態變成了不活躍
iso active yes
VM active yes
[root@localhost ~]# virsh pool-list --all
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
iso active yes
VM active yes
[root@localhost ~]# ls /etc/libvirt/storage/
VM.xml autostart default.xml iso.xml
(2)通過virsh創建一個自定義存儲池
#定義一個存儲池
[root@localhost ~]# virsh pool-define --help
setlocale: No such file or directory
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> file containing an XML pool description
[root@localhost ~]# virsh pool-define-as guest_images dir --target "/guest_images2" #guest_images存儲池名字,target目標路徑
Pool guest_images defined
[root@localhost ~]# virsh pool-list --all
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
guest_images inactive no #新定義的存儲池並沒有啟動
iso active yes
VM active yes
[root@localhost ~]# virsh pool-start guest_images #啟動存儲池
error: Failed to start pool guest_images #啟動失敗
error: cannot open directory '/guest_images2': 沒有那個文件或目錄
#我們不要用手動去創建存儲池目錄,因為可能會有權限問題。
#創建一個定義好的存儲池的存儲目錄
root@localhost ~]# virsh pool-build guest_images
Pool guest_images built
[root@localhost ~]# ll -d /gue*
drws--S--- 2 root root 6 May 6 16:25 /guest_images
drwxr-xr-x 2 root root 6 May 11 10:55 /guest_images2
[root@localhost ~]# virsh pool-start guest_images #啟動存儲池
Pool guest_images started
[root@localhost ~]# virsh pool-list --all
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
guest_images active no #存儲池處於啟動狀態
iso active yes
VM active yes
[root@localhost ~]# virsh pool-autostart guest_images #標記存儲池自動啟動
setlocale: No such file or directory
Pool guest_images marked as autostarted
[root@localhost ~]# virsh pool-list --all
setlocale: No such file or directory
Name State Autostart
-------------------------------------------
default active yes
guest_images active yes
iso active yes
VM active yes
刪除自定義的存儲池
[root@localhost ~]# virsh pool-destroy guest_images #停止存儲池的活動
Pool guest_images destroyed
[root@localhost ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
default active yes
guest_images inactive yes
iso active yes
VM active yes
[root@localhost ~]# virsh pool-delete guest_images #刪除存儲池目錄
Pool guest_images deleted
[root@localhost ~]# ll -d /gue* #存儲池的存儲目錄已經被刪除了
drws--S--- 2 root root 6 May 6 16:25 /guest_images
[root@localhost ~]# virsh pool-info guest_images #存儲池配置文件還在
Name: guest_images
UUID: 4f37b935-1529-4623-a367-e5e3435d3b55
State: inactive
Persistent: yes
Autostart: yes
[root@localhost ~]# ll /etc/libvirt/storage/
total 16
-rw-------. 1 root root 508 Apr 4 20:39 VM.xml
drwxr-xr-x. 2 root root 78 May 11 10:58 autostart
-rw-------. 1 root root 538 Mar 30 12:12 default.xml
-rw------- 1 root root 539 May 11 10:41 guest_images.xml #在這里
-rw-------. 1 root root 511 Apr 4 10:16 iso.xml
[root@localhost ~]# virsh pool-undefine guest_images #清除存儲池配置文件
Pool guest_images has been undefined
[root@localhost ~]# ll /etc/libvirt/storage/ #已經沒了
total 12
-rw-------. 1 root root 508 Apr 4 20:39 VM.xml
drwxr-xr-x. 2 root root 54 May 11 11:23 autostart
-rw-------. 1 root root 538 Mar 30 12:12 default.xml
-rw-------. 1 root root 511 Apr 4 10:16 iso.xml
[root@localhost ~]# virsh pool-info guest_images #配置文件已經沒了
error: failed to get pool 'guest_images'
error: 未找到存儲池: 沒有與名稱 'guest_images' 匹配的存儲池
5.4.4 基於分區的存儲池
- [x] libvirtd會自動mount分區
- [x] 准備分區並創建文件系統
# fdisk /dev/sdc
# mkfs.ext4 /dev/sdc1
- [x] 創建:
- Source Path:塊設備名
- Target Path:mount到的目錄名
# virsh pool-define-as guest_images_fs fs --source-dev "/dev/sdc1" --target "/guest_images2"
操作准備
給虛擬機再加入一塊磁盤,分區並進行格式化來繼續我們的實驗
#將新磁盤sdc分兩個分區,並進行格式化。
[root@localhost ~]# ll /dev/sdc
brw-rw---- 1 root disk 8, 32 5月 11 20:12 /dev/sdc
brw-rw---- 1 root disk 8, 33 5月 11 20:12 /dev/sdc1
brw-rw---- 1 root disk 8, 34 5月 11 20:12 /dev/sdc2
[root@localhost ~]# rm -rf /guest_images/ #清除舊存儲池存放目錄
(1)通過virt-manager創建一個基於分區的存儲池
#驗證創建的存儲池
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_fs 活動 是 #有了並處於活動狀態
iso 活動 是
VM 活動 是
[root@localhost ~]# virsh pool-info guest_images_fs
名稱: guest_images_fs
UUID: e50f34cc-89a5-47e3-80e5-324b91b0d001
狀態: running
持久: 是
自動啟動: 是
容量: 9.72 GiB
分配: 36.02 MiB
可用: 9.68 GiB
[root@localhost ~]# ll -d /guest_images/
drwxr-xr-x 3 root root 4096 5月 11 20:14 /guest_images/ #存儲池目錄被自動創建了
[root@localhost ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/cl-root 17G 8.9G 8.1G 53% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 4.0K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 144M 871M 15% /boot
/dev/mapper/vmvg-lvvm1 40G 7.0G 31G 19% /vm
tmpfs 183M 8.0K 183M 1% /run/user/0
/dev/sdc1 9.8G 37M 9.2G 1% /guest_images #磁盤/dev/sdc1被自動掛載了
[root@localhost ~]# 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>e50f34cc-89a5-47e3-80e5-324b91b0d001</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@localhost ~]# mount | grep sdc1 #查看mount掛載的信息
/dev/sdc1 on /guest_images type ext4 (rw,relatime,data=ordered)
最后同學們在重啟電腦驗證存儲池自動激活時是否可以自動掛載磁盤/dev/sdc1,在這里我就不繼續演示了。
然后我們清理掉guest_images_fs存儲池信息,繼續實驗
(2)通過virsh命令創建一個基於分區的存儲池
[root@localhost ~]# virsh pool-define-as guest_images_fs fs \
> --source-dev "/dev/sdc1" --target "/guest_images2"
定義池 guest_images_fs
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_fs 不活躍 否 #新定義的存儲池未啟動
iso 活動 是
VM 活動 是
[root@localhost ~]# ll -d /gue* #沒有目標掛載目錄
drwxr-xr-x 2 root root 6 5月 11 20:22 /guest_images
root@localhost ~]# virsh pool-build guest_images_fs #創建存儲池存儲目錄
構建池 guest_images_fs
[root@localhost ~]# ll -d /guest*
drwxr-xr-x 2 root root 6 5月 11 20:22 /guest_images
drwxr-xr-x 2 root root 6 5月 11 20:52 /guest_images2 #存在了
[root@localhost ~]# virsh pool-start guest_images_fs #啟動存儲池
池 guest_images_fs 已啟動
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_fs 活動 否 #啟動成功
iso 活動 是
VM 活動 是
[root@localhost ~]# virsh pool-autostart guest_images_fs #標記存儲池開機自啟動
池 guest_images_fs 標記為自動啟動
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_fs 活動 是
iso 活動 是
VM 活動 是
[root@localhost ~]# mount | grep /dev/sdc1 #已經自動掛載
/dev/sdc1 on /guest_images2 type ext4 (rw,relatime,data=ordered)
5.4.5 基於LVM的存儲池
- [x] 基於LVM的存儲池要求使用全部磁盤分區
- [x] 創建時存儲池,有兩種方法
- 使用現有的VG
- 創建新的VG
- Target Path:新的卷組名
- Source Path:存儲設備的位置
- Build Pool:會創建新的VG
- [x] 創建
# 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@localhost storage]# ll /dev/sdc*
brw-rw---- 1 root disk 8, 32 5月 11 23:25 /dev/sdc
brw-rw---- 1 root disk 8, 33 5月 11 23:25 /dev/sdc1 #這就是我們實驗用的分區
#創建一個卷組
[root@localhost storage]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@localhost storage]# vgcreate guest_images_lvm /dev/sdc1
Volume group "guest_images_lvm" successfully created
#驗證存儲池創建狀態
[root@localhost storage]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm 活動 是
iso 活動 是
VM 活動 是
[root@localhost storage]# 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>a95d43e7-0d20-4347-8069-9ae39074058d</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>
圖形界面創建成功
(2)通過virt-manager創建vg並創建LVM存儲池
先清除之前創建的存儲池
#清除之前的vg卷組
[root@localhost storage]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 19.00g 0
guest_images_lvm 1 0 0 wz--n- 20.00g 20.00g
vmvg 1 1 0 wz--n- 40.00g 0
[root@localhost storage]# vgremove guest_images_lvm
Volume group "guest_images_lvm" successfully removed
[root@localhost storage]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 19.00g 0
vmvg 1 1 0 wz--n- 40.00g 0
#清除之前的pv狀態
[root@localhost storage]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 19.00g 0
/dev/sdb1 vmvg lvm2 a-- 40.00g 0
/dev/sdc1 lvm2 --- 20.00g 20.00g
[root@localhost storage]# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@localhost storage]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 19.00g 0
/dev/sdb1 vmvg lvm2 a-- 40.00g 0
#通過fdisk清除/dev/sdc1的磁盤分區,不要通過mkfs.ext4方式來清楚/dev/sdc1,不然圖形軟件無法自動pv化磁盤
[root@localhost storage]# ll /dev/sdc*
brw-rw---- 1 root disk 8, 32 5月 11 23:41 /dev/sdc
#驗證存儲池創建狀態
[root@localhost storage]# 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@localhost storage]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm2 活動 是
iso 活動 是
VM 活動 是
[root@localhost storage]# 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@localhost storage]# 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@localhost storage]# vgremove guest_images_lvm2
Volume group "guest_images_lvm2" successfully removed
[root@localhost storage]# 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@localhost storage]# pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped.
#virsh命令創建vg並創建基於LVM的存儲池
[root@localhost storage]# virsh pool-define-as guest_images_lvm3 logical --source-dev=/dev/sdc --source-name=libvirt_lvm --target=/dev/libvirt_vg
定義池 guest_images_lvm3
[root@localhost storage]# vgs #我們發現virsh命令沒有自動創建vg
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 19.00g 0
vmvg 1 1 0 wz--n- 40.00g 0
[root@localhost storage]# pvs #我們發現virsh命令沒有自動創建pv
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 19.00g 0
/dev/sdb1 vmvg lvm2 a-- 40.00g 0
#virsh命令創建卷組libvirt_lvm
[root@localhost storage]# virsh pool-build guest_images_lvm3
構建池 guest_images_lvm3
[root@localhost storage]# 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 libvirt_lvm lvm2 a-- 20.00g 20.00g
[root@localhost storage]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 19.00g 0
libvirt_lvm 1 0 0 wz--n- 20.00g 20.00g
vmvg 1 1 0 wz--n- 40.00g 0
#啟動LVM存儲池並標記開機自啟動
[root@localhost storage]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm3 不活躍 否
iso 活動 是
VM 活動 是
[root@localhost storage]# virsh pool-start guest_images_lvm3
池 guest_images_lvm3 已啟動
[root@localhost storage]# virsh pool-autostart guest_images_lvm3
池 guest_images_lvm3 標記為自動啟動
[root@localhost storage]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm3 活動 是
iso 活動 是
VM 活動 是
5.4.6 基於NFS的存儲池
# virsh pool-define-as --name nfstrial2 --type netfs --source-host 192.1688.200.132 --source-path /nfsshare --target /nfstrial2
環境准備
在一台KVM虛擬機中創建NFS共享存儲文件系統
創建基於NFS共享存儲的存儲池
第一步:在一台虛擬機中yum安裝nfs服務端
#這是一台NAT鏈接的虛擬機,yum安裝nfs
[root@localhost ~]# yum -y install rpcbind nfs-utils
[root@localhost ~]# mkdir /nfsshare
[root@localhost ~]# useradd nfsnobody
[root@localhost ~]# chown nfsnobody.nfsnobody /nfsshare
[root@localhost ~]# cat /etc/exports #因為虛擬機是NAT模式,若要宿主機能夠掛載需要開通兩個網段權限
/nfsshare 192.168.122.0(rw,sync) 192.168.200.0(rw,sync)
[root@localhost ~]# service rpcbind start
Starting rpcbind: [ OK ]
[root@localhost ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
第二步:virt-manager創建基於NFS的存儲池
#在宿主機中測試共享目錄
[root@localhost ~]# df -h | grep nfsshare #已經自動掛載
192.168.122.123:/nfsshare 6.7G 899M 5.4G 14% /nfs1
[root@localhost ~]# mount | grep nfsshare #已經自動掛載
192.168.122.123:/nfsshare on /nfs1 type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.122.1,local_lock=none,addr=192.168.122.123)
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
iso 活動 是
nfs1 活動 是 #已經處於活動狀態
VM 活動 是
[root@localhost ~]# 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>a662a36a-8927-4aa9-a73b-67bad0a378ef</uuid>
<capacity unit='bytes'>0</capacity>
<allocation unit='bytes'>0</allocation>
<available unit='bytes'>0</available>
<source>
<host name='192.168.122.123'/> #源共享主機IP
<dir path='/nfsshare'/> #源共享主機目錄
<format type='auto'/>
</source>
<target>
<path>/nfs1</path> #目標掛載目錄
</target>
</pool>
到這里就配置完畢了,同學們可以自動對共享目錄進行寫入測試
另外,glusterfs的配置方法也是類似的噢....
5.5 存儲卷
5.5.1 存儲卷概述
- [x] 存儲池被分割為存儲卷(Storage Volume)
- [x] 存儲卷
- 文件
- 塊設備(如物理分區,LVM邏輯卷等)
- libvirt管理的其他類型存儲的抽象
[root@localhost ~]# 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 存儲卷管理
- [x] 創建
- [x] 克隆
- [x] 刪除
(1) 演示:存儲卷的創建
[root@localhost ~]# 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、qcow2、qed、vmdk
--backing-vol <string> 提取快照時的后端卷
--backing-vol-format <string> 提取快照時的后端卷格式
--prealloc-metadata 預先分配的元數據(用於 qcow2 而不是整個分配)
--print-xml 打印 XML 文檔,但不能定義/創建
基於目錄的存儲池中的存儲卷管理
#查看所有的存儲池
[root@localhost ~]# virsh pool-list
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
iso 活動 是
nfs1 活動 是
VM 活動 是 #目標存儲池
#查看VM存儲的xml文檔
[root@localhost ~]# virsh pool-dumpxml VM
<pool type='dir'> #基於目錄的存儲池
<name>VM</name>
<uuid>8594b419-685a-477d-88bd-c8f1b057073e</uuid>
<capacity unit='bytes'>42137255936</capacity>
<allocation unit='bytes'>7494885376</allocation>
<available unit='bytes'>34642370560</available>
<source>
</source>
<target>
<path>/vm</path> #存儲池位置
<permissions>
<mode>0755</mode>
<owner>0</owner>
<group>0</group>
</permissions>
</target>
</pool>
#查看VM存儲池的所有存儲卷
[root@localhost ~]# virsh vol-list VM
名稱 路徑
------------------------------------------------------------------------------
Base_CentOS6.5.qcow2 /vm/Base_CentOS6.5.qcow2
centos6.5-2.qcow2 /vm/centos6.5-2.qcow2
chensiqi.qcow2 /vm/chensiqi.qcow2
CRM-disk0.qcow2 /vm/CRM-disk0.qcow2
ERP-disk0.qcow2 /vm/ERP-disk0.qcow2
HR-disk0.qcow2 /vm/HR-disk0.qcow2
LNMP-disk1.qcow2 /vm/LNMP-disk1.qcow2
LNMP-disk1.vmdk /vm/LNMP-disk1.vmdk
lost+found /vm/lost+found
OA-disk0.qcow2 /vm/OA-disk0.qcow2
#向VM存儲池里創建一個存儲卷
[root@localhost ~]# virsh vol-create-as VM test1.qcow2 1G --format qcow2
創建卷 test1.qcow2
#查看test1.qcow2的卷信息的兩種方式
[root@localhost ~]# virsh vol-info /vm/test1.qcow2
名稱: test1.qcow2
類型: 文件
容量: 1.00 GiB
分配: 196.00 KiB
[root@localhost ~]# virsh vol-info test1.qcow2 --pool VM
名稱: test1.qcow2
類型: 文件
容量: 1.00 GiB
分配: 196.00 KiB
基於LVM的存儲池中的存儲卷管理
#定義一個基於LVM的存儲池
[root@localhost ~]# ll /dev/sdc*
brw-rw---- 1 root disk 8, 32 5月 12 00:05 /dev/sdc
[root@localhost ~]# 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@localhost ~]# pvcreate -y /dev/sdc
Wiping ext4 signature on /dev/sdc.
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# virsh pool-build guest_images_lvm
構建池 guest_images_lvm
#啟動基於LVM的存儲池
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm 不活躍 否
iso 活動 是
nfs1 活動 是
VM 活動 是
[root@localhost ~]# virsh pool-start guest_images_lvm
池 guest_images_lvm 已啟動
[root@localhost ~]# virsh pool-autostart guest_images_lvm
池 guest_images_lvm 標記為自動啟動
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
guest_images_lvm 活動 是
iso 活動 是
nfs1 活動 是
VM 活動 是
#向LVM存儲池中創建一個存儲卷
[root@localhost ~]# virsh vol-create-as guest_images_lvm lvvoll 1G
創建卷 lvvoll
[root@localhost ~]# virsh vol-create-as guest_images_lvm lvvol2 2G
創建卷 lvvol2
[root@localhost ~]# virsh vol-create-as guest_images_lvm lvvol3 3G
創建卷 lvvol3
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM
使用 test2.qcow2 克隆的卷 test1.qcow2
[root@localhost ~]# virsh vol-list VM
名稱 路徑
------------------------------------------------------------------------------
Base_CentOS6.5.qcow2 /vm/Base_CentOS6.5.qcow2
centos6.5-2.qcow2 /vm/centos6.5-2.qcow2
chensiqi.qcow2 /vm/chensiqi.qcow2
CRM-disk0.qcow2 /vm/CRM-disk0.qcow2
ERP-disk0.qcow2 /vm/ERP-disk0.qcow2
HR-disk0.qcow2 /vm/HR-disk0.qcow2
LNMP-disk1.qcow2 /vm/LNMP-disk1.qcow2
LNMP-disk1.vmdk /vm/LNMP-disk1.vmdk
lost+found /vm/lost+found
OA-disk0.qcow2 /vm/OA-disk0.qcow2
test1.qcow2 /vm/test1.qcow2
test2.qcow2 /vm/test2.qcow2 #克隆后的盤
[root@localhost ~]# virsh vol-info /vm/test2.qcow2
名稱: test2.qcow2
類型: 文件
容量: 1.00 GiB
分配: 196.00 KiB
#克隆基於LVM的存儲池中的存儲卷
[root@localhost ~]# virsh vol-clone lvvoll lvvol4 --pool guest_images_lvm
使用 lvvol4 克隆的卷 lvvoll
[root@localhost ~]# 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@localhost ~]# virsh vol-info /dev/guest_images_lvm/lvvol4
名稱: lvvol4
類型: 塊
容量: 1.00 GiB
分配: 1.00 GiB
(3) 演示:存儲卷的刪除
#查看命令幫助
[root@localhost ~]# 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@localhost ~]# virsh vol-delete lvvol4 guest_images_lvm
卷 lvvol4 被刪除
[root@localhost ~]# virsh vol-delete lvvol3 guest_images_lvm
卷 lvvol3 被刪除
[root@localhost ~]# virsh vol-delete lvvol2 guest_images_lvm
卷 lvvol2 被刪除
[root@localhost ~]# virsh vol-delete lvvoll guest_images_lvm
卷 lvvoll 被刪除
[root@localhost ~]# virsh vol-list guest_images_lvm
名稱 路徑
------------------------------------------------------------------------------
#刪除基於LVM的存儲池
root@localhost ~]# virsh pool-destroy guest_images_lvm
銷毀池 guest_images_lvm
[root@localhost ~]# virsh pool-undefine guest_images_lvm
池 guest_images_lvm 已經被取消定義
[root@localhost ~]# virsh pool-list --all
名稱 狀態 自動開始
-------------------------------------------
default 活動 是
iso 活動 是
nfs1 活動 是
VM 活動 是
#刪除基於目錄的存儲池中的存儲卷
[root@localhost ~]# virsh vol-delete /vm/test2.qcow2
卷 /vm/test2.qcow2 被刪除
5.5.3 向虛擬機添加卷
- [x] 通過virt-manager添加新設備
- 通過圖形管理程序添加新設備
- [x] attach-device
- 通過XML添加新的設備
- [x] attach-disk
- 通過參數添加新的磁盤設備
(1)通過virt-manager添加新設備
假如我們想要向一個虛擬機中添加一塊虛擬磁盤
點開一個虛擬機,運行狀態的也可以
選擇硬件添加
添加虛擬磁盤
進入運行中的虛擬機的交互界面
(2)通過XML文件添加新的設備
#創建一個disk類型的xml文檔
[root@localhost ~]# 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@localhost ~]# virsh domblklist --help
NAME
domblklist - 列出所有域塊
SYNOPSIS
domblklist <domain> [--inactive] [--details]
DESCRIPTION
獲取域塊設備小結
OPTIONS
[--domain] <string> 域名,id 或 uuid
--inactive 獲取不活躍而不是運行的配置
--details type 和 device 值的附加顯示
#查看虛擬機的磁盤掛載情況
[root@localhost ~]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
1 centos6.5 running #一個正在運行的虛擬機
- Base_CentOS7 關閉
- centos6.5-2 關閉
- centos6.5-3 關閉
- crm 關閉
- erp 關閉
- hr 關閉
- LNMP 關閉
- oa 關閉
- vm2 關閉
[root@localhost ~]# virsh domblklist centos6.5 #查看虛擬機的磁盤設備掛載情況
目標 源
------------------------------------------------
vda /var/lib/libvirt/images/centos6.5.qcow2
hda /iso/CentOS-6.5-x86_64-bin-DVD1.iso
#將磁盤類型的xml文檔加入到虛擬機
[root@localhost ~]# virsh attach-device --help #查看命令幫助
NAME
attach-device - 從一個XML文件附加裝置
SYNOPSIS
attach-device <domain> <file> [--persistent] [--config] [--live] [--current]
DESCRIPTION
從一個XML文件附加裝置.
OPTIONS
[--domain] <string> 域名,id 或 uuid
[--file] <string> XML 文件
--persistent 讓實時更改持久 #虛擬機永久生效
--config 影響下一次引導 #掛載的磁盤重啟虛擬機生效
--live 影響運行的域
--current 影響當前域
[root@localhost ~]# virsh attach-device centos6.5 /tmp/disks.xml --persistent #將xml文件添加進虛擬機
成功附加設備
[root@localhost ~]# virsh domblklist centos6.5
目標 源
------------------------------------------------
vda /var/lib/libvirt/images/centos6.5.qcow2
vdb /vm/test1.qcow2 #存儲卷添加成功
hda /iso/CentOS-6.5-x86_64-bin-DVD1.iso
通過virt-manager查看磁盤已經添加成功,如下圖:
(3)通過參數添加新的磁盤設備
#克隆一個存儲卷
[root@localhost ~]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM
使用 test2.qcow2 克隆的卷 test1.qcow2
[root@localhost ~]# ll /vm/test2.qcow2
-rw------- 1 qemu qemu 1245184 5月 18 22:00 /vm/test2.qcow2
#掛載一個存儲卷到虛擬機
[root@localhost ~]# virsh attach-disk centos6.5 --source=/vm/test2.qcow2 --target=vdc
成功附加磁盤
[root@localhost ~]# virsh domblklist centos6.5
目標 源
------------------------------------------------
vda /var/lib/libvirt/images/centos6.5.qcow2
vdb /vm/test1.qcow2
vdc /vm/test2.qcow2 #掛載成功
hda /iso/CentOS-6.5-x86_64-bin-DVD1.iso