Creating Volume Groups and Online Expansion in Linux Operating System

For a server backend (database) operations personnel, adding a volume group directory and online expansion of directories in the Linux operating system is a common operation. In large operations teams, the division of labor is quite detailed, and it may not be necessary for database operations personnel to perform directory expansion operations; this may be handled by operating system operations personnel. The following is a brief description of how to create physical volume groups, logical volumes, and mount the volume group to a newly created directory or expand it online to an existing directory in the Linux operating system.Creating Volume Groups and Online Expansion in Linux Operating SystemCreating Physical and Logical Volumes and DirectoriesChecking Available Storage Devices

[root@eachroom ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   40G  0 disk
└─vda1 253:1    0   40G  0 part 
/vdb    253:16   0  400G  0 disk

Partitioning Storage Devices (can be left unpartitioned)

[root@eachroom ~]# fdisk /dev/vdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xf7bd955e.
Command (m for help): n
Partition type:   p   primary (0 primary, 0 extended, 4 free)   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-838860799, default 2048): Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-838860799, default 838860799): +400G
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-838860799, default 838860799): +409590M
Partition 1 of type Linux and of size 400 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@eachroom ~]#

Creating Physical Volume Group

[root@eachroom ~]# vgcreate contos-data /dev/vdb1
 Physical volume "/dev/vdb1" successfully created.  Volume group "contos-data" successfully created
[root@eachroom ~]#

Checking Volume Group Information

[root@eachroom ~]# lvmlvm> vgdisplay  --- Volume group ---  
 VG Name               contos-data  
 System ID  
 Format                lvm2  
 Metadata Areas        1  
 Metadata Sequence No  1  
 VG Access             read/write  
 VG Status             resizable  
 MAX LV                0  
 Cur LV                0  
 Open LV               0  
 Max PV                0  
 Cur PV                1  
 Act PV                1  
 VG Size               <399.99 GiB  
 PE Size               4.00 MiB  
 Total PE              102397  
 Alloc PE / Size       0 / 0  
 Free  PE / Size       102397 / <399.99 GiB  
 VG UUID               LeuLp9-P0Tx-F1cU-Oxyg-G6Ce-T434-3BAzYelvm>lvm>lvm> exit  Exiting.
[root@eachroom ~]#

Creating Logical Volume Group

[root@eachroom ~]# lvcreate -L 399G -n data-lv01 contos-data  
 Logical volume "data-lv01" created.
[root@eachroom ~]# 

Logical Volume Group Name: data-lv01

Physical Volume Group Name: contos-data

This is where the logical volume group is added to the physical volume group. If a physical volume group is not specified during the creation of the logical volume group, the system will automatically create a physical volume group and add the logical volume group to this physical volume group by default.

Checking Volume Group Information Again

[root@eachroom ~]# lvmlvm> vgdisplay  --- Volume group ---  
 VG Name               contos-data  
 System ID               
 Format                lvm2  
 Metadata Areas        1  
 Metadata Sequence No  2  
 VG Access             read/write  
 VG Status             resizable  
 MAX LV                0  
 Cur LV                1  
 Open LV               0  
 Max PV                0  
 Cur PV                1  
 Act PV                1  
 VG Size               <399.99 GiB  
 PE Size               4.00 MiB  
 Total PE              102397  
 Alloc PE / Size       102144 / 399.00 GiB  
 Free  PE / Size       253 / 1012.00 MiB  
 VG UUID               LeuLp9-P0Tx-F1cU-Oxyg-G6Ce-T434-3BAzYelvm>lvm>lvm> exit  Exiting.
[root@eachroom ~]#[root@eachroom ~]#[root@eachroom ~]# lsblk
NAME                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                         253:0    0   40G  0 disk
└─vda1                      253:1    0   40G  0 part 
/vdb                         253:16   0  400G  0 disk
└─vdb1                      253:17   0  400G  0 part  └─contos--data-data--lv01 252:0    0  399G  0 lvm
[root@eachroom ~]#

The logical volume group has been successfully created.

Creating New Directory

[root@eachroom ~]# mkdir /data
[root@eachroom ~]#[root@eachroom ~]#[root@eachroom ~]# ls -lrttotal 564-rw-r--r-- 1 root root 575048 Aug 15 10:22 zabbix-agent-6.0.30-release1.el7.x86_64.rpm
[root@eachroom ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         12G     0   12G   0% /dev
tmpfs            12G     0   12G   0% /dev/shm
tmpfs            12G  8.7M   12G   1% /run
tmpfs            12G     0   12G   0% /sys/fs/cgroup
dev/vda1        40G  3.4G   35G   9% /
tmpfs           2.4G     0  2.4G   0% /run/user/1000
[root@eachroom ~]#

Formatting the Newly Created Logical Volume Group

[root@eachroom ~]# mkfs.ext4 /dev/mapper/contos--data-data--lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
26148864 inodes, 104595456 blocks
5229772 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=22523412483192 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,        102400000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@eachroom ~]#

This formats the volume group into ext4 format.

Mounting the Logical Volume Group to the Newly Created Directory

[root@eachroom ~]# mount /dev/contos-data/data-lv01 /data
[root@eachroom ~]# df -h
Filesystem                           Size  Used Avail Use% Mounted on
devtmpfs                              12G     0   12G   0% /dev
tmpfs                                 12G     0   12G   0% /dev/shm
tmpfs                                 12G  8.7M   12G   1% /run
tmpfs                                 12G     0   12G   0% /sys/fs/cgroup
dev/vda1                             40G  3.4G   35G   9% /
dev/mapper/contos--data-data--lv01  393G   73M  373G   1% /data

Editing Auto-Mount

[root@eachroom ~]# vi /etc/fstab
/dev/contos-data/data-lv01                /data                   ext4    defaults        1 1

At this point, the volume group and directory creation is complete.

Online Expansion of Existing Directory

Checking Available Storage Devices

[root@kbsnode01 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   30G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   29G  0 part   
├─centos-root 253:0    0   27G  0 lvm  /
└─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   30G  0 disk 
sr0              11:0    1  4.4G  0 rom  /run/media/admin/CentOS 7 x86_64
[root@kbsnode01 ~]# 

Here we see a 30G sdb device available.

Checking Volume Group Information

[root@kbsnode01 ~]# lvmlvm> pvcreate /dev/sdb1  
 Physical volume "/dev/sdb1" successfully created.
lvm> lvm> pvdisplay  --- Physical volume ---  
 PV Name               /dev/sda2  
 VG Name               centos  
 PV Size               <29.00 GiB / not usable 3.00 MiB  
 Allocatable           yes   PE Size               4.00 MiB  
 Total PE              7423  
 Free PE               1  
 Allocated PE          7422  
 PV UUID               uAzNfq-axBO-b1A2-uILr-iiih-I7yI-8ORmqg  
 "/dev/sdb1" is a new physical volume of "29.00 GiB"  
 --- NEW Physical volume ---  
 PV Name               /dev/sdb1  
 VG Name                 
 PV Size               29.00 GiB  
 Allocatable           NO  
 PE Size               0     
 Total PE              0  
 Free PE               0  
 Allocated PE          0  
 PV UUID               rvGqkk-HQ04-ceFG-1NAk-XDJc-IAhT-kP5jgelvm> lvm> vgdisplay  --- Volume group ---  
 VG Name               centos  
 System ID               
 Format                lvm2  
 Metadata Areas        1  
 Metadata Sequence No  3  
 VG Access             read/write  
 VG Status             resizable  
 MAX LV                0  
 Cur LV                2  
 Open LV               2  
 Max PV                0  
 Cur PV                1  
 Act PV                1  
 VG Size               <29.00 GiB  
 PE Size               4.00 MiB  
 Total PE              7423  
 Alloc PE / Size       7422 / 28.99 GiB  
 Free  PE / Size       1 / 4.00 MiB  
 VG UUID               TmEeQp-aLi2-Wz2A-HgAK-suly-MFNf-VFiNC0lvm> 

Volume Group Expansion

lvm> vgextend centos /dev/sdb1  
 Volume group "centos" successfully extended
lvm> lvm> lvm> lvm> vgdisplay  --- Volume group ---  
 VG Name               centos  
 System ID               
 Format                lvm2  
 Metadata Areas        2  
 Metadata Sequence No  4  
 VG Access             read/write  
 VG Status             resizable  
 MAX LV                0  
 Cur LV                2  
 Open LV               2  
 Max PV                0  
 Cur PV                2  
 Act PV                2  
 VG Size               57.99 GiB  
 PE Size               4.00 MiB  
 Total PE              14846  
 Alloc PE / Size       7422 / 28.99 GiB  
 Free  PE / Size       7424 / 29.00 GiB  
 VG UUID               TmEeQp-aLi2-Wz2A-HgAK-suly-MFNf-VFiNC0lvm> lvm> lvextend -l +100%FREE /dev/centos/root  
 Size of logical volume centos/root changed from 26.99 GiB (6910 extents) to 55.99 GiB (14334 extents).  
 Logical volume centos/root successfully resized.
lvm> [root@kbsnode01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 893M     0  893M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M   11M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
dev/mapper/centos-root   27G  4.1G   23G  16% /
/dev/sda1               1014M  187M  828M  19% /boot
tmpfs                    182M   24K  182M   1% /run/user/1000
/dev/sr0                 4.5G  4.5G     0 100% /run/media/admin/CentOS 7 x86_64
tmpfs                    182M     0  182M   0% /run/user/0
[root@kbsnode01 ~]# 

The above operations can be executed in the operating system interactive mode

[root@kbsnode01 ~]# vgextend centos /dev/sdb1
[root@kbsnode01 ~]# lvextend -l +100%FREE /dev/mapper/centos-root

Formatting the Logical Volume

[root@kbsnode01 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1768960 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=7075840, imaxpct=25         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=3455, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 7075840 to 14678016
[root@kbsnode01 ~]# [root@kbsnode01 ~]# [root@kbsnode01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 893M     0  893M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M   11M  900M   2% /run
dev/mapper/centos-root   56G  4.1G   52G   8% /
/dev/sda1               1014M  187M  828M  19% /boot
tmpfs                    182M   24K  182M   1% /run/user/1000
/dev/sr0                 4.5G  4.5G     0 100% /run/media/admin/CentOS 7 x86_64
tmpfs                    182M     0  182M   0% /run/user/0
[root@kbsnode01 ~]# 

Comparing the previous volume group expansion steps, we can see that 29G has been expanded to the / directory.

In summary, online expansion consists of five steps: check devices — check volume group information — extend physical volume group — extend logical volume — format logical volume.

lsblk
vgextend centos /dev/sdb1
lvextend -l +100%FREE /dev/mapper/centos-root
xfs_growfs /dev/mapper/centos/root

If using the ext4 file format, then use the following command during formatting:

resize2fs /dev/mapper/centos/root

Leave a Comment