Introduction
LVM stands for logical volume management and it’s software that is used to manage multiple volumes and filesystems. You can easily create, grow and and shrink partitions as required. LVM manages three main components, volume groups, physical volumes and logical volumes. Volume groups are a combination of physical and logical volumes. Physical Volumes correspond to disks and Logical Volumes correspond to partitions that hold a filesystem.
Features:
- Easily manage large hard disk farms by adding and removing hard disk on the fly without downtime.
- Easily resize partitions and volume groups, and add an extra hard disk to a current volume group to increase space.
- Easily take a backup by taking snapshots of the logical volumes.
- Using the traditional disk partitioning method, you can create only four primary partitions. LVM allows you to create unlimited number of partitions.
In this post, we will learn how to setup lvm and manage multiple hard disk using lvm.
Note: This tutorial assumes you are using Ubuntu and have three hard disks (/dev/sda, /dev/sdb and /dev/sdc).
Requirements
- A Ubuntu-16.04 server or desktop running on your system.
- Three hard disk installed on your system.
- A non-root user account with sudo privilege set up on your system.
Create Partitions
First, you will need to create partitions on two hard disks (sdb, sdc).
You can create a partition on disk (sdb) by running the following command:
sudo fdisk /dev/sdb
Answer all the questions as shown below:
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Repeat the same process for disk (sdc)
sudo fdisk /dev/sdc
Now, re-read the partition table by running the following command:
sudo partprobe /dev/sdb
sudo partprobe /dev/sdc
Create the Physical Volumes
The next step is to create a physical volume on each hard disk.
You can create physical volume on disk /dev/sdb1 and /dev/sdc1 by running the following command:
sudo pvcreate /dev/sdb1 /dev/sdc1
You should see the following output:
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
Verify the physical volume with the following command:
sudo pvdisplay /dev/sdb1 /dev/sdc1
Output:
"/dev/sdb1" is a new physical volume of "1023.00 MiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1023.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 09aIFM-u7uN-1Q88-7vzu-R7La-wrLa-mTPKUi
"/dev/sdc1" is a new physical volume of "1023.00 MiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 1023.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID IHdQSm-oh49-suQ9-ltPO-j8sM-c14c-IHyN2C
Create the Volume Group
A volume group is a combination of one or more physical volume. Let’s create the volume group vgroup1 on physical volume (/dev/sdb1, /dev/sdc1).
sudo vgcreate vgroup1 /dev/sdb1 /dev/sdc1
Now, check the volume group with the following command:
sudo vgdisplay vgroup1
Output:
--- Volume group ---
VG Name vgroup1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID rPOq9S-NlYe-rssG-s7kN-dCBN-ynBZ-pjyz9s
Creating the Logical Volume on Volume Group
The volume group is a collection of logical volumes. You can assign free space from volume group when logical volumes are full. In this example, we will create three logical volumes logicalv1, logicalv2 and logicalv3 of 300MB size.
To do this, run the following commands:
sudo lvcreate vgroup1 -L +300MB -n logicalv1
sudo lvcreate vgroup1 -L +300MB -n logicalv2
lvcreate vgroup1 -L +300MB -n logicalv3
- vgroup1 specifies the name of volume group.
- -L flag specifies the size of the logical volume.
- -n flag specifies the name of the logical volume.
Now, verify the logical volumes by running the following command:
sudo lvdisplay
Output:
--- Logical volume ---
LV Path /dev/vgroup1/logicalv1
LV Name logicalv1
VG Name vgroup1
LV UUID AQUEJR-Ax2S-JU1F-j2jw-dcmO-UZQo-PoaSkb
LV Write Access read/write
LV Creation host, time Node1, 2016-09-22 22:00:52 +0530
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2
--- Logical volume ---
LV Path /dev/vgroup1/logicalv2
LV Name logicalv2
VG Name vgroup1
LV UUID XjPMhi-40PB-SGyq-9Oq3-cryH-Lgfe-yCLYeb
LV Write Access read/write
LV Creation host, time Node1, 2016-09-22 22:01:28 +0530
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:3
--- Logical volume ---
LV Path /dev/vgroup1/logicalv3
LV Name logicalv3
VG Name vgroup1
LV UUID ot8uyL-tkU2-Aixy-kR4P-i9k8-1BDa-ElSrKL
LV Write Access read/write
LV Creation host, time Node1, 2016-09-22 22:01:32 +0530
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:4
Format and Mount Logical Volumes
Once the logical volume is created, it’s time to format and mount it.
To format the logical volumes with ext4 file system, run the following command:
sudo mkfs.ext4 /dev/vgroup1/logicalv1
Output:
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
Repeat the same process for /dev/vgroup1/logicalv2 and /dev/vgroup1/logicalv3:
sudo mkfs.ext4 /dev/vgroup1/logicalv2
sudo mkfs.ext4 /dev/vgroup1/logicalv3
Next, create a directory for mount point:
sudo mkdir /mnt/lv1-mount /mnt/lv2-mount /mnt/lv3-mount
Next, you will need to edit /etc/fstab file to mount the logical volume at boot time:
sudo nano /etc/fstab
Add the following lines:
/dev/vgroup1/logicalv1 /mnt/lv1-mount ext4 defaults 0 0
/dev/vgroup1/logicalv2 /mnt/lv2-mount ext4 defaults 0 0
/dev/vgroup1/logicalv3 /mnt/lv2-mount ext4 defaults 0 0
Run following command for the changes to take effect without rebooting the system:
sudo mount -a
To verify mount point, run the following command:
sudo df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/Node1--vg-root 9.0G 2.7G 5.9G 31% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 967M 4.0K 967M 1% /dev
tmpfs 196M 460K 195M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 977M 0 977M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 37M 187M 17% /boot
/dev/mapper/vgroup1-logicalv1 283M 2.1M 262M 1% /mnt/lv1-mount
/dev/mapper/vgroup1-logicalv2 283M 2.1M 262M 1% /mnt/lv2-mount
/dev/mapper/vgroup1-logicalv3 283M 2.1M 262M 1% /mnt/lv3-mount
Resize Logical Volumes
One of the most useful features of LVM is to increase or decrease the size of the logical volumes. You can resize logical volumes without rebooting the server.
Increase Logical Volume
In some cases, you will need to increase the size of the low space logical volume partition. You can easily expand logical volume size using the lvextend command.
In this example, we will expand logical volume logicalv1 from 300MB to 400MB:
You can do this by running the following command:
sudo lvextend -L+100M /dev/vgroup1/logicalv1
Output:
Extending logical volume logicalv1 to 400.00 MiB
Logical volume logicalv1 successfully resized
The logical volume size is 400 MB, but the file system on that volume is still only 300 MB.
You can make the file system use the entire 400 MB available, run the following command:
sudo resize2fs /dev/vgroup1/logicalv1
Output:
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/vgroup1/logicalv1 is mounted on /mnt/lv1-mount; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vgroup1/logicalv1 is now 409600 blocks long.
Now let’s see the size of re-sized logical volume using.
sudo lvdisplay /dev/vgroup1/logicalv1
Output:
--- Logical volume ---
LV Path /dev/vgroup1/logicalv1
LV Name logicalv1
VG Name vgroup1
LV UUID AQUEJR-Ax2S-JU1F-j2jw-dcmO-UZQo-PoaSkb
LV Write Access read/write
LV Creation host, time Node1, 2016-09-22 22:00:52 +0530
LV Status available
# open 1
LV Size 400.00 MiB
Current LE 100
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
Decrease Logical Volume
Before reducing logical volume size, it is important to backup the data.
In this example, we will reduce the logical volume logicalv2 from 300MB to 200MB.
You need to perform following steps to reduce the logical volume.
First, you need to unmount the logical volume logicalv2, you can do this by running the following command:
sudo umount /dev/vgroup1/logicalv2
Then you need to check the file system error on that volume.
To check the file system by running the following command:
sudo e2fsck -f /dev/vgroup1/logicalv2
Output:
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgroup1/logicalv2: 11/76912 files (0.0% non-contiguous), 19969/307200 blocks
Next, run the following command to reduce the logicalv2 from 300 MB to 200 MB:
sudo resize2fs -p /dev/vgroup1/logicalv2 200M
The output looks some thing like this:
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/vgroup1/logicalv2 to 204800 (1k) blocks.
Begin pass 3 (max = 38)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vgroup1/logicalv2 is now 204800 blocks long.
Now, checking the file system again with the following command:
sudo e2fsck -f /dev/vgroup1/logicalv2
Mount the logical volume back to the mount point:
sudo mount /dev/vgroup1/logicalv2 /mnt/lv2-mount/
Check the size of the logical volume with the following command:
sudo df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/Node1--vg-root 9.0G 2.7G 5.9G 31% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 967M 4.0K 967M 1% /dev
tmpfs 196M 464K 195M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 977M 0 977M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 37M 187M 17% /boot
/dev/mapper/vgroup1-logicalv1 380M 2.3M 355M 1% /mnt/lv1-mount
/dev/mapper/vgroup1-logicalv3 283M 2.1M 262M 1% /mnt/lv3-mount
/dev/mapper/vgroup1-logicalv2 186M 1.6M 171M 1% /mnt/lv2-mount
Now, see the result of physical volume, volume group and logical volume by running the following command:
sudo pvs
Output:
PV VG Fmt Attr PSize PFree
/dev/sdb1 vgroup1 lvm2 a-- 1020.00m 20.00m
/dev/sdc1 vgroup1 lvm2 a-- 1020.00m 1020.00m
sudo vgs
Output:
VG #PV #LV #SN Attr VSize VFree
vgroup1 2 3 0 wz--n- 1.99g 1.02g
`
`sudo lvs`
Output:
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
logicalv1 vgroup1 -wi-ao--- 400.00m
logicalv2 vgroup1 -wi-ao--- 300.00m
logicalv3 vgroup1 -wi-ao--- 300.00m
LVM Snapshot
LVM Snapshots can be used to take a backup copy of logical volume. You can easily recover deleted files using LVM Snapshots, if you have accidentally deleted any file after creating a snapshot.
Creating a Snapshot
In this example, we will create a snapshot of logical volume named logicalv1.
To create the snapshot of logical volume, run the following command:
sudo lvcreate -L 300MB -s -n logicalv1_snap /dev/vgroup1/logicalv1
Where -s creates a snapshot and -n specify the name of snapshot.
You can also remove snapshot by running the following command:
sudo lvremove /dev/vgroup1/logicalv1_snap
You can list the newly created snapshot with the following command:
sudo lvs
Output:
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root Node1-vg -wi-ao--- 9.26g
swap_1 Node1-vg -wi-ao--- 512.00m
logicalv1 vgroup1 owi-aos-- 400.00m
logicalv1_snap vgroup1 swi-a-s-- 300.00m logicalv1 0.00
logicalv2 vgroup1 -wi-ao--- 300.00m
logicalv3 vgroup1 -wi-ao--- 300.00m
Next, let’s add some files (around 100MB) in to logicalv1, so there is enough space to backup our changes in snap volume.
Check the status of snapshot again using the following command:
sudo lvs
Output:
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root Node1-vg -wi-ao--- 9.26g
swap_1 Node1-vg -wi-ao--- 512.00m
logicalv1 vgroup1 owi-aos-- 400.00m
logicalv1_snap vgroup1 swi-a-s-- 300.00m logicalv1 17.42
logicalv2 vgroup1 -wi-ao--- 300.00m
logicalv3 vgroup1 -wi-ao--- 300.00m
You can see that 17% of snapshot volume was used now. For more information of logicalv1 run the following command:
sudo lvdisplay /dev/vgroup1/logicalv1
Output:
--- Logical volume ---
LV Path /dev/vgroup1/logicalv1
LV Name logicalv1
VG Name vgroup1
LV UUID AQUEJR-Ax2S-JU1F-j2jw-dcmO-UZQo-PoaSkb
LV Write Access read/write
LV Creation host, time Node1, 2016-09-22 22:00:52 +0530
LV snapshot status source of
logicalv1_snap [active]
LV Status available
# open 1
LV Size 400.00 MiB
Current LE 100
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
Restoring a Snapshot
You can easily restore any deleted files in logical volume by restoring snapshot which we have created early.
You will need to unmount the file system, before restoring snapshot.
sudo umount /mnt/lv1-mount
Now, restore the logicalv1_snap by using the lvconvert command:
sudo lvconvert --merge /dev/vgroup1/logicalv1_snap
Output:
Merging of volume logicalv1_snap started.
logicalv1: Merged: 71.2%
logicalv1: Merged: 100.0%
Merge of snapshot into logical volume logicalv1 has finished.
Logical volume "logicalv1_snap" successfully removed
After the merge is completed, snapshot volume will be removed automatically. Now you can see the size of your partition by running the following command:
sudo df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/Node1--vg-root 9.0G 2.7G 5.9G 31% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 967M 4.0K 967M 1% /dev
tmpfs 196M 468K 195M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 977M 0 977M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 37M 187M 17% /boot
/dev/mapper/vgroup1-logicalv3 283M 2.1M 262M 1% /mnt/lv3-mount
/dev/mapper/vgroup1-logicalv2 186M 1.6M 171M 1% /mnt/lv2-mount
/dev/mapper/vgroup1-logicalv1 380M 2.3M 355M 1% /mnt/lv1-mount
You can also check the size of the logical volume by running the following command:
sudo lvs
Output:
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root Node1-vg -wi-ao--- 9.26g
swap_1 Node1-vg -wi-ao--- 512.00m
logicalv1 vgroup1 -wi-ao--- 400.00m
logicalv2 vgroup1 -wi-ao--- 300.00m
logicalv3 vgroup1 -wi-ao--- 300.00m
Extend Volume Group
In some cases, your volume group is under low space. Then you will need to extend your volume group.
In this example, you need to install an extra hard disk (/dev/sdd) on your system, then perform following steps to extend volume group.
First, create partition on disk (/dev/sdd):
sudo fdisk /dev/sdd
Answer all the questions as shown below:
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Now, re-read the partition table by running the following command:
sudo partprobe /dev/sdd
Now, create physical volume on /dev/sdd1:
sudo pvcreate /dev/sdd1
Now, extend the volume group named vgroup1 by running the following command:
sudo vgextend vgroup1 /dev/sdd1
After extending volume group, verify the size of the volume group by running the following command:
sudo vgdisply
Output:
--- Volume group ---
VG Name vgroup1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 11
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 3
Act PV 3
VG Size 2.99 GiB
PE Size 4.00 MiB
Total PE 765
Alloc PE / Size 250 / 1000.00 MiB
Free PE / Size 515 / 2.01 GiB
VG UUID rPOq9S-NlYe-rssG-s7kN-dCBN-ynBZ-pjyz9s
As you can see that extra 1 GB has been added to your Volume Group.