- 存储的性能几乎老是虚拟化的瓶颈
- 经过多个硬盘驱动以分布磁盘I/O来实现存储解决方案
- 考虑部署集中化的SAN/NFS来实现高可用性和实时迁移
基于文件系统的存储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
固定 Fixedvim
- 在配置时,指定磁盘大小
- 无论在虚拟磁盘上实际存储多少数据,都将占用相同大小主机磁盘空间
动态 Dynamic后端
- 增加到最大容量,可是只根据需求使用更多的空间
差别 Differencingcentos
- 由于建立是差别磁盘,因此只保存变动的数据
- 例如,将操做系统安装在父盘,而后建立差别化磁盘来执行进一步配置
- 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
qemu-img 是一个功能强制磁盘镜像管理工具
qemu-img --help 包括如下功能网络
- check 检查完整性
- create 建立镜像
- commit 提交更改
- compare 比较
- convert 转换
- info 得到信息
- map 映射
- snapshot 快照管理
- rebase 在已有的镜像的基础上建立新的镜像
- resize 调整大小
- amend 修订镜像格式选项
[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
[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
- 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
- 存储与基础镜像(父)磁盘的变化
基础镜像(父)磁盘不会改变
差别磁盘隔离变化
多个差别磁盘可使用相同的基础镜像(父)磁盘- 优势:标准化基础镜像,节省空间
- 缺点:增长了开销,较差的性能
#建立父磁盘并安装父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 #子系统建立完毕
父子系统的方式,其实就是克隆的方式,咱们若是要经过命令来实现就是如此操做。
若是在图形界面下,相似的方式以下图
#语法格式 [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虚拟机中找一个关闭状态的虚拟机进行导出
(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
语法格式
[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
磁盘快照
- 对磁盘数据进行快照
- 主要用于虚拟机备份等场合
内存快照
- 对虚拟机的内存/设备信息进行保存
- 该机制同时用于休眠恢复,迁移等场景
- 主要使用virsh save(qemu migrate to file)实现,只能对运行的虚拟机进行
检查点快照
- 同时保存虚拟机的磁盘快照和内存快照
- 用于将虚拟机恢复到某个时间点
- 能够保证数据的一致性
按快照信息保存分为:
- 内置快照:快照数据和base磁盘数据放在一个qcow2文件中
- 外置快照:快照数据单独的qcow2文件存放
按虚拟机状态能够分为:
- 关机态快照:数据能够保持一致性
- 运行态快照:数据没法保持一致性,相似与系统crash后的磁盘数据。使用是可能须要fsck等操做。
按磁盘数量能够分为:
- 单盘:单盘快照不涉及原子性
- 多盘:涉及原子性。主要分两个方面:1,是全部盘快照点相同 2,全部盘要么都快照成功,要么都快照失败。主要依赖于qemu的transaction实现
语法格式:
[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否则会出现问题。
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
[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中的存储池相关命令
virsh中的存储卷相关命令
#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
准备目录
- 设置目录权限
# chown root:root /guest_images/
# chmod 700 /guest_images
- 经过virt-manager建立
- 经过virsh建立
# virsh pool-define-as guest_images dir --target "/guest_images2"
存储池实操:
#建立存储池目录 [root@KVM ~]# mkdir /guest_images [root@KVM ~]# chown root:root /guest_images [root@KVM ~]# chmod 700 /guest_images/
(1)经过virt-manager建立存储池
#用命令查看存储池状态 [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删除存储池
[root@KVM ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_dir 不活动 否 iso 活动 是 vm 活动 是
[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' 匹配的存储池
- 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建立一个基于分区的存储池
#验证建立的存储池 [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)
基于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
#验证存储池建立状态 [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
#验证存储池建立状态 [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 活动 是
# 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的存储池
#在宿主机中测试共享目录 [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>
存储池被分割为存储卷(Storage Volume)
存储卷
- 文件
- 块设备(如物理分区,LVM逻辑卷等)
- libvirt管理的其余类型存储的抽象
[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 擦除卷
- 建立
- 克隆
- 删除
(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 被删除
- 经过virt-manager添加新设备 :经过图形管理程序添加新设备
- attach-device :经过XML添加新的设备
- attach-disk :经过参数添加新的磁盘设备
(1)经过virt-manager添加新设备
假如咱们想要向一个虚拟机中添加一块虚拟磁盘
点开一个虚拟机,运行状态的也能够
选择硬件添加
添加虚拟磁盘
进入运行中的虚拟机的交互界面
(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查看磁盘已经添加成功,以下图:
(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 -