Creating new LVM Partition Linux
At a glance#
- Purpose: Turn a newly attached disk into a mounted, persistent LVM volume.
- Applies to: Any Linux distribution with
lvm2installed. - Risk: Medium —
pvcreatedestroys existing data on the target disk. - Time: About 20 minutes.
Overview#
LVM (Logical Volume Manager) sits between physical disks and filesystems, and is the reason a server can grow its storage without downtime or repartitioning. It has three layers:
| Layer | What it is | Command |
|---|---|---|
| PV — Physical Volume | A disk or partition handed to LVM | pvcreate |
| VG — Volume Group | A pool of one or more PVs | vgcreate |
| LV — Logical Volume | A slice of the pool that gets a filesystem | lvcreate |
The filesystem goes on the LV, not on the disk. Because an LV can be extended across any free space in its VG, adding capacity later is a live operation.
Warning: pvcreate writes LVM metadata to the start of the device and makes any existing filesystem unreachable. Confirm the device path before running it.
Before you start#
- Root access.
- A new disk attached to the machine (added in vSphere, or physically installed).
lvm2installed — present by default on most server images. If not:sudo apt install lvm2orsudo yum install lvm2.
Identify the new disk#
lsblkThe new disk has no partitions and no mount points:
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part /
sdb 8:16 0 100G 0 disk <-- the new diskIf it does not appear, rescan the SCSI bus rather than rebooting:
echo "- - -" | sudo tee /sys/class/scsi_host/host0/scan
lsblkConfirm the disk is genuinely empty:
sudo blkid /dev/sdbNo output means no recognised filesystem. Any output means stop and check before continuing.
Procedure#
1. Create the physical volume#
You can hand LVM a whole disk or a partition. Using the whole disk is simpler and is what the examples below do.
sudo pvcreate /dev/sdbFor multiple disks in one go:
sudo pvcreate /dev/sdb /dev/sdcConfirm:
sudo pvs2. Create the volume group#
The volume group is the pool. Give it a name that describes its purpose — vgdata, vgapp — rather than something generic.
sudo vgcreate vgdata /dev/sdbConfirm, and note the free space:
sudo vgs
sudo vgdisplay vgdata3. Create the logical volume#
To use all available space in the group:
sudo lvcreate -n lvdata -l 100%FREE vgdataTo allocate a fixed size and leave the rest free for later:
sudo lvcreate -n lvdata -L 50G vgdata| Flag | Meaning |
|---|---|
-n | Name of the logical volume |
-L | Size as an absolute value (50G) |
-l | Size as an extent count or percentage (100%FREE) |
Tip: Leaving some space unallocated in the VG is good practice. It gives you room to take an LVM snapshot before a risky change, and to grow whichever volume actually turns out to need it.
The new volume appears at /dev/<vg-name>/<lv-name>:
sudo lvs
ls -l /dev/vgdata/lvdata4. Create a filesystem#
sudo mkfs.xfs /dev/vgdata/lvdata| Filesystem | Choose when | Command |
|---|---|---|
xfs | General purpose, large files, heavy writes. Grows but cannot shrink. | mkfs.xfs |
ext4 | You may need to shrink it later. | mkfs.ext4 |
5. Create a mount point and mount it#
sudo mkdir -p /data
sudo mount /dev/vgdata/lvdata /data
df -hT /dataAt this point the volume works but will not survive a reboot.
6. Make the mount persistent#
Get the UUID:
sudo blkid /dev/vgdata/lvdataEdit fstab:
sudo nano /etc/fstabAdd a line using the UUID:
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /data xfs defaults 0 2The six fields are: device, mount point, filesystem type, options, dump flag, and fsck order (0 for root, 2 for other filesystems).
Warning: Test the entry before rebooting. An invalid fstab line can stop the server booting and force a rescue-mode recovery. ``bash sudo umount /data sudo mount -a df -hT /data`Ifmount -acompletes silently and/data` is mounted, the entry is correct.
Verification#
sudo pvs # physical volume present
sudo vgs # volume group with expected size
sudo lvs # logical volume present
df -hT /data # mounted with expected capacityThen reboot and confirm /data returns automatically:
sudo reboot
# after it comes back
df -hT /dataExtending later#
The reason for using LVM. Once more space is added to the VG:
sudo lvextend -l +100%FREE /dev/vgdata/lvdata
sudo xfs_growfs /data # xfs
sudo resize2fs /dev/vgdata/lvdata # ext4Both can be done while the filesystem is mounted and in use. See Extending partition size LVM Linux.
Troubleshooting#
| Symptom | Cause and fix |
|---|---|
Device /dev/sdb not found or ignored by filtering | The kernel has not registered the disk. Rescan the SCSI bus, or reboot. |
Device /dev/sdb excluded by a filter | An old signature is present. Inspect with wipefs /dev/sdb; clear it with sudo wipefs -a /dev/sdb only if the disk is confirmed empty. |
Volume group "vgdata" not found | Name typo, or the VG is not active. Run sudo vgchange -ay. |
mkfs reports the device is busy | Something already has it open. Check with sudo lsof /dev/vgdata/lvdata. |
| Mount lost after reboot | Missing or incorrect fstab entry. Recheck the UUID with blkid. |
| Boot fails after fstab edit | Boot to rescue mode, comment out the line, and retest with mount -a. |