Thursday, May 11, 2023

How to Create and Manage LVM  Oracle Linux 8 on OCI

In enterprise Linux environments, Logical Volume Manager (LVM) provides flexibility in managing disk space by allowing dynamic resizing and better abstraction of physical storage devices.

In this post, we'll walk through adding a new disk, creating a volume group (VG), and then creating a logical volume (LV) on an Oracle Linux 8 server, specifically in an OCI (Oracle Cloud Infrastructure) environment.

Senerio

We want to:

  1. Initialize a new disk (/dev/sdb) for use with LVM.

  2. Create a new volume group vg_data.

  3. Create a logical volume lv_data with 350 GB of space.

Step 1: Create a Physical Volume (PV)

Before we can use a new disk with LVM, it must be initialized as a Physical Volume.

 [root@omoicsso opc]# pvcreate /dev/sdb

  Physical volume "/dev/sdb" successfully created.

/dev/Sdb is the hardware partition created in the OCI console as the block volume and assigned to the Linux server. It is raw storage from the OCI

[root@omoicsso opc]# pvs

  PV         VG        Fmt  Attr PSize   PFree

  /dev/sda3  ocivolume lvm2 a--   45.47g      0

  /dev/sdb             lvm2 ---  400.00g 400.00g

PVS lists all the physical volumes that are associated with this server 

Step 2: Create a Volume Group (VG)

Next, we group one or more physical volumes into a Volume Group:

[root@omoicsso opc]# vgcreate vg_data /dev/sdb

  Volume group "vg_data" successfully created

We had created the   volume group in the physical volume as vg_data -- it can be any name 

[root@omoicsso opc]# vgdisplay

  --- Volume group ---

  VG Name               vg_data

  System ID

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  1

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                0

  Open LV               0

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               <400.00 GiB

  PE Size               4.00 MiB

  Total PE              102399

  Alloc PE / Size       0 / 0

  Free  PE / Size       102399 / <400.00 GiB

  VG UUID               iWc996-XIO2-M3WL-oFxr-1LtR-ndXf-KxM0bL


  --- Volume group ---

  VG Name               ocivolume

  System ID

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  23

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               2

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               45.47 GiB

  PE Size               4.00 MiB

  Total PE              11641

  Alloc PE / Size       11641 / 45.47 GiB

  Free  PE / Size       0 / 0

  VG UUID               Hu90og-5IAx-0g5d-Q272-rmZL-L5fw-Z4R7lw

Here we have two volume groups,, so two groups have been displayed 

Step 3: Create a Logical Volume (LV)

Now, we need to created  a Logical Volume of 350 GB from vg_data:

[root@omoicsso opc]#  lvcreate -n lv_data -L 350G vg_data

  Logical volume "lv_data" created.

Once the volume group is created with the required size, you can format the logical volume using your preferred file system, such as XFS or ext4, and then mount it on the Linux system.

[root@omoicsso opc]# lvdisplay

  --- Logical volume ---

  LV Path                /dev/vg_data/lv_data

  LV Name                lv_data

  VG Name                vg_data

  LV UUID                mkJZtP-E5k6-vsmN-4lNf-9Qcg-DrIl-U5bKhe

  LV Write Access        read/write

  LV Creation host, time omoicsso, 2023-05-11 11:57:00 +0000

  LV Status              available

  # open                 0

  LV Size                350.00 GiB

  Current LE             89600

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     4096

  Block device           252:2


  --- Logical volume ---

  LV Path                /dev/ocivolume/oled

  LV Name                oled

  VG Name                ocivolume

  LV UUID                nLXc2g-VyUO-dcUo-u1R1-HFOA-znRZ-PYAeBu

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2023-01-17 19:39:46 +0000

  LV Status              available

  # open                 1

  LV Size                10.00 GiB

  Current LE             2560

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     4096

  Block device           252:1


  --- Logical volume ---

  LV Path                /dev/ocivolume/root

  LV Name                root

  VG Name                ocivolume

  LV UUID                0MG24k-y6uq-CJo6-75bH-Qfak-Glvd-U2E9YK

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2023-01-17 19:39:47 +0000

  LV Status              available

  # open                 1

  LV Size                35.47 GiB

  Current LE             9081

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     4096

  Block device           252:0


We have three LVs, so the lvdisplay command displays all the LV in the server 


[root@omoicsso opc]#

At this point:

  • /dev/sdb is being used as an LVM physical volume.

  • You've grouped it into a volume group called vg_data.

  • A 350 GB logical volume (lv_data) is ready for formatting and mounting.


Create the file system 

mkfs.xfs /dev/vg_data/lv_data

Create A directory and Mount the lv 

mkdir /data
mount /dev/vg_data/lv_data /data

And optionally, add it to /etc/fstab for persistent mounting.

 Conclusion

Using LVM makes your Linux server storage much more flexible, especially in cloud environments like OCI where disks can be resized or replaced frequently. You can simplify expansion, backups, and migration by structuring your storage into volume groups and logical volumes.

Stay tuned for future posts on resizing volumes and taking LVM snapshots!


No comments:

Post a Comment