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 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

DONE!!

Here are the commands and the actual screen output in detail

Again, specific to my example of growing the single disk in the chassis on the VM side, commands will be similar if you are adding disks via SAN or in the chassis or even adding a VM disk

View current disk with df -h


[root@cents63-64bit ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cents6364bit-lv_root
7.5G 4.5G 2.7G 64% /
tmpfs 499M 112K 499M 1% /dev/shm
/dev/sda1 485M 49M 411M 11% /boot
[root@cents63-64bit ~]#

View current disk geometry with fdisk -l


[root@cents63-64bit ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

[root@cents63-64bit ~]#

Poweroff so we can present/view the new disks or new disk geometry


[root@cents63-64bit ~]# poweroff

On the VM side, this is nothing more than just going into the “Edit Settings” and changing the disk size to something larger. In this case I changed it to 20gig from 10gig.

Power it on now.

Use fdisk -l to see disk geometry


[root@cents63-64bit ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

[root@cents63-64bit ~]#

First thing to do is to create a new partition /dev/sda3. If you are adding new disks, you’d create a new /dev/sdb1 or /dev/sdc1, etc.

Create the partition using fdisk command


[root@cents63-64bit ~]# fdisk /dev/sda

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): 3
First cylinder (1-20480, default 1): 10241
Last cylinder, +cylinders or +size{K,M,G} (10241-20480, default 20480):
Using default value 20480

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@cents63-64bit ~]#

Reboot server and do a fdisk -l to see changes


[root@cents63-64bit ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/sda3 10241 20480 10485760 83 Linux

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000

[root@cents63-64bit ~]#

pvdisplay to see current PVs


[root@cents63-64bit ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_cents6364bit
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID SfGdvH-Lb0a-Kmrl-Uo9J-66aG-80T8-OsfPqK

[root@cents63-64bit ~]#

pvcreate to add the new partition as a PV for use in the LVM


[root@cents63-64bit ~]# pvcreate /dev/sda3
Writing physical volume data to disk "/dev/sda3"
Physical volume "/dev/sda3" successfully created
[root@cents63-64bit ~]#

Use pvdisplay to see your new PVs in the PV list


[root@cents63-64bit ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_cents6364bit
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID SfGdvH-Lb0a-Kmrl-Uo9J-66aG-80T8-OsfPqK

"/dev/sda3" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID HLtl0o-UtMC-kWwp-zcJW-oG2e-Aj5T-Km5nF8

[root@cents63-64bit ~]#

Use vgdisplay to find the VG name you want to add the new PV to


[root@cents63-64bit ~]# vgdisplay
--- Volume group ---
VG Name vg_cents6364bit
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 9.51 GiB
PE Size 4.00 MiB
Total PE 2434
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 0 / 0
VG UUID WxnNuc-rHwk-NBVF-10kX-xgoC-u5Ns-DNXhJz
[root@cents63-64bit ~]#

Use vgextend to add the PV to it


[root@cents63-64bit ~]# vgextend vg_cents6364bit /dev/sda3
Volume group "vg_cents6364bit" successfully extended
[root@cents63-64bit ~]#

Use vgdisplay to see the new size of the VG. Note the TOTAL PE and FREE PE have increased


[root@cents63-64bit ~]# vgdisplay
--- Volume group ---
VG Name vg_cents6364bit
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 19.50 GiB
PE Size 4.00 MiB
Total PE 4993
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 2559 / 10.00 GiB
VG UUID WxnNuc-rHwk-NBVF-10kX-xgoC-u5Ns-DNXhJz
[root@cents63-64bit ~]#

Now find the LV to extend. Use lvdisplay to get an LV list


[root@cents63-64bit ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_root
LV Name lv_root
VG Name vg_cents6364bit
LV UUID oLUr3z-9omt-bX4S-APi3-Veov-2rKr-XymbzO
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:32 -0700
LV Status available
# open 1
LV Size 7.54 GiB
Current LE 1930
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_swap
LV Name lv_swap
VG Name vg_cents6364bit
LV UUID sbDqbF-RGPn-PdaZ-BBRd-JHTb-qnh9-X2rf03
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:45 -0700
LV Status available
# open 1
LV Size 1.97 GiB
Current LE 504
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@cents63-64bit ~]#

Grow the LV using lvextend. For arguments you need the number of PE’s to grow the LV by and the LV name itself


[root@cents63-64bit ~]# lvextend -l +2559 /dev/vg_cents6364bit/lv_root
Extending logical volume lv_root to 17.54 GiB
Logical volume lv_root successfully resized
[root@cents63-64bit ~]#

lvdisplay command shows the new size


[root@cents63-64bit ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_root
LV Name lv_root
VG Name vg_cents6364bit
LV UUID oLUr3z-9omt-bX4S-APi3-Veov-2rKr-XymbzO
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:32 -0700
LV Status available
# open 1
LV Size 17.54 GiB
Current LE 4489
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_swap
LV Name lv_swap
VG Name vg_cents6364bit
LV UUID sbDqbF-RGPn-PdaZ-BBRd-JHTb-qnh9-X2rf03
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:45 -0700
LV Status available
# open 1
LV Size 1.97 GiB
Current LE 504
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@cents63-64bit ~]#

Use the resize2fs to grow the filesystem itself.

Some OS may not be able to do it while the filesystem is mounted which means you’d need to boot into a failsafe or rescue mode. With Centos 6.3 it was able to do it on-the-fly while mounted


[root@cents63-64bit ~]# resize2fs /dev/vg_cents6364bit/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_cents6364bit/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_cents6364bit/lv_root to 4596736 (4k) blocks.
The filesystem on /dev/vg_cents6364bit/lv_root is now 4596736 blocks long.

Use df command to see the new filesystem size


[root@cents63-64bit ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cents6364bit-lv_root
18G 4.5G 12G 28% /
tmpfs 499M 272K 499M 1% /dev/shm
/dev/sda1 485M 49M 411M 11% /boot
[root@cents63-64bit ~]#

DONE!

Good luck and have fun!

, , Linux, Unix

37 thoughts on “Extending A Linux Disk With LVM – Extending Root Partition

  1. U 10.04 using webmin it’s confusing, drive 1 is default install LVM, now I want to add a drive and extend drive 1′s VLM with drive 2 giving me 1 bigger LVM 1+2=1, after that I want to add another drive and have it separate then incremental backup /home or whatever to that drive twice a day, so my plan is play with webmin some more if that doesn’t work I’ll try this if this doesn’t work then I guess I’ll jest do a hardware raid 5 and externally backup, HUmmm

    • Hi Ken. Is webmin a GUI? I haven’t used GUIs for this before except with Veritas which was somewhat straightforward though I eventually went to command line (and when you call Veritas support they usually do command line also).

      You should be able to do the above to expand your first LVM.

      You can definitely then add a second drive and create a new VG then LV no problem also, probably mount it as something like /backup. You would use pvcreate the same way, and then instead of vgextend, use vgcreate to create a new VG.

      So to summarize

      Adding:
      pvcreate (so the disk can be used with LVM)
      vgextend

      New
      pvcreate
      vgcreate

      Feel free to post back with specific questions and I can help you out.

  2. Hi Ben, I wanted to thank you for your help I found it very useful. I jest switched over from an all Windows environment Win 2008r2 servers, Win7 desktops to an all Ubuntu 12.04.2 LTS servers, OpenSuse 13.3 desktops.
    This is a link to that “webmin” thing I was taking about http://www.webmin.com/ I heard of it years ago and seen it was still around so I thought I’s give it a try maybe it’s “stable” so far it’s ok, I don’t know if I trust it 100% though I’d really rather not use it at all but I found it helpful here and there so.
    I also found this free Google ebook that was helpful here is a link to that, http://books.google.com/books?id=OWxsVjef1iIC&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false
    Anyway what got me stuck was I think “fdisk” it would actually simulate what you were doing to the drive until you were finished with the changes then it gave you the option to commit or “m” make changes this I did not know, I was doing changes to the drive and checking the physical drive in another terminal after each step in the process and not seeing any changes done that’s what got me.
    Thanks again…

    • Ahhh yes that’s a tricky one. You have to “w” to write the changes you made before you exit. If you read through my transcript above you’ll see that I do that.

      The “m” is a menu for help. The fdisk menu is quite detailed and if you take time to look through it you should get a good feel for what’s available.

      Thanks for the link to the free ebook!

      By the way, redhat/centos has a lot of really good free ebooks on linux system administration. While some of it is redhat/centos specific, a lot of the commands are the same across linux distributions.

      (I have only used Ubuntu sparingly and it definitely is quite different than most of the linux distros)

  3. Addendum, something I’ve been using lately. An option to just use all remaining space available

    lvextend -l +100%FREE [lvname]

    Go forth and extend!

  4. Sometimes I find it necessary/helpful to use partprobe and cfdisk to extend space with the storage with the system live. Especially in cases where I want to extend an existing disk using pvresize followed by lvextend without having to create a new partition and add it in to the volume group.

    • So here’s an interesting question: what is the advantage of extending a physical partition over adding an additional one? Just that you may run out of partitions?

      Thanks Robert B. for the comment!

  5. This is one of the best blog posts I’ve read. Thanks so much for the exact step by step. I ran through exactly what you have shown and it worked perfectly the first time. Thanks again!

  6. Ups… you didn’t change id partition type on fdisk command to ’8e’ Linux LVM. Is this a fail? Actually /dev/sda3 has ‘Linux’ type. Could I change this with fdisk directly?

    • Good point, and it is something I should have done. It actually will work without it. However, it is good practice to change the type to Linux LVM (type 8e) if at the very least for bookeeping reasons. Thanks for pointing it out and I’ll modify the post to include it.

  7. Thanks!!! This worked like a charm to extend my filled /root partition… You saved me a lot of time and troubles! Thanks! All the best for 2014!

Leave a Comment

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

Spam protection by WP Captcha-Free