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.

1 thought on “Adding a new LVM partition with GNU parted

  1. Nishad Patankar

    Fantastic.. I was looking rigorously for partitioning GPT disk and creating logical partitions out from free space through parted tool. Found none until i stuck here.
    Thanks a lot Syed Ali..!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s