Extend volume in Ubuntu server virtual machine under vSphere

Submitted by zubanst on Mon, 08/05/2019 - 17:05
  •  In vSphere management console make a snapshot of the Ubuntu server
  • Edit settings of the virtual machine and add a new drive

 vSphere disk

  •  Set the size, save and reboot the virtual machine
  • After reboot connect to the server in a ssh  session
  • List the partitions and drives

 sudo fdisk -l

  •  Create new primary partition on the added drive (/dev/sdc in my case), assuming defaults. Alter defaults if needed

 sudo fdisk /dev/sdc

 n   (create new partition, select start and end cylinders, all free space is selected by default)

 Partition type:

    p   primary (1 primary, 0 extended, 3 free)

    e   extended

 Select (default p):

 Using default response p

 Partition number (1-4, default 1):

 Using default value 1

 First sector (x-y, default x):

 Using default value x

 Last sector, +sectors or +size{K,M,G} (x-y, default y):

 Using default value y

 w  (save partition table and exit)

  •  Reboot

 sudo reboot

  •  Get the name of your volume group

 sudo vgs

 VG      #PV #LV #SN Attr   VSize  VFree

 mail-vg   2   1   0 wz--n- 10.73g 96.00m

  •  Extend the volume group with the created partition then verify your volume group again once complete with vgs

 sudo vgextend mail-vg /dev/sdc1

  •  Extend the Logical Volume with lvextend. To know path to the logical volume, list the File systems and look the content of /dev/mapper

 df -k

 Filesystem     1K-blocks    Used Available Use% Mounted on

 udev             2010212       4   2010208   1% /dev

 tmpfs             404836     596    404240   1% /run

 /dev/dm-0       10929408 4970476   5426828  48% /

 none                   4       0         4   0% /sys/fs/cgroup

 none                5120       4      5116   1% /run/lock

 none             2024180       0   2024180   0% /run/shm

 none              102400       0    102400   0% /run/user

 /dev/sda1         240972   70884    157647  32% /boot

 ls -l /dev/mapper/

 crw------- 1 root root 10, 236 Feb 16 08:56 control

 lrwxrwxrwx 1 root root       7 Feb 16 09:13 mail--vg-root -> ../dm-0

 sudo lvextend -L +3.9G /dev/mapper/mail--vg-root

  •  Extend the Filesystem to fill the logical volume

 sudo resize2fs /dev/mapper/mail--vg-root

  •  Check and reboot

 df -k

 sudo reboot

 

Important to remember is the path of the resize Physical Volume -> Volume Group -> Logical Volume -> Filesystem. We must follow this flow or the resize will either fail or encounter problems. Hopefully this will save you time and give you a little more background on the flow of a resize.