kvm增加虚拟磁盘大小
缘起
最近用libvirt建了一个专门运行pi-hole的kvm虚拟机,由于目的非常明确,安装了debian系统,同时只分配了2G的存储空间。
经过一段时间的运行,各种日志、软件的添加,系统分区已经空间不足了,因此需要动态扩容一下。
折腾过程
拓展磁盘空间
1、关闭虚拟机(这里debian是我虚拟机的名称)
virsh shutdown debian
2、备份老的虚拟磁盘文件(一定要备份,防止意外)
3、拓展虚拟磁盘空间
# 查看磁盘信息 sudo qemu-img info /kvm/debian/debian.qcow2 # 增大磁盘空间,这里增加1G sudo qemu-img resize /kvm/debian/debian.qcow2 +1G
4、开启虚拟机
virsh start debian
虚拟机内重新分配磁盘空间
此时通过ssh进入到虚拟机内,通过fdisk调整磁盘空间。
一般情况下,如果是只分了一个区,可以先删除所有的分区,然后创建一个新分区,提示是否删除老的ext 信息是,选择不删除,这样就可以保留磁盘信息,同时扩容了。
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# 查看磁盘分区信息
Command (m for help): p
Disk /dev/vda: 3 GiB, 3221225472 bytes, 6291456 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
Disklabel type: dos
Disk identifier: 0xfedeeee6
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 3145727 3143680 1.5G 83 Linux
# 删除分区,这里删除了第一个分区C
ommand (m for help): d
Selected partition 1Partition 1 has been deleted.
# 重新创建一个分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)Select (default p): p
Partition number (1-4, default 1):
First sector (2048-6291455, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-6291455, default 6291455):
Created a new partition 1 of type 'Linux' and of size 3 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help):
Command (m for help): p
Disk /dev/vda: 3 GiB, 3221225472 bytes, 6291456 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
Disklabel type: dos
Disk identifier: 0xfddefff6
Device Boot Start End Sectors Size Id Type
/dev/vda1 2048 6291455 6289408 3G 83 Linux
# 保存退出
Command (m for help): w
The partition table has been altered.
Syncing disks.之后reboot重新启动即可。
参考资料
https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/
https://computingforgeeks.com/resize-ext-and-xfs-root-partition-without-lvm/
发表评论