Extending VM LVM disk

I have a CentOS VMs running in VMware, one of the VMs I was running was out of disk space. The disk was originally 85GB, I tried to increase it to 345GB. In an effort to increase the disk size, I tried:

  1. Shutdown the VM
  2. Increase the size of the disk
  3. Power on the VM
  4. Use parted to resize the partition

Unfortunately, step 4 did not work, parted complained that it could not detect the filesystem:

# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 344GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 538MB 537MB primary ext4 boot
2 538MB 85.9GB 85.4GB primary lvm

(parted) resize
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition number? 2
Start?  [538MB]?
End?  [85.9GB]? 344GB
Error: Could not detect file system.

It looks like parted does not like LVM. The next thing that I did is to add a new partition, then use ‘pvcreate’ to create a new physical extent, and then add that to the volume group, as seen below:

# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 344GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  538MB   537MB   primary  ext4         boot
 2      538MB   85.9GB  85.4GB  primary               lvm

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]?
Start? 85.9
End? 344GB
Warning: You requested a partition from 85.9MB to 344GB.
The closest location we can manage is 85.9GB to 344GB.
Is this still acceptable to you?
Yes/No? yes
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 344GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  538MB   537MB   primary  ext4         boot
 2      538MB   85.9GB  85.4GB  primary               lvm
 3      85.9GB  344GB   258GB   primary

(parted) quit

#ls -l /dev/sda
sda   sda1  sda2  sda3

# pvcreate /dev/sda3
  dev_is_mpath: failed to get device for 8:3
  Physical volume "/dev/sda3" successfully created
  
# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  vg0  lvm2 a--   79.50g      0
  /dev/sda3       lvm2 a--  240.00g 240.00g
  
# vgextend  vg0 /dev/sda3
  Volume group "vg0" successfully extended

# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg0    2   4   0 wz--n- 319.49g 240.00g

Adding a new LVM partition with GNU parted

In this brief article I will explain how to add a new physical parition to an existing disk, and then to use that new parition to create a mountable logical volume.
Let’s assume that we have a 1TB disk, and we are running CentOS/RedHat, in this case I am using version 6.5

First attempt to view partition table, turns out that disk has GUID Partition table:

# fdisk -l /dev/sda

WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.


Disk /dev/sda: 999.7 GB, 999653638144 bytes
255 heads, 63 sectors/track, 121534 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: 0x6929f946

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1      121535   976224255+  ee  GPT

Let’s try to view partition table using GNU parted, and then add a 189GB partition.
The command ‘print free’ shows the free space at the end.
I used the command ‘mkpart’ to add the partition. Notice that partition 3 stops at 211GB.
So I can create a new partition from 211GB onwards. The end of free space is 1000GB, so I picked 400GB as my end, leaving another 600GB free for later use.
I called my new partition ‘bigdisk’ and it’s numbered 4.

# parted /dev/sda
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: Dell Virtual Disk (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  211MB   210MB   fat16              boot
 2      211MB   840MB   629MB   ext4
 3      840MB   211GB   210GB                      lvm
        211GB   1000GB  789GB   Free Space

(parted) mkpart
Partition name?  []? bigdisk
File system type?  [ext2]? ext4
Start? 211GB
End? 400GB
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
(parted) print
Model: Dell Virtual Disk (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  211MB  210MB  fat16                 boot
 2      211MB   840MB  629MB  ext4
 3      840MB   211GB  210GB                        lvm
 4      211GB   400GB  189GB               bigdisk

I have decided to remove the 189GB partition and increase it’s size. So instead of starting at 211GB and ending at 400GB, I am ending at 600GB.
This gives me a 389GB partition.

# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Dell Virtual Disk (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  211MB  210MB  fat16                 boot
 2      211MB   840MB  629MB  ext4
 3      840MB   211GB  210GB                        lvm
 4      211GB   400GB  189GB               bigdisk

(parted) rm 4
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
(parted) mkpart
Partition name?  []? bigdisk
File system type?  [ext2]? ext4
Start? 211GB
End? 600GB
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
(parted) print
Model: Dell Virtual Disk (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  211MB  210MB  fat16                 boot
 2      211MB   840MB  629MB  ext4
 3      840MB   211GB  210GB                        lvm
 4      211GB   600GB  389GB               bigdisk

(parted) quit
Information: You may need to update /etc/fstab.

I will now view my LVS physical extent:

# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda3  vg_hv1 lvm2 a--  195.31g 11.71g

I need to add the 389GB partition as a physical extent. I know it’s /dev/sda4 because when I ran parted it showed partition #4.

# pvcreate /dev/sda4
  dev_is_mpath: failed to get device for 8:4
  Physical volume "/dev/sda4" successfully created
# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda3  vg_hv1 lvm2 a--  195.31g  11.71g
  /dev/sda4         lvm2 a--  362.70g 362.70g

I have one volume group on my system:

# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  vg_hv1   1   7   0 wz--n- 195.31g 11.71g

I am going to add another volume group, I don’t have to do this. I can simply extend my existing volume group, but I want to make another volume group in order to logically separate my applications.

# vgcreate vg_ic  /dev/sda4
  Volume group "vg_ic" successfully created
# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  vg_hv1   1   7   0 wz--n- 195.31g  11.71g
  vg_ic    1   0   0 wz--n- 362.70g 362.70g

Now I can create my logical volumes as needed. My existing logical volumes are:

# lvs
  LV      VG     Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_home vg_hv1 -wi-ao---- 19.53g
  lv_root vg_hv1 -wi-ao---- 39.06g
  lv_swap vg_hv1 -wi-ao----  7.81g
  lv_tmp  vg_hv1 -wi-ao----  9.77g
  lv_var  vg_hv1 -wi-ao----  9.77g
  lv_vm1  vg_hv1 -wi-ao---- 48.83g
  lv_vm2  vg_hv1 -wi-ao---- 48.83g

Adding additional logical volumes:

# lvcreate -L 80G vg_ic -n lv_cdrive
  Logical volume "lv_cdrive" created
# lvs
  LV        VG     Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_home   vg_hv1 -wi-ao---- 19.53g
  lv_root   vg_hv1 -wi-ao---- 39.06g
  lv_swap   vg_hv1 -wi-ao----  7.81g
  lv_tmp    vg_hv1 -wi-ao----  9.77g
  lv_var    vg_hv1 -wi-ao----  9.77g
  lv_vm1    vg_hv1 -wi-ao---- 48.83g
  lv_vm2    vg_hv1 -wi-ao---- 48.83g
  lv_cdrive vg_ic  -wi-a----- 80.00g

That’s about it! Next step can be to use ‘mkfs.ext4 /dev/mapper/vg_ic-lv_cdrive’ if you want to install ext4 on the LVM, followed by mounting it in /etc/fstab.
Or you can use it with KVM to install a VM on. If you like to add disk in another way, do share your experience.