這一章節(jié)我們將來(lái)探討下如何熱添加虛擬磁盤(pán)檐蚜,熱擴(kuò)容虛擬磁盤(pán)宁仔。熱添加、熱擴(kuò)容意味著您可以這些操作可以在虛擬機(jī)保持運(yùn)行的狀態(tài)下執(zhí)行只盹,而不必關(guān)閉或者暫停虛擬機(jī)。KVM支持掛載物理磁盤(pán)或者虛擬磁盤(pán)兔院。為了創(chuàng)建虛擬磁盤(pán)殖卑,我們可以使用qemu-img
命令工具創(chuàng)建“qcow2”或者“img”格式的虛擬磁盤(pán)。當(dāng)然使用強(qiáng)大的dd
命令去創(chuàng)建一個(gè)非稀疏格式的的磁盤(pán)也是一個(gè)不錯(cuò)的選擇坊萝。
為虛擬機(jī)掛載SAN/SCSI物理磁盤(pán)
- 為宿主機(jī)掛載物理磁盤(pán)“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
- 查看vm當(dāng)前磁盤(pán)映射關(guān)系;
[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 -
- 將“sdb”掛載到虛擬機(jī)的“vdb”磁盤(pán)上屹堰;
[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 -
- 登錄到虛擬機(jī)驗(yàn)證掛載情況;
[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
創(chuàng)建并掛載虛擬磁盤(pán):
- 創(chuàng)建虛擬磁盤(pán)肛冶,我們可以使用
qemu-img
或dd
命令創(chuàng)建虛擬磁盤(pán)街氢。
_請(qǐng)留意我們?cè)趧?chuàng)建“qcow2”格式磁盤(pán)的時(shí)候使用了“preallocation=metadata”選項(xiàng)扯键,如果不適用預(yù)分配格式,掛載的虛擬磁盤(pán)將讀取不到設(shè)置的大小_
[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
- 掛載創(chuàng)建的虛擬磁盤(pán)珊肃;
[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
熱擴(kuò)容虛擬磁盤(pán)
- 格式化荣刑、mount虛擬磁盤(pán)馅笙;
[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)
- 查看已掛載的虛擬磁盤(pán)信息,找到“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
- 下面我們來(lái)為“vdb”擴(kuò)容董习;
__注意不要減少虛擬磁盤(pán)的大小爱只,這是不允許的,有可能會(huì)造成數(shù)據(jù)的丟失__
[root@test-kvm images]# virsh qemu-monitor-command centos7 --hmp "block_resize drive-virtio-disk1 2G"
- 現(xiàn)在虛擬磁盤(pán)的大小已經(jīng)擴(kuò)展到2G,但是要想使用這些擴(kuò)容的容量恬试,我們需要登錄到虛擬機(jī)使用命令
xfs_growfs
調(diào)整一下掛載文件的大小窝趣。
[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