Create and Mount LVM Volume

In this article we will look into creating LVM volume on remaining not partitioned space and formatting logical volume with ext4 file system . We also will look into mounting it via fstab. In our LAB we will use CentOS7 vm to demonstrate our steps.


Canada colocation

 

Creating LVM Volume

Lets start by determining free space with parted command.

[root@localhost ~]# parted /dev/sda  unit MiB print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 8192MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start    End      Size     Type     File system     Flags
        0.03MiB  1.00MiB  0.97MiB           Free Space
 1      1.00MiB  201MiB   200MiB   primary  xfs             boot
 2      201MiB   1201MiB  1000MiB  primary  linux-swap(v1)
 3      1201MiB  2201MiB  1000MiB  primary  xfs
        2201MiB  8192MiB  5991MiB           Free Space

from the output we can see that we already have 3 partitions / /boot and swap. We also have 6G of free space.
We will now created 4th partition with parted. The size will be 660G

parted /dev/sda mkpart primary 2308 3000

And now we can see that partition is created.

[root@localhost ~]# parted /dev/sda  unit MiB print free              
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 8192MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start    End      Size     Type     File system     Flags
        0.03MiB  1.00MiB  0.97MiB           Free Space
 1      1.00MiB  201MiB   200MiB   primary  xfs             boot
 2      201MiB   1201MiB  1000MiB  primary  linux-swap(v1)
 3      1201MiB  2201MiB  1000MiB  primary  xfs
 4      2201MiB  2861MiB  660MiB   primary
        2861MiB  8192MiB  5331MiB           Free Space

Our next step would be to create physical volume on /dev/sda4 partition

[root@localhost ~]# pvcreate /dev/sda4 -v
    Set up physical volume for "/dev/sda4" with 1351680 available sectors
    Zeroing start of device /dev/sda4
    Writing physical volume data to disk "/dev/sda4"
  Physical volume "/dev/sda4" successfully created

If you get error message stating that pvcreate not found, you can run

 yum install lvm2

on Centos based system


Lets now create volume group called VGdemo

[root@localhost ~]# vgcreate -v VGdemo /dev/sda4
    Adding physical volume '/dev/sda4' to volume group 'VGdemo'
    Archiving volume group "VGdemo" metadata (seqno 0).
    Creating volume group backup "/etc/lvm/backup/VGdemo" (seqno 1).
  Volume group "VGdemo" successfully created

We will now create logical volume demovol1 of size lets say 250M for now. We will go over on how to expand this volume at the later time.

[root@localhost ~]# lvcreate -L 250M -n demovol1 VGdemo -v
    Finding volume group "VGdemo"
  Rounding up size to full physical extent 252.00 MiB
    Archiving volume group "VGdemo" metadata (seqno 1).
    Creating logical volume demovol1
    Creating volume group backup "/etc/lvm/backup/VGdemo" (seqno 2).
    Activating logical volume "demovol1".
    activation/volume_list configuration setting not defined: Checking only host tags for VGdemo/demovol1
    Creating VGdemo-demovol1
    Loading VGdemo-demovol1 table (253:0)
    Resuming VGdemo-demovol1 (253:0)
    Wiping known signatures on logical volume "VGdemo/demovol1"
    Initializing 4.00 KiB of logical volume "VGdemo/demovol1" with value 0.
  Logical volume "demovol1" created.

Next step is formatting our newly created LVM volume with ext4

[root@localhost ~]# mke2fs -t ext4 /dev/VGdemo/demovol1
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
64512 inodes, 258048 blocks
12902 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
32 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

Mount using fstab

First lets determine UUID using tune2fs command

[root@localhost ~]# tune2fs -l /dev/VGdemo/demovol1 | grep UUID
Filesystem UUID:          cac53166-6455-4e16-8b92-5d7c68fc1cf3

Create directory that we want to use for mounting our new share.

mkdir /mymntdir

Lets now edit fstab file and add entry for our new mount

vi /etc/fstab 
UUID=cac53166-6455-4e16-8b92-5d7c68fc1cf3 /mymntdir     ext4    defaults 0 0

Now in order to mount our newly created share we run mount -a

[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda3                    997M  846M  151M  85% /
devtmpfs                     489M     0  489M   0% /dev
tmpfs                        497M     0  497M   0% /dev/shm
tmpfs                        497M  6.5M  491M   2% /run
tmpfs                        497M     0  497M   0% /sys/fs/cgroup
/dev/sda1                    197M   99M   99M  50% /boot
/dev/mapper/VGdemo-demovol1  241M  2.1M  222M   1% /mymntdir