LVM (Logical Volume Manager)

Define LVM & How its Work

Management of LVM

Create partition storage in LVM and change the id

Getting ready

$ vagrant up
$ vagrant ssh

How to do it

From the man pages LVM, it LVM2 tools.

Make physical device

check the device for prepare

$ sudo fdisk -l
Part the device
$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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 (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Using default response p
Partition number (1-4, default 1): 1
First sector (2048-23316719, default 2048): 2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-23316719, default 23316719): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Using default response p
Partition number (2-4, default 2): 2
First sector (4196352-23316719, default 4196352): 4196352
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-23316719, default 23316719): +2G
Partition 2 of type Linux and of size 2 GiB is set

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

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): p

Disk /dev/sdb: 11.9 GB, 11938160640 bytes, 23316720 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
Disk label type: dos
Disk identifier: 0xa65b2d5f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
$ sudo cat /proc/partitions
$ sudo partprobe
Make Physical Volume
$ sudo pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

For check the Physical Volume

$ sudo pvdisplay /dev/sdb1 /dev/sdb2
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               TtBqPu-WGl4-1jEP-4iGj-BdMJ-ETqv-GSIpBb

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg0
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               afc7sR-NfsC-v9Pk-PLpn-QxyI-juf6-0Xyjmx

also use for display the physical volume

$ sudo pvs

Make Volume group

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

For check the volume group

$ sudo vgdisplay vg0
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm1
  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                2
  Act PV                2
  VG Size               3.99 GiB
  PE Size               4.00 MiB
  Total PE              1022
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1022 / 3.99 GiB
  VG UUID               oRO5rK-mR2r-n9cJ-BXNj-jGmJ-3KKX-gpCZqR

also use

$ sudo vgs

Make Logical Volume

$ sudo lvcreate -L 3G vg0 -n lv1
  Logical volume "lv1" created

For check the Logical Volume

$ sudo lvdisplay /dev/vg0/lv1
  --- Logical volume ---
  LV Path                /dev/vg0/lv1
  LV Name                lv1
  VG Name                vg0
  LV UUID                UwDqVy-FEW5-cs5R-nI0a-08Rk-fkcG-WZzLrN
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-05-24 02:22:20 -0400
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             768
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

also use

$ sudo lvs

Now create File system

$ sudo mkfs.xfs /dev/vg0/lv1 
meta-data=/dev/vg0/lv1           isize=256    agcount=4, agsize=196608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=786432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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

Create mounting Directory

$ sudo mkdir /mnt/lvstorage

Entry in fstab for auto or permanent mount

$ sudo vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Aug  1 12:31:59 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        1 1
UUID=4fe961ff-8508-4d2e-8997-947b6f2a57c4 /boot                   xfs     defaults        1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/vg0/lv1    /mnt/lvstorage  xfs     defaults        0 0
~                                                                               
~                                                                               
~                                                                               
~                                                                               
"/etc/fstab" 12L, 510C                                        12,1          All
$ sudo mount -a

Check the mount device

$ df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       7.2G  1.5G  5.8G  21% /
devtmpfs                devtmpfs  251M     0  251M   0% /dev
tmpfs                   tmpfs     258M     0  258M   0% /dev/shm
tmpfs                   tmpfs     258M  4.5M  253M   2% /run
tmpfs                   tmpfs     258M     0  258M   0% /sys/fs/cgroup
/dev/sda1               xfs       521M  130M  391M  25% /boot
/dev/mapper/vg0-lv1     xfs       3.3G   34M  3.2G   2% /mnt/lvstorage

Delete or Remove LVM

  1. unmount the filesystem
    $ sudo umount /dev/vg0/lv1
    
  2. Remove the entry from the fstab
    $ sudo vim /etc/fstab
    
  3. Then remove logical volume
    $ sudo lvremove /dev/vg0/lv1
    Do you really want to remove active logical volume lv1? [y/n]: y
    Logical volume "lv1" successfully removed
    
  4. Remove the volume group
    $ sudo vgremove /dev/vg0
    Volume group "vg0" successfully removed
    
  5. Remove the physical volume
    $ sudo pvremove /dev/sdb1 /dev/sdb2
    Labels on physical volume "/dev/sdb1" successfully wiped
    Labels on physical volume "/dev/sdb2" successfully wiped
    

Making Extend lvm partition.

Its For XFS file system

$ sudo lvextend -L 3500M /dev/vg0/lv1
  Extending logical volume lv1 to 3.42 GiB
  Logical volume lv1 successfully resized
$ df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       7.2G  1.5G  5.8G  21% /
devtmpfs                devtmpfs  251M     0  251M   0% /dev
tmpfs                   tmpfs     258M     0  258M   0% /dev/shm
tmpfs                   tmpfs     258M  4.5M  253M   2% /run
tmpfs                   tmpfs     258M     0  258M   0% /sys/fs/cgroup
/dev/sda1               xfs       521M  130M  391M  25% /boot
/dev/mapper/vg0-lv1     xfs       3.3G   34M  3.2G   2% /mnt/lvstorage
$ sudo xfs_growfs /dev/vg0/lv1 
meta-data=/dev/mapper/vg0-lv1    isize=256    agcount=4, agsize=196608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=786432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 786432 to 896000
$ df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       7.2G  1.5G  5.8G  21% /
devtmpfs                devtmpfs  251M     0  251M   0% /dev
tmpfs                   tmpfs     258M     0  258M   0% /dev/shm
tmpfs                   tmpfs     258M  4.5M  253M   2% /run
tmpfs                   tmpfs     258M     0  258M   0% /sys/fs/cgroup
/dev/sda1               xfs       521M  130M  391M  25% /boot
/dev/mapper/vg0-lv1     xfs       3.7G   34M  3.7G   1% /mnt/lvstorage

Now For ext4 file system

Extend ext4 file system

$ sudo lvextend -L 3500M /dev/vg0/lv1 
  Extending logical volume lv1 to 3.42 GiB
  Logical volume lv1 successfully resized
$ sudo resize2fs -f /dev/vg0/lv1 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv1 is mounted on /mnt/lvstorage; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv1 is now 896000 blocks long.

Reduce ext4 file system

$ sudo umount /dev/vg0/lv1
$ sudo fsck -f /dev/vg0/lv1
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg0-lv1: 11/229376 files (0.0% non-contiguous), 33862/896000 blocks
$ resize2fs /dev/vg0/lv1 3G
resize2fs 1.42.9 (28-Dec-2013)
open: Permission denied while opening /dev/vg0/lv1
[vagrant@localhost ~]$ sudo resize2fs /dev/vg0/lv1 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv1 to 786432 (4k) blocks.
The filesystem on /dev/vg0/lv1 is now 786432 blocks long.
$ sudo lvreduce -L 3G /dev/vg0/lv1 
  WARNING: Reducing active logical volume to 3.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
  Reducing logical volume lv1 to 3.00 GiB
  Logical volume lv1 successfully resized
$ sudo mount -a
$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       6.7G  1.4G  5.4G  21% /
devtmpfs                devtmpfs  240M     0  240M   0% /dev
tmpfs                   tmpfs     246M     0  246M   0% /dev/shm
tmpfs                   tmpfs     246M  4.4M  241M   2% /run
tmpfs                   tmpfs     246M     0  246M   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  124M  373M  25% /boot
/dev/mapper/vg0-lv1     ext4      2.9G  9.0M  2.8G   1% /mnt/lvstorage

Extent lvm partition

Create the LV of size 50 Physical Extent value by creating vg of size 16MB

  1. create lvm physical partition(/dev/sdb3)
  2. create physical volume
    $ sudo pvcreate /dev/sdb3
    Physical volume "/dev/sdb3" successfully created
    
  3. create vg extent
    $ sudo vgcreate -s 16M vg1 /dev/sdb3
    Volume group "vg1" successfully created
    
    $ sudo vgdisplay vg1
    --- Volume group ---
    VG Name               vg1
    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               800.00 MiB
    PE Size               16.00 MiB
    Total PE              50
    Alloc PE / Size       0 / 0   
    Free  PE / Size       50 / 800.00 MiB
    VG UUID               a8ZQzt-aHlA-i23x-4RCC-nsIT-4YaX-kFjzKC
    
  4. create logical volume
    $ sudo lvcreate -L 800M /dev/vg1 -n lv2
    Logical volume "lv2" created
    
    $ sudo lvdisplay /dev/vg1/lv2
    --- Logical volume ---
    LV Path                /dev/vg1/lv2
    LV Name                lv2
    VG Name                vg1
    LV UUID                5Nfkk2-cwRk-L3k1-eVp6-u5tp-WOOw-qQYV33
    LV Write Access        read/write
    LV Creation host, time localhost.localdomain, 2016-05-24 03:31:34 -0400
    LV Status              available
    # open                 0
    LV Size                800.00 MiB
    Current LE             50
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     8192
    Block device           253:4
    

results matching ""

    No results matching ""