ESX/vSphere grow disk under Linux

If you are running VMware ESX/vSphere with a Linux VM that is running out of disk space it is usually a trivial task. Assuming you have a simple partition layout like this:

/dev/sda1 - 128M - /boot
/dev/sda2 - 2G - swap
/dev/sda3 - 25G - /

If you want to grow the root partition follow these instructions. If you are growing a block device that has no partitions (eg. /dev/sdb is an ext3 file system) then you do not need to do the fdisk step and you do not need to reboot – just rescan the device and grow the file system.


First, grow the disk in ESX/vSphere to the size you want. Once this has been done you need to tell the VM to rescan the SCSI bus so that it is aware the disk has changed size. You need to know the device ID, if you have the lsscsi package installed, run that:

root@server:~# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0
[2:0:0:0]    disk    VMware   Virtual disk     1.0   /dev/sda
[2:0:1:0]    disk    VMware   Virtual disk     1.0   /dev/sdb
root@server:~#

In this case /dev/sda has the ID 2:0:0:0. Send the rescan command:

root@server:~# echo 1 > /sys/bus/scsi/drivers/sd/2\:0\:0\:0/rescan
root@server:~# dmesg | tail
[819063.753408] sd 2:0:0:0: [sda] 54525952 512-byte logical blocks: (27.9 GB/26.0 GiB)
[819063.753539] sd 2:0:0:0: [sda] Cache data unavailable
[819063.753542] sd 2:0:0:0: [sda] Assuming drive cache: write through
[819063.753805] sda: detected capacity change from 26843545600 to 27917287424
root@server:~#

The root partition can be resized now. If this is a block device with no partitions you can skip this step.

Using fdisk, note down the current layout then delete and recreate the root partition (you may or may not see the Partition 1 does not end on cylinder boundary error, ignore it):

root@server:~# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p <--- NOTE THE OUTPUT OF THIS COMMAND IN CASE SOMETHING BREAKS

Disk /dev/sda: 27.9 GB, 27917287424 bytes
255 heads, 63 sectors/track, 3394 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: 0x000b4824

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32         125      749568   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             125        3264    25213952   83  Linux

Command (m for help): d <---- DELETE
Partition number (1-4): 3

Command (m for help): n <---- CREATE PARTITION AGAIN
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (125-3394, default 125): <--- THIS MUST BE THE SAME AS THE START VALUE FROM THE INITIAL LISTING
Using default value 125
Last cylinder, +cylinders or +size{K,M,G} (125-3394, default 3394):
Using default value 3394

Command (m for help): p   <---- VALIDATE OUTPUT

Disk /dev/sda: 27.9 GB, 27917287424 bytes
255 heads, 63 sectors/track, 3394 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: 0x000b4824

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32         125      749568   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             125        3394    26262881   83  Linux

Command (m for help): w    <--- WRITE CHANGES
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.
root@server:~#

You MUST ensure that the start value is the same before and after otherwise your system will not boot in most cases. Reboot the system so that the partition table is reread. Once the system has rebooted you can then resize your filesystem. Use the appropriate tool for your file system to do this, for ext3 this is resize2fs:

root@server:~# resize2fs -p /dev/sda3
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/sda3 to 6565720 (4k) blocks.
The filesystem on /dev/sda3 is now 6565720 blocks long.

root@server:~#

The file system has now been extended for your vm.

Posted in Linux and tagged , , , , , , .

2 Comments

  1. Warning: I have used this technique successfully with vSphere 5.0, but with vSphere 5.5 this was bad and a 2nd try at fdisk the volume was corrupted. I did better with a vSphere resize with VM off, and resizing via gparted-liveCD.

  2. For a safer approach, note that fdisk recommends to change the accounting usints from legacy default for msdos mbr (cylinders) into exact sector counts. Just press ‘u’ at the start of fdisk procedure (before ‘p’rinting the old state), and you are safely underway to recreate the expanded partition starting from the exact same location.
    HTH, Jim Klimov

Leave a Reply

Your email address will not be published. Required fields are marked *