Linux – How to resize the disk of a Fedora guest VM in VMWare ESXi

fedoralinuxvmware-esxi

How do I resize (specifically increase) the disk size of a Fedora guest VM running under VMWare ESXi 4.1?

I have a Fedora 16 VM with an lvm2 formatted disk, and I've increased its disk size using the vSphere client from 50GB to about 250GB. I rebooted the guest, and it correctly shows this size using fdisk -l /dev/sda. However, df -H still shows the old size.

I've found a few KB articles explaining how to resize partitions for some flavors of Linux, but nothing for Fedora with ext4. That article seems to imply I have to create a completely new partition, and that I can't simply expand the existing partition. Using Gparted, it also prevents me from simply resizing the existing partition. Is this impossible to do under Linux?

Edit: It turns out the drive is formatted as LVM2, which apparently most Linux tools, such as GParted, don't support.

Edit: The partition in logical volume I need to resize is the root partition. Is there any way to do this without a LiveCD?

Best Answer

This is a bit of a cut'n'paste of a document I wrote for internal use and although it probably over-answers your question I thought I'd put it on here in case it's of use to you or others OK.

  1. Login to the machine as root or sudo each of the following commands, enter fdisk –l, you should see something like this;

    Disk /dev/sda: 21.1 GB, 21xxxxxxxxx bytes
    255 heads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          13      104391   83  Linux
    /dev/sda2              14        2610    20860402+  8e  Linux LVM
    

    In this case I've altered the values but as you can see this machine has a single ~20GB root virtual disk with two partitions, sda1 and sda2, sda2 is our first LVM 'physical volume', see how LVM uses a partition type of '8e'.

  2. Now type pvdisplay, you'll see a section for this first PV (sda2) like this;

      --- Physical volume ---
    PV Name               /dev/sda2
    VG Name               rootvg
    PV Size               19.89 GB / not usable 19.30 MB
    Allocatable           yes (but full)
    PE Size (KByte)       32768
    Total PE              636
    Free PE               0
    Allocated PE          636
    PV UUID               PgwRdY-EvCC-b5lO-Qrnx-tkrd-m16k-eQ9beC
    

    This shows that this second partition (sda2) is mapped to a 'volume group' called 'rootvg'.

  3. Now we can increase the size of the virtual disk using the usual vSphere VSClient by selecting the VM, choosing to 'edit settings', then selecting 'Hard Disk 1'. You can then increase the 'Provisioned Size' number – so long as there are no snapshots in place anyway – and select OK. This will take a few seconds to complete.

  4. If you then switch back to the Linux VM and enter

    echo "- - -" > /sys/class/scsi_host/hostX/scan
    

    where the X character is likely to be zero, it will perform a SCSI bus rescan, then run fdisk –l, you should see something like;

    Disk /dev/sda: 42.2 GB, 42xxxxxxxxx bytes
    255 heads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          13      104391   83  Linux
    /dev/sda2              14        2610    20860402+  8e  Linux LVM
    

    You'll see that the disk size has increased, in this case to ~40GB from ~20GB but that the partition table remains the same.

  5. We now need to create a new LVM partition, type parted, you should see something like this;

    GNU Parted 1.8.1
    Using /dev/sda
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted)
    

    You'll now need to create a new partition for the extra new space, type 'p' to see the current partition table such as this;

    Model: VMware Virtual disk (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start   End     Size    Type     File system  Flags
     1      32.3kB  107MB   107MB   primary  ext3         boot
     2      107MB   21.5GB  21.4GB  primary               lvm
    

    Then type mkpart, then select 'p' for 'Primary', for file system type enter 'ext3', for start enter a number a little higher than the combination of both 'sizes' listed above (i.e. 107MB + 21.4GB, so say 21.6GB), for end type the size of the disk (i.e. in this case 42.9GB). Once you press enter it will create this new primary partition, type 'p' to show the new partition table, you should see something like;

    Model: VMware Virtual disk (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start   End     Size    Type     File system  Flags
     1      32.3kB  107MB   107MB   primary  ext3         boot
     2      107MB   21.5GB  21.4GB  primary               lvm
     3      21.5GB  42.9GB  21.5GB  primary               ext3
    

    You'll see that the new partition started after the first two and fills the available space, unfortunately we had to set it to a type of 'ext3', so let's change that.

  6. Type 't', then the partition number (in our case 3 as it's the third partition), then for the 'hex code' enter '8e' – once you'd done this type 'p' again and you should see it change to 'Linux LVM';

    Disk /dev/sda: 42.9 GB, 42949672960 bytes
    ads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        1          13      104391   83  Linux
    /dev/sda2           14        2610    20860402+  8e  Linux LVM
    /dev/sda3         2611        5221    20972857+  8e  Linux LVM
    
  7. Now we need to create a new LVM 'physical volume' in this new partition, type pvcreate /dev/sda3, this should then create a new LVM PV called /dev/sda3, type pvdisplay to check;

    --- Physical volume ---
    PV Name               /dev/sda3
    VG Name              
    PV Size               20.00 GB / not usable 1.31 MB
    Allocatable           no
    PE Size (KByte)       0
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               gpYPUv-XdeL-TxKJ-GYCa-iWcy-9bG6-tfZtSh
    

    You should see something similar to above.

  8. Now we need to extend the 'rootvg Volume Group', or create a new one for non-root 'volume group', type vgdisplay to list all 'volume groups', here's an example;

    --- Volume group ---
    VG Name               rootvg
    System ID
    Format                lvm2
    Metadata Areas        2
    Metadata Sequence No  19
    VG Access             read/write
    VG Status             resizable
    MAX LV                0
    Cur LV                8
    Open LV               8
    Max PV                0
    Cur PV                2
    Act PV                2
    VG Size               21.3 GB
    PE Size               32.00 MB
    Total PE              1276
    Alloc PE / Size       846 / 26.44 GB
    Free  PE / Size       430 / 13.44 GB
    VG UUID               tGM4ja-k6es-la0H-LcX6-1FMY-6p2g-SRYtfY
    
    • If you want to extend the 'rootvg Volume Group' type vgextend rootvg /dev/sda3, once you press enter you should see a message saying the 'volume group' has been extended.

    • If you wanted to create a new 'volume group' you'll need to use the vgcreate command – probably best call me for help with that.

    Once extended enter vgdisplay again to see that the 'rootvg' 'volume group' has indeed been extended such as here;

    --- Volume group ---
    VG Name               rootvg
    System ID
    Format                lvm2
    Metadata Areas        2
    Metadata Sequence No  19
    VG Access             read/write
    VG Status             resizable
    MAX LV                0
    Cur LV                8
    Open LV               8
    Max PV                0
    Cur PV                2
    Act PV                2
    VG Size               39.88 GB
    PE Size               32.00 MB
    Total PE              1276
    Alloc PE / Size       846 / 26.44 GB
    Free  PE / Size       430 / 13.44 GB
    VG UUID               tGM4ja-k6es-la0H-LcX6-1FMY-6p2g-SRYtfY
    

    You can see the 'VG Size' is as expected.

  9. Now we need to extend the 'logical volume', type lvdisplay to show our 'logical volumes', you'll see something like;

    --- Logical volume ---
    LV Name                /dev/rootvg/var
    VG Name                rootvg
    LV UUID                NOP1jF-09Xt-LkX5-ai4w-Srqb-xGka-nYbI2J
    LV Write Access        read/write
    LV Status              available
    # open                 1
    LV Size                3.00 GB
    Current LE             320
    Segments               3
    Allocation             inherit
    Read ahead sectors     auto
    currently set to       256
    Block device           253:2
    

    If we want to expand the /var file system from 3GB to 10GB then type lvextend –L 10G /dev/rootvg/var, now type lvdisplay again, you'll see the 'logical volume' has grown to 10GB;

    --- Logical volume ---
    LV Name                /dev/rootvg/var
    VG Name                rootvg
    LV UUID                NOP1jF-09Xt-LkX5-ai4w-Srqb-xGka-nYbI2J
    LV Write Access        read/write
    LV Status              available
    # open                 1
    LV Size                10.00 GB
    Current LE             320
    Segments               3
    Allocation             inherit
    Read ahead sectors     auto
    currently set to     256
    Block device           253:2
    
  10. Now the last thing we need to do is to grow the actual file system, this doesn't have to use all of the newly added space by the way. Enter df –h to show the current filesystems, here's an example;

    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/rootvg-root
                          2.0G  1.4G  495M  74% /
    /dev/mapper/rootvg-home
                          248M  124M  113M  53% /home
    /dev/mapper/rootvg-var
                          3.0G  1.1G  1.8G  30% /var
    /dev/mapper/rootvg-usr
                          3.0G  936M  1.9G  34% /usr
    /dev/mapper/rootvg-opt
                          3.0G  811M  2.0G  29% /opt
    

    If we want to expand the /var file system from 3GB to 10GB then type resize2fs /dev/mapper/rootvg-var (or on CentOS maybe xfs_growfs /dev/mapper/rootvg-var, or similar commands depending on the type of file system). When you press enter the actual filesystem will grow, this may take time, enter df –h once completed to check.

    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/rootvg-root
                          2.0G  1.4G  495M  74% /
    /dev/mapper/rootvg-home
                          248M  124M  113M  53% /home
    /dev/mapper/rootvg-var
                          9.88G  1.1G  8.2G  12% /var
    /dev/mapper/rootvg-usr
                          3.0G  936M  1.9G  34% /usr
    /dev/mapper/rootvg-opt
                          3.0G  811M  2.0G  29% /opt
    

You're now finished!