I recently had a couple new CentOS Linux servers brought online at a colo that a company I work for uses. I had the colo do a very simple install of CentOS so I could handle the details without having to remove a bunch of packages we didn’t need. The servers have two one terabyte drives installed in a RAID 1 configuration which provides us with one terabyte of usable disk space and upon initial configuration had a logical volume group created with three logical volumes. Each of the logical volumes, which included /var, /usr, and /, only had two gigabytes of space so I needed to first expand those logical volumes and later will be creating a large logical volume used for database data. Below I describe expanding already existing logical volumes when there is room to grow in the logical volume group.
Remotely Expand Partitions That Are Part Of A Logical Volume Group:
Let me start by saying it has been awhile since I have needed to expand partitions like this and it has become way easier than it used to be which was a nice surprise. First use the below command to see how large the logical volume group is using the pvdisplay command as shown below.
Display Logical Volume Group Details:
- [root@dev ~]# pvdisplay
- --- Physical volume ---
- PV Name /dev/sda3
- VG Name VG0
- PV Size 918.87 GB / not usable 28.23 MB
- Allocatable yes
- PE Size (KByte) 32768
- Total PE 29403
- Free PE 4763
- Allocated PE 24640
- PV UUID HBaltO-GWW8-000O-tjRe-gfKd-33VZ-ffGtCP
- [root@dev ~]#
As you can see above we have roughly 920GB of usable space available in the logical volume group. The above numbers are a bit off from what it would actually be in this situation since when I started expanding the logical volumes there was only around six GB in use. Anyhow I forgot to save the command so the pvdisplay output above is after expanding the existing logical volumes and adding a 700GB database partition. As you can see above though there is still free space available noted by the “Free PE” or Free Physical Extent. Once you have verified that space exists on the logical volume using the pvdisplay command you can view the logical volumes that are configured using lvscan as shown below for the details of which logical volume to expand.
List Logical Volumes Configured Using lvscan:
- [root@dev /]# lvscan
- ACTIVE '/dev/VG0/VG0_root' [2.00 GB] inherit
- ACTIVE '/dev/VG0/VG0_usr' [2.00 GB] inherit
- ACTIVE '/dev/VG0/VG0_var' [2.00 GB] inherit
- [root@dev /]#
Shown in the output above you can see that there are three logical volumes configured each with a size of 2GB. Now use the df command as shown below to see if the entire available space for each logical volume is mounted.
Show List Of Mounted Partitions On CentOS Linux:
- [root@dev /]# df -kh
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/VG0-VG0_root
- 2.0G 765M 1.1G 41% /
- /dev/mapper/VG0-VG0_usr
- 2.0G 1.4G 477M 75% /usr
- /dev/mapper/VG0-VG0_var
- 2.0G 265M 1.6G 15% /var
- /dev/sda1 122M 27M 89M 24% /boot
- tmpfs 3.9G 0 3.9G 0% /dev/shm
- [root@dev /]#
Now that we have an idea of the logical volumes that are configured, how those logical volumes relate to the partitions that are mounted, and the available space in the logical volume itself we can decide how we want to allocate the data and select which partitions to expand. All of this can be done remotely and without having to unmount any partition as long as there is space in the logical volume group itself. So for instance if we decide that we wanted to expand the /var partition to 20GB total space we would first expand the logical volume for the /var partition which we can see is /dev/VG0/VG0_var using the lvextend command as shown in the example below.
Use lvextend To Expand A Partition On Linux:
- [root@dev /]# lvextend -L+18G /dev/VG0/VG0_var
- Extending logical volume VG0_var to 20.00 GB
- Logical volume VG0_var successfully resized
- [root@dev /]#
Now that the logical volume has been expanded you can see the changes with lvscan which will now display /dev/VG0/VG0_var as having 20GB of total space however if you issue “df -kh” it will still show the /var partition as only have the original 2GB of data. This is because we need to resize the ext3 file system with the resize2fs command as shown in the below example.
Resize ext2 or ext3 File System Using resize2fs On Linux:
- [root@dev /]# resize2fs /dev/VG0/VG0_var
- resize2fs 1.39 (29-May-2006)
- Filesystem at /dev/VG0/VG0_var is mounted on /var; on-line resizing required
- Performing an on-line resize of /dev/VG0/VG0_var to 5242880 (4k) blocks.
- The filesystem on /dev/VG0/VG0_var is now 5242880 blocks long.
- [root@dev /]#
Depending on how much space you are adding to the partition this command may take awhile to complete so be patient. Once the command does complete the /var partition will now have the full 20GB available as shown in the below df command output.
Display Partitions In Human Readable Format On Linux Server:
- [root@dev /]# df -kh
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/VG0-VG0_root
- 2.0G 765M 1.1G 41% /
- /dev/mapper/VG0-VG0_usr
- 2.0G 1.4G 477M 75% /usr
- /dev/mapper/VG0-VG0_var
- 20G 268M 19G 2% /var
- /dev/sda1 122M 27M 89M 24% /boot
- tmpfs 3.9G 0 3.9G 0% /dev/shm
- [root@dev /]#
Your expanded partition is available for use immediately and the steps above are all that needs to be completed for this to be the case. Since the partition is already mounted via fstab no other changes are necessary and the expanded partition will mount with the entire space available upon the Linux server rebooting.