Correct increase in disk size in a virtual machine

Without pretending to be complete, I still think that this can be useful to system administrators.

The disk size increase in the virtual machine occurred with the following input: qcow2 virtual file format, the virtual machine uses lvm and ext4, the root partition is in the extended partition. The action usually occurs at night, when the load is minimal and downtime does not put much strain on the nerves. Although when working with highload projects, adrenaline still stands out enough to think 10 times before doing anything. Therefore, before starting the process, it is better to turn off the SMS notification system so as not to frighten colleagues with messages like “Server down” in the middle of the night.

1. Turn off the virtual machine
I did this via the GUI by clicking on the red power button in virt-manager. If there is no virt-manager, this can be done by issuing the shutdown command on the command line of the virtual machine.
2. On the hypervisor, increase the file size (in my case, by 200 gigabytes)
qemu-img resize /path/to/vm-disk.qcow2 +200G

3. We cling the disk to another (service) virtual machine through the managing machine with virt-manager, an alternative is to boot from a CD with lvm support.
Accordingly, when booting from LiveCD, vdb changes to vda
4. We start the service machine (it should also have lvm on it) via virt-manager.
5. Next, on a service (or LVM liveCD) machine:
parted /dev/vdb

get the disk size:

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 1288GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  256MB   255MB   primary   ext2         boot
 2      257MB   1000GB  1000GB  extended
 5      257MB   1000GB  1000GB  logical                lvm

increase the extended partition, if this is not done we get Error: Can't have overlapping partitions. ubuntu parted -gparted

(parted) resizepart 2
End?  [1000GB]? 1288Gb

increase the logical root partition

(parted) resizepart 5
End?  [1000GB]? 1288Gb
(parted) q

Now you need to increase the size of the physical disk in lvm

pvresize /dev/vdb5

increase the size of the logical disk in lvm

root@vm-service:/etc# lvextend  /dev/vm-db-0-vg/root -l +100%FREE
 lvextend  /dev/vm-db-0-vg/root -l +100%FREE
File descriptor 7 (pipe:[7918]) leaked on lvextend invocation. Parent PID 1378: bash (на это можно не обращать внимания)
  Extending logical volume root to 1.12 TiB
  Logical volume root successfully resized
root@vm-service:/etc# resize2fs /dev/vm-db-0-vg/root

The output of resize2fs should look like this:
The filesystem on /dev/vm-db-0-vg/root is now 231278592 blocks long.

Now check and fix the file system:

fsck -f /dev/mapper/vm--db--0--vg-root

the disk is ready
6. turn off the service machine , disconnect the disk from it in virt-manager
from the command line, without using the GUI to manage virtual machines, you can do this with virsh, the use of which is well described here: managing virtual machines from the command line
7. Start the server
An increase with a minimum downtime, almost on the fly, checked on lvm2 / ext4 can be done as follows:
1. An increase in file size by 200 gigabytes is performed on the hypervisor
qemu-img resize /path/to/vm-disk.qcow2 +200G

2. Rebooting the virtual machine
3. On the virtual machine
parted /dev/vda

Let's see the size of the physical disk and all logical partitions
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 1288GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  256MB   255MB   primary   ext2         boot
 2      257MB   1000GB  1000GB  extended
 5      257MB   1000GB  1000GB  logical                lvm

increase extended partition

 (parted) resizepart 2
End?  [1000GB]? 1288Gb

increase the logical root partition

(parted) resizepart 5
End?  [1000GB]? 1288Gb
(parted) q

Now you need to increase the size of the physical disk in lvm

pvresize /dev/vda5

increase the size of the logical disk in lvm

root@vm-db-0:/etc# lvextend  /dev/vm-db-0-vg/root -l +100%FREE
 lvextend  /dev/vm-db-0-vg/root -l +100%FREE
File descriptor 7 (pipe:[7918]) leaked on lvextend invocation. Parent PID 1378: bash 
  Extending logical volume root to 1.12 TiB
  Logical volume root successfully resized
root@vm-db-0:/etc# resize2fs /dev/vm-db-0-vg/root

In this case, you cannot check and fix the file system, fsck -f / dev / mapper / vm - db - 0 - vg-root will kill the file system
Check what happened:

df -h

Continuation of the topic

Also popular now: