1. LVM扩容根目录(不用新添加磁盘)
https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/
如何在 KVM 中扩展/增加/增长虚拟磁盘?。我个人将 KVM 用于所有 Linux 虚拟化项目。有时,我需要为正在运行的 VM(来宾)扩展或添加磁盘空间以满足不断增长的软件需求。 KVM 使用 QEMU,它支持多种图像类型,其中包括 raw、cow、qcow、qcow2、vmdk、vdi 等。
“原生”和最灵活的类型是 qcow2,它支持写入时复制、加密、压缩和 VM 快照。
1.1. 关闭 KVM 上的虚拟机 在扩展来宾计算机虚拟磁盘之前,您需要先将其关闭。
1 2 3 4 $ sudo virsh list Id Name State ----------------------- 4 rhel8 running
如果您的来宾计算机处于运行状态,请使用其 ID 或名称将其关闭。
1 2 $ sudo virsh shutdown rhel8 Domain rhel8 is being shutdown
在继续管理其磁盘之前确认它确实已关闭。
1 2 3 $ sudo virsh list Id Name State --------------------
1.2. 扩展您的 KVM 来宾操作系统磁盘 找到您的来宾操作系统磁盘路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ sudo virsh domblklist rhel8 Target Source ----------------------------------------------- vda /var/lib/libvirt/images/rhel8.qcow2 sda - OR use: $ sudo virsh dumpxml rhel8 | egrep 'disk type' -A 5 <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/var/lib/libvirt/images/rhel8.qcow2'/> <backingStore/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/> -- <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <target dev='sda' bus='sata'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk>
您可以从 Virtual Machine Manager GUI 获取相同的信息。我的虚拟机磁盘位于‘/var/lib/libvirt/images/rhel8.qcow2‘。
1 2 3 4 5 6 7 8 9 10 11 $ sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2 image: /var/lib/libvirt/images/rhel8.qcow2 file format: qcow2 virtual size: 30G (42949672960 bytes) disk size: 2.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false
1.3. 扩展来宾 VM 磁盘 由于我们知道我们的虚拟机磁盘的位置,让我们将其扩展到我们想要的容量。
1 sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
请注意 qemu-img 无法调整具有快照的图像的大小。您需要先删除所有 VM 快照。看这个例子:
1 2 3 4 5 6 7 8 9 10 11 $ sudo virsh snapshot-list rhel8 Name Creation Time State -------------------------------------------------- snapshot1 2019-04-16 08:54:24 +0300 shutoff $ sudo virsh snapshot-delete --domain rhel8 --snapshotname snapshot1 Domain snapshot snapshot1 deleted $ sudo virsh snapshot-list rhel8 Name Creation Time State -------------------------------
然后使用磁盘容量前的“+”扩展磁盘。
1 2 $ sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G Image resized.
您还可以使用 virsh 命令调整大小。这需要域正在运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2 image: /var/lib/libvirt/images/rhel8.qcow2 file format: qcow2 virtual size: 30G (42949672960 bytes) disk size: 2.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false $ sudo virsh start rhel8 $ sudo virsh blockresize rhel8 /var/lib/libvirt/images/rhel8.qcow2 40G Block device '/var/lib/libvirt/images/rhel8.qcow2' is resized
使用 fdisk 命令确认磁盘大小。
1 2 3 4 5 $ sudo fdisk -l /var/lib/libvirt/images/rhel8.qcow2 Disk /var/lib/libvirt/images/rhel8.qcow2: 30.2 GiB, 32399818752 bytes, 63280896 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
1.4. 增加虚拟机分区 现在启动虚拟机
1 2 $ sudo virsh start rhel8 Domain rhel8 started
以 root 用户身份或使用具有 sudo 的用户帐户通过 SSH 连接到您的 VM。
1 2 3 $ ssh rhel8 Last login: Fri Apr 19 06:11:19 2019 from 192.168.122.1 [jmutai@rhel8 ~]$
检查您的新磁盘布局。
1 2 3 4 5 6 7 8 $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom vda 252:0 0 40G 0 disk ├─vda1 252:1 0 1G 0 part /boot └─vda2 252:2 0 29G 0 part ├─rhel-root 253:0 0 26.9G 0 lvm / └─rhel-swap 253:1 0 2.1G 0 lvm [SWAP]
我的 VM 总磁盘容量现在是 40GB,以前是 30GB。扩展虚拟机根磁盘
1.5. 安装growpart命令 Ubuntu / Debian安装
1 sudo apt install cloud-guest-utils
CentOS安装
1 sudo yum -y install cloud-utils-growpart
可以通过传递 -h 参数来查看帮助页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ growpart -h growpart disk partition rewrite partition table so that partition takes up all the space it can options: -h | --help print Usage and exit --fudge F if part could be resized, but change would be less than 'F' bytes, do not resize (default: 1048576) -N | --dry-run only report what would be done, show new 'sfdisk -d' -v | --verbose increase verbosity / debug -u | --update R update the the kernel partition table info after growing this requires kernel support and 'partx --update' R is one of: - 'auto' : [default] update partition if possible - 'force' : try despite sanity checks (fail on failure) - 'off' : do not attempt - 'on' : fail if sanity checks indicate no support Example: - growpart /dev/sda 1 Resize partition 1 on /dev/sda
现在使用growpart 扩展您的分区。在此示例中,我们将扩展磁盘 /dev/vda 中的分区 2。用正确的值替换 2 和 /dev/vda。
1 2 $ sudo growpart /dev/vda 2 CHANGED: partition=2 start=2099200 old: size=18872320 end=20971520 new: size=60815327,end=62914527
确认更改是否成功
1 2 3 4 5 6 7 8 $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom vda 252:0 0 40G 0 disk ├─vda1 252:1 0 1G 0 part /boot └─vda2 252:2 0 39G 0 part ├─rhel-root 253:0 0 26.9G 0 lvm / └─rhel-swap 253:1 0 2.1G 0 lvm [SWAP]
1.6. 调整根逻辑卷的大小以占用所有空间 调整物理卷大小
1 2 3 4 5 6 7 $ sudo pvresize /dev/vda2 Physical volume "/dev/vda2" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized $ sudo pvs PV VG Fmt Attr PSize PFree /dev/vda2 rhel lvm2 a-- <39.00g 10.00g
检查配置的卷组的大小
1 2 3 $ sudo vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- <39.00g 10.00g
然后使用扩展卷组调整根文件系统使用的逻辑卷的大小:
1 sudo lvextend -r -l +100%FREE /dev/name-of-volume-group/root
这将扩展逻辑卷以使用卷组中的所有可用容量。使用 + 符号将值添加到逻辑卷的实际大小。
使用的命令选项:
-l – 以逻辑盘区为单位扩展或设置逻辑卷大小
-r – 与逻辑卷一起调整底层文件系统的大小
这是我的安装文件系统扩展的示例:
1 2 3 4 5 6 $ df -hT | grep mapper /dev/mapper/rhel-root xfs 27G 1.9G 26G 8% / $ sudo lvextend -r -l +100%FREE /dev/mapper/rhel-root Size of logical volume rhel/root changed from <26.93 GiB (6893 extents) to <36.93 GiB (9453 extents). Logical volume rhel/root successfully resized.
如果您更喜欢手动设置要扩展的大小,请使用命令选项:
1 -L, --size [+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]
更多选项如下:
M for megabytes
G for gigabytes
T for terabytes
P for petabytes
E for exabytes
如果没有 + 符号,则该值被视为绝对值。
1 2 # Add 20 gigabytes to the current logical volume size $ sudo lvextend -r -L +20G /dev/name-of-volume-group/root
1.7. 更新文件系统上的更改(如果您没有在第 3 步中使用 -r 选项) 您的根文件系统仍将显示旧大小。
1 2 $ df -hT | grep mapper /dev/mapper/rhel-root xfs 27G 1.9G 26G 8% /
让我们让文件系统报告实际大小,包括扩展。
对于 ext4 文件系统
1 sudo resize2fs /dev/name-of-volume-group/root
对于 xfs 文件系统
1 2 3 4 5 6 7 8 9 10 11 12 $ sudo xfs_growfs / meta-data=/dev/mapper/rhel-root isize=512 agcount=4, agsize=1764608 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=7058432, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=3446, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 7058432 to 9679872
参考:
Other guides: