The Logical Volume Manager commands in Linux facilitate adding new disks and volumes to the operating system. It’s easy to do this on the fly without requiring downtime or rebooting; though, I always recommend making a full backup of all data on any existing volumes first as well as taking a snapshot of the virtual machine(s) involved.
There are two options when it comes to adding volumes:
In this article, I’ll explain how to achieve both.
Adding the full disk capacity to a new volume using LVM
To add a new volume using the entirety of the new disk capacity, the goal is to add a 10GB mount point called /repos in a new volume group called repos.
Many shops use VMware exclusively for their servers, so this scenario takes place in a vSphere environment, but the steps involved would be the same if a disk were added to a physical server.
1. Examine the disks present
Log into the server, sudo to elevated root privileges, then examine the disks present by running:
lsblk
Which returns:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 49G 0 part
│ ├─rhel-root 253:0 0 20G 0 lvm /
│ ├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
│ ├─rhel-home 253:3 0 1G 0 lvm /home
│ ├─rhel-var 253:4 0 10G 0 lvm /var
│ ├─rhel-var_log 253:5 0 20G 0 lvm /var/log
│ ├─rhel-var_opt 253:6 0 3G 0 lvm /var/opt
│ ├─rhel-var_tmp 253:7 0 3G 0 lvm /var/tmp
│ ├─rhel-usr_local 253:8 0 10G 0 lvm /usr/local
│ ├─rhel-var_log_audit 253:9 0 1012M 0 lvm /var/log/audit
│ ├─rhel-opt 253:10 0 65G 0 lvm /opt
│ ├─rhel-opt_fireeye 253:11 0 2G 0 lvm /opt/fireeye
│ ├─rhel-besclient 253:12 0 8G 0 lvm /var/opt/BESClient
│ ├─rhel-opt_encase 253:13 0 2G 0 lvm /opt/encase
│ └─rhel-tmp 253:14 0 15G 0 lvm /tmp
sdb 8:16 0 850G 0 disk
└─appvg-vaplv 253:2 0 850G 0 lvm /opt/vap
2. Add a new disk via the vSphere console
Next, log into the vSphere console to add a new disk via these steps:
- Right-click the VM.
- Choose Edit Settings.
- Choose Add New Device.
- Choose Hard Disk.
- Add the new disk using the desired size specifications (for the purpose of this article, I chose to add a 10GB disk).
Before moving on, run partprobe
on the host for good measure to ensure the new disk was found.
3. Examine the disks present again to view the new hard disk
Run lsblk
again, which will return:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 49G 0 part
│ ├─rhel-root 253:0 0 20G 0 lvm /
│ ├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
│ ├─rhel-home 253:3 0 1G 0 lvm /home
│ ├─rhel-var 253:4 0 10G 0 lvm /var
│ ├─rhel-var_log 253:5 0 20G 0 lvm /var/log
│ ├─rhel-var_opt 253:6 0 3G 0 lvm /var/opt
│ ├─rhel-var_tmp 253:7 0 3G 0 lvm /var/tmp
│ ├─rhel-usr_local 253:8 0 10G 0 lvm /usr/local
│ ├─rhel-var_log_audit 253:9 0 1012M 0 lvm /var/log/audit
│ ├─rhel-opt 253:10 0 65G 0 lvm /opt
│ ├─rhel-opt_fireeye 253:11 0 2G 0 lvm /opt/fireeye
│ ├─rhel-besclient 253:12 0 8G 0 lvm /var/opt/BESClient
│ ├─rhel-opt_encase 253:13 0 2G 0 lvm /opt/encase
│ └─rhel-tmp 253:14 0 15G 0 lvm /tmp
sdb 8:16 0 850G 0 disk
└─appvg-vaplv 253:2 0 850G 0 lvm /opt/vap
sdc 8:32 0 10G 0 disk
In these results, we see the new 10GB disk added as sdc.
4. Add a primary partition to the new hard disk
Run fdisk
to add a primary partition to the disk, so the file system would recognize it. This step includes several commands:
fdisk -u -c /dev/sdc
This will return this message:
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 0xf9417ab7.
Command (m for help):
From here, there are six steps:
- Press n to create a new partition. This returns:
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
- Press p to create a primary partition. This returns:
Partition number
(1-4, default 1):
- Hit enter to accept the default of 1. This returns:
First sector (2048-20971519, default 2048):
- Hit enter to accept the default of 2048. This returns:
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
- Hit enter to accept the default of 20971519. This returns:
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help):
- Press w to write the changes. If successful, this returns:
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
You’ll then want to create the /repos directory by running:
mkdir /repos
5. Create the physical and logical volumes from the new partition
The next step is to create the physical volume from the new partition by running:
pvcreate /dev/sdc1
This command will return:
Physical volume "/dev/sdc1" successfully created.
To create the new volume group, run:
vgcreate repos /dev/sdc1
This command will return:
Volume group "repos" successfully created.
Running vgs
confirms the addition of 10GB volume group with this message:
VG #PV #LV #SN Attr VSize VFree
appvg 1 1 0 wz--n- <850.00g 0
repos 3 14 0 wz--n- <208.99g 10.00g
You’ll want to create the logical volume (lv_repos) next:
lvcreate /n lv_repos -size 10G repos
A successful command returns:
Logical volume "repos" created
Then, create an ext3 file system for this logical volume with the command:
mkfs.ext3 /dev/repos/lv_repos
Which returns a notification that the file system has been successfully created.
6. Mount the new file system
To mount the new file system, run the command:
mount /dev/repos/lv_repos /repos
Ensure this file system will automatically mount the next time the server is rebooted by adding the following entry to /etc/fstab:
/dev/repos/lv_repos /repos ext4 defaults 0 0
Splitting the full disk capacity among multiple volumes
We will create two logical volumes called repos1 and repos2, each totaling 5GB.
First, follow steps 1–4 as shown above for adding the full disk capacity to a new volume. Then, create the /repos1 and /repos2 directories.
To do so, you’ll want to create the logical volumes using only the desired amount of space; in this case just 5GB:
lvcreate -n repo1 -size 5G repos
This returns:
Logical volume "repo1" created
For the second logical volume:
lvcreate -n repo2 -size 5G repos
This returns:
Volume group "repos" has insufficient free space (1279 extents): 1280 required.
As it turns out, you have to account for a slight bit of overhead in terms of disk space allocated to the first logical volume; it’s actually about 5.1GB used.
To account for this, run:
lvcreate -n repo2 -size 4.9G repos
Which returns:
Logical volume "repo2" created.
Close enough.
Then, run lvs
to confirm the logical volumes:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
repo1 repos -wi-a----- 5.00g
repo2 repos -wi-a----- 4.90g
Learn more about LVM
Working with LVM to perform disk and volume operations is quick, easy and reliable. I have performed on-the-fly disk management actions and never once experienced a technical issue much less an operating system crash or data loss.
For more information on LVM, check out the Complete Beginners Guide to LVM by linuxhandbook.com.
Read next: How to expand and shrink LVM volumes (TechRepublic)