Working with logical volumes (part 1)
Part one of working with logical volumes will cover the basic’s involved in creating logical volumes.
TL;DR
For those of you who just want the order of the commands.
sudo pvcreate </path/to/device>
sudo vgcreate <vgname> </path/to/device>
sudo lvcreate -n <lvname> -L <size> <vgname>
sudo mkfs.<filesystem> </path/to/lv>
What you need to follow this guide
- A free disk (I used an empty virtual machine disk)
- Any Linux distribution (In this example I’ll be using Fedora 26, but the commands are the same across the entire Linux spectrum)
- LVM packages (lvm2 - usually pre-installed)
What is LVM?
Logical Volume Management (LVM
) offers a way to abstract a disk, multiple disks, or disk partitions into one logical volume. LVM
file systems can be rearranged, resized, moved, removed, created, and deleted on the fly. They offer incredible flexibility when setting up a new system or when rethinking the storage layout of an existing system.
Logical Volume Management filesystems are made up of 4 major parts
- The physical device (physical volume)
- This is the storage device or devices that will make up the volume group.
- The Volume Group
- The volume group is essentially a disk or a group of physical devices that have been separated into a group. The Volume Group is just a pool of disks. It can contain just one disk (or partition), or many disks.
- The volume group is a logical volume made up of physical volumes
- The Logical Volume
- The logical volume is some portion of the volume group that will be dedicated to a particular filesystem. One volume group can have many live volumes.
-
The filesystem
- After you create a Live Volume you must format it into one of the many Linux filesystems. Most likely
ext4
orxfs
.
- After you create a Live Volume you must format it into one of the many Linux filesystems. Most likely
Here is what you should remember if you are new to LVM
- Physical Volumes (the raw storage), make up Logical Volume Groups.
- Volume Groups are a logical representation of physical devices.
- Volume Groups contain Live Volumes which are formatted as filesystems.
- Filesystems are responsible for storing your data.
Create a new Volume
Step one - identify your storage device
We can identify our storage devices using the fdisk
command
sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 0E3D261B-275C-49D0-8C9A-427B5CEEAD4F
Device Start End Sectors Size Type
/dev/sda1 2048 411647 409600 200M EFI System
/dev/sda2 411648 2508799 2097152 1G Linux filesystem
/dev/sda3 2508800 62912511 60403712 28.8G Linux LVM
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/mapper/fedora-root: 25.8 GiB, 27703377920 bytes, 54108160 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/mapper/fedora-swap: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
The device I want is shown as Disk /dev/sdb 10GiB
Step two - create the physical volume
Use the pvcreate
command to create the new physical volume.
sudo pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
Step three - create the volume group
The vgcreate
command, creates a new volume group
sudo vgcreate vgtest /dev/sdb
Volume group "vgtest" successfully created
Step four - create a logical volume
sudo lvcreate -n testlv -L 2G vgtest
Logical volume "testlv" created.
Maybe another one
sudo lvcreate -n omg_testlv -L 2G vgtest
Logical volume "omg_testlv" created.
lvcreate
takes several arguments. You must specify a name, a size, and the volume group to attach it to.
Step five - make the filesystems
I’m formatting my file systems as xfs
in this case.
Start with testlv
sudo mkfs.xfs /dev/mapper/vgtest-testlv
meta-data=/dev/mapper/vgtest-testlv isize=512 agcount=4, agsize=131072 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0, rmapbt=0, reflink=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
<br /r>
Then omg_testlv
sudo mkfs.xfs /dev/mapper/vgtest-omg_testlv
meta-data=/dev/mapper/vgtest-omg_testlv isize=512 agcount=4, agsize=131072 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0, rmapbt=0, reflink=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Step 3 - add the new filesystems to your fstab
Make a couple directories as mount points.
sudo mkdir /testlv
sudo mkdir /omgtestlv
Edit the /etc/fstab
file and append the following.
/dev/mapper/vgtest-testlv /testlv xfs defaults 0 0
/dev/mapper/vgtest-omg_testlv /omgtestlv xfs defaults 0 0
Mount the filesystems
sudo mount -a
How do I know that this did something?
Check your file system mounts with df
df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 938M 0 938M 0% /dev
tmpfs tmpfs 950M 0 950M 0% /dev/shm
tmpfs tmpfs 950M 2.4M 948M 1% /run
tmpfs tmpfs 950M 0 950M 0% /sys/fs/cgroup
/dev/mapper/fedora-root ext4 26G 5.6G 19G 24% /
tmpfs tmpfs 950M 28K 950M 1% /tmp
/dev/sda2 ext4 976M 109M 800M 12% /boot
/dev/sda1 vfat 200M 9.5M 191M 5% /boot/efi
tmpfs tmpfs 190M 24K 190M 1% /run/user/42
tmpfs tmpfs 190M 40K 190M 1% /run/user/1000
/dev/mapper/vgtest-testlv xfs 2.0G 35M 2.0G 2% /testlv
/dev/mapper/vgtest-omg_testlv xfs 2.0G 35M 2.0G 2% /omgtestlv
Notice our new volumes in the output of df
.
Verify the Logical Volume with lvdisplay
sudo lvdisplay
--- Logical volume ---
LV Path /dev/vgtest/testlv
LV Name testlv
VG Name vgtest
LV UUID zw2JB7-gmmZ-4llC-FLf1-jJhE-B82R-Xz4Cxc
LV Write Access read/write
LV Creation host, time fedora01, 2017-11-01 21:24:33 -0400
LV Status available
# open 1
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/vgtest/omg_testlv
LV Name omg_testlv
VG Name vgtest
LV UUID h6eLKT-Aq5u-UXem-dgtA-OIXs-z901-lNRhNp
LV Write Access read/write
LV Creation host, time fedora01, 2017-11-01 21:25:28 -0400
LV Status available
# open 1
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
If you are going to be working with LVM on a regular basis you will want to familiarize yourself with the following commands:
vgdisplay
, vgscan
, pvdisplay
, pvscan
, lvdisplay
, lvscan
Helpful Links: