Extend and Reduce existing logical volumes

For this exercise, we going to extend the LV root from 5.29 GB to 6.29 GB:

 LV   VG    Attr       LSize
root myvg1 -wi-ao---- 5.29g

First check what we have: pvs, vgs, lvs and free space with parted command:

# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 4.00m
/dev/vdb1 vg1 lvm2 a-- 248.00m 68.00m
/dev/vdb2 vg1 lvm2 a-- 248.00m 248.00m
/dev/vdb3 vg2 lvm2 a-- 240.00m 0
/dev/vdb5 vg2 lvm2 a-- 240.00m 48.00m

[root@cliente1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root myvg1 -wi-ao---- 5.29g
swap myvg1 -wi-ao---- 716.00m
lv01 vg1 -wi-ao---- 100.00m
lv02 vg1 -wi-ao---- 80.00m
lv03 vg2 -wi-ao---- 320.00m
lv04 vg2 -wi-ao---- 112.00m

[root@cliente1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 1 2 0 wz--n- 6.00g 4.00m
vg1 2 2 0 wz--n- 496.00m 316.00m
vg2 2 2 0 wz--n- 480.00m 48.00m

[root@cliente1 ~]# parted /dev/vdb unit GB print free
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2.25GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
0.00GB 0.00GB 0.00GB Free Space
1 0.00GB 0.26GB 0.26GB primary lvm
2 0.26GB 0.53GB 0.26GB primary lvm
3 0.53GB 0.79GB 0.26GB primary lvm
4 0.79GB 2.25GB 1.47GB extended
0.79GB 0.79GB 0.00GB Free Space
5 0.79GB 1.05GB 0.26GB logical lvm
1.05GB 2.25GB 1.20GB Free Space

Now we will create a new partition with the free space:

[root@cliente1 ~]# fdisk /dev/vdb
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
All primary partitions are in use
Adding logical partition 6
First sector (2054144-4404019, default 2054144):
Using default value 2054144
Last sector, +sectors or +size{K,M,G} (2054144-4404019, default 4404019):
Using default value 4404019
Partition 6 of type Linux and of size 1.1 GiB is set
Command (m for help): t
Partition number (1-6, default 6):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 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: 0x3c9dc864
Device Boot Start End Blocks Id System
/dev/vdb1 2048 514047 256000 8e Linux LVM
/dev/vdb2 514048 1026047 256000 8e Linux LVM
/dev/vdb3 1026048 1538047 256000 8e Linux LVM
/dev/vdb4 1538048 4404019 1432986 5 Extended
/dev/vdb5 1540096 2052095 256000 8e Linux LVM
/dev/vdb6 2054144 4404019 1174938 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@cliente1 ~]# partprobe -s
/dev/vda: msdos partitions 1 2
/dev/vdb: msdos partitions 1 2 3 4 <5 6>

Extend the volume group

[root@cliente1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root myvg1 -wi-ao---- 5.29g
swap myvg1 -wi-ao---- 716.00m
lv01 vg1 -wi-ao---- 100.00m
lv02 vg1 -wi-ao---- 80.00m
lv03 vg2 -wi-ao---- 320.00m
lv04 vg2 -wi-ao---- 112.00m

[root@cliente1 ~]# vgextend myvg1 /dev/vdb6
Physical volume "/dev/vdb6" successfully created.
Volume group "myvg1" successfully extended

[root@cliente1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 2 2 0 wz--n- 7.11g 1.12g <-- new vol.size and free space
vg1 2 2 0 wz--n- 496.00m 316.00m
vg2 2 2 0 wz--n- 480.00m 48.00m

Extend the logical volume root

[root@cliente1 ~]# lvextend /dev/myvg1/root -L +1G -r -t
TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
Size of logical volume myvg1/root changed from 5.29 GiB (1355 extents) to 6.29 GiB (1611 extents).
Logical volume myvg1/root successfully resized.

[root@cliente1 ~]# lvextend /dev/myvg1/root -L +1G -r
Size of logical volume myvg1/root changed from 5.29 GiB (1355 extents) to 6.29 GiB (1611 extents).
Logical volume myvg1/root successfully resized.
meta-data=/dev/mapper/myvg1-root isize=512 agcount=4, agsize=346880 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1387520, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 1387520 to 1649664

[root@cliente1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root myvg1 -wi-ao---- 6.29g <-- new size
swap myvg1 -wi-ao---- 716.00m
lv01 vg1 -wi-ao---- 100.00m
lv02 vg1 -wi-ao---- 80.00m
lv03 vg2 -wi-ao---- 320.00m
lv04 vg2 -wi-ao---- 112.00m

Also we can check the new size with the df -h command:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/myvg1-root 6.3G 3.0G 3.4G 48% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 920M 152K 920M 1% /dev/shm
tmpfs 920M 9.0M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/vda1 1014M 173M 842M 17% /boot
/dev/mapper/vg2-lv03 302M 2.1M 280M 1% /mnt/lv03
/dev/mapper/vg2-lv04 105M 1.6M 96M 2% /mnt/lv04
/dev/mapper/vg1-lv02 74M 1.6M 67M 3% /mnt/lv02
/dev/mapper/vg1-lv01 93M 1.6M 85M 2% /mnt/lv01
tmpfs 184M 4.0K 184M 1% /run/user/42
tmpfs 184M 12K 184M 1% /run/user/1000
tmpfs 184M 0 184M 0% /run/user/0

Reduce the logical volume lv03

For this second exercise, we going to reduce the LV lv03 from 320 MB to 200 MB(approx):

LV   VG    Attr       LSize 
lv03 vg2 -wi-ao---- 320.00m

First check what we have :

[root@cliente1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 2 2 0 wz--n- 7.11g 124.00m
vg1 2 2 0 wz--n- 496.00m 316.00m
vg2 2 2 0 wz--n- 480.00m 48.00m

[root@cliente1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root myvg1 -wi-ao---- 6.29g  
swap myvg1 -wi-ao---- 716.00m
lv01 vg1 -wi-ao---- 100.00m
lv02 vg1 -wi-ao---- 80.00m
lv03 vg2 -wi-ao---- 320.00m
lv04 vg2 -wi-ao---- 112.00m

and reduce the volume:

[root@cliente1 ~]# lvreduce /dev/vg2/lv03 -L 200M -r -t
[root@cliente1 ~]# lvreduce /dev/vg2/lv03 -L 200M -r
Rounding size to boundary between physical extents: 208.00 MiB.
Do you want to unmount "/mnt/lv03"? [Y|n] y
fsck from util-linux 2.23.2
/dev/mapper/vg2-lv03: 11/81920 files (0.0% non-contiguous), 20607/327680 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg2-lv03 to 212992 (1k) blocks.
The filesystem on /dev/mapper/vg2-lv03 is now 212992 blocks long.
Size of logical volume vg2/lv03 changed from 320.00 MiB (20 extents) to 208.00 MiB (13 extents).
Logical volume vg2/lv03 successfully resized.

Note: If you use:
-L +200M –> It means that it will increase (or decrease if you use -200M) in 200MB the volume size.

-L 200M –> It means that the volume size will be 200MB.

 
[root@cliente1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 2 2 0 wz--n- 7.11g 124.00m
vg1 2 2 0 wz--n- 496.00m 316.00m
vg2 2 2 0 wz--n- 480.00m 160.00m <-- more free space

[root@cliente1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root myvg1 -wi-ao---- 6.29g
swap myvg1 -wi-ao---- 716.00m
lv01 vg1 -wi-ao---- 100.00m
lv02 vg1 -wi-ao---- 80.00m
lv03 vg2 -wi-ao---- 208.00m <-- new volume size
lv04 vg2 -wi-ao---- 112.00m
[root@cliente1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/myvg1-root 6.3G 3.0G 3.4G 48% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 920M 152K 920M 1% /dev/shm
tmpfs 920M 9.0M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/vda1 1014M 173M 842M 17% /boot
/dev/mapper/vg1-lv01 93M 1.6M 85M 2% /mnt/lv01
/dev/mapper/vg1-lv02 74M 1.6M 67M 3% /mnt/lv02
/dev/mapper/vg2-lv04 105M 1.6M 96M 2% /mnt/lv04
tmpfs 184M 4.0K 184M 1% /run/user/42
tmpfs 184M 12K 184M 1% /run/user/1000
tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg2-lv03 194M 1.8M 178M 1% /mnt/lv03

How to extend a logical volume to 100% of the available free space

For this example, the logical volume /vg1/lv01 and /vg2/lv03 will be increased to 100% of the available free space.

[root@cliente1 ~]# lvextend /dev/vg1/lv01 -l +100%FREE -r
Size of logical volume vg1/lv01 changed from 100.00 MiB (25 extents) to 416.00 MiB (104 extents).
Logical volume vg1/lv01 successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg1-lv01 is mounted on /mnt/lv01; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/mapper/vg1-lv01 is now 425984 blocks long.

[root@cliente1 ~]# lvextend /dev/vg2/lv03 -l +100%FREE -r
Size of logical volume vg2/lv03 changed from 208.00 MiB (13 extents) to 368.00 MiB (23 extents).
Logical volume vg2/lv03 successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg2-lv03 is mounted on /mnt/lv03; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/vg2-lv03 is now 376832 blocks long.

Remove a physical volume from a group:

For this example, we going to remove(move) the PV /dev/vdb3 and reduce the VG vg2

First check and choose the PV to remove:

[root@cliente1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 0
/dev/vdb1 vg1 lvm2 a-- 248.00m 0
/dev/vdb2 vg1 lvm2 a-- 248.00m 0
/dev/vdb3 vg2 lvm2 a-- 240.00m 160.00m <-- this PV
/dev/vdb5 vg2 lvm2 a-- 240.00m 192.00m
/dev/vdb6 myvg1 lvm2 a-- 1.12g 124.00m

Move the PE of the vdb3 :

Note: With pvmove command, the system moves these PEs to the next PV (vdb5 in this case).

[root@cliente1 ~]# pvmove /dev/vdb3
/dev/vdb3: Moved: 0.00%
/dev/vdb3: Moved: 100.00%

[root@cliente1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 0
/dev/vdb1 vg1 lvm2 a-- 248.00m 0
/dev/vdb2 vg1 lvm2 a-- 248.00m 0
/dev/vdb3 vg2 lvm2 a-- 240.00m 240.00m <-- new size free
/dev/vdb5 vg2 lvm2 a-- 240.00m 112.00m <-- less free space now
/dev/vdb6 myvg1 lvm2 a-- 1.12g 124.00m

Now we remove the PV vdb3 from the VG vg2 so that the size of the VG vg2 is updated:

[root@cliente1 ~]# vgreduce vg2 /dev/vdb3 
Removed "/dev/vdb3" from volume group "vg2"

[root@cliente1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 0
/dev/vdb1 vg1 lvm2 a-- 248.00m 0
/dev/vdb2 vg1 lvm2 a-- 248.00m 0
/dev/vdb3 lvm2 --- 250.00m 250.00m <-- vdb3 is not in any VG
/dev/vdb5 vg2 lvm2 a-- 240.00m 112.00m
/dev/vdb6 myvg1 lvm2 a-- 1.12g 124.00m

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *