Expanding a disk in CentOS

Source data:

  • OS: CentOS Linux release 7.6.1810(Core).
  • Initial size LVM: 4.4 GB.
  • Task: Expand the LVM to 10 GB (to the entire available disk volume).

Note

Important! Before making any changes, be sure to make a backup copy of all server data. Any manipulation of the partitions carries a risk of loss of data.

Step 1

At this stage we have one physical volume (PV), one volume group (VG) and one logical volume (LV).

fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x000a25ba

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    10485759     4729856   8e  Linux LVM

Disk /dev/mapper/vg0-rootfs: 4840 MB, 4840226816 bytes, 9453568 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/vdb: 214.7 GB, 214748364800 bytes, 419430400 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 label type: dos
Disk identifier: 0x66b4980d

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   419430399   209714176   83  Linux
# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vg0-rootfs  4,4G  2,0G  2,2G  48% /
devtmpfs                2,0G     0  2,0G   0% /dev
tmpfs                   2,0G     0  2,0G   0% /dev/shm
tmpfs                   2,0G  8,4M  2,0G   1% /run
tmpfs                   2,0G     0  2,0G   0% /sys/fs/cgroup
/dev/vda1               477M  121M  327M  28% /boot
tmpfs                   396M     0  396M   0% /run/user/0
/dev/vdb1               197G   61M  187G   1% /data
# pvs
  PV         VG  Fmt  Attr PSize  PFree
  /dev/vda2  vg0 lvm2 a--  <4,51g    0
# vgs
  VG  #PV #LV #SN Attr   VSize  VFree
  vg0   1   1   0 wz--n- <4,51g    0
# lvs
  LV     VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  rootfs vg0 -wi-ao---- <4,51g

As can be seen from the output, there is no accessible place in the physical volume or volume group for expanding the logical volume. To expand the logical volume it is necessary to create a new physical volume. Then once the new physical volume has been added to the logical volume, the latter can be expanded.

To create a new partition on the hard disk, use the fdisk program:

# fdisk /dev/vda

fdisk /dev/vda
  • “n”– is for creating the new partition on the disk;
  • “p”– is for allocating the primary to the new partition.

Specify the number that the partition will be given. The first and last sectors are specified by default. After this a message is issued that a partition in Linux format 5 GB has been created.

# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (10485760-20971519, default 10485760):
Using default value 10485760
Last sector, +sectors or +size{K,M,G} (10485760-20971519, default 20971519):
Using default value 20971519
Partition 3 of type Linux and of size 5 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
Disk identifier: 0x000a25ba

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    10485759     4729856   8e  Linux LVM
/dev/vda3        10485760    20971519     5242880   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Having successfully created a partition, it is necessary to create a new physical volume based on it:

# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created.

The pvs command returns that the physical volume has been successfully created:

# pvs
PV         VG  Fmt  Attr PSize  PFree
/dev/vda2  vg0 lvm2 a--  <4,51g    0
/dev/vda3      lvm2 ---   5,00g 5,00g

Step 2

In the next step, the physical volume created is added to the volume group to extend the size of the group.

# vgextend vg0 /dev/vda3
Volume group "vg0" successfully extended

The lvdisplay can be used to view the list of all logical volumes currently accessible:

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/rootfs
  LV Name                rootfs
  VG Name                vg0
  LV UUID                5qbxoS-AZZj-vG93-9q7d-XhzN-X0Nd-LJ3PEK
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-09-14 14:20:30 +0200
  LV Status              available
  # open                 1
  LV Size                <4,51 GiB
  Current LE             1154
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

One of these is accessible; it has the name ‘rootfs’. The root partition ( / ) is present on this logical volume.

The logical volume lvm-rootfs can now be extended by an additional available 5 GB (from 15 GB to 20 GB). The ’vgdisplay’ command shows the free physical extend (PE):

  vgdisplay
--- Volume group ---
VG Name               vg0
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  3
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                2
Act PV                2
VG Size               9,50 GiB
PE Size               4,00 MiB
Total PE              2433
Alloc PE / Size       1154 / <4,51 GiB
Free  PE / Size       1279 / <5,00 GiB
VG UUID               4YhkXZ-oYz5-yeMo-N9O7-owQM-MveR-VlY1wX

This is the amount of PE to which we extend the LV rootfs:

# lvextend -l +1279 /dev/vg0/rootfs
Size of logical volume vg0/rootfs changed from <4,51 GiB (1154 extents) to 9,50 GiB (2433 extents).
Logical volume vg0/rootfs successfully resized.

Having extended the LV it is necessary to extend the file system to the entire available volume:

# resize2fs /dev/vg0/rootfs
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/rootfs is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg0/rootfs is now 2491392 blocks long.

Now look at the new size of the LV:

  # lvdisplay
--- Logical volume ---
LV Path                /dev/vg0/rootfs
LV Name                rootfs
VG Name                vg0
LV UUID                5qbxoS-AZZj-vG93-9q7d-XhzN-X0Nd-LJ3PEK
LV Write Access        read/write
LV Creation host, time localhost.localdomain, 2017-09-14 14:20:30 +0200
LV Status              available
# open                 1
LV Size                9,50 GiB
Current LE             2433
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     8192
Block device           253:0
# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vg0-rootfs  9,3G  1,9G  7,0G  21% /
devtmpfs                2,0G     0  2,0G   0% /dev
tmpfs                   2,0G     0  2,0G   0% /dev/shm
tmpfs                   2,0G  8,5M  2,0G   1% /run
tmpfs                   2,0G     0  2,0G   0% /sys/fs/cgroup
/dev/vdb1               197G   61M  187G   1% /data
/dev/vda1               477M  121M  327M  28% /boot
tmpfs                   396M     0  396M   0% /run/user/0

It can be seen that the size has increased from 4.4 GB. to 9.3 GB: