管理虚拟存储(二)
管理虚拟存储(二)五.管理虚拟存储5.1 虚拟磁盘概述5.1.1 虚拟化项目中存储的注意事项
5.1.2 KVM存储模式
5.1.3 虚拟磁盘类型
5.1.4 KVM支持的虚拟磁盘类型
[[email?protected] ~]# 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概述
5.2.2 创建虚拟磁盘[[email?protected] ~]# 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 [[email?protected] ~]# qemu-img create t1.img 1g #只输入磁盘名和大小创建 Formatting 't1.img',fmt=raw size=1073741824 #默认的磁盘格式fmt=raw [[email?protected] ~]# qemu-img info t1.img #查看虚拟磁盘的信息 image: t1.img #文件名称 file format: raw #文件格式 virtual size: 1.0G (1073741824 bytes) disk size: 0 #虚拟磁盘大小 [[email?protected] ~]# ll -h t1.img #磁盘空间尺寸是0???? -rw-r--r--. 1 root root 1.0G 12月 30 14:49 t1.img #ll查看磁盘是1G没错 [[email?protected] ~]# du -sh t1.img #但我们用du查看一下,发现磁盘真实空间的占用还真是0 0 t1.img
#假如我们想看一下各种磁盘格式所附带的-o option都有什么,我们可以这么做 [[email?protected] ~]# qemu-img create -f raw -o ? #raw格式磁盘只有一个尺寸大小选项 Supported options: size Virtual disk size [[email?protected] ~]# 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命令模拟创建一个没有空洞的文件 [[email?protected] ~]# dd if=/dev/zero of=flat1.img bs=1024k count=1000 记录了1000+0 的读入 记录了1000+0 的写出 1048576000字节(1.0 GB)已复制,32.3206 秒,32.4 MB/秒 [[email?protected] ~]# qemu-img info flat1.img image: flat1.img file format: raw virtual size: 1.0G (1048576000 bytes) disk size: 1.0G #磁盘占用也是1G,没有空洞 #ext4文件格式是支持空洞文件的创建的,例如dd命令我们也可以实现空洞文件的创建 [[email?protected] ~]# 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/秒 [[email?protected] ~]# qemu-img info flat2.img image: flat2.img file format: raw virtual size: 1.0G (1073741824 bytes) disk size: 0 #实际磁盘占用0 空洞文件被复制以后,那么还是空洞文件吗? [[email?protected] ~]# du -sh flat* 1000M flat1.img 0 flat2.img [[email?protected] ~]# cp flat1.img flat1a.img [[email?protected] ~]# cp flat2.img flat2a.img [[email?protected] ~]# du -sh flat* 1000M flat1a.img #非空洞文件复制后还是非空洞 1000M flat1.img 0 flat2a.img #空洞文件复制后还是空洞文件 0 flat2.img #利用--sparse=always将非空洞文件复制成空洞文件 [[email?protected] ~]# du -sh flat* 1000M flat1a.img 1000M flat1.img 0 flat2a.img 0 flat2.img [[email?protected] ~]# cp flat1.img flat1b.img --sparse=always [[email?protected] ~]# du -sh flat* 1000M flat1a.img 0 flat1b.img #加了参数的cp复制出来的是空洞文件 1000M flat1.img 0 flat2a.img 0 flat2.img #利用--sparse=never将空洞文件复制成非空洞文件 [[email?protected] ~]# du -sh flat* 1000M flat1a.img 0 flat1b.img 1000M flat1.img 0 flat2a.img 0 flat2.img [[email?protected] ~]# cp flat2.img flat2b.img --sparse=never [[email?protected] ~]# 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 检查虚拟磁盘[[email?protected] vm]# pwd /vm [[email?protected] vm]# du -sh ce* 1.1G centos6.5.qcow2 [[email?protected] 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 [[email?protected] 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)
[[email?protected] 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,full) #预分配策略 lazy_refcounts Postpone refcount updates #preallocation=off创建磁盘 [[email?protected] 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 [[email?protected] 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创建磁盘 [[email?protected] 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 [[email?protected] 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创建磁盘 [[email?protected] 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 [[email?protected] 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创建磁盘 [[email?protected] 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 [[email?protected] 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 #四种预分配策略进行对比 [[email?protected] 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 [[email?protected] vm]# du -sh test* 196K test1.qcow2 516K test2.qcow2 1.1G test3.qcow2 1.1G test4.qcow2 通过对比我们发现预分配策略里,off和metadata预分配策略都属于空洞文件,而falloc和full属于非空洞文件。 5.2.5 后备差异虚拟磁盘
#创建父磁盘并安装父Centos6.5操作系统 [[email?protected] vm]# qemu-img create -f qcow2 Base_CentOS6.5.qcow2 qemu-img: Base_CentOS6.5.qcow2: Image creation needs a size parameter [[email?protected] 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 [[email?protected] 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 [[email?protected] ~]# virsh list Id 名称 状态 ---------------------------------------------------- 3 centos6.5 running 6 vm1 running 8 Base_CentOS7 running #创建OA后备差异虚拟磁盘,OA子系统盘 [[email?protected] ~]# 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 [[email?protected] 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子系统盘 [[email?protected] 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 [[email?protected] 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 [[email?protected] 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 [[email?protected] 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 #创建四种子系统虚拟机 [[email?protected] 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 开始安装...... 创建域...... ##中间省略.... 域创建完成。 [[email?protected] 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 开始安装...... 创建域...... ##中间省略.... 域创建完成。 [[email?protected] 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 开始安装...... 创建域...... ##中间省略.... 域创建完成。 [[email?protected] 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 开始安装...... 创建域...... ##中间省略.... 域创建完成。 [[email?protected] 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 #子系统创建完毕
5.2.6 虚拟磁盘格式转换(虚拟机迁移案例)#语法格式 [[email?protected] 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 案例:不同格式虚拟机的迁移 (2)先将.vmdk虚拟磁盘文件拷入到KVM虚拟机中 [[email?protected] vm]# ll -h *.vmdk -rw-r--r--. 1 root root 924M 12月 31 20:56 LNMP-disk1.vmdk [[email?protected] 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)然后进行磁盘文件的格式转换 [[email?protected] vm]# du -sh LNMP-disk1.vmdk 924M LNMP-disk1.vmdk [[email?protected] vm]# qemu-img convert -O qcow2 LNMP-disk1.vmdk LNMP-disk1.qcow2 [[email?protected] vm]# du -sh LNMP* 2.5G LNMP-disk1.qcow2 924M LNMP-disk1.vmdk (4)然后根据虚拟磁盘迁移前的VMware配置,导入到新的KVM虚拟机
#导入新的KVM虚拟机 [[email?protected] 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 开始安装...... 域创建完成。 [[email?protected] vm]# virsh list Id 名称 状态 ---------------------------------------------------- 13 LNMP running 5.2.7 调整虚拟磁盘大小
[[email?protected] vm]# qemu-img --help | grep resize resize [-q] filename [+ | -]size
#实操 [[email?protected] 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 [[email?protected] vm]# qemu-img resize LNMP-disk1.qcow2 +10G #增加10G尺寸 Image resized. [[email?protected] 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 快照/检查点概述
5.3.2 磁盘快照分类
5.3.3 管理磁盘快照语法格式: [[email?protected] 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 #显示快照列表 快照管理之磁盘快照实操: #创建磁盘快照 [[email?protected] vm]# qemu-img snapshot -l /vm/Base_CentOS6.5.qcow2 [[email?protected] vm]# qemu-img snapshot -c s1 /vm/Base_CentOS6.5.qcow2 #创建磁盘快照 [[email?protected] 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 #关闭虚拟机回滚磁盘快照 #在虚拟机里删除一个文件然后进行磁盘回滚 [[email?protected] vm]# qemu-img snapshot -a s1 /vm/Base_CentOS6.5.qcow2
5.4 存储池5.4.1 存储池的基础概念
[[email?protected] ~]# cd /etc/libvirt/storage/ [[email?protected] 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文件 [[email?protected] 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 [[email?protected] 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文件的话,会是如下内容 [[email?protected] 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中的存储卷相关命令 5.4.2 显示池与卷的信息#pool-list 帮助 [[email?protected] 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 #查看所有的存储池 [r[email?protected] storage]# virsh pool-list 名称 状态 自动开始 ------------------------------------------- default 活动 是 iso 活动 是 vm 活动 是 #查看某个存储池的详细信息 [[email?protected] storage]# virsh pool-info vm 名称: vm UUID: b1a25def-9ecf-46d7-93d7-2c59c5594e5c 状态: running #存储池状态 持久: 是 #是否是永久性存储池 自动启动: 是 #是否随系统开机启动 容量: 16.99 GiB #总容量大小 分配: 12.58 GiB #已经分配容量 可用: 4.41 GiB #可用容量 #查看某个存储池中的所有存储卷 [[email?protected] 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 基于目录的存储池
存储池实操: #创建存储池目录 [[email?protected] ~]# mkdir /guest_images [[email?protected] ~]# chown root:root /guest_images [[email?protected] ~]# chmod 700 /guest_images/ (1)通过virt-manager创建存储池 #用命令查看存储池状态 [[email?protected] ~]# virsh pool-list 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_dir 活动 是 #创建完毕 iso 活动 是 vm 活动 是 #查看新创建的存储池的配置文件 [[email?protected] ~]# 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删除存储池 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_dir 不活动 否 iso 活动 是 vm 活动 是 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 iso 活动 是 vm 活动 是 [[email?protected] ~]# ls /etc/libvirt/storage/ autostart default.xml iso.xml vm.xml (2)通过virsh创建一个自定义存储池 #定义一个存储池 [[email?protected] ~]# 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 池描述的文件 [[email?protected] ~]# virsh pool-define-as guest_images dir --target "/guest_images2" #guest_images存储池名字,target目标路径 定义池 guest_images [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images 不活跃 否 #新定义的存储池并没有启动 iso 活动 是 vm 活动 是 [[email?protected] ~]# virsh pool-start guest_images 错误:启动池 guest_images 失败 错误:cannot open directory '/guest_images2': 没有那个文件或目录 #我们不要用手动去创建存储池目录,因为可能会有权限问题。 #创建一个定义好的存储池的存储目录 [[email?protected] ~]# virsh pool-build guest_images 构建池 guest_images [[email?protected] ~]# 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 [[email?protected] ~]# virsh pool-start guest_images #启动存储池 池 guest_images 已启动 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images 活动 否 #存储池处于启动状态 iso 活动 是 vm 活动 是 [[email?protected] ~]# virsh pool-autostart guest_images #标记存储池自动启动 池 guest_images 标记为自动启动 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images 活动 是 iso 活动 是 vm 活动 是 删除自定义的存储池 [[email?protected] ~]# virsh pool-destroy guest_images #停止存储池的活动 销毁池 guest_images [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images 不活跃 是 iso 活动 是 vm 活动 是 [[email?protected] ~]# virsh pool-delete guest_images #删除存储池目录 池 guest_images 被删除 [[email?protected] ~]# ll -d /gue* #存储池的存储目录已经被删除了 drwx------. 2 root root 6 1月 1 17:01 /guest_images [[email?protected] ~]# virsh pool-info guest_images #但存储池配置文件还在 名称: guest_images UUID: e850074b-232a-457f-b7dd-06dd45a72410 状态: 不活跃 持久: 是 自动启动: 是 [[email?protected] ~]# 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 [[email?protected] ~]# virsh pool-undefine guest_images #清除存储池配置文件 池 guest_images 已经被取消定义 [[email?protected] ~]# 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 [[email?protected] ~]# virsh pool-info guest_images #配置文件已经没了 错误:获得池 'guest_images' 失败 错误:未找到存储池: 没有与名称 'guest_images' 匹配的存储池 5.4.4 基于分区的存储池
#将新磁盘sdc分两个分区,并进行格式化。 [[email?protected] ~]# ll /dev/sdc* brw-rw----. 1 root disk 8,32 1月 4 08:37 /dev/sdc [[email?protected] ~]# mkfs.ext4 /dev/sdc #磁盘格式化 [[email?protected] ~]# fdisk /dev/sdc #以下省略若干。。。 [[email?protected] ~]# 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 [[email?protected] /]# mkfs.ext4 /dev/sdc1 #使用前把磁盘格式化一下 [[email?protected] ~]# rm -rf /guest_images/ #清除旧存储池存放目录 (1)通过virt-manager创建一个基于分区的存储池 #验证创建的存储池 [[email?protected] /]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_fs 活动 是 #有了并处于活动状态 [[email?protected] /]# 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 [[email?protected] /]# ll -d /guest_images/ drwxr-xr-x. 3 root root 4096 1月 5 16:17 /guest_images/ [[email?protected] /]# 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被自动挂载了 [[email?protected] /]# 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> [[email?protected] /]# mount | grep sdc1 #查看mount挂载的信息 /dev/sdc1 on /guest_images type ext4 (rw,relatime,seclabel,data=ordered) [[email?protected] /]# reboot #重启检查一下是否可以自动挂载 [[email?protected] ~]# 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
(2)通过virsh命令创建一个基于分区的存储池 [[email?protected] ~]# virsh pool-define-as guest_images_fs fs --source-dev "/dev/sdc1" --target "/guest_images2" 定义池 guest_images_fs [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_fs 不活跃 否 #新定义的存储池未启动 [[email?protected] ~]# ll -d /gue* #没有目标挂载目录 drwxr-xr-x. 2 root root 6 1月 5 16:14 /guest_images [[email?protected] ~]# virsh pool-build guest_images_fs #创建存储池存储目录 构建池 guest_images_fs [[email?protected] ~]# 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 #存在了 [[email?protected] ~]# virsh pool-start guest_images_fs #启动存储池 池 guest_images_fs 已启动 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_fs 活动 否 #启动成功 [[email?protected] ~]# virsh pool-autostart guest_images_fs #标记存储池开机自启动 池 guest_images_fs 标记为自动启动 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_fs 活动 是 [[email?protected] ~]# mount | grep /dev/sdc1 #已经自动挂载 /dev/sdc1 on /guest_images2 type ext4 (rw,data=ordered) 5.4.5 基于LVM的存储池
(1)以手动的方式创建VG并通过virt-manager创建基于lvm的存储池 #fdisk分出一个全部容量的分区 [[email?protected] ~]# mkfs.ext4 /dev/sdc #磁盘格式化 [[email?protected] ~]# fdisk /dev/sdc #以下省略若干。。。 [[email?protected] ~]# 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 #这就是我们实验用的分区 #创建一个卷组 [[email?protected] ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created. [[email?protected] ~]# vgcreate guest_images_lvm /dev/sdc1 Volume group "guest_images_lvm" successfully created #验证存储池创建状态 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_lvm 活动 是 [[email?protected] ~]# 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卷组 [[email?protected] ~]# 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 [[email?protected] ~]# vgremove guest_images_lvm Volume group "guest_images_lvm" successfully removed [[email?protected] ~]# 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状态 [[email?protected] ~]# 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 [[email?protected] ~]# pvremove /dev/sdc1 Labels on physical volume "/dev/sdc1" successfully wiped. [[email?protected] ~]# 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化磁盘 [[email?protected] ~]# ll /dev/sdc* brw-rw----. 1 root disk 8,32 1月 5 19:33 /dev/sdc #验证存储池创建状态 [[email?protected] ~]# 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 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_lvm2 活动 是 [[email?protected] ~]# 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痕迹 [[email?protected] ~]# 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 [[email?protected] ~]# vgremove guest_images_lvm2 Volume group "guest_images_lvm2" successfully removed [[email?protected] ~]# 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 [[email?protected] ~]# pvremove /dev/sdc Labels on physical volume "/dev/sdc" successfully wiped. #virsh命令创建vg并创建基于LVM的存储池 [[email?protected] ~]# virsh pool-define-as guest_images_lvm3 logical --source-dev=/dev/sdc --source-name=libvirt_lvm --target=/dev/libvirt_vg 定义池 guest_images_lvm3 [[email?protected] ~]# vgs #我们发现virsh命令没有自动创建vg VG #PV #LV #SN Attr VSize VFree centos_kvm 1 2 0 wz--n- <19.00g 0 [[email?protected] ~]# pvs #我们发现virsh命令没有自动创建pv PV VG Fmt Attr PSize PFree /dev/sda2 centos_kvm lvm2 a-- <19.00g 0 #virsh命令创建卷组libvirt_lvm [[email?protected] ~]# virsh pool-build guest_images_lvm3 构建池 guest_images_lvm3 [[email?protected] ~]# 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 [[email?protected] ~]# 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 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- guest_images_lvm3 不活跃 否 [[email?protected] ~]# virsh pool-start guest_images_lvm3 池 guest_images_lvm3 已启动 [[email?protected] ~]# virsh pool-autostart guest_images_lvm3 池 guest_images_lvm3 标记为自动启动 [[email?protected] ~]# 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
第一步:在一台虚拟机中yum安装nfs服务端 #这是一台NAT链接的虚拟机,yum安装nfs [[email?protected] ~]# yum -y install rpcbind nfs-utils [[email?protected] ~]# rpm -qa rpcbind nfs-utils nfs-utils-1.3.0-0.61.el7.x86_64 rpcbind-0.2.0-47.el7.x86_64 [[email?protected] ~]# mkdir /nfs1 [[email?protected] ~]# mkdir /nfsshare [[email?protected] ~]# useradd nfsnobody [[email?protected] ~]# chown nfsnobody.nfsnobody /nfsshare [[email?protected] ~]# cat /etc/exports #因为虚拟机是NAT模式,若要宿主机能够挂载需要开通两个网段权限 /nfsshare 192.168.122.0/24(rw,sync) 192.168.200.0/24(rw,sync) #启动rpc与nfs [[email?protected] ~]# systemctl restart rpcbind.service [[email?protected] ~]# systemctl start nfs.service 第二步:virt-manager创建基于NFS的存储池 #在宿主机中测试共享目录 [[email?protected] ~]# df -h | grep nfsshare #已经自动挂载 192.168.200.26:/nfsshare 17G 3.9G 14G 23% /nfs1 [[email?protected] ~]# mount | grep nfsshare #已经自动挂载 192.168.200.26:/nfsshare on /nfs1 type nfs4 (rw,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) [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_lvm3 活动 是 nfs1 活动 是 [[email?protected] ~]# 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 存储卷概述
[[email?protected] ~]# 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) 演示:存储卷的创建 [[email?protected] ~]# 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 文档,但不能定义/创建 1)基于目录的存储池中的存储卷管理 #查看所有的存储池 [[email?protected] /]# virsh pool-list 名称 状态 自动开始 ------------------------------------------- default 活动 是 ios 活动 是 nfs1 活动 是 VM 活动 是 #目标存储池 #查看VM存储的xml文档 [[email?protected] /]# 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存储池的所有存储卷 [[email?protected] /]# virsh vol-list VM 名称 路径 ------------------------------------------------------------------------------ centos6.5-2.qcow2 /VM/centos6.5-2.qcow2 #向VM存储池里创建一个存储卷 [[email?protected] /]# virsh vol-create-as VM test1.qcow2 1G --format qcow2 创建卷 test1.qcow2 #查看test1.qcow2的卷信息的两种方式 [[email?protected] /]# virsh vol-info /VM/test1.qcow2 名称: test1.qcow2 类型: 文件 容量: 1.00 GiB 分配: 196.00 KiB [[email?protected] /]# virsh vol-info test1.qcow2 --pool VM 名称: test1.qcow2 类型: 文件 容量: 1.00 GiB 分配: 196.00 KiB 2)基于LVM的存储池中的存储卷管理 #定义一个基于LVM的存储池 [[email?protected] /]# ll /dev/sdc* brw-rw----. 1 root disk 8,32 1月 10 22:46 /dev/sdc [[email?protected] /]# virsh pool-define-as guest_images_lvm logical --source-dev=/dev/sdc --target=/dev/vg_libvirt 定义池 guest_images_lvm #构建LVM存储池(如果构建失败,可手动pvcreate -y /dev/sdc后再执行) [[email?protected] /]# pvcreate -y /dev/sdc Wiping ext4 signature on /dev/sdc. Physical volume "/dev/sdc" successfully created. [[email?protected] /]# virsh pool-build guest_images_lvm 构建池 guest_images_lvm #启动基于LVM的存储池 [[email?protected] /]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_lvm 不活跃 否 iso 活动 是 nfs1 活动 是 VM 活动 是 [[email?protected] /]# virsh pool-start guest_images_lvm 池 guest_images_lvm 已启动 [[email?protected] /]# virsh pool-autostart guest_images_lvm 池 guest_images_lvm 标记为自动启动 [[email?protected] /]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 guest_images_lvm 活动 是 iso 活动 是 nfs1 活动 是 VM 活动 是 #向LVM存储池中创建一个存储卷 [[email?protected] /]# virsh vol-create-as guest_images_lvm lvvoll 1G 创建卷 lvvoll [[email?protected] /]# virsh vol-create-as guest_images_lvm lvvol2 2G 创建卷 lvvol2 [[email?protected] /]# virsh vol-create-as guest_images_lvm lvvol3 3G 创建卷 lvvol3 [[email?protected] /]# 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) 演示:存储卷的克隆 [[email?protected] /]# 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 [[email?protected] /]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM 使用 test2.qcow2 克隆的卷 test1.qcow2 [[email?protected] /]# virsh vol-list VM 名称 路径 ------------------------------------------------------------------------------ centos6.5-2.qcow2 /VM/centos6.5-2.qcow2 test1.qcow2 /VM/test1.qcow2 test2.qcow2 /VM/test2.qcow2 #克隆后的盘 [[email?protected] /]# virsh vol-info /VM/test2.qcow2 名称: test2.qcow2 类型: 文件 容量: 1.00 GiB 分配: 196.00 KiB #克隆基于LVM的存储池中的存储卷 [[email?protected] /]# virsh vol-clone lvvoll lvvol4 --pool guest_images_lvm 使用 lvvol4 克隆的卷 lvvoll [[email?protected] /]# 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 [[email?protected] /]# virsh vol-info /dev/guest_images_lvm/lvvol4 名称: lvvol4 类型: 块 容量: 1.00 GiB 分配: 1.00 GiB (3) 演示:存储卷的删除 #查看命令帮助 [[email?protected] ~]# 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存储池中的存储卷 [[email?protected] ~]# virsh vol-delete lvvol4 guest_images_lvm 卷 lvvol4 被删除 [[email?protected] ~]# virsh vol-delete lvvol3 guest_images_lvm 卷 lvvol3 被删除 [[email?protected] ~]# virsh vol-delete lvvol2 guest_images_lvm 卷 lvvol2 被删除 [[email?protected] ~]# virsh vol-delete lvvoll guest_images_lvm 卷 lvvoll 被删除 [[email?protected] ~]# virsh vol-list guest_images_lvm 名称 路径 ------------------------------------------------------------------------------ #删除基于LVM的存储池 [[email?protected] ~]# virsh pool-destroy guest_images_lvm 销毁池 guest_images_lvm [[email?protected] ~]# virsh pool-undefine guest_images_lvm 池 guest_images_lvm 已经被取消定义 [[email?protected] ~]# virsh pool-list --all 名称 状态 自动开始 ------------------------------------------- default 活动 是 ios 活动 是 nfs1 活动 是 VM 活动 是 #删除基于目录的存储池中的存储卷 [[email?protected] ~]# virsh vol-delete /VM/test2.qcow2 卷 /VM/test2.qcow2 被删除 5.5.3 向虚拟机添加卷
(1)通过virt-manager添加新设备 点开一个虚拟机,运行状态的也可以 选择硬件添加 添加虚拟磁盘 进入运行中的虚拟机的交互界面 (2)通过XML文件添加新的设备 #创建一个disk类型的xml文档 [[email?protected] ~]# vim /tmp/disks.xml [[email?protected] ~]# 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命令的帮助 [[email?protected] ~]# virsh domblklist --help NAME domblklist - 列出所有域块 SYNOPSIS domblklist <domain> [--inactive] [--details] DESCRIPTION 获取域块设备小结 OPTIONS [--domain] <string> domain name,id or uuid --inactive 获取不活跃而不是运行的配置 --details type 和 device 值的附加显示 #查看虚拟机的磁盘挂载情况 [[email?protected] ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 4 centos6.5 running #一个正在运行的虚拟机 - centos6.5-2 关闭 [[email?protected] ~]# virsh domblklist centos6.5 #查看虚拟机的磁盘设备挂载情况 目标 源 ------------------------------------------------ vda /var/lib/libvirt/images/centos6.5.qcow2 hda - #将磁盘类型的xml文档加入到虚拟机 [[email?protected] ~]# 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 影响当前域 [[email?protected] ~]# virsh attach-device centos6.5 /tmp/disks.xml --persistent #将xml文件添加进虚拟机 成功附加设备 [[email?protected] ~]# virsh domblklist centos6.5 目标 源 ------------------------------------------------ vda /var/lib/libvirt/images/centos6.5.qcow2 vdb /VM/test1.qcow2 hda - 通过virt-manager查看磁盘已经添加成功,如下图: (3)通过参数添加新的磁盘设备 #克隆一个存储卷 [[email?protected] ~]# virsh vol-clone test1.qcow2 test2.qcow2 --pool VM 使用 test2.qcow2 克隆的卷 test1.qcow2 [[email?protected] ~]# ll /VM/test2.qcow2 -rw-------. 1 qemu qemu 1245184 1月 11 00:17 /VM/test2.qcow2 #挂载一个存储卷到虚拟机 [[email?protected] VM]# virsh attach-disk centos6.5 --source=/VM/test2.qcow2 --target=vdc 成功附加磁盘 [[email?protected] VM]# virsh domblklist centos6.5 目标 源 ------------------------------------------------ vda /var/lib/libvirt/images/centos6.5.qcow2 vdb /VM/test1.qcow2 vdc /VM/test2.qcow2 hda - (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |