In this blog post, I will demonstrate how to use LVM to create flexible XFS filesystems where it allows us to extend or reduce its size without disrupting services.
Instructions
Let’s initiate installing LVM:
1 |
[root@ip-172-30-24-230 ~]# yum install lvm* -y |
Next, the disk should be already attached to the instance, and we can verify with fdisk
command:
1 2 3 4 5 6 |
[root@ip-172-30-24-230 ~]# fdisk -l Disk /dev/xvdf: 100 GiB, 107374182400 bytes, 209715200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes |
Now, we will create a partition in the disk:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[root@ip-172-30-24-230 ~]# fdisk /dev/xvdf Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xaab5a147. Command (m for help): p Disk /dev/xvdf: 100 GiB, 107374182400 bytes, 209715200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xaab5a147 Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-209715199, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): Created a new partition 1 of type 'Linux' and of size 100 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. |
With the partition created, we will create the Volume Group
:
1 2 3 |
a[root@ip-172-30-24-230 ~]# vgcreate vg_percona /dev/xvdf1 Physical volume "/dev/xvdf1" successfully created. Volume group "vg_percona" successfully created |
The following step is to create the Logical Volume
:
1 2 |
[root@ip-172-30-24-230 ~]# lvcreate -l 100%FREE -n mysql_data vg_percona Logical volume "mysql_data" created. |
We can see our progress using lvdisplay
to see the logical volume status:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-30-24-230 ~]# lvdisplay --- Logical volume --- UUID=935ebc4e-90b8-432b-a8f6-8a9934a94892 / xfs defaults,noatime,nodiratime 0 0 LV Path /dev/vg_percona/mysql_data LV Name mysql_data VG Name vg_percona LV UUID CDjTBT-N8LY-xQZS-MUcP-LdZR-q4wF-BbMCnh LV Write Access read/write LV Creation host, time ip-172-30-24-230.ec2.internal, 2020-07-03 15:57:27 +0000 LV Status available # open 0 LV Size <100.00 GiB Current LE 25599 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 |
Subsequently, the next step is to create the directory that we will use if it does not exist already:
1 |
[root@ip-172-30-24-230 ~]# mkdir /mysql_data |
Now, we create the XFS filesystem:
1 2 3 4 5 6 7 8 9 10 11 |
[root@ip-172-30-24-230 ~]# mkfs.xfs /dev/vg_percona/mysql_data meta-data=/dev/vg_percona/mysql_data isize=512 agcount=4, agsize=6553344 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=26213376, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=12799, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 |
And, let’s mount the partition:
1 |
[root@ip-172-30-24-230 ~]# mount /dev/vg_percona/mysql_data /mysql_data/ |
And it is done, we can check that using df
command:
1 2 3 4 5 6 7 8 9 |
[root@ip-172-30-24-230 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 3.8G 0 3.8G 0% /dev tmpfs 3.9G 8.0K 3.9G 1% /dev/shm tmpfs 3.9G 17M 3.8G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/xvda2 80G 1.7G 79G 3% / tmpfs 781M 0 781M 0% /run/user/1000 <strong>/dev/mapper/vg_percona-mysql_data 100G 746M 100G 1% /mysql_data</strong> |
If you want to auto-mount when rebooting the OS, you can add the new partition to the fstab
:
1 2 3 |
[root@ip-172-30-24-230 ~]# cat /etc/fstab UUID=935ebc4e-90b8-432b-a8f6-8a9934a94892 / xfs defaults,noatime,nodiratime 0 0 /dev/mapper/vg_percona-mysql_data /mysql_data xfs defaults,noatime,nodiratime 0 0 |
Conclusion
Using LVM is an excellent option to have flexible and easy management of disks and to extend the partition over time. If LVM is planned to be used in advance when deploying the instance, it can save time and avoid application downtime in the future.
For AWS using EBS volumes, it is possible to use the API itself to extend the partition size, which may be more straightforward and makes LVM redundant:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/requesting-ebs-volume-modifications.html
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html
It might be interesting to use LVM on EC2 in case you want to overcome disk bandwidth and IOPs limitations since there is a limit for it. Another alternative is to switch to io1
or another type of disk. This topic may be of intense discussion since many aspects need to be taken into consideration to decide the disk architecture such as price and technical requirements, which are not the subject of this post.