The Logical Volume Manager commands in Linux make it easy to add new disks and volumes to the operating system. It’s easy to do in no time, with no downtime or reboots required. However, I always recommend first making a full backup of all data on all existing volumes and taking a snapshot of the affected virtual machine(s).
There are two options when adding volumes:
- Adding full disk capacity to an existing volume
- Split disk capacity into new volumes
In this article, I explain how to achieve both.
Adding full disk capacity to a new volume with LVM
To add a new volume that uses all 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 exclusively use VMware for their servers, so this scenario takes place in a vSphere environment, but the steps required would be the same if adding a hard drive to a physical server.
1. Examine the existing disks
Login to the server, sudo to elevated root privileges, then examine the existing disks 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 from the vSphere console
Next, log in to the vSphere console to add a new disk using these steps:
- Right-click the VM.
- Choose Edit Settings.
- Select Add new device.
- Choose Hard Drive.
- Add the new hard drive with the desired size specifications (for the purposes of this article, I chose to add a 10GB hard drive).
Before you continue, run partprobe
on the host to ensure that the new disk was found.
3. Examine the existing disks again to see the new disk
Run lsblk
again what 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
sdc 8:32 0 10G 0 disk
In these results we see the new 10GB hard drive added as sdc.
4. Add a primary partition to the new hard drive
Run fdisk
to add a primary partition to the hard drive so that the file system recognizes it. This step involves 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):
- Press Enter to accept the default value of 1. This returns:
First sector (2048-20971519, default 2048):
- Press Enter to accept the default value of 2048. This returns:
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
- Press Enter to accept the default value 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. On success, the following is returned:
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
You 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 returns:
Physical volume "/dev/sdc1" successfully created.
To create the new volume group, run:
vgcreate repos /dev/sdc1
This command returns:
Volume group "repos" successfully created.
Operation vgs
confirms adding the 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
Next you want to create the logical volume (lv_repos):
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 following command:
mkfs.ext3 /dev/repos/lv_repos
Returns a notification that the file system was created successfully.
6. Mount the new file system
Run the following command to mount the new file system:
mount /dev/repos/lv_repos /repos
Ensure that this file system is automatically mounted the next time the server restarts by adding the following entry to /etc/fstab:
/dev/repos/lv_repos /repos ext4 defaults 0 0
Splitting the full disk capacity into multiple volumes
We create two logical volumes named repos1 and repos2, each 5GB.
First, follow steps 1-4 as shown above to add full disk capacity to a new volume. Then create directories /repos1 and /repos2.
To do this, you should create the logical volumes with only the desired amount of storage space; in this case only 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, there's a bit of overhead involved in terms of the space allocated to the first logical volume; it is actually about 5.1 GB occupied.
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 fast, easy, and reliable. I've performed on-the-fly disk management operations and have never faced any technical issue, let alone OS crash or data loss.
For more information on LVM, see linuxhandbook.com's Complete Beginners Guide to LVM.
Read Next: How to Expand and Shrink LVM Volumes (TechRepublic)