Add space to a LVM partition on virtual machine running linux operating system without restart

There are many solutions which are running on virtual machines no matter of what kind (vmware, kvm, xen etc). I will assum that the disk you add is a scsi disk. Ones you have added the disk to the running linux operating system on a virtual machine the new disk will not be always seen. A scan of the scsi devices is needed. You will need to run the following commands:

#echo "- - -" > /sys/class/scsi_host/host#/scan

where # will be a digit. You can find the digit you need by running an ls

#ls /sys/class/scsi_host/host?

After doing this you run fdisk -l to see the new partition. As example will be a new partition of 400G

fdisk -l output

After you have confirmed that the disk can be seen on the linux you will have to create a new partition on the new disk to add it to the LVM. This can be done by running fdisk /dev/sdX where x will be a letter specific to your new disk (in my case this is d)

#fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor     Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x99bc5403.
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):
We need to create a new patition therefore we need to press "n"

Command (m for help): n
Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
Create a new primary partiton then set it's type as Linux LVM "8e"

Select (default p): p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-838860799, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-838860799, default 838860799): 
Using default value 838860799

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
To see the new partition press "p"

Command (m for help): p

Disk /dev/sdd: 429.5 GB, 429496729600 bytes
255 heads, 63 sectors/track, 52216 cylinders, total 838860800 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 identifier: 0x99bc5403

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048   838860799   419429376   8e  Linux LVM
To save the changes press "w"

 Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Now it's type to add the new partition to LVM. First we need to create a physical volume from the new partition.

#pvcreate /dev/sdd1
   Physical volume "/dev/sdd1" successfully created

Next we need to extend the volume group with the new partition

#vgextend <volume_group_name> /dev/sdd1

To see the new size you can run vgdisplay

# vgdisplay 
  --- Volume group ---
  VG Name               <volume_group_name>
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               815.48 GiB
  PE Size               4.00 MiB
  Total PE              208764
  Alloc PE / Size       106365 / 415.49 GiB
  Free  PE / Size       102399 / 400.00 GiB
  VG UUID               DiaLkN-XDqh-5pdO-jteG-929K-zZ1B-9TX5I5

Now let's add the new space to the LVM partition

#lvextend -L +400G /dev/<volume_group_name>/<partition_name>

Sometimes when you add the full free space you will get an error that insuffucient free space then redo the above command with several megabytes less:

#lvextend -L +399.99G /dev/<volume_group_name>/<partition_name>
  Rounding size to boundary between physical extents: 399.99 GiB
  Extending logical volume data to 709.48 GiB
  Logical volume data successfully resized

All we need to do now it's to run resize2fs to make linux operating system see the full free space without restart.

#resize2fs /dev/<volume_group_name>/<partition_name>
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/<volume_group_name>/<partition_name> is mounted on /<mount_path>; on-line resizing required
old_desc_blocks = 20, new_desc_blocks = 45
Performing an on-line resize of /dev/pve/data to 185988096 (4k) blocks.
The filesystem on /dev/<volume_group_name>/<partition_name> is now 185988096 blocks long.

With this done you are ready to use the new space added to your linux operating system.

Keresztes Péter-Zoltán

Read more posts by this author.

Subscribe to IT Ghost

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!