CDLab Technology · Providing professional DevOps practice and tools

Linux - disk reseize on the fly

 

Expansion of disk space "on the fly" in Linux-systems is possible both when using LVM and using ext3 / ext4 file systems.

 

When using LVM on the system

We run the fdisk -l command in order to make sure that the volume of the disk (virtual or physical, it does not matter) has increased.

fdisk -l

Disk /dev/vda: 300 GiB, 322122547200 bytes, 629145600 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: 0xf8e4f5ce

Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 499711 497664 243M 83 Linux
/dev/vda2 501758 629145599 628643842 30G 5 Extended
/dev/vda5 501760 629145599 628643840 30G Linux LVM

Disk /dev/mapper/debian--vg-root: 27.8 GiB, 319715016704 bytes, 624443392 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 /dev/mapper/debian--vg-swap_1: 2 GiB, 2147483648 bytes, 4194304 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


We need to expand the section on which the LVM is located and then the LVM partition itself:

Install parted

apt install parted

run the command:

parted /dev/vda

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 322GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 256MB 255MB primary ext2 boot
2 257MB 322GB 30GB extended
5 257MB 322GB 30GB logical lvm

and then we expand: the extended-section and the section with LVM

(parted) resizepart 2 100%

(parted) resizepart 5 100%

We check that everything went well

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 322GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 256MB 255MB primary ext2 boot
2 257MB 322GB 322GB extended
5 257MB 322GB 322GB logical lvm

(parted) quit

After that, we need to extend the LVM partition.

pvresize /dev/vda5

and then the root section where we have the system installed.

lvresize --extents +100%FREE --resizefs /dev/mapper/debian10--template--vg-root

That's all.

 

When using ext3 / ext4 on the system

We run the fdisk -l command in order to make sure that the volume of the disk (virtual or physical, it does not matter) has increased.

For the next step, we need the growpart utility, it is included in the cloud-guest-utils package, so we install it.

apt install cloud-guest-utils

Then we execute the command:

growpart  /dev/vdb1

, where /dev/vdb is the block device with which the operation is performed, and 1 is the number of the partition that we want to expand.

In our case, this is the section /dev/vdb1.

Then we execute the command, after which the partition is expanded.

resize2fs /dev/vdb1 

All operations are performed on the fly, without stopping/restarting the server, services, unmounting disks, etc.

Create date 11.11.2020