📜 ⬆️ ⬇️

Proper increase in disk size in a virtual machine

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

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

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

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

get 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 you do not get it, we get Error: Can't have overlapping partitions. ubuntu parted -gparted
 (parted) resizepart 2 End? [1000GB]? 1288Gb 

increase 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 drive 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 be 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 , disable the virt-manager disk from it
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 minimal downtime, almost on the fly, tested for lvm2 / ext4 can be done like this:
1. Increasing the file size by 200 gigabytes is performed on the hypervisor
 qemu-img resize /path/to/vm-disk.qcow2 +200G 

2. Reboot 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 the extended partition
 (parted) resizepart 2 End? [1000GB]? 1288Gb 

increase 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 drive 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 correct the file system, fsck -f / dev / mapper / vm - db - 0 - vg-root will kill the file system
Check what happened:
 df -h 

Continuing the theme

')

Source: https://habr.com/ru/post/252973/


All Articles