Monday, March 21, 2011

Expand virtual disk with Linux LVM

This article presents the way to expand the linux LVM when the virtual disk has to be expanded.



Current filesystem layout is as below :

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvroot 993M 207M 746M 22% /
/dev/sda1 99M 11M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/rootvg-lvhome 484M 33M 432M 8% /home
/dev/mapper/rootvg-lvtmp 289M 17M 260M 7% /tmp
/dev/mapper/rootvg-lvusr 1.8G 1.5G 166M 91% /usr
/dev/mapper/rootvg-lvvar 993M 65M 887M 7% /var

[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvhome rootvg -wi-ao 512.00M
lvroot rootvg -wi-ao 1.00G
lvswap rootvg -wi-ao 1.00G
lvtmp rootvg -wi-ao 320.00M
lvusr rootvg -wi-ao 1.78G
lvvar rootvg -wi-ao 1.00G

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rootvg lvm2 a- 5.88G 288.00M

[root@localhost ~]# fdisk -l

Disk /dev/sda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 13 104391 83 Linux
/dev/sda2 14 783 6185025 8e Linux LVM

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name rootvg
PV Size 5.90 GB / not usable 23.88 MB
Allocatable yes
PE Size (KByte) 32768
Total PE 188
Free PE 9
Allocated PE 179

PV UUID 2Ub0Jj-x8Ug-Jnkm-FWmx-5WDL-yNwr-tHSvDH


Now expand the current virtual disk to the new size :



New disk layout as seen below :

[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 13 104391 83 Linux
/dev/sda2 14 783 6185025 8e Linux LVM

To change the PV to use the rest of the disk :

#fdisk /dev/sda

then type "p"

then type "d" to delete and select "2" partition number

then type "n" to create a new partition

choose "primary" and "2" as before

(Note: do not change the begin cylinder, else the data will be destroyed. Specify the end cylinder or use default to extend to full disk)

then type "t" to change the partition type and type "2" to select partition 2

type "8e" for LVM as the partition type

type "w" and quit from fdisk.

Reboot the system to see the changes.

After reboot, notice the new LVM partition layout :

[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 13 104391 83 Linux
/dev/sda2 14 1305 10377990 8e Linux LVM

The previous PV size is as below :

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rootvg lvm2 a- 5.88G 288.00M

Change the PV size to new size with following command :

[root@localhost ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Notice the new PV free size is 4 GB extra compared to before.

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rootvg lvm2 a- 9.88G 4.28G

Now change the filesystems to use the new size :

[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvhome rootvg -wi-ao 512.00M
lvroot rootvg -wi-ao 1.00G
lvswap rootvg -wi-ao 1.00G
lvtmp rootvg -wi-ao 320.00M
lvusr rootvg -wi-ao 1.78G
lvvar rootvg -wi-ao 1.00G

[root@localhost ~]# lvresize --size 2G /dev/rootvg/lvroot
Extending logical volume lvroot to 2.00 GB
Logical volume lvroot successfully resized

[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvhome rootvg -wi-ao 512.00M
lvroot rootvg -wi-ao 2.00G
lvswap rootvg -wi-ao 1.00G
lvtmp rootvg -wi-ao 320.00M
lvusr rootvg -wi-ao 1.78G
lvvar rootvg -wi-ao 1.00G

Notice the change in the LV size for "lvroot"

Now change the filesystem to reflect the changes :

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvroot
993M 207M 746M 22% /
/dev/sda1 99M 11M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/rootvg-lvhome
484M 33M 432M 8% /home
/dev/mapper/rootvg-lvtmp
289M 17M 260M 7% /tmp
/dev/mapper/rootvg-lvusr
1.8G 1.5G 166M 91% /usr
/dev/mapper/rootvg-lvvar
993M 66M 887M 7% /var

[root@localhost ~]# resize2fs /dev/rootvg/lvroot 2G
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/rootvg/lvroot is mounted on /; on-line resizing required
Performing an on-line resize of /dev/rootvg/lvroot to 524288 (4k) blocks.
The filesystem on /dev/rootvg/lvroot is now 524288 blocks long.

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvroot
2.0G 207M 1.7G 11% /
/dev/sda1 99M 11M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/rootvg-lvhome
484M 33M 432M 8% /home
/dev/mapper/rootvg-lvtmp
289M 17M 260M 7% /tmp
/dev/mapper/rootvg-lvusr
1.8G 1.5G 166M 91% /usr
/dev/mapper/rootvg-lvvar
993M 66M 887M 7% /var

Notice the change in available size for "lvroot" filesystem.

Likewise follow the same procedure to expand the rest of the filesystems to make use of the free space available. I did it in the following way.

After resizing all filesystems, my new disk layout is as follows :

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rootvg lvm2 a- 9.88G 0

Notice the new PFree is "0" because I have allocated all the free space ot filesystems seen below.

[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvhome rootvg -wi-ao 1.56G
lvroot rootvg -wi-ao 2.00G
lvswap rootvg -wi-ao 1.00G
lvtmp rootvg -wi-ao 320.00M
lvusr rootvg -wi-ao 3.00G
lvvar rootvg -wi-ao 2.00G

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvroot
2.0G 208M 1.7G 11% /
/dev/sda1 99M 11M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/rootvg-lvhome
1.6G 34M 1.5G 3% /home
/dev/mapper/rootvg-lvtmp
289M 17M 260M 7% /tmp
/dev/mapper/rootvg-lvusr
3.0G 1.5G 1.4G 53% /usr
/dev/mapper/rootvg-lvvar
2.0G 66M 1.8G 4% /var

Happy expanding the disk size in a virtual environment :)

Do see my post on how to shrink the LVM disk in VMware.

Shrinking VMware virtual disks with Linux OS and LVM partitioned

Here I present a method to shrink the virtual disk with Redhat Linux OS installed and the filesystems part of LVM.

Current disk information and partition layout is as follows :






[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 13 104391 83 Linux
/dev/sda2 14 1305 10377990 8e Linux LVM

[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvhome rootvg -wi-ao 1.56G
lvroot rootvg -wi-ao 2.00G
lvswap rootvg -wi-ao 1.00G
lvtmp rootvg -wi-ao 320.00M
lvusr rootvg -wi-ao 3.00G
lvvar rootvg -wi-ao 2.00G

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rootvg lvm2 a- 9.88G 0

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvroot
2.0G 217M 1.7G 12% /
/dev/sda1 99M 11M 83M 12% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/rootvg-lvhome
1.6G 34M 1.5G 3% /home
/dev/mapper/rootvg-lvtmp
289M 17M 260M 7% /tmp
/dev/mapper/rootvg-lvusr
3.0G 1.6G 1.3G 55% /usr
/dev/mapper/rootvg-lvvar
2.0G 68M 1.8G 4% /var

Now we see some free space left on /, /usr, /var, /home filesystems.

Next, shutdown the server and add a harddisk of smaller size.

(Note : the smaller hard disk should be able to fit the used space of the original disk)




In rescue mode, verify the filesystems mounted :
















Run filesystem check on the filesystems :

#e2fsck -f /dev/rootvg/lvroot
#e2fsck -f /dev/rootvg/lvhome
#e2fsck -f /dev/rootvg/lvusr
#e2fsck -f /dev/rootvg/lvvar






























Reboot the server after making the changes.




































Reboot the server.