Extend, Expand or Resize an LVM root partition ‘/’ in linux
If your linux root partition ‘/’ is an LVM, you could easily extend the drive. Make use of the commands listed later in this post.
First let me explain how an LVM is setup.
Physical Volumes | These are real physical hard drives or partitions attached to the machine.Favorite commands: pvdisplay, pvcreate, pvresize |
Volume Groups | Several physical volumes are combined to form a volume group. Multiple Volume groups are also possible. Additional PVs are added to a VG to expand it anytime online.Favorite commands: vgdisplay, vgextend, vgcreate |
Logical Volumes | Logical Volumes are carved out of a Volume Group. Logical Volumes could be expanded anytime if its VG has enough free space availableFavorite commands: lvdisplay, lvextend, lvcreate |
In short, several physical volumes(PV) are first created. Volume groups(VG) are then built on top of multiple PVs. Logical Volumes(LV) are then carved out of any VG until the VGs run out of storage. Adding more PVs to a VG would help increase the storage in the VG.
Now lets get to work. Continue reading.
Follow below steps to get your LVM extended.
To get to know free space on each drive and the filesystem names, execute “df -h”.
administrator@ubuntu:~$ df -h Filesystem Size Used Avail Use{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} Mounted on /dev/mapper/ubuntu--vg-root 9.0G 6.8G 1.8G 80{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} / none 4.0K 0 4.0K 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /sys/fs/cgroup udev 235M 4.0K 235M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /dev tmpfs 50M 284K 49M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run none 5.0M 0 5.0M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/lock none 246M 0 246M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/shm none 100M 0 100M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/user /dev/mapper/sher_vg-sher_lv_1 16G 13G 2.8G 82{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /home/administrator/datacenter /dev/sda1 236M 47M 178M 21{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /boot
From above output, “/” mount point file system has 1.8G Available space, 9.0 G Size, 80{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} used and filesystem name is /dev/mapper/ubuntu–vg-root
Now that we have the filesystem name. Lets see the “lvscan” to view all LVMs.
administrator@ubuntu:~/datacenter$ sudo lvscan ACTIVE '/dev/sher_vg/sher_lv_1' [15.99 GiB] inherit ACTIVE '/dev/ubuntu-vg/root' [9.26 GiB] inherit ACTIVE '/dev/ubuntu-vg/swap_1' [508.00 MiB] inherit
Oops. The lvm names doesn’t match with those in “df” command output.
Here is how you get to know the right LVM.
First lets try the command with /dev/sda1 filesystem
administrator@ubuntu:~$ sudo lvdisplay /dev/sda1 Volume group "sda1" not found Skipping volume group sda1
/dev/sda1 is not an LVM and we got the message as expected. Now lets try with /dev/mapper/ubuntu–vg-root
administrator@ubuntu:~$ sudo lvdisplay /dev/mapper/ubuntu--vg-root --- Logical volume --- LV Path /dev/ubuntu-vg/root LV Name root VG Name ubuntu-vg LV UUID 7HkCVg-CfZl-rM07-IqT3-tja7-ev0D-xbcqhs LV Write Access read/write LV Creation host, time ubuntu, 2014-06-23 12:45:57 +0530 LV Status available # open 1 LV Size 9.26 GiB Current LE 2371 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1
Yeah. It’s an LVM named “/dev/ubuntu-vg/root”. And the LV belong to VG named “ubuntu-vg”. Lets see the vgdisplay.
administrator@ubuntu:~$ sudo vgdisplay ubuntu-vg --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 9 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 15.75 GiB PE Size 4.00 MiB Total PE 4033 Alloc PE / Size 2498 / 9.76 GiB Free PE / Size 1535 / 6.00 GiB VG UUID tqn9JX-VIlv-O7Mj-Hpn9-PTps-V2An-3j3Wm0
From this vgdisplay output, (Free PE / Size: 1535 / 6.00 GiB) we have enough freespace in the VG. If there was no space available in the VG, follow below steps. Else you could skip a few.
Have a look at all the PVs we have with command “pvscan”
administrator@ubuntu:~$ sudo pvscan [sudo] password for administrator: PV /dev/sdb VG sher_vg lvm2 [8.00 GiB / 0 free] PV /dev/sdc VG sher_vg lvm2 [8.00 GiB / 0 free] PV /dev/sda5 VG ubuntu-vg lvm2 [7.76 GiB / 0 free] PV /dev/sdd VG ubuntu-vg lvm2 [8.00 GiB / 5.00 GiB free] Total: 4 [31.75 GiB] / in use: 4 [31.75 GiB] / in no VG: 0 [0 ]
We have 4 Physical Volumes as listed above.
administrator@ubuntu:~$ cat /proc/partitions major minor #blocks name 11 0 63252 sr0 8 0 8388608 sda 8 1 248832 sda1 8 2 1 sda2 8 5 8136704 sda5 8 16 8388608 sdb 8 32 8388608 sdc 8 48 8388608 sdd 252 0 16769024 dm-0 252 1 9711616 dm-1 252 2 520192 dm-2
Before attaching new hard drive to the box, note that “sdd” is the last one in the above list that starts with letter ‘s’. When you add a new hard drive, It would be shown next to it hopefully as “sde”. Now shutdown your machine. Attach the hard drive and turn it back on. Execute the same command again.
administrator@ubuntu:~$ cat /proc/partitions major minor #blocks name 11 0 63252 sr0 8 0 8388608 sda 8 1 248832 sda1 8 2 1 sda2 8 5 8136704 sda5 8 16 8388608 sdb 8 32 8388608 sdc 8 48 8388608 sdd 8 64 8388608 sde 252 0 16769024 dm-0 252 1 9711616 dm-1 252 2 520192 dm-2
Yes. Got the same output as expected. The new drive has appeared there as “sde”. Now lets create a partition table in the new disk.
administrator@ubuntu:~$ sudo fdisk /dev/sde Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xb7fa5d8b. 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):
fdisk says the disk does not have a valid partition table. Don’t worry. Simply type ‘w’ and hit enter.
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
A valid partition table is now written to the new disk. This step could be skipped. I have added PVs and used them without this step in my past without any issues. I am explaining this here to avoid improper/lengthy/confusing output for “sudo fdisk -l” command afterwards.
Next, the disk “sde” has to be converted to a Physical Volume(PV).
administrator@ubuntu:~$ sudo pvcreate /dev/sde Physical volume "/dev/sde" successfully created
To verify, execute “pvscan” to view the new PV.
administrator@ubuntu:~$ sudo pvscan PV /dev/sdb VG sher_vg lvm2 [8.00 GiB / 0 free] PV /dev/sdc VG sher_vg lvm2 [8.00 GiB / 0 free] PV /dev/sda5 VG ubuntu-vg lvm2 [7.76 GiB / 0 free] PV /dev/sdd VG ubuntu-vg lvm2 [8.00 GiB / 5.00 GiB free] PV /dev/sde lvm2 [8.00 GiB] Total: 5 [39.75 GiB] / in use: 4 [31.75 GiB] / in no VG: 1 [8.00 GiB]
Add the newly created PV to VG ubuntu-vg
administrator@ubuntu:~$ sudo vgextend ubuntu-vg /dev/sde Volume group "ubuntu-vg" successfully extended
VG is now extended successfully over the new PV. Lets see vgdisplay below.
administrator@ubuntu:~$ sudo vgdisplay ubuntu-vg --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 16 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 3 Act PV 3 VG Size 23.75 GiB PE Size 4.00 MiB Total PE 6080 Alloc PE / Size 2498 / 9.76 GiB Free PE / Size 3582 / 13.99 GiB VG UUID tqn9JX-VIlv-O7Mj-Hpn9-PTps-V2An-3j3Wm0
Wow. Now we have ample space in ubuntu-vg. Free PE/Size = 3582/13.99 GiB and Cur PV = 3.
Lets now continue with the steps to extend the LVM “/dev/ubuntu-vg/root” mounted on “/” as filesystem “/dev/mapper/ubuntu–vg-root”
administrator@ubuntu:~$ sudo lvextend -L +1G /dev/ubuntu-vg/root Extending logical volume root to 10.26 GiB Logical volume root successfully resized
/dev/ubuntu-vg/root LV is now resized to 10.26 GB. Now verify the lvdisplay output.
administrator@ubuntu:~$ sudo lvdisplay /dev/ubuntu-vg/root --- Logical volume --- LV Path /dev/ubuntu-vg/root LV Name root VG Name ubuntu-vg LV UUID 7HkCVg-CfZl-rM07-IqT3-tja7-ev0D-xbcqhs LV Write Access read/write LV Creation host, time ubuntu, 2014-06-23 12:45:57 +0530 LV Status available # open 1 LV Size 10.26 GiB Current LE 2627 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1
Good. We have additional 1 GB available in LVM. That means the LV is extended successfully. But wait. Lets see if the space is now usable by the Operating system.
administrator@ubuntu:~$ df -h Filesystem Size Used Avail Use{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} Mounted on /dev/mapper/ubuntu--vg-root 9.0G 6.8G 1.8G 80{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} / none 4.0K 0 4.0K 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /sys/fs/cgroup udev 235M 4.0K 235M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /dev tmpfs 50M 292K 49M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run none 5.0M 0 5.0M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/lock none 246M 0 246M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/shm none 100M 0 100M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/user /dev/sda1 236M 47M 178M 21{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /boot /dev/mapper/sher_vg-sher_lv_1 16G 13G 2.8G 82{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /home/administrator/datacenter
Oops. Its not usable yet. “df -h” output still shows only 9.0G for the LVM “/” partition. “resize2fs” command is gonna help us now.
administrator@ubuntu:~$ sudo resize2fs /dev/ubuntu-vg/root resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/ubuntu-vg/root is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/ubuntu-vg/root is now 2690048 blocks long.
Cool. The space should now be usage. Lets have a look at “df -h” once again.
administrator@ubuntu:~$ df -h Filesystem Size Used Avail Use{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} Mounted on /dev/mapper/ubuntu--vg-root 10G 6.8G 2.7G 72{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} / none 4.0K 0 4.0K 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /sys/fs/cgroup udev 235M 4.0K 235M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /dev tmpfs 50M 292K 49M 1{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run none 5.0M 0 5.0M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/lock none 246M 0 246M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/shm none 100M 0 100M 0{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /run/user /dev/sda1 236M 47M 178M 21{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /boot /dev/mapper/sher_vg-sher_lv_1 16G 13G 2.8G 82{88dc7f9385b2c8971f38421926506de3e3fc5e843c9520f0674d4b7de6ae62da} /home/administrator/datacenter
Yes. The LVM “/” partition is now with size 10G.
Job Well Done !!! 😀
Now suppose by mistake you accidentally did too much “lvextend” before “resize2fs”, you could reduce it back with “lvreduce”. CAUTION! there are chances of data loss. Hence be ready with all necessary backups before proceeding. Also this becomes impossible once “resize2fs” command is executed.
administrator@ubuntu:~$ sudo lvreduce -L -1G /dev/ubuntu-vg/root WARNING: Reducing active and open logical volume to 9.26 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce root? [y/n]: y Reducing logical volume root to 9.26 GiB Logical volume root successfully resized