How to set up LVM

0
192
(Last Updated On: October 15, 2018)

Introduction

Storage is one of those cardinal components that your server cannot do without and thus demands resolute attention no matter what. This is a brief guide on how to implement LVM on your linux server or workstation. Thank you for visiting and we hope it will be of help to assist you in your quest/project.

“Failure is the condiment that gives success its flavor.”
— Truman Capote

Setting up LVM Storage

I bet you have heard or even better used LVM before. Logical volume management (LVM) technology simplifies how storage management is done. LVM virtualizes storage and proffers system administrators with a more flexible fashion of managing disk storage compared to the old paradigm of partitioning. Logical volume management works by dividing the physical volumes (PVs) into physical extents (PEs) which are thereafter mapped onto logical extents (LEs). The Logical Extents are thereafter grouped into volume groups (VGs). As you can guess, these spawned Volume Groups are combined into logical volumes (LVs) that act as the aforesaid virtual disk partitions. LVM makeS it really simple to resize and move storage volumes whenever required.
Having known that, let us now dive into setting up LVM. Am going to use a flash drive but the procedure is the same for any other drive or device(hard drive etc).

Step One

List available devices and partitions using
fdisk. As you can see from the output, there is a physical device labelled /dev/sdb

➤ sudo fdisk -l                                                                                                
Disk /dev/sda: 119.2 GiB, 128035676160 bytes, 250069680 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: 0xa3bc85b8

Device     Boot    Start       End   Sectors  Size Id Type
/dev/sda1       62517248 250067789 187550542 89.4G 83 Linux
/dev/sda2           2048  62517247  62515200 29.8G 83 Linux

Partition table entries are not in disk order.

Disk /dev/sdb: 14.9 GiB, 15938355200 bytes, 31129600 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: 0x2a4e70c2

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        8192 31129599 31121408 14.9G  c W95 FAT32 (LBA)

Step Two

Prepare the physical device by formatting it either through fdisk, parted or gdisk. We are going to use fdisk. First we will delete the existing partitions and create new ones.

➤ sudo fdisk /dev/sdb 
                                                                                        
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.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
No free sectors available.

#deleting the exiting partitions

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.
 
Command (m for help): d

Selected partition 1
Partition 1 has been deleted.

#creating new partitions
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): 
First sector (2048-31129599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-31129599, default 31129599): +7G

Created a new partition 1 of type 'Linux' and of size 7 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 
First sector (14682112-31129599, default 14682112): 
Last sector, +sectors or +size{K,M,G,T,P} (14682112-31129599, default 31129599): 

Created a new partition 2 of type 'Linux' and of size 7.9 GiB.

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Failed to remove partition 1 from system: Device or resource busy
Failed to add partition 1 to system: Device or resource busy
Failed to add partition 2 to system: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.

Confirm the LVM partitions by typing fdisk -l as follows:

➤ sudo fdisk -l                                                                                               
[sudo] password for penchant: 
Disk /dev/sda: 119.2 GiB, 128035676160 bytes, 250069680 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: 0xa3bc85b8

Device     Boot    Start       End   Sectors  Size Id Type
/dev/sda1       62517248 250067789 187550542 89.4G 83 Linux
/dev/sda2           2048  62517247  62515200 29.8G 83 Linux

Partition table entries are not in disk order.


Disk /dev/sdb: 14.9 GiB, 15938355200 bytes, 31129600 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: 0x2a4e70c2

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1           2048 14682111 14680064    7G 8e Linux LVM
/dev/sdb2       14682112 31129599 16447488  7.9G 8e Linux LVM


Partition table entries are not in disk order.

Step Three

Create Physical Volumes using pvcreate

➤ sudo pvcreate /dev/sdb1 /dev/sdb2                                                                           
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

Confirm the physical volumes using pvdisplay command

➤ sudo pvdisplay                                                                                              
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               tech
  PV Size               7.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1791
  Free PE               214
  Allocated PE          1577
  PV UUID               cGnGfI-oVG7-9CcY-kdmK-aR4R-iZY9-O9gD0g
   
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               tech
  PV Size               7.84 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2007
  Free PE               0
  Allocated PE          2007
  PV UUID               UvewNB-Z2d1-T3L1-c92C-rOLa-lcrg-19zuPk

Step Four

Create a volume group using vgcreatewith a name of your choice. Am going to use ‘tech’

sudo vgcreate tech /dev/sdb1 /dev/sdb2                                                                      
  Volume group "tech" successfully created

Step Five

Create a logical volume with a name and size of your choice using lvcreate with some options and switches as shown below.
Option -n is used to specify the name of the logical volume
Option -L specifies the size. It can be in MiB for Megabytes or GiB for Gigabytes.

➤ sudo lvcreate -n part1 -L 14GiB tech                                                                        
  Logical volume "part1" created.

Once the above command is issued, a device known as /dev/tech/part1 will be created. You can confirm this by invoking lvdisplay command. However, this device has no file system.

➤ sudo lvdisplay                                                                                              
  --- Logical volume ---
  LV Path                /dev/tech/part1 
  LV Name                part1
  VG Name                tech
  LV UUID                O1qtcJ-dDAj-gPoL-nZn0-VUMs-rVwe-f31OHq
  LV Write Access        read/write
  LV Creation host, time computing-pc, 2018-10-14 00:39:25 +0300
  LV Status              available
  # open                 0
  LV Size                14.00 GiB
  Current LE             3584
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

You can view the volume groups as well using vgdisplay

Step Six

Load a file system of your choice into the created logical volume. Let us load xfs file system. You can load ext3, ext4, brtfs and others as you wish here.

➤ sudo mkfs -t xfs /dev/tech/part1                                                                            
meta-data=/dev/tech/part1        isize=512    agcount=4, agsize=917504 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=0
data     =                       bsize=4096   blocks=3670016, 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=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

With that done, we have successfully created a logical volume /dev/tech/part1 with xfs file system. Cheers guys and thank you for visiting our site. Stay tuned for more guides and tutorials.