Resizing disk. ESX, Ubuntu 12.04 server

From MyWiki

Jump to: navigation, search

Thanks a million to the author of the article, which we used as the starting point to successfully resize the disk!


We run out of space on one of the VMs we have and space was added in vCenter to the VM by expanding existing vmdk by extra 3G+

Normally, I'd add another vmdk, create PV and expand volume group with subsequent expanding of LV and filesystem. This case is different - space added to existing disk, so we need to work on partition table.

The only disk in the VM is /dev/sda. Original disk size was 17G and we extended it to 20G via vCenter.

Re-scan to see the extra space:

# echo 1 > /sys/block/sda/device/rescan

Checking that we get the 20G:

# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    33552383    16525313    5  Extended
/dev/sda5          501760    33552383    16525312   8e  Linux LVM

The main thing above is the "Start" column. Take the note of the start of the Extended partition /dev/sda2 and the logical /dev/sda5! We will need it later.

Now we delete the logical and extended partitions, because we want to expand it to the end of the drive:

Command (m for help): d
Partition number (1-5): 5

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    33552383    16525313    5  Extended

Command (m for help): d  
Partition number (1-5): 2

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux

Now we re-create the two partitions and make sure that we line up the Extended one to its original Start position:

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 
Using default value 2
First sector (499712-39845887, default 499712): 501758
Last sector, +sectors or +size{K,M,G} (501758-39845887, default 39845887): 
Using default value 39845887

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    39845887    19672065    5  Extended

Now we re-create the Logical partition. At this stage we won't be able to set the Start to the original position just yet. We will accept the default, then change it using Expert mode:

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l 
Adding logical partition 5.   
First sector (503806-39845887, default 503806):
Using default value 503806
Last sector, +sectors or +size{K,M,G} (503806-39845887, default 39845887):
Using default value 39845887

Command (m for help): x

Expert command (m for help): b
Partition number (1-5): 5 
New beginning of data (501759-39845887, default 503806): 501760

Return back to main menu and confirm that we had it right:

Expert command (m for help): r

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    39845887    19672065    5  Extended
/dev/sda5          501760    39845887    19672064   83  Linux

Don't forget to set the type back to Linux LVM:

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 20.4 GB, 20401094656 bytes
255 heads, 63 sectors/track, 2480 cylinders, total 39845888 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: 0x0002b8e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    39845887    19672065    5  Extended
/dev/sda5          501760    39845887    19672064   8e  Linux LVM

Now we can save the changes and reboot. Don't mind the warning, reboot:

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

After reboot, we check the current PV size before we change it:

# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               sysvg
  PV Size               15.76 GiB / not usable 2.05 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4034
  Free PE               99
  Allocated PE          3935
  PV UUID               lmJ0zN-2ZTu-g5mr-g7rO-fWLh-fAy6-OamfcY

Resizing the PV and checking that PV got the extra space and it's also visible on VG level:

   
# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               sysvg
  PV Size               18.76 GiB / not usable 2.81 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4802
  Free PE               867
  Allocated PE          3935
  PV UUID               lmJ0zN-2ZTu-g5mr-g7rO-fWLh-fAy6-OamfcY
   
# vgdisplay 
  --- Volume group ---
  VG Name               sysvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  20
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                7
  Open LV               7
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               18.76 GiB
  PE Size               4.00 MiB
  Total PE              4802
  Alloc PE / Size       3935 / 15.37 GiB
  Free  PE / Size       867 / 3.39 GiB
  VG UUID               LtPaDF-iNlt-6JRW-WgnE-GRGC-NEyZ-cXIWkx

Extend the LV and resize filesystem:

# lvextend -L+2G /dev/sysvg/varlv 
  Extending logical volume varlv to 10.86 GiB
  Logical volume varlv successfully resized

# resize2fs /dev/sysvg/varlv 
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sysvg/varlv is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sysvg/varlv to 2847744 (4k) blocks.
The filesystem on /dev/sysvg/varlv is now 2847744 blocks long.

# df -h /var
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/sysvg-varlv    11G  3.8G  6.5G  37% /var

The above method requires reboot, while adding and extra virtual disk, creating PV on top of it and expanding VG would not. Although, there are limits to how many disks you can add to a VM, so the usual planing is a good thing ;-)

Personal tools