Monday, 28 October 2019

The Logical Volume Manager - Creating Physical Volumes (PV),Volume Groups (VG), Logical Volumes (LV)

Benefits of using LVM

  • Flexible capacity - When using logical volumes, file systems can extend across multiple disks, since you can aggregate disks and partitions into a single logical volume.
  • Resizeable storage pools - You can extend logical volumes or reduce logical volumes in size with simple software commands, without reformatting and repartitioning the underlying disk devices.
  • Online data relocation - To deploy newer, faster, or more resilient storage subsystems, you can move data while your system is active. Data can be rearranged on disks while the disks are in use. For example, you can empty a hot-swappable disk before removing it.
  • Convenient device naming  - Logical storage volumes can be managed in user-defined groups, which you can name according to your convenience.
  • Disk striping  -You can create a logical volume that stripes data across two or more disks. This can dramatically increase throughput.
  • Mirroring volumes - Logical volumes provide a convenient way to configure a mirror for your data.
  • Volume Snapshots  - Using logical volumes, you can take device snapshots for consistent backups or to test the effect of changes without affecting the real data.

Levels of abstraction


    Logical Volumes (LV) -  a volume group is divided up into logical volumes.
    There are three types of LVM logical volumes: linear volumes, striped volumes, and mirrored volumes.
    Volume Group (VG) - Physical volumes are combined into volume groups.
    Physical Volume (PV) - is a block device such as a partition or
    whole disk

    Three types of volumes

    linear volume  - aggregates multiple physical volumes into one logical volume.

    striped volume  - enhances performance by writing data to a predetermined number of physical volumes in round-round fashion. With striping, I/O can be done in parallel. 

    mirrored volumes - maintain identical copies of data on different devices.

    Detailed information about different types of volumes here

    Commands

    lvmdiskscan - show all storage devices which have ability to be used with LVM.

    [root@rifle ~]# lvmdiskscan
      /dev/centos_rifle/root [      <6.20 GiB]
      /dev/sda1              [       1.00 GiB]
      /dev/centos_rifle/swap [     820.00 MiB]
      /dev/sda2              [      <7.00 GiB] LVM physical volume
      /dev/sdb               [      <1.04 GiB]
      /dev/sdc               [     519.34 MiB]
      4 disks
      1 partition
      0 LVM physical volume whole disks
      1 LVM physical volume


    lsblk               -  lists information about all or the specified block devices. It queries the /sys virtual file system to obtain the information that it displays.
    [root@rifle ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 8G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 7G 0 part ├─centos_rifle-root 253:0 0 6.2G 0 lvm / └─centos_rifle-swap 253:1 0 820M 0 lvm [SWAP] sdb 8:16 0 1G 0 disk sdc 8:32 0 519.3M 0 disk sr0 11:0 1 1024M 0 rom
    lsblk -p           - show all paths to the devices

    [root@rifle ~]# lsblk -p
    NAME                              MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    /dev/sda                                8:0    0     8G  0 disk
    ├─/dev/sda1                         8:1    0     1G  0 part /boot
    └─/dev/sda2                         8:2    0     7G  0 part
      ├─/dev/mapper/centos_rifle-root 253:0    0   6.2G  0 lvm  /
      └─/dev/mapper/centos_rifle-swap 253:1    0   820M  0 lvm  [SWAP]
    /dev/sdb                            8:16   0     1G  0 disk
    /dev/sdc                            8:32   0 519.3M  0 disk
    /dev/sr0                           11:0    1  1024M  0 rom

    fdisk -l            - shows extended information about all disks
    df -h                - shows the storage which attached to your linux systemin human                          readable format

    1.Create one or more physical volumes. 

    pvcreate initializes the disk for use by LVM and lists what we created

    [root@rifle ~]# pvcreate /dev/sdb
    Physical volume "/dev/sdb" successfully created.

    [root@rifle ~]# pvs
      PV         VG           Fmt  Attr PSize  PFree
      /dev/sda2  centos_rifle lvm2 a--  <7.00g     0
      /dev/sdb                lvm2 ---  <1.04g <1.04g

    2.Create a volume group from those one or more physical volumes and view volume groups.

    [root@rifle ~]# vgcreate vg_mydata /dev/sdb
      Volume group "vg_mydata" successfully created

    [root@rifle ~]# vgs
      VG           #PV #LV #SN Attr   VSize  VFree
      centos_rifle   1   2   0 wz--n- <7.00g    0
      vg_mydata      1   0   0 wz--n-  1.03g 1.03g

    3.Create one or more logical volumes from the volume group. 

    [root@rifle ~]# lvcreate -L 1G -n lv_mydata vg_mydata
    Logical volume "lv_mydata" created.
    [root@rifle ~]# lvs     # you can also use lvdisplay command
    [root@rifle ~]# lvs
      LV        VG           Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      root      centos_rifle -wi-ao----  <6.20g
      swap      centos_rifle -wi-ao---- 820.00m
      lv_mydata vg_mydata    -wi-ao----   1.00g

    We also can let the command use percentage of the free space:
    lvcreate -l 100%FREE -n lv_mydata vg_mydata

    Now we can threat the LV above as usual disk partition

    4.Create a file system on the new partition

    [root@rifle ~]# mkfs -t ext4 /dev/vg_mydata/lv_mydata
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    65536 inodes, 262144 blocks
    13107 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=268435456
    8 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376

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

    5.Mount the new file system

    [root@rifle ~]# mkdir /mydata #create new directory [root@rifle ~]# mount /dev/vg_mydata/lv_mydata /mydata/ [root@rifle ~]# df -h /mydata/ #show free space Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_mydata-lv_mydata 976M 2.6M 907M 1% /mydata

    6.Mount the new partition in the boot time

    Edit /etc/fstab and add the following line at the end:
    /dev/vg_mydata/lv_mydata   /mydata   ext4 defaults  0 0

    You can test mount by dismounting /mydata and mount it againg by using the following commands:
    [root@rifle ~]# umount /mydata
    [root@rifle ~]# mount -a    #mount filesystems using /etc/fstab configuration file
    [root@rifle ~]# mount |grep "/mydata"  #check if /mydata is mounted
    /dev/mapper/vg_mydata-lv_mydata on /mydata type ext4  (rw,relatime,seclabel,data=ordered)

    7. Deleting Logical Volumes, Volume Groups and Physical Volumes





    No comments: