Create LVM Volume

How to create an LVM volume

Only needed if no Physical Volume exists or additional disks are added

create physical volume and view it:

# adds the physical disk to lvm
$ pvcreate /deb/sdx
#view if the new physical disk is visible
$ pvs
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sdx          lvm2 a--  <1.82t <1.14t

Only needed when no VG exists or additional one is needed.

a VG can contain multiple physical

Create volume group on physical volume:

# create volume group
$ vgcreate testvg /dev/sdx
#view vulume groups
$ vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  testvg   1   0   0 wz--n- <1.82t <1.14t

Now we create the logical volume on the volume group there can be multiple logical volumes on an volume group

# create new logical volume
$ lvcreate -n testlv -L1G datavg                                                                                   
  Logical volume "testlv" created
# -n sets the name of the logical volume
# -L sets the Size in G
# last option is the volume group to place the lv on

# check if correct
$ lvs
   LV     VG     Attr       LSize   Pool Origin                                               
  testlv datavg -wi-a-----   1.00g                                                    

The lv is now setup but not usable

Before we can use it, it needs to have an Filesystem

we will use mkfs for this. Filesystem can be chosen as you prefer
(mkfs.ext4, mkfs.xfs, mkfs.btrfs)

create filesystem on lv:

$ mkfs.ext4 /dev/mapper/datavg-testlv                                                                              
mke2fs 1.46.5 (30-Dec-2021)
Device blocks are discarded: done                        
A file system with 262144 (4k) blocks and 65536 inodes is created.
UUID of the file system: 89f36f09-9155-466d-af6e-180524b832ce
Superblock backup copies stored in blocks: 
	32768, 98304, 163840, 229376

when requesting memory for the group tables: done                        
inode tables are written: done                        
The journal (8192 blocks) is created: done
The superblocks and the file system usage information are
written: done

Now it is ready to be used and receive Data.

Mount it once:

# mount to /mnt/test (folder has to exist)
$ mount /dev/mapper/datavg-testlv /mnt/test
# check if mount is up
$ mount | grep testlv                                                                                              
/dev/mapper/datavg-testlv on /mnt/test type ext4 (rw,relatime)

Mount permanently:

permanent mount will be done via /etc/fstab. the entries in this file will be mounted every time the OS is booted or the command “mount -a” is issued

open file for edit:

$ vim /etc/fstab

Add the following entry (don’t forget to change according to your setup ex.: path, filesystem other mount options)

# add the following line at the end of file
/dev/mapper/datavg-testlv /mnt/test ext4 defaults 0 0

mount and check

$ mount -av                                                                                                       
/mnt/test                : successfully mounted
$ mount | grep testlv                                                                                            
/dev/mapper/datavg-testlv on /mnt/test type ext4 (rw,relatime)

Now you can place data and other stuff on the volume

Leave a Reply

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