本文将演示如何添加虚拟磁盘活LUN到KVM guest虚拟机,以及如何在线更改现有虚拟磁盘大小。整个操作无须停止guest操作系统。KVM支持物理LUN映射和虚拟磁盘映射到虚拟机。要映射虚拟磁盘,需要使用qemu-img
命令来创建虚拟磁盘映像,而磁盘格式或者是img
或者是qcow2
。当然,你还是使用dd
命令创建一个"无空隙"磁盘映像。
映射SAN或SCSI磁盘到KVM虚拟机
使用root账号登录到KVM Hypervisor主机(物理服务器)
这里假设我们在hypervisor节点从SAN存储获得了"/dev/sdb
" LUN
[root@UA-HA ~]# virsh list
Id Name State
----------------------------------------------------
32 UAKVM2 running
[root@UA-HA ~]#
[root@UA-HA ~]# virsh domblklist UAKVM2 --details
Type Device Target Source
------------------------------------------------
file disk vda /var/lib/libvirt/images/UAKVM2.qcow2
block cdrom hda -
[root@UA-HA ~]#
[root@UA-HA ~]# virsh attach-disk UAKVM2 --source /dev/sdb --target vdb --persistent
Disk attached successfully
[root@UA-HA ~]#
[root@UA-HA ~]# virsh domblklist UAKVM2 --details
Type Device Target Source
------------------------------------------------
file disk vda /var/lib/libvirt/images/UAKVM2.qcow2
block disk vdb /dev/sdb
block cdrom hda -
[root@UA-HA ~]#
登录到UAKVM2
KVM虚拟机中查看新添加的磁盘
[root@UA-KVM1 ~]# fdisk -l /dev/vdb
Disk /dev/vdb: 536 MB, 536870912 bytes, 1048576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@UA-KVM1 ~]#
以上已经成功在线将SAN LUN映射到KVM虚拟机的/dev/sdb
添加新虚拟磁盘到KVM Guests
要映射一个虚拟磁盘到KVM虚拟机需要以下步骤:
[root@UA-KVM1 ~]# fdisk -l |grep vd |grep -v Linux
Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/vdb: 536 MB, 536870912 bytes, 1048576 sectors
[root@UA-KVM1 ~]#
登录到KVM Hypervisor主机(物理服务器)
[root@UA-HA images]# cd /var/lib/libvirt/images
[root@UA-HA images]# qemu-img create -f qcow2 UAKVM2.disk2.qcow2 1G
Formatting 'UAKVM2.disk2.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off
[root@UA-HA images]#
注意:所有环境中的存储池路径将不同
也可以使用以下方式创建虚拟磁盘,即raw格式:
[root@UA-HA images]# qemu-img create -f raw UAKVM2.disk3.img 256M
Formatting 'UAKVM2.disk3.img', fmt=raw size=268435456
[root@UA-HA images]#
或者创建预分配存储磁盘(预分配存储可以获得更好的性能)
[root@UA-HA images]# dd if=/dev/zero of=UAKVM2.disk4.img bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 14.6078 s, 71.8 MB/s
[root@UA-HA images]#
检查存储池中的虚拟磁盘大小
[root@UA-HA images]# du -sh UAKVM2.disk*
196K UAKVM2.disk2.qcow2 --- qcow2 formatted virtual disk file (thin)
0 UAKVM2.disk3.img --- raw formatted virtual disk file (thin)
1000M UAKVM2.disk4.img --- raw formatted virtual disk file using dd command.
[root@UA-HA images]#
[root@UA-HA images]# virsh attach-disk UAKVM2 --source /var/lib/libvirt/images/UAKVM2.disk2.qcow2 --target vdc --persistent
Disk attached successfully
[root@UA-HA images]#
[root@UA-HA images]# virsh domblklist UAKVM2 --details
Type Device Target Source
------------------------------------------------
file disk vda /var/lib/libvirt/images/UAKVM2.qcow2
block disk vdb /dev/sdb
file disk vdc /var/lib/libvirt/images/UAKVM2.disk2.qcow2
block cdrom hda -
[root@UA-HA images]#
[root@UA-KVM1 ~]# fdisk -l /dev/vdc
Disk /dev/vdc: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@UA-KVM1 ~]#
以上我们成功地在线添加了KVM虚拟机的虚拟磁盘
在KVM中调整虚拟磁盘
[root@UA-KVM1 ~]# df -h /orastage
Filesystem Size Used Avail Use% Mounted on
/dev/vdc 1014M 33M 982M 4% /orastage
[root@UA-KVM1 ~]# mount -v |grep /orastage
/dev/vdc on /orastage type xfs (rw,relatime,attr2,inode64,noquota)
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# fdisk -l /dev/vdc
Disk /dev/vdc: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@UA-KVM1 ~]#
登录到KVM Hypervisor主机(物理服务器)
[root@UA-HA ~]# virsh domblklist UAKVM2 --details
Type Device Target Source
------------------------------------------------
file disk vda /var/lib/libvirt/images/UAKVM2.qcow2
block disk vdb /dev/sdb
file disk vdc /var/lib/libvirt/images/UAKVM2.disk2.qcow2
block cdrom hda -
[root@UA-HA ~]#
[root@UA-HA ~]# virsh pool-list
Name State Autostart
-------------------------------------------
default active yes
[root@UA-HA ~]#
[root@UA-HA ~]# virsh pool-refresh default
Pool default refreshed
[root@UA-HA ~]#
使用virsh vol-list
命令列出虚拟磁盘(vdc = UAKVM2.disk2.qcow2
)
[root@UA-HA ~]# virsh vol-list default
Name Path
------------------------------------------------------------------------------
UAKVM2.disk2.qcow2 /var/lib/libvirt/images/UAKVM2.disk2.qcow2
UAKVM2.disk3.img /var/lib/libvirt/images/UAKVM2.disk3.img
UAKVM2.disk4.img /var/lib/libvirt/images/UAKVM2.disk4.img
UAKVM2.qcow2 /var/lib/libvirt/images/UAKVM2.qcow2
[root@UA-HA ~]#
使用qemu-monitor
列出UAKVM2
域分配到块设备
[root@UA-HA ~]# virsh qemu-monitor-command UAKVM2 --hmp "info block"
drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/UAKVM2.qcow2 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=/dev/sdb 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/UAKVM2.disk2.qcow2 ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-ide0-0-0: removable=1 locked=0 tray-open=0 io-status=ok [not inserted]
[root@UA-HA ~]#
上述命令输出显示UAKVM2.disk2.qcow2
被影射到drive-virtio-disk2
[root@UA-HA images]# virsh qemu-monitor-command UAKVM2 --hmp "block_resize drive-virtio-disk2 2G"
[root@UA-HA images]#
登录到KVM虚拟机UAKVM2
然后检查vdc
磁盘大小
[root@UA-KVM1 ~]# fdisk -l /dev/vdc
Disk /dev/vdc: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# df -h /orastage
Filesystem Size Used Avail Use% Mounted on
/dev/vdc 1014M 33M 982M 4% /orastage
[root@UA-KVM1 ~]# mount -v |grep /orastage
/dev/vdc on /orastage type xfs (rw,relatime,attr2,inode64,noquota)
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# xfs_growfs /orastage/
meta-data=/dev/vdc isize=256 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 262144 to 1310720
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# df -h /orastage/
Filesystem Size Used Avail Use% Mounted on
/dev/vdc 2.0G 33M 2.0G 1% /orastage
[root@UA-KVM1 ~]#
以上成功增加了虚拟机磁盘大小
系列教程编译
本系列教程根据 UnixArena Linux KVM 系列教程编译,共分以下文章: