Dynamically Increase Storage Capacity with LVM (Without Rebooting)

  1. Flexibly adjust storage capacity: Multiple devices can be joined together and presented to the system as a sort of virtual, large, contiguous device, equal to the sum of all parts added to it. And it’s very easy to expand it, by just adding more Cloud Disks and without having to reboot the system or stop the services that are writing/reading to/from disk.
  2. Snapshots: Snapshots are useful for migrating consistent backups or quickly reverting changes to the original content. Without a snapshot, when we start to copy files from one system to another, we might hit odd cases like this: file A and file B belong to the same application. While file A is being copied to another system, the application makes a change to both files and then file B is copied to the remote location. Now we have an inconsistent backup because one file reflects the state before a change was made and the other reflects the state after the change. Snapshots give us a way to freeze the filesystem and copy that frozen state to another location.
  3. Striping: We can create a type of volume that writes data simultaneously to multiple disks. So if we have to write a file of 300MB, it can write 100MB to disk 1, 100MB to disk 2 and 100MB to disk 3, all in parallel. This makes writes and reads faster. The drawback is that if only one device fails in a striped volume, the whole volume is lost.
  4. Thin pools: We can “trick” the system into believing we have much more storage space than in reality. An example scenario: we estimate that while today we only need 100GB of storage, in a few years we might need 2TB. We can launch an instance backed by an additional Cloud Disk of 100GB, create a thin volume of 2TB in (virtual) size and then create a filesystem on it. Although the size is virtual, before our real storage gets full, we will have to add more disks. So when we see that about 90GB are used, we can add another disk of 200GB. In a few months when that gets close to full, we can re-estimate how much we need for the next months and add another disk to back our thin volume. Since the size of our thin volume never changes, we don’t need to resize the filesystem each time we grow our storage pool.

Backup Your Data!

Any data that is important should be backed up. A cloud datacenter is much better than a home computer at keeping information safely stored, because of redundant copies being made, power being much more reliable, etc. But hardware failures can happen, instances can get hacked, and users themselves can make mistakes and accidentally wipe, overwrite and corrupt data.

Prerequisites

Log in to the ECS Cloud Console, create a new instance and choose Debian 9 as your Linux distribution. 1GB of RAM will suffice. Besides the system disk, add 3 data disks (sized at 20GB), like in the following picture:

LVM Terminology

PV — Physical Volume: the first thing LVM needs is a physical storage device. This can be an entire disk, a partition or even a file (loopback).

Install LVM

Update package manager information on the system:

apt update
apt upgrade
systemctl reboot
apt install lvm2

Build LVM Storage Pool and Create a Logical Volume

We first need to configure our physical devices. Let’s list the available ones on the system:

lvmdiskscan
pvcreate /dev/vdb /dev/vdc
vgcreate group1 /dev/vdb /dev/vdc
lvcreate --extents 100%VG --name volume1 group1

Create a Filesystem and Mount it

To make a logical volume useful, we must create a filesystem on it. You can use any filesystem you like. In this example we’ll use the ext4 filesystem.

mkfs.ext4 /dev/group1/volume1
mkdir /mnt/volume1
mount /dev/group1/volume1 /mnt/volume1

How to Migrate Data to a Logical Volume and Automatically Mount it at Boot

Usually, we will want to use volumes that can be flexibly expanded, to host the data of storage hungry applications, such as database or file servers. Let’s take MySQL/MariaDB as an example. One solution is to mount our volume in a location such as /mnt/volume1 and then change the database server configuration to keep its largest files there. It’s possible we may also have to setup correct permissions with chmod. When dealing with multiple programs that need to store a lot of data, we may find it more convenient to go another route. For example, we can migrate the whole /var directory (where a lot of applications keep variable, possibly fast growing files) to our volume and then mount the LV in /var. This way, the volume will be the one hosting files dropped in /var, instead of the root disk (the disk containing the operating system). Let's go through an example.

cp -axv /var/* /mnt/volume1/
echo '/dev/group1/volume1 /var ext4 defaults 0 2' >> /etc/fstab
systemctl reboot

How to Inspect LVM Structures

While maintaining a LVM pool, we will need to periodically monitor the free space available. We may also want to see how physical extents are distributed, progress on mirroring or moving data around and so on.

How to Extend Volume Groups and Resize Logical Volumes

In this tutorial, we already have 3 disks available and we only used 2 so we can skip adding another Cloud Disk.

pvcreate /dev/vdd
vgextend group1 /dev/vdd
lvextend --verbose --extents +100%FREE --resizefs /dev/group1/volume1

How to Replace Multiple Small Cloud Disks with One that is Larger

After a while, we might end up with too many disks in our pool. LVM has a solution for that, allowing us to remove multiple older, smaller disks and replace them with a single, larger one.

pvcreate /dev/vde
vgextend group1 /dev/vde
pvmove --atomic /dev/vdb /dev/vde
vgreduce group1 /dev/vdb
pvs
pvremove /dev/vdb

How to Manage LVM Snapshots

From time to time, we have to backup data. Snapshots allow us to freeze the state of files and directories, so we can copy them to another location. They’re also used to have a point that we can quickly revert to, in case something goes wrong, for example before upgrading an operating system to the next major version.

lvcreate --snapshot group1/volume1 --size 5G --name snap1
mkdir /mnt/snapshot
mount /dev/group1/snap1 /mnt/snapshot/
umount /mnt/volume1
lvconvert --merge group1/snap1
Can't merge until origin volume is closed.
Merging of snapshot group1/snap1 will occur on next activation of group1/volume1.

How to Create a Striped Volume to Increase Read/Write Performance

To begin, we have to unmount our volume (skip if already done in previous section):

umount /mnt/volume1
lvremove /dev/group1/volume1
lvcreate --stripes 3 --size 10G --name stripedvolume group1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com