Sub topic 4
Adding Capacity from a New Disk
This chapter covers the first way to get free space into a volume group: attach a new disk and add it. It assumes you can use the command line with sudo, know what block devices, partitions, and mount points are, and can read lsblk and df output. It also assumes you know the LVM layers (physical volume, volume group, logical volume) from the earlier chapters. The command examples use a volume group named vg_data with a logical volume lv_app mounted at /srv/data.
Why adding a disk comes first
A logical volume can only grow into free extents in its volume group. If the group has none, lvextend fails with an "insufficient free space" error. Free space can come from two places:
- A new disk that you add to the group. This chapter covers it.
- An existing disk that has been enlarged, such as a virtual or cloud disk. The next chapter covers it.
Adding a disk takes two commands, pvcreate and vgextend. Neither command changes the filesystem or the logical volume, and neither needs the server to stop. Your applications keep running while you do this.
Step 1: Identify the new disk
The most dangerous part of this task is choosing the wrong device. pvcreate writes LVM metadata to the start of the device, so running it against a disk that holds data destroys that data.
List all block devices with the columns that help you tell them apart:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,SERIAL
sudo pvsA new, unused disk has these properties:
- It shows the size you expect.
- It has no child partitions.
- Its
FSTYPEandMOUNTPOINTScolumns are empty. - It does not appear in the
pvsoutput.
In this example the new disk is /dev/sdc, 50 GiB.
Device names such as /dev/sdc are assigned at boot or attach time and can differ between reboots or between servers. Before you run a destructive command, check the name against something stable. The serial number from lsblk, or a link in /dev/disk/by-id/, matches what your hypervisor or cloud console reports for the volume you attached. On cloud instances the name may also look like /dev/nvme1n1, and the order of NVMe devices is not guaranteed.
Once a disk is a physical volume, LVM identifies it by a UUID stored in its label. A later device rename does not break the volume group.
Check for leftover signatures
A disk that was used before may carry an old filesystem, RAID, or LVM signature. pvcreate refuses to overwrite some of these, and it asks for confirmation on others. List the signatures first. wipefs without options only reads them:
sudo wipefs /dev/sdcNo output means the disk is clean. If it lists signatures you know are obsolete, you can erase them:
sudo wipefs -a /dev/sdcStep 2: Decide between a whole disk and a partition
You can create a physical volume on the raw disk or on a partition. Both work, and the choice is mostly a matter of policy.
| Approach | Advantage | Drawback |
|---|---|---|
| Whole disk (`/dev/sdc`) | Simplest. Growing the disk later needs only `pvresize`, with no partition step. | Other tools may see a disk with no partition table and treat it as empty, so someone may overwrite it by mistake. |
| Partition (`/dev/sdc1`) | A partition table marks the disk as in use. Some installers and tools expect it. | Growing the disk later needs `growpart` or similar before `pvresize`. |
Many teams use the whole disk for data volumes and add partitions only where their tooling expects them. Follow whatever the rest of your servers do, so the next administrator is not surprised.
If you choose a partition, create one that spans the disk and mark it as LVM. The sgdisk command comes from the gdisk package:
sudo sgdisk -n 1:0:0 -t 1:8e00 /dev/sdc
sudo partprobe /dev/sdc
lsblk /dev/sdcYou then use /dev/sdc1 instead of /dev/sdc in the commands below. The rest of this chapter uses the whole disk.
Step 3: Create the physical volume
pvcreate writes an LVM label and a metadata area to the device and divides the remaining space into fixed-size extents, which are the units LVM allocates:
sudo pvcreate /dev/sdcExpected output:
Physical volume "/dev/sdc" successfully created.Check the result:
sudo pvs PV VG Fmt Attr PSize PFree
/dev/sdb vg_data lvm2 a-- <100.00g 0
/dev/sdc lvm2 --- 50.00g 50.00gThe new physical volume has an empty VG column. It is initialized but not yet part of any group, so no logical volume can use it. The lsblk -f command now shows LVM2_member in the FSTYPE column for /dev/sdc.
What `pvcreate` can refuse
| Message or symptom | Likely cause | What to do |
|---|---|---|
| `Device /dev/sdc excluded by a filter` | The `filter` or `global_filter` setting in `/etc/lvm/lvm.conf` rejects the device. | Read the filter. Edit it only if it is wrong for this device. |
| `Can't open /dev/sdc exclusively. Mounted filesystem?` | The device is mounted or in use by another subsystem. | Stop. You may have the wrong device. |
| `WARNING: ext4 signature detected ... Wipe it?` | The disk carries an old filesystem. | Answer `n` unless you are certain the data is disposable. Then use `wipefs -a`. |
| `Device /dev/sdc not found` | The disk is not visible to the kernel yet. | Check that the attach succeeded. Rescan the SCSI bus or re-check the cloud console. |
Avoid pvcreate -ff as a reflex. It forces creation over existing data and skips the checks that protect you.
Step 4: Add the physical volume to the volume group
vgextend takes the volume group name and one or more physical volumes:
sudo vgextend vg_data /dev/sdcExpected output:
Volume group "vg_data" successfully extendedIf you give vgextend a device that is not yet a physical volume, it initializes the device first. Running pvcreate separately is clearer, because it gives you a point to pause and check with pvs.
You can add several disks in one command by listing each device after the group name. For example, sudo vgextend vg_data /dev/sdc /dev/sdd adds two disks.
Step 5: Confirm the new free space
sudo vgs vg_data VG #PV #LV #SN Attr VSize VFree
vg_data 2 1 0 wz--n- <149.99g <50.00gRead it this way:
#PVwent from 1 to 2.VSizegrew by about the size of the new disk. It is slightly smaller than the raw disk size because LVM reserves some space for its metadata and rounds down to whole extents.VFreeis now the amount you can hand to logical volumes.
Check that nothing else changed:
sudo lvs vg_data
df -hT /srv/datalv_app and the mounted filesystem show the same size as before. That is expected: the pool grew, but nothing is using the new space yet. The logical volume and the filesystem still have to be extended. They are the subjects of the next chapters.
The whole procedure in one block:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,SERIAL
sudo wipefs /dev/sdc
sudo pvcreate /dev/sdc
sudo vgextend vg_data /dev/sdc
sudo vgs vg_dataWhat happens inside the group
Knowing the internal behavior explains several details you will see in practice.
Extents. A volume group divides every physical volume into physical extents (PEs) of equal size. The default is 4 MiB. The group has a single extent size, and you set it when you create the group. vgextend cannot change it, so a new disk is always split into the same-sized extents as the existing ones. You can see the value with vgdisplay vg_data, in the PE Size line.
Metadata. LVM stores a copy of the group's configuration on every physical volume, and also keeps a text backup in /etc/lvm/backup/ and previous versions in /etc/lvm/archive/. Each vgextend writes a new version. If the metadata is damaged, vgcfgrestore can use these files to recover. Copy /etc/lvm/backup/vg_data to a safe place before you start, along with your normal data backups.
Allocation. By default, a logical volume uses the "normal" allocation policy. When you extend a logical volume, LVM first tries to place the new extents on the physical volume where the volume already lives, and it uses another physical volume when there is not enough room. Your lv_app can therefore end up with some extents on /dev/sdb and some on /dev/sdc. You can ask for specific placement by giving a device after the lvextend command, as in lvextend -L +20G /dev/vg_data/lv_app /dev/sdc.
Failure domains: a gotcha with several disks
Adding a disk makes one volume out of more than one device, with no redundancy. A logical volume that spans /dev/sdb and /dev/sdc depends on both. If either disk fails, the filesystem on that volume loses part of its blocks, and in most cases you cannot read it normally.
This applies even if the second disk was added only for a little extra space. Treat the group as only as reliable as its weakest disk. Several ways to reduce the risk:
- Use disks that are themselves redundant, such as hardware RAID, a cloud block volume with built-in replication, or a RAID device from
mdadm. - Use LVM mirroring or RAID logical volumes, which are a separate topic.
- Keep backups and test restoring them.
- If the data is critical and the new disk is local and unreplicated, consider enlarging an existing disk instead (see the next chapter).
Undoing a mistake before using the space
If you added the wrong disk, you can remove it again as long as no logical volume uses its extents. Check that PFree equals PSize for that device in pvs, and then run:
sudo vgreduce vg_data /dev/sdc
sudo pvremove /dev/sdcvgreduce takes the physical volume out of the group, and pvremove wipes the LVM label. If some extents are already in use, move them to another physical volume first with pvmove /dev/sdc. This copies data and can take a long time. It needs enough free extents elsewhere in the group. Run it only with a current backup.
Common mistakes
- Adding the disk, then looking at
dfand seeing no change.dfreports the filesystem, which has not been grown yet. Checkvgsfor the newVFree. - Using the wrong device name. Check serial numbers and sizes, not just the name.
- Extending the wrong group. A server can have several groups, such as
vg_dataandvg_system. Runvgsfirst and copy the name exactly. - Forgetting that the disk must be present at boot. If the new disk is a cloud or network volume that attaches late, the system may boot with a missing physical volume, and the group may not activate. Make sure the attachment is permanent and that your boot process waits for it.
- Skipping a backup before changing storage. Adding a disk is low risk, but the steps right after it, extending the volume and growing the filesystem, are where mistakes cost data. Back up first.
Exercise
You run pvcreate /dev/sdc and pvs shows the device with an empty VG column. Then you run df -h /srv/data and the size has not changed. Is something wrong? What is the next command to run to make the space available, and what will it show?
Show answer
Nothing is wrong. The physical volume is initialized but not yet in any volume group, so no logical volume can use it, and the filesystem has not changed in any case. Run sudo vgextend vg_data /dev/sdc, then sudo vgs vg_data. The #PV count goes up by one and VFree rises by about the size of the new disk. df still shows the old size until the logical volume and then the filesystem are extended.
Exercise
A colleague adds a local, unreplicated disk to a volume group and extends lv_app so that part of it lives on the new disk. What risk did this create, and name two ways to reduce it?
Show answer
The logical volume now depends on both disks, and LVM adds no redundancy by itself. If either disk fails, part of the filesystem is lost, so the chance of losing the volume is higher than with one disk. To reduce the risk, use redundant storage underneath (hardware RAID, an mdadm RAID device, or replicated cloud volumes), use LVM mirror or RAID logical volumes, and keep tested backups.
Exercise
You attached a disk to the wrong volume group by mistake. No logical volume has been extended yet. Which two commands remove it again, and when would you need pvmove first?
Show answer
Run sudo vgreduce <vgname> <device> to take the physical volume out of the group, then sudo pvremove <device> to wipe its LVM label. You need pvmove <device> first only if some extents on that device are already allocated to a logical volume. It moves them to other physical volumes in the group, which must have enough free extents.