Search This Blog

Tuesday 8 March 2016

Extending A Linux Disk With LVM – Extending Root Partition

A quick tutorial of something I did today. We use vmware here a lot and instead of installing an OS each time a VM is requested, we have operating systems installed on VM templates "ready-to-go" and then deploy the template to a VM per request.

As you can guess, the VM templates are "minimal" on resources (including disk space) so when we get a VM request, we often have to extend the root partition per the user requirement.

So here are my exact steps (no cutting anything out) using the LVM (Logical Volume Manager) commands to extend a root disk. OS is Centos 6.3 a.k.a. Redhat 6.3 on a VM.

NOTE: If you are not using a VM but a physical server and adding disks to the chassis or via a SAN, all the commands are the same, you'd just be using different disk devices like /dev/sdb, or /dev/sdc, and the like.

extending root partition using lvm on linux

A few terms

PV = Physical Volume. When you add a new disk or new partition, you need to designate it as a PV so it can be used by the LVM
VG = Volume Group. This is a group of PVs
LV = Logical Volume. This is an abstraction of disk space carved out from a Volume Group (VG) which the OS can then use just as it would a regular hard drive
PE = Physical Extents. Think of this like "blocks". When you do a "vgdisplay" you will see the PE size (mine was 4mb), the total number of PE's in the VG and the free PEs available. With some multiplication you would calculate out the total size of the VG and the total free space available on the VG.

Here are my exact linux commands in a nutshell:

# df -h
To see the current disk usage
# fdisk -l
To see the current partitioning
# poweroff
Turn the VM off so we can extend the disk on the VM side. For physical servers you would just be adding a new SAS/SATA disk to the chassis or adding a LUN on a SAN and presenting it which does *not* necessarily require a poweroff

Intermission to add new disk and power on if necessary

# fdisk -l
To see the new disk size on /dev/sda which we just extended, or to see new disks we just presented (/dev/sdb, /dev/sdc, etc.)
# fdisk /dev/sda
To create new partitions for the OS to use. These new partitions will be added to the VG so we can extend the LV that the "/" partition is on. My newly created partition in the example is /dev/sda3. For new disks you would use /dev/sdb, or /dev/sdc. If you are using an extended /dev/sda like in my case, you will need to reboot for the changes to be seen
# fdisk -l
To see the new partitions
# pvdisplay
View current physical volumes a.k.a. pv
# pvcreate /dev/sda3
Allow Linux OS to use the new partition in LVM
# pvdisplay
See the new pv /dev/sda3
# vgdisplay
View the current volume groups
# vgextend vg_cents6364bit /dev/sda3
Add the new PV /dev/sda3 to the existing VG vg_cents6364bit
# vgdisplay
Now you can see the new size of the VG vg_cents6364bit. Note the new amount of free PE's (physical extents)
# lvdisplay
View the current LV. In my example, /dev/vg_cents6364bit/lv_root which is the root partition
# lvextend -l +2559 /dev/vg_cents6364bit/lv_root
Now make the LV larger. Growing the LV /dev/vg_cents6364bit/lv_root by 2559 PEs
# lvdisplay
Now you can see the larger size of the LV
# resize2fs /dev/vg_cents6364bit/lv_root
Online resize of the actual filesystem now on that LV
# df -h
You can see the new size now using the "df" command

No comments:

Post a Comment