KVM的热添加技术之硬盘管理
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.kvm虚拟机热添加硬盘
1.查看KVM虚拟机现状
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 17:49:46 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
[root@yinzhengjie-kvm01 ~]#
2.创建一块qcow2磁盘类型设备
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img create -f qcow2 /opt/yinzhengjie-kvm01_disk01.qcow2 20G
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /opt/yinzhengjie-kvm01_disk01.qcow2
-rw-r--r--. 1 root root 193K Apr 9 18:10 /opt/yinzhengjie-kvm01_disk01.qcow2
[root@yinzhengjie-kvm189 ~]#
温馨提示:
个人建议创建虚拟机磁盘的时候,建议前缀和虚拟机名称保持一致,然后使用"_"作为连接符,后续才是我们自定义的磁盘名称。
3.为指定KVM虚拟机热添加磁盘
[root@yinzhengjie-kvm189 ~]# virsh attach-disk yinzhengjie-kvm01 /opt/yinzhengjie-kvm01_disk01.qcow2 vdb --subdriver qcow2
温馨提示:
如果上面的"vbd"在KVM虚拟机中已经被使用了,则可能指定的盘符命名无效哟,甚至KVM虚拟机会自动修改盘符的名称呢~比如变成了"vdc"之类的。
4.KVM虚拟机验证添加硬盘
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 17:57:05 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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@yinzhengjie-kvm01 ~]#
5.KVM虚拟机格式化新磁盘并测试
[root@yinzhengjie-kvm01 ~]# mkfs.xfs /dev/vdb
meta-data=/dev/vdb isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# mount /dev/vdb /mnt/
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
/dev/vdb 20G 33M 20G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# cp /etc/os-release /mnt/
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 393 Apr 9 18:17 os-release
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# umount /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# ll /mnt
total 0
[root@yinzhengjie-kvm01 ~]#
6.移除KVM虚拟机的磁盘设备
1.移除磁盘设备(如上图所示)
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh detach-disk yinzhengjie-kvm01 vdb
Disk detached successfully
[root@yinzhengjie-kvm189 ~]#
2.连接KVM虚拟机测试是否移除成功(如下图所示)
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 18:12:24 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
[root@yinzhengjie-kvm01 ~]#
二.kvm虚拟机磁盘的扩容
1.KVM虚拟机扩容规范操作
- 1.在KVM虚拟机中卸载需要扩容的挂载点;
- 2.在KVM宿主机中移除需要扩容的磁盘设备;
- 3.在KVM宿主机使用"qemu-img resize"工具扩容磁盘设备;
- 4.在KVM宿主机附加硬盘到虚拟机;
- 5.在KVM虚拟机重新挂载硬盘设备;
- 6.更新vb*文件系统的元数据;
温馨提示:
这种方式不仅仅适用于咱们自己使用的KVM环境,也适合所有云环境主机的磁盘扩容流程哟~你可以使用阿里云或者腾讯云测试。
2.KVM虚拟机磁盘扩容案例
2.1 实验环境准备
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
/dev/vdc 20G 33M 20G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdc: 21.5 GB, 21474836480 bytes, 41943040 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@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 393 Apr 9 18:17 os-release
[root@yinzhengjie-kvm01 ~]#
温馨提示:
本案例就是咱们的实验环境,你也可以根据自己的情况进行微调即可。
2.2 KVM虚拟机卸载挂载点
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
/dev/vdc 20G 33M 20G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# umount /mnt/
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
[root@yinzhengjie-kvm01 ~]#
2.3 在KVM宿主机中移除需要扩容的磁盘设备
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh detach-disk yinzhengjie-kvm01 vdc
Disk detached successfully
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 18:37:05 on ttyS0
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
[root@yinzhengjie-kvm01 ~]#
2.4 在KVM宿主机扩容磁盘设备
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm01_disk01.qcow2
image: /opt/yinzhengjie-kvm01_disk01.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img resize /opt/yinzhengjie-kvm01_disk01.qcow2 40G
Image resized.
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm01_disk01.qcow2
image: /opt/yinzhengjie-kvm01_disk01.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 27M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# ll -h /opt/yinzhengjie-kvm01_disk01.qcow2
-rw-r--r--. 1 root root 12M Apr 9 18:46 /opt/yinzhengjie-kvm01_disk01.qcow2
[root@yinzhengjie-kvm189 ~]#
2.5 在KVM宿主机附加硬盘到虚拟机
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh attach-disk yinzhengjie-kvm01 /opt/yinzhengjie-kvm01_disk01.qcow2 vdc --subdriver qcow2
Disk attached successfully
[root@yinzhengjie-kvm189 ~]#
2.6 在KVM虚拟机重新挂载硬盘设备
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdc: 42.9 GB, 42949672960 bytes, 83886080 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@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# mount /dev/vdc /mnt/
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 393 Apr 9 18:17 os-release
[root@yinzhengjie-kvm01 ~]#
2.7 更新vb*文件系统的元数据
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt # 注意看,磁盘的大小貌似没有识别到磁盘扩容了
/dev/vdc 20G 33M 20G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# xfs_growfs /dev/vdc # 更新xfs文件系统的元数据(超级块)信息
meta-data=/dev/vdc isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, 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 5242880 to 10485760
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt # 不难发现,数据更新后,就可以识别到40GB磁盘啦~
/dev/vdc 40G 33M 40G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
三.KVM热添加硬盘永久生效
1.临时生效的配置重启虚拟机配置丢失
1.重启前查看虚拟机信息
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
1 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 18:43:50 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
/dev/vdc 40G 33M 40G 1% /mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# umount /mnt/
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# df -h | grep mnt
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdc: 42.9 GB, 42949672960 bytes, 83886080 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@yinzhengjie-kvm01 ~]#
2.重启KVM虚拟机
[root@yinzhengjie-kvm189 ~]# virsh reboot yinzhengjie-kvm01
Domain yinzhengjie-kvm01 is being rebooted
[root@yinzhengjie-kvm189 ~]#
3.验证磁盘是否丢失(很明显,磁盘配置丢失了,因为KVM虚拟机启动时会加载配置文件)
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Tue Apr 9 19:07:06 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
[root@yinzhengjie-kvm01 ~]#
2.永久配置命令
KVM虚拟机添加磁盘的两种方式:
- 永久添加磁盘(只是把配置写入到配置文件中,但需要重启虚拟机生效)
virsh attach-disk yinzhengjie-kvm01 /opt/yinzhengjie-kvm01_disk01.qcow2 vdb --subdriver qcow2 --config
- 临时生效(重启后就会失效)
virsh attach-disk yinzhengjie-kvm01 /opt/yinzhengjie-kvm01_disk01.qcow2 vdb --subdriver qcow2
KVM虚拟机移除磁盘的两种方式:
- 永久移除磁盘(只是把配置写入到配置文件中,但需要重启虚拟机生效)
virsh detach-disk yinzhengjie-kvm01 vdb --config
- 临时生效(重启后就会失效)
virsh detach-disk yinzhengjie-kvm01 vdb
温馨提示:
如果想要验证是否永久生效,可以使用"virsh edit yinzhengjie-kvm01"命令来观察虚拟机的配置文件,主要查看以下字段信息即可:
<domain type='kvm'>
...
<devices>
...
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/opt/yinzhengjie-kvm01.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
...
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/18120204,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。