Logical volume management (LVM) creates an easy to use layer over physical disks. You can combine multiple disks and create logical storage volumes. This provides specific benefits such as:
- No restriction on disk size
- Increased disk throughput
- Mirroring volumes for business critical data
- Volume snapshots
- Easy backups and restores using snapshots
- Easy data relocation
- Resizing storage pools (add or remove disks) without reformatting disks
Tutorial details | |
---|---|
Difficulty | Advanced (rss) |
Root privileges | Yes |
Requirements | Linux with LVM |
Time | 15m |
Warning: Be careful with lvm/mkfs.ext4 and other commands, and device names as wrong device name can wipe out all data. Proceed with caution and always keep full backups.
Step 1 – Find out information about existing LVM
LVM Storage Management divided into three parts:
- Physical Volumes (PV) – Actual disks (e.g. /dev/sda, /dev,sdb, /dev/vdb and so on)
- Volume Groups (VG) – Physical volumes are combined into volume groups. (e.g. my_vg = /dev/sda + /dev/sdb.)
- Logical Volumes (LV) – A volume group is divided up into logical volumes (e.g. my_vg divided into my_vg/data, my_vg/backups, my_vg/home, my_vg/mysqldb and so on)
Type the following commands to find out information about each part.
How to display physical volumes (pv)
Type the following pvs command to see info about physical volumes:
$ sudo pvs
Sample outputs:
$ sudo pvdisplay
Sample outputs:
From above output it is clear that our volume group named ubuntu-box-1-vg is made of a physical volume named /dev/vda5.
How to display information about LVM volume Groups (vg)
Type any one of the following vgs command/vgdisplay command to see information about volume groups and its attributes:
$ sudo vgs
OR
$ sudo vgdisplay
Sample outputs:
How to display information about LVM logical volume (lv)
Type any one of the following lvs command/lvdisplay command to see information about volume groups and its attributes:
$ sudo lvs
OR
$ sudo lvdisplay
Sample outputs:
- /dev/ubuntu-box-1-vg/root – Root file system
- /dev/ubuntu-box-1-vg/swap_1 – Swap space
Based upon above commands, you can get a basic idea how LVM organizes storage device into Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV):
Fig.05: How LVM organizes storage device into Physical Volumes (PV), Volume Groups (VG), & Logical Volumes (LV)
Step 2 – Find out information about new disk
You need to add a new disk to your server. In this example, for demo purpose I added a new disk drive, and it has 5GiB size. To find out information about new disks run:
$ sudo fdisk -l
OR
$ sudo fdisk -l | grep '^Disk /dev/'
Sample outputs:
$ sudo lvmdiskscan
Sample outputs:
/dev/ram0 [ 64.00 MiB] /dev/ubuntu-box-1-vg/root [ 37.49 GiB] /dev/ram1 [ 64.00 MiB] /dev/ubuntu-box-1-vg/swap_1 [ 2.00 GiB] /dev/vda1 [ 487.00 MiB] /dev/ram2 [ 64.00 MiB] /dev/ram3 [ 64.00 MiB] /dev/ram4 [ 64.00 MiB] /dev/ram5 [ 64.00 MiB] /dev/vda5 [ 39.52 GiB] LVM physical volume /dev/ram6 [ 64.00 MiB] /dev/ram7 [ 64.00 MiB] /dev/ram8 [ 64.00 MiB] /dev/ram9 [ 64.00 MiB] /dev/ram10 [ 64.00 MiB] /dev/ram11 [ 64.00 MiB] /dev/ram12 [ 64.00 MiB] /dev/ram13 [ 64.00 MiB] /dev/ram14 [ 64.00 MiB] /dev/ram15 [ 64.00 MiB] /dev/vdb [ 5.00 GiB] 2 disks 18 partitions 0 LVM physical volume whole disks 1 LVM physical volume
Step 3 – Create physical volumes (pv) on new disk named /dev/vdb
Type the following command:
$ sudo pvcreate /dev/vdb
Sample outputs:
Physical volume "/dev/vdb" successfully created
Now run the following command to verify:
$ sudo lvmdiskscan -l
Sample outputs:
WARNING: only considering LVM devices /dev/vda5 [ 39.52 GiB] LVM physical volume /dev/vdb [ 5.00 GiB] LVM physical volume 1 LVM physical volume whole disk 1 LVM physical volume
Step 4 – Add newly created pv named /dev/vdb to an existing lv
Type the following command to add a physical volume /dev/vdb to “ubuntu-box-1-vg” volume group:
$ sudo vgextend ubuntu-box-1-vg /dev/vdb
Sample outputs:
Volume group "ubuntu-box-1-vg" successfully extended
Finally, you need extend the /dev/ubuntu-box-1-vg/root to create total 45GB (/dev/vdb (5G)+ existing /dev/ubuntu-box-1-vg/root (40G))
$ sudo lvm lvextend -l +100%FREE /dev/ubuntu-box-1-vg/root
Sample outputs:
Size of logical volume ubuntu-box-1-vg/root changed from 37.49 GiB (9597 extents) to 42.52 GiB (10885 extents). Logical volume root successfully resized.
However, if you run df -h or any other command you will still see /dev/ubuntu-box-1-vg/root as 40G. You need to run the following command to enlarge the filesystem created inside the “root” volume:
$ sudo resize2fs -p /dev/mapper/ubuntu--box--1--vg-root
Sample outputs:
resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/mapper/ubuntu--box--1--vg-root is mounted on /; on-line resizing required old_desc_blocks = 3, new_desc_blocks = 3 The filesystem on /dev/mapper/ubuntu--box--1--vg-root is now 11146240 (4k) blocks long.
Verify it:
$ df -H
Sample outputs:
Filesystem Size Used Avail Use% Mounted on
udev 1.1G 0 1.1G 0% /dev
tmpfs 146M 12M 135M 9% /run
/dev/mapper/ubuntu--box--1--vg-root 45G 2.3G 41G 6% /
tmpfs 512M 0 512M 0% /dev/shm
tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs 512M 0 512M 0% /sys/fs/cgroup
/dev/vda1 495M 109M 361M 24% /boot
tmpfs 103M 0 103M 0% /run/user/0
🐧 13 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Is it possible to remove a hard disk to from an vg without hurt?
Yes. Only if you have free extents. Otherwise data loss will occur.
You’ll have to make sure that no extents are being used on the disk of which you want to remove. Otherwise you’ll have to move them with pvmove to the other disk. See this blog post for an example: http://adminotes.blogspot.nl/2012/02/how-to-migrate-data-to-smaller-disks.html
My advise is to build a similar setup in Virtualbox to test the commands of which you want to execute. If everything goes well, you’ll probably have a better feeling on performing the change on the real setup.
There is a high chance of data loss/corruption if you mess the commands up. So handle with care :)
I think LVM = Logical Volume Management and not Linux Volume Management
Excellent! Worked to perfection. Thank you.
The instruction is excellent. Although when it got to the resize I got a bad superblock error. I found I needed to use xfs_growfs
Recommend a step prior to the resize step to look in fstab.
e.g. cat /etc/fsab
and check the filesystem type. In my case – I have xfs – which is handled slightly different. Hence xfs_gowfs
Thanks
Thank you very much. Well explained
if disk xfs
xfs_growfs istead resize2fs
Is it a good idea to mix NVMe SSD,Sata SSD, Spinning Hard Drive in a LVM? Or should the storage types be heterogenous?
Followed the article, was able to increase
/dev/mapper/ubuntu–vg-root 31G 4.0G 25G 14% /
to
/dev/mapper/ubuntu–vg-root 86G 4.3G 78G 6% /
Now the VM (ubuntu18.04 LTS) does not boot, I get grub rescue> prompt.
I’d appreciate if you could email me …
Paul I have the same issue , VM will not boot… Any advice on resolving
Thanks for this, it was very helpful as I could not recall how to do it on a new server.
Great article.
Can you please add to this article how to deal with situations when the first volume is encrypted?