Thursday, 26 January 2017

Logical Volume Manager (LVM)

Below is the Practical steps of LVM concept:

1. Create 3 partition of size 200MB, 300MB, and 400MB

2. Convert the partition into physical volumes

3. Combine the first 2 physical volumes (200MB, and 300MB)
   into a volume group.

4. Create a logical volume from this volume group of size 450MB.

****************************************************
Last login: Tue Mar  8 21:45:12 2016 from 192.168.224.1
[root@keshavkummari ~]#
[root@keshavkummari ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a1858

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39         914     7031808   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             914        1045     1048576   82  Linux swap / Solaris

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb6683521

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          15        8032+  83  Linux
/dev/sdb3              16          16        8032+  83  Linux

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xde3e26fe

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1           1        8001   83  Linux
[root@keshavkummari ~]#
[root@keshavkummari ~]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (2-130, default 2):
Using default value 2
Last cylinder, +cylinders or +size{K,M,G} (2-130, default 130): +200M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (28-130, default 28):
Using default value 28
Last cylinder, +cylinders or +size{K,M,G} (28-130, default 130): +200M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (54-130, default 54):
Using default value 54
Last cylinder, +cylinders or +size{K,M,G} (54-130, default 130): +300M

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xde3e26fe

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1           1        8001   83  Linux
/dev/sdc2               2          27      208845   83  Linux
/dev/sdc3              28          53      208845   83  Linux
/dev/sdc4              54          92      313267+  83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@keshavkummari ~]# partprobe /dev/sdc



*********************************************************
Step 1: Create physical volumes:
Syntax: pvcreate <partition1> <partition2> <partition3>
*********************************************************
[root@keshavkummari ~]# pvcreate /dev/sdc2 /dev/sdc3 /dev/sdc4
  Physical volume "/dev/sdc2" successfully created
  Physical volume "/dev/sdc3" successfully created
  Physical volume "/dev/sdc4" successfully created


*********************************************************
Step 2: To see the physical volumes:
*********************************************************
[root@keshavkummari ~]# pvdisplay  or pvs
  "/dev/sdc2" is a new physical volume of "203.95 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc2
  VG Name
  PV Size               203.95 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               XGcKMj-T11u-kZ4d-N2UA-NdrJ-cuNI-oE3Ttp

  "/dev/sdc3" is a new physical volume of "203.95 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc3
  VG Name
  PV Size               203.95 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4xWrEA-zhpS-6KII-5kOb-jO21-9mGF-Pzlwgg

  "/dev/sdc4" is a new physical volume of "305.93 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc4
  VG Name
  PV Size               305.93 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               aqKZ8V-nniQ-4pUS-OfwY-jKmm-CktX-pcCVB4

*********************************************************
Step 3: Create a volume group
Syntax: vgcreate <volume_group_name> <physical_volume1> <physical_volume2>

*********************************************************
 
[root@keshavkummari ~]# vgcreate data /dev/sdc2 /dev/sdc3
  Volume group "data" successfully created

*********************************************************
Step 4: To see the volume group :
*********************************************************
 
[root@keshavkummari ~]# vgdisplay
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        2
  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               400.00 MiB
  PE Size               4.00 MiB
  Total PE              100
  Alloc PE / Size       0 / 0
  Free  PE / Size       100 / 400.00 MiB
  VG UUID               S5L3xc-tasb-JL69-jPNG-gRib-lSfA-Kw1s3y


*********************************************************
Step 5: Create a logical volume
Syntax: lvcreate -L <size> <volume_group_name> -n <logical volume name>

*********************************************************

[root@keshavkummari ~]# lvcreate -L 150mb data -n backupdata
  Rounding up size to full physical extent 152.00 MiB
  Logical volume "backupdata" created

*********************************************************
Step 6: To see the logical volume:
*********************************************************
 
[root@keshavkummari ~]# lvdisplay  or lvs
  --- Logical volume ---
  LV Path                /dev/data/backupdata
  LV Name                backupdata
  VG Name                data
  LV UUID                tcOHbt-MTDv-jVgV-ftgC-mVfa-SySR-PW5Ye9
  LV Write Access        read/write
  LV Creation host, time keshavkummari.localdomain, 2016-03-08 23:30:07 -0800
  LV Status              available
  # open                 0
  LV Size                152.00 MiB
  Current LE             38
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

*********************************************************
Step 7: Format the logical volume and mount it:
*********************************************************

[root@keshavkummari ~]# mkfs.ext3 /dev/data/backupdata
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
38912 inodes, 155648 blocks
7782 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
19 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

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

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@keshavkummari ~]# mkdir /backup
[root@keshavkummari ~]# mount /dev/data/backupdata /backup/

*********************************************************
Step 8:
1. The size of the logical volume is to be increased by 400MB.
2. The volume group does not have sufficient free space.
So first extend the volume group.

Extending the volume group:
Syntax: vgextend <volume_group_name> <physical_volume_name>

*********************************************************

[root@keshavkummari ~]# vgextend data /dev/sdc3
  Physical volume '/dev/sdc3' is already in volume group 'data'
  Unable to add physical volume '/dev/sdc3' to volume group 'data'.

*********************************************************
Step 9: Resizing the logical value:
Syntax: lvresize -L <+size> <logical_volume_name>

*********************************************************
To extend the logical volume by 200MB:
 
[root@keshavkummari ~]# lvresize -L +200M /dev/data/backupdata
  Extending logical volume backupdata to 352.00 MiB
  Logical volume backupdata successfully resized

Resize the filesystem:
**********************
 
[root@keshavkummari ~]# resize2fs /dev/data/backupdata
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/data/backupdata is mounted on /backup; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/data/backupdata to 360448 (1k) blocks.
The filesystem on /dev/data/backupdata is now 360448 blocks long.

*********************************************************
Step 10: To see the logical volume:
*********************************************************

[root@keshavkummari ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/data/backupdata
  LV Name                backupdata
  VG Name                data
  LV UUID                tcOHbt-MTDv-jVgV-ftgC-mVfa-SySR-PW5Ye9
  LV Write Access        read/write
  LV Creation host, time keshavkummari.localdomain, 2016-03-08 23:30:07 -0800
  LV Status              available
  # open                 1
  LV Size                352.00 MiB
  Current LE             88
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

*********************************************************
Step 11: Removing the logical volume:
Syntax: lvremove <logical_volume_name>
*********************************************************
 
[root@keshavkummari ~]# umount /backup/
[root@keshavkummari ~]# lvremove /dev/data/backupdata
Do you really want to remove active logical volume backupdata? [y/n]: n
  Logical volume backupdata not removed
Note: If "y"
then : Logical volume backupdata has been  removed successfully.


*********************************************************

NOTE : LVREDUCE IS NOT WORK WITH XFS FILES SYSTEM

Stpe 12: Reducing Logical Volume (LVM)
*********************************************************
To Reduce a logical volume there are 5 steps needed to be done very carefully.

While extending a volume we can extend it while the volume under mount status (online),
but for reduce we must need to unmount the file system before reducing.

5 steps below:

1. unmount the file system for reducing.
[root@keshavkummari ~]# umount /backup/

2. Check the file system after unmount.
[root@keshavkummari ~]# e2fsck -ff /dev/data/backupdata

[root@server ~]# e2fsck -ff /dev/data/backupdata
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/data/backupdata: 11/13328 files (9.1% non-contiguous), 6823/53248 blocks

Note: Must pass in every 5 steps of file-system check if not there might be
some issue with your file-system.

3. Reduce the file system.

[root@server ~]# resize2fs /dev/data/backupdata
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 53248 blocks long.  Nothing to do!

4. Reduce the Logical Volume size than Current size.
[root@server ~]# lvreduce -L -20M  /dev/data/backupdata
Logical volume lvm-minnu successfully resized

5. Recheck the file system for error.
[root@server ~]# e2fsck -ff /dev/data/backupdata

6. Remount the file-system back to stage.
[root@server ~]# mount /dev/data/backupdata /backup

******************** Keshav Kummari ******************** 

No comments:

Post a Comment