Wise people learn when they can; fools learn when they must - Arthur Wellesley

Friday, 31 March 2017

RHEL6-16-LOGICAL VOLUME MANAGER (LVM)-3


RHEL6-16-LOGICAL VOLUME MANAGER (LVM)-3

LVM OPERATIONS:

How to extend/increase volume group space?
“vgextend”
How to extend/increase logical volume space?
“lvextend”
How to reduce/decrease logical volume space?
“lvreduce”
How to reduce/decrease volume group space?
“vgreduce”



[root@rhel6-client1 ~]# vgs
  VG     #PV #LV #SN Attr   VSize VFree
  myvg01   3   4   0 wz--n- 9.99g 9.20g
  vg01     2   4   0 wz--n- 7.99g    0 ççç No free space
  vg02     1   3   0 wz--n- 2.00g    0

I have 3 vg’s and I want to increase the size for “vg01”

Let’s check how many PV’s I have.

[root@rhel6-client1 ~]# pvs
  PV         VG     Fmt  Attr PSize PFree
  /dev/sdb1  vg01   lvm2 a--  5.00g    0
  /dev/sdb2  vg01   lvm2 a--  3.00g    0
  /dev/sdb3  vg02   lvm2 a--  2.00g    0
  /dev/sdc1  myvg01 lvm2 a--  5.00g 4.55g
  /dev/sdc2  myvg01 lvm2 a--  2.00g 1.75g
  /dev/sdc3  myvg01 lvm2 a--  3.00g 2.89g

There is no PV which I can add to “vg01”, I need another PV.

[root@rhel6-client1 ~]# fdisk -cu /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x84af01a1.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@rhel6-client1 ~]# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created

[root@rhel6-client1 ~]# pvs
  PV         VG     Fmt  Attr PSize PFree
  /dev/sdb1  vg01   lvm2 a--  5.00g    0
  /dev/sdb2  vg01   lvm2 a--  3.00g    0
  /dev/sdb3  vg02   lvm2 a--  2.00g    0
  /dev/sdc1  myvg01 lvm2 a--  5.00g 4.55g
  /dev/sdc2  myvg01 lvm2 a--  2.00g 1.75g
  /dev/sdc3  myvg01 lvm2 a--  3.00g 2.89g
  /dev/sdd1         lvm2 a--  2.00g 2.00g ççç

EXTEND VOLUME GROUP:

[root@rhel6-client1 ~]# vgextend vg01 /dev/sdd1
  Volume group "vg01" successfully extended

[root@rhel6-client1 ~]# vgs
  VG     #PV #LV #SN Attr   VSize VFree
  myvg01   3   4   0 wz--n- 9.99g 9.20g
  vg01     3   4   0 wz--n- 9.99g 2.00g ççç 2gb added and free
  vg02     1   3   0 wz--n- 2.00g    0

Now I need to increase LV space under vg01.

[root@rhel6-client1 ~]# mkfs.ext4 /dev/vg01/lvol0
mke2fs 1.41.12 (17-May-2010)
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

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

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@rhel6-client1 ~]# mkdir /lvol0
[root@rhel6-client1 ~]# mount /dev/vg01/lvol0 /lvol0
[root@rhel6-client1 ~]# df -kh /lvol0
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lvol0
                     1008M   34M  924M   4% /lvol0
[root@rhel6-client1 ~]#

EXTEND LOGICAL VOLUME GROUP:

[root@rhel6-client1 ~]# lvextend -L +300M /dev/vg01/lvol0
  Extending logical volume lvol0 to 1.29 GiB
  Logical volume lvol0 successfully resized


Size is increased but it is not effective,

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-ao---   1.29g

[root@rhel6-client1 ~]# df -kh /lvol0
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lvol0
                     1008M   34M  924M   4% /lvol0

RESIZING FILE SYSTEM:

[root@rhel6-client1 ~]# resize2fs /dev/vg01/lvol0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lvol0 is mounted on /lvol0; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg01/lvol0 to 338944 (4k) blocks.
The filesystem on /dev/vg01/lvol0 is now 338944 blocks long.

[root@rhel6-client1 ~]# df -kh /lvol0
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lvol0
                      1.3G   34M  1.2G   3% /lvol0

Now I think, there was no need to increase the size of “lvol0”, so I want my space back.

Better to take the backup of contents before reducing, to avoid any mishap.
Unmount the FS

[root@rhel6-client1 ~]# fsck -y /dev/vg01/lvol0
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/vg01-lvol0: clean, 11/90112 files, 14242/338944 blocks

[root@rhel6-client1 ~]# e2fsck -ff /dev/vg01/lvol0
e2fsck 1.41.12 (17-May-2010)
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/vg01/lvol0: 11/90112 files (0.0% non-contiguous), 14242/338944 blocks 

[root@rhel6-client1 ~]# resize2fs /dev/vg01/lvol0 700M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lvol0 to 179200 (4k) blocks.
The filesystem on /dev/vg01/lvol0 is now 179200 blocks long.

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a----   1.29g

REDUCING LOGICAL VOLUME GROUP:

[root@rhel6-client1 ~]# lvreduce -L -500M /dev/vg01/lvol0
  WARNING: Reducing active logical volume to 824.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 824.00 MiB
  Logical volume lvol0 successfully resized

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a---- 824.00m
[root@rhel6-client1 ~]#

I did wrong, I wanted to reduce it to 700MB. What I supposed to do is to cut 600MB from 1.29G to get 700MB.

[root@rhel6-client1 ~]# lvreduce -L -100M /dev/vg01/lvol0
  WARNING: Reducing active logical volume to 724.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 724.00 MiB
  Logical volume lvol0 successfully resized
You have new mail in /var/spool/mail/root

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a---- 724.00m

Or we can also go with PE size by –l,

[root@rhel6-client1 ~]# lvextend -L +600M /dev/vg01/lvol0
  Extending logical volume lvol0 to 1.29 GiB
  Logical volume lvol0 successfully resized

[root@rhel6-client1 ~]# resize2fs /dev/vg01/lvol0
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lvol0 to 338944 (4k) blocks.
The filesystem on /dev/vg01/lvol0 is now 338944 blocks long.

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a----   1.29g

Now we need to reduce the FS size to 700MB.

RESIZING FILE SYSTEM:

[root@rhel6-client1 ~]# resize2fs /dev/vg01/lvol0 700M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lvol0 to 179200 (4k) blocks.
The filesystem on /dev/vg01/lvol0 is now 179200 blocks long.

[root@rhel6-client1 ~]# e2fsck /dev/vg01/lvol0
e2fsck 1.41.12 (17-May-2010)
/dev/vg01/lvol0: clean, 11/49152 files, 11542/179200 blocks

[root@rhel6-client1 ~]# vgdisplay /dev/vg01 |grep -i "pe size"
  PE Size               4.00 MiB

We want to make a FS of 700MB size.

[root@rhel6-client1 ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1.29*1024
1320.96 => total size in MB
1320-600 => subtracting 600Mb to get 700MB of FS size
720 => got the size
600/4 => PE size is of 4MB, dividing 600MB with 4MB to get exact PE size
150 => this is the PE size we need to reduce from total

[root@rhel6-client1 ~]# lvreduce -l 150 /dev/vg01/lvol0
  WARNING: Reducing active logical volume to 600.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 600.00 MiB
  Logical volume lvol0 successfully resized

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a---- 600.00m

OOOPPPPsssssss…. Again did mistake. Instead of reducing I marked it for grand total 600MB.

I supposed to minus the PE size from total space. Let’s do it again.

[root@rhel6-client1 ~]# lvextend -L +700M /dev/vg01/lvol0
  Extending logical volume lvol0 to 1.27 GiB
  Logical volume lvol0 successfully resized

[root@rhel6-client1 ~]# lvreduce -l -150 /dev/vg01/lvol0
  WARNING: Reducing active logical volume to 700.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 700.00 MiB
  Logical volume lvol0 successfully resized

[root@rhel6-client1 ~]# lvs |grep lvol0
  lvol0         vg01   -wi-a---- 700.00m
[root@rhel6-client1 ~]#

Now Good.

[root@rhel6-client1 ~]# resize2fs /dev/vg01/lvol0
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 179200 blocks long.  Nothing to do!

We need to take care of “-“ or “no sign”.
-   Means reduce it from whatever it is now.
No sign means make it of whatever size I had given.

Now I want to reduce the volume group “vg01” and get my /dev/sdd1 back.

Let’s check the status first,

[root@rhel6-client1 ~]# pvs
  PV         VG     Fmt  Attr PSize PFree
  /dev/sdb1  vg01   lvm2 a--  5.00g 324.00m
  /dev/sdb2  vg01   lvm2 a--  3.00g      0
  /dev/sdb3  vg02   lvm2 a--  2.00g      0
  /dev/sdc1  myvg01 lvm2 a--  5.00g   4.55g
  /dev/sdc2  myvg01 lvm2 a--  2.00g   1.75g
  /dev/sdc3  myvg01 lvm2 a--  3.00g   2.89g
  /dev/sdd1  vg01   lvm2 a--  2.00g   2.00g ç its free

REDUCING VOLUME GROUP:

[root@rhel6-client1 ~]# vgreduce vg01 /dev/sdd1
  Removed "/dev/sdd1" from volume group "vg01"

[root@rhel6-client1 ~]# pvs
  PV         VG     Fmt  Attr PSize PFree
  /dev/sdb1  vg01   lvm2 a--  5.00g 324.00m
  /dev/sdb2  vg01   lvm2 a--  3.00g      0
  /dev/sdb3  vg02   lvm2 a--  2.00g      0
  /dev/sdc1  myvg01 lvm2 a--  5.00g   4.55g
  /dev/sdc2  myvg01 lvm2 a--  2.00g   1.75g
  /dev/sdc3  myvg01 lvm2 a--  3.00g   2.89g
  /dev/sdd1         lvm2 a--  2.00g   2.00g



No comments:

Post a Comment