🗃️ How to create LVM logical volumes on Linux – IT is good

🗃️ How to create LVM logical volumes on Linux – IT is good

The storage system is one of those cardinal components that your server cannot do without, and therefore requires close attention, no matter what.

This is a quick guide to implementing LVM on your linux server or workstation.

Thank you for visiting and we hope it will help you in your searches/projects.

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

Setting up LVM storage on Linux

Surely you have already heard, and even better used LVM.

Logical Volume Management (LVM) technology simplifies storage management.

LVM virtualizes storage and offers system administrators a more flexible way to manage disk storage than the old partitioning paradigm.

Logical volumes are managed by dividing physical volumes (PVs) into physical extents (PEs), which are then mapped to logical extents (LEs).

The logical extents are then organized into Volume Groups (VGs).

As you can guess, these generated volume groups are combined into logical volumes (LVs), which act as the aforementioned virtual disk partitions.

LVM makes it very easy to resize and move storage volumes when needed.

Knowing this, let’s now dive into setting up LVM.

I’ll be using a flash drive, but the procedure is the same for any other drive or device (hard drive, etc.).

Step 1: Identify the device

List available devices and partitions with fdisk.

As you can see from the output, there is a physical device labeled /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 2: Create a Partition for the LVM Device

Prepare the physical device by formatting it with fdisk, parted, or gdisk.

We will 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 (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 presence of 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 3: Create a physical volume

Creating physical volumes with pvcreate

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

Confirm physical volumes with 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 4: Create a volume group

Create a volume group with vgcreate with a name of your choice.

I’m going to use ‘tech’.

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

Step 5: Create a logical volume

Create a logical volume with a name and size of your choice with lvcreate using some options and switches as shown below.

  • The -n option is used to specify the name of the logical volume.
  • The -L option 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.

After executing the above command, a device called /dev/tech/part1 will be created.

You can confirm this by calling the lvdisplay command.

However, this device does not have a 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 also view volume groups with vgdisplay

Step 6: Create a file system on a logical volume

Load the file system of your choice into the created logical volume.

Let’s load the xfs file system.

You can download ext3, ext4, brtfs and others as you wish.

$ 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

After that, we successfully created the logical volume /dev/tech/part1 with the xfs file system.

Thanks guys and thanks for visiting our site.

Stay tuned and stay tuned for new guides and tutorials.

see also:

Share this article:

fb-share-icon



HOWTO,Linux,LVM,

#create #LVM #logical #volumes #Linux #good

Leave a Comment

Your email address will not be published. Required fields are marked *