Learning KVM - part6 挂载并扩容磁盘

这一章节我们将来探讨下如何热添加虚拟磁盘,热扩容虚拟磁盘。热添加、热扩容意味着您可以这些操作可以在虚拟机保持运行的状态下执行,而不必关闭或者暂停虚拟机。KVM支持挂载物理磁盘或者虚拟磁盘。为了创建虚拟磁盘,我们可以使用qemu-img命令工具创建“qcow2”或者“img”格式的虚拟磁盘。当然使用强大的dd命令去创建一个非稀疏格式的的磁盘也是一个不错的选择。


为虚拟机挂载SAN/SCSI物理磁盘

  1. 为宿主机挂载物理磁盘“sdb”;
[root@test-kvm images]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  436K  0 rom  
vda    253:0    0   20G  0 disk 
└─vda1 253:1    0   20G  0 part /
sdb    253:16   0    1G  0 disk 
  1. 查看vm当前磁盘映射关系;
[root@test-kvm images]# virsh list
Id    Name                           State
----------------------------------------------------
 1     generic                        running
 4     centos7                        running

[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
file       cdrom      hda        -
  1. 将“sdb”挂载到虚拟机的“vdb”磁盘上;
[root@test-kvm images]# virsh attach-disk centos7 --source /dev/sdb --target vdb --persistent
Disk attached successfully

[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
block      disk       vdb        /dev/sdb
file       cdrom      hda        -
  1. 登录到虚拟机验证挂载情况;
[root@test-kvm images]# virsh console centos7
Connected to domain centos7
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-514.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Tue Jun 13 11:22:34 on ttyS0
[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0          11:0    1 1024M  0 rom  
vda         252:0    0   10G  0 disk 
├─vda1      252:1    0    1G  0 part /boot
└─vda2      252:2    0    9G  0 part 
  ├─cl-root 253:0    0    8G  0 lvm  /
  └─cl-swap 253:1    0    1G  0 lvm  [SWAP]
vdb         252:16   0    1G  0 disk 

创建并挂载虚拟磁盘:

  1. 创建虚拟磁盘,我们可以使用qemu-imgdd命令创建虚拟磁盘。
_请留意我们在创建“qcow2”格式磁盘的时候使用了“preallocation=metadata”选项,如果不适用预分配格式,挂载的虚拟磁盘将读取不到设置的大小_
[root@test-kvm images]# qemu-img create -f qcow2 -o preallocation=metadata centos7.disk1.qcow2 1G
Formatting 'centos7.disk1.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 preallocation='metadata'  lazy_refcounts=off 
[root@test-kvm images]# qemu-img create -f raw centos7.disk2.img 256M
Formatting 'centos7.disk2.img', fmt=raw size=268435456 
[root@test-kvm images]# dd if=/dev/zero of=centos7.disk3.img bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 0.792503 s, 1.3 GB/s
[root@test-kvm images]# ll centos7.disk*
-rw-r--r-- 1 root root 1074135040 Jun 13 13:57 centos7.disk1.qcow2
-rw-r--r-- 1 root root  268435456 Jun 13 13:57 centos7.disk2.img
-rw-r--r-- 1 root root 1048576000 Jun 13 13:58 centos7.disk3.img
  1. 挂载创建的虚拟磁盘;
[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk1.qcow2 --target vdb --persistent
Disk attached successfully

[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk2.img --target vdc --persistent
Disk attached successfully

[root@test-kvm images]# virsh attach-disk centos7 --source /var/lib/libvirt/images/centos7.disk3.img --target vdd --persistent
Disk attached successfully

热扩容虚拟磁盘

  1. 格式化、mount虚拟磁盘;
[root@localhost ~]# mkdir /storage
[root@localhost /]# mkfs.xfs /dev/vdb
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost /]# mount /dev/vdb /storage
[root@localhost /]# mount -v | grep storage
/dev/vdb on /storage type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
  1. 查看已挂载的虚拟磁盘信息,找到“vdb”的block id;
[root@test-kvm images]# virsh domblklist centos7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.img
file       disk       vdb        /var/lib/libvirt/images/centos7.disk1.qcow2
file       disk       vdc        /var/lib/libvirt/images/centos7.disk2.img
file       disk       vdd        /var/lib/libvirt/images/centos7.disk3.img

[root@test-kvm images]# virsh pool-list
 Name                 State      Autostart 
-------------------------------------------
 default              active     yes       

[root@test-kvm images]# virsh vol-list default
 Name                 Path                                    
------------------------------------------------------------------------------
 CentOS-7-x86_64-Minimal-1611.iso /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1611.iso
 centos7.disk1.qcow2  /var/lib/libvirt/images/centos7.disk1.qcow2
 centos7.disk2.img    /var/lib/libvirt/images/centos7.disk2.img
 centos7.disk3.img    /var/lib/libvirt/images/centos7.disk3.img
 centos7.img          /var/lib/libvirt/images/centos7.img     
 cirros-0.3.4-x86_64-disk.img /var/lib/libvirt/images/cirros-0.3.4-x86_64-disk.img

[root@test-kvm images]# virsh qemu-monitor-command centos7 --hmp "info block"
drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.img ro=0 drv=qcow2 encrypted=0 bps=0 bps_rd=0   bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk1: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk1.qcow2 ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk2: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk2.img ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk3: removable=0 io-status=ok file=/var/lib/libvirt/images/centos7.disk3.img ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
  1. 下面我们来为“vdb”扩容;
__注意不要减少虚拟磁盘的大小,这是不允许的,有可能会造成数据的丢失__
[root@test-kvm images]# virsh qemu-monitor-command centos7 --hmp "block_resize drive-virtio-disk1 2G"
  1. 现在虚拟磁盘的大小已经扩展到2G,但是要想使用这些扩容的容量,我们需要登录到虚拟机使用命令xfs_growfs调整一下挂载文件的大小。
[root@test-kvm images]# virsh console centos7
Connected to domain centos7
Escape character is ^]

[root@localhost /]# lsblk | grep vdb
vdb         252:16   0    2G  0 disk /storage
[root@localhost /]# df -h /storage/
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb       1015M   33M  983M   4% /storage
[root@localhost /]# mount -v | grep storage
/dev/vdb on /storage type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost /]# xfs_growfs /storage/
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=262240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262240 to 524288
[root@localhost /]# df -h /storage/
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb        2.0G   33M  2.0G   2% /storage

你可能感兴趣的:(Learning KVM - part6 挂载并扩容磁盘)