5

How to Extend Partitions in VMs After Expanding Virtual Disks

 3 years ago
source link: https://www.nakivo.com/blog/increasing-the-size-of-a-disk-partition/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NAKIVO Blog > VMware Administration and Backup > VMware Setup > Increasing the Size of a Disk Partition: A How-to Guide

Increasing the Size of a Disk Partition: A How-to Guide

Subscribe banner

Let’s imagine this scenario: you’ve created a virtual disk, created partitions, and installed a guest operating system on one of the partitions on that virtual disk. Later, as you’re working, you realize that performance is suffering because there’s not enough disk space. Insufficient disk space on a system partition degrades performance. With VMware virtualization software providing flexible options to configure virtual hardware such as CPU, memory, and disk devices, you can extend the size of an existing virtual disk. However, you must resize a partition manually after that. Disk space added after you extend a virtual disk is unallocated.

This blog post explains how to extend partitions and contains the following main parts:

  • Prerequisites
  • Resizing disk partitions in Linux:
    • Resizing a virtual disk in the ESXi command line
    • Extending a Linux partition with the Disks utility
    • Using parted to extend Linux partitions
    • Using GParted on Live CD
    • Resizing LVM volumes
  • Extending a partition in the Windows GUI:
    • Expanding a virtual disk in vSphere Client
    • Using the Disk Management snap-in in the Windows guest
  • Using diskpart to extend Windows partitions:
    • Expanding a virtual disk in VMware Workstation
    • Using diskpart on the Windows installation medium
  • Using a helper VM
  • Using VMware vCenter Converter

Prerequisites

Some conditions must be met before you resize a virtual disk and the partitions on that disk.

Virtual disk size can be increased while a virtual machine is running. However, you may need to reboot a VM for the operating system to detect the resized disk.

To be resized, a virtual disk must not have snapshots. If your virtual machine has snapshots, delete them before you resize a virtual disk and adjust disk partition size.

It is recommended that you back up your VM before doing operations with disks. Changing disk partition size may be risky if you make a mistake during the configuration process.

Resizing Disk Partitions in Linux

In this section, I explain how to extend partitions in Linux after expanding virtual disks. There are different methods to adjust disk partition size and extend Linux partitions. You can use the command line tools or tools with the graphical user interface. First, I will expand a virtual disk of a Linux VM on an ESXi server.

Resizing a virtual disk in the ESXi command line

First, enable ESXi shell and Bash Console in the Direct Console User Interface (DCUI) of ESXi or in VMware Host Client. Then connect to ESXi shell via SSH or open ESXi shell directly on an ESXi host.

Go to the directory where the needed virtual disk of your virtual machine is located

cd /vmfs/volumes/datastore2/Ubuntu19/

Check the content of the directory and the size of the files:

ls -al

Checking the virtual disk size on an ESXi server before increasing the disk size

I have a thin provisioned virtual disk Ubuntu19.vmdk whose maximum size is 20 GB, and the current virtual disk size is about 5.6 GB (the total size of files stored on a virtual disk). The file size is displayed in bytes in the command line.

Run the command to expand a virtual disk from 20 GB to 30 GB. Enter the virtual disk target size that you want to have – not the size of the increment you want to add to the virtual disk. In my example, I increase the size of my 10-GB virtual disk to 30 GB by using the command like below:

vmkfstools -X 30G Ubuntu19.vmdk

Select a vmdk disk file, not -flat.vmdk. Enter the needed disk size and virtual disk file according to your needs and configuration. G means gigabytes, M – megabytes, K – kilobytes, T – terabytes, etc. If you want to have an eager-zeroed thick disk, use a command like this one vmkfstools -X 30G -d eagerzeroedthick vm-disk-name.vmdk

Grow 100% done – this output means that the virtual disk was expanded successfully. Check the new size of the virtual disk:

ls -al

As you see in the output, the size of the virtual disk has been increased.

The virtual disk size has been increased by using the command line interface

Extending a Linux partition with the Disks utility

New versions of Ubuntu have a built-in Disks utility with a user-friendly graphical user interface (GUI) that supports changing disk partition size with a few clicks.

Power on the virtual machine after extending your virtual disk, and boot Ubuntu Linux that is installed on the virtual machine.

Click Dash > Utility > Disks, or type Disks in the Dash menu (the analog of the Start menu in Windows). Choose the appropriate item once the Disks utility icon is found and displayed. After resizing a virtual disk, Linux partitions are not extended automatically, and you see the unused (unpartitioned) space on the disk. In my example, I installed Ubuntu by using the default partitioning scheme where I have one big / (root) partition on a disk.

How to extend partitions on Ubuntu with the Disks utility

Let’s expand the / (root) partition on the virtual disk on which Ubuntu is installed directly from this installed instance of Ubuntu in the Disks utility. Select the first partition that is mounted to the / directory in Linux, click the settings icon, and in the context menu, click Resize.

If you have multiple partitions, you can change the size of those partitions in the Disks utility.

Changing disk partition size in Ubuntu with the Disks utility

In the Resize Volume window, enter new parameters, such as partition size, free space following, or difference. As an alternative, move the slider from the current size to the desired size. Then click the Resize button.

How to adjust disk partition size in Ubuntu

Enter your password for modifying partition settings and changing disk partition size.

Now, the partition is extended on the virtual disk whose size was increased.

Changing disk partition size was completed successfully

If you use Linux other than Ubuntu, and your Linux version doesn’t have the Disks utility to adjust disk partition size, use parted that is available on all Linux distributions.

Using parted to extend Linux partitions

Parted is a command-line utility to manage disk partitions in Linux. Parted is pre-installed in Ubuntu. However, if this utility is not installed on your Linux, install this utility manually from software repositories (with root permissions).

On Debian and Ubuntu systems:

sudo apt-get install parted

On RHEL (Red Hat Enterprise Linux), CentOS, and Fedora:

yum install parted

On Fedora 22+ versions:

dnf install parted

On OpenSUSE:

zypper install parted

Unmount partitions before doing operations with partitions in parted. Operations with unmounted partitions can harm the data on the disk. Use the umount command to unmount a partition, for example:

umount /dev/sdb1

Check whether a partition is mounted with one of these commands:

mount

df -h

cat /proc/mounts

If you need to resize a system partition that is used by the installed operating system, boot from a live CD/DVD/USB flash drive because you cannot unmount the partition that is in use by the operating system. I boot from the Ubuntu 19 live installation media in this example and select the Try Ubuntu option. Select the ISO file of Ubuntu 19 in the configuration of a virtual CD/DVD drive of the VM. On the following screenshot, see the VM settings with CD/DVD drive configuration of a VM in VMware vSphere Client.

Configuring virtual DVD settings in VMware vSphere Client

Start a virtual machine. Click the VM screen, and press F2 once the VM is powered on to enter the virtual BIOS of the VM. Do it promptly because you only have about one second to press F2. Select the CD/DVD drive of a VM as the first boot device.

Note: When you select the installation disk, the virtual CD/DVD ROM is selected as the first boot device inside the VM. After finishing the installation of a guest OS, the virtual hard disk drive on which the guest OS is installed is automatically set as the first boot device of the VM. If you want to boot from a virtual CD/DVD drive, select the appropriate first boot device in BIOS settings of the VM.

Select the Boot tab in BIOS. Select a boot device, and press + and – to edit the order of boot devices. Save changes and exit (go to the Exit tab).

Once you run a command in parted, changes are written to the disk immediately. Confirmation questions appear when you run commands to make changes with disk partitions.

Open the terminal, and run parted with root permissions

sudo parted

/dev/sda is the disk that opens by default. As my Ubuntu VM has only one virtual disk, opening /dev/sda is the right option.

To select another disk, use the command

select /dev/sdb

to select /dev/sdb for operations with partitions on that drive.

List all existing Linux partitions on the selected disk.

print

Remember the number of the needed partition (1 in my case).

You can list all partitions on all connected disks

print all

Check free (unpartitioned) disk space on the selected disk

print free

My unused disk space is between 21.5 GB and 32.2 GB because I increased the disk size, but the disk partition was not increased automatically.

Let’s fix this situation and extend the existing disk partition.

Enter the command in parted to resize the partition:

resizepart

Enter the number of the needed partition whose size you want to increase. My partition on /dev/sda has the number 1 in this example.

Partition number? 1

Enter the end size for the partition (in my case, the partition starts at 1049 kB and must end at 32.2 GB). If you don’t enter units, megabytes is used by default.

End? 32.2GB

Warning: Shrinking a partition can cause data loss, are you sure you want to continue?

Yes/No? y

Changes should now be written to the disk.

Verify that changes have been written to the disk.

print

The size of the partition has been increased successfully.

Using GParted on Live CD

GParted is an alternative edition of parted with the GUI. This utility is free, just like parted.

Download the ISO bootable image from the official website.

If you boot from a Live CD with GParted, you can resize partitions on different Linux distributions, including Linux distributions without a disk partitioning utility that resizes partitions on the fly. You can also resize partitions on which Linux is installed when it is not possible to resize these partitions from the OS because the OS mounts and uses them for regular operation.

Insert the bootable ISO image of GParted into a virtual CD/DVD drive of a virtual machine whose disk you are resizing.

Boot from this ISO image (I explained how to boot from the ISO image in the previous section).

In the boot menu, select GParted Live (Default settings), and press Enter.

Then you have to select some options.

Configuring console data

You can leave the default value (Don’t touch keymap), and hit OK to continue.

Which language do you prefer?

Select your language. I select 33 (US English). Press Enter to keep the default value and continue, or type the needed option number, then press Enter.

Which mode do you prefer?

Select 0 (Continue to start X to use GParted automatically).

Now you see the GUI of GParted. The interface of GParted is user-friendly. Select the needed disk in the top right corner (/dev/sda in my case). Partitions of the selected disk are displayed with information about them.

The partition I want to resize (expand) is /dev/sda1. I have 10 GB of unallocated disk space after increasing the size of the virtual disk from 20 GB to 30 GB. Select the needed partition, click Partition > Resize/Move (or right-click the needed partition to open this menu).

In the opened window, enter a new disk size in megabytes or use the mouse to adjust disk partition size. Click Resize/Move after you set the correct new configuration.

In GParted changes are not written to the disk immediately. You must apply configuration changes to write them. Click Edit > Apply All Operations to apply the changes.

Now the partition size is increased to the maximum possible size on the expanded virtual disk of the VMware VM. Shut down the VM, eject the ISO image with GParted, start your VM, and boot from the virtual hard disk drive that you have expanded and that contains the partition whose size was increased.

You can check the size of partitions after booting your Linux with the df -h command. On the following screenshot, see the /dev/sda1 partition size before resizing and after resizing.

Using LVM

If your Linux installed on a VM uses LVM to manage disk volumes, you have at least two options for disk management when increasing the size of a virtual disk.

  1. Increase the size of an existing virtual disk. Create a new partition (a partition type must be Linux LVM with the ID of 8e) on an extended disk, add this partition to an existing volume group, and extend the logical volume with the new partition. If MBR is used, the maximum number of primary partitions is four.
  2. Add a new virtual disk, create a partition on the new virtual disk, add this partition to an existing volume group, and expand the volume group by using a new LVM partition on a new virtual disk.

Extending a Partition in the Windows GUI

In this section of the blog post, I explain how to increase the size of a virtual disk in VMware vCenter by using vSphere Client and how to extend partition size in the Windows VM. The version of Windows installed on the VM as a guest OS is Windows Server 2019 in my example. There is one 20-GB virtual disk with one system partition (C:\) on which Windows is installed. The workflow for Windows 10, Windows 8, and Windows 7 is similar.

Expanding a virtual disk in vSphere Client

First, you must increase the size of a virtual disk used by a virtual machine. In this example, I use VMware vSphere Client to increase the virtual disk size. Select a virtual machine whose virtual disk you want to expand, right-click the VM, and in the menu that opens, hit Edit Settings to open the virtual machine settings.

In the Virtual Hardware tab, expand settings for the needed hard disk, for example, Hard disk 1. Enter the new size for the selected hard disk. In my example, I expand a virtual disk from 20 GB to 30 GB. Hit OK to save settings and expand the disk. If hard disk settings are greyed out, power off the VM and try again. Make sure that there are no snapshots for VM disks.

Using the Disk Management snap-in in the Windows guest

The virtual hard disk has now been expanded, but the size of partitions remains the same. Open Computer Management, and then open Disk Management. In Windows Server 2016 and 2019, click Tools > Computer Management in Server Manager. In Windows 10 and earlier Windows versions, right-click the My Computer (This PC) icon, and in the context menu, click Computer Management.

In the left pane of the Computer Management window, click Disk Management.

You can open the Disk Management snap-in in different Windows versions from the command line interface by running the diskmgmt.msc command.

Right-click the needed partition, and in the context menu, click Extend Volume.

Note: There must be free space right after the partition that you want to extend. If you have two partitions on a disk (C: and D:), and you want to extend the first partition (C:), your unallocated disk space should be between the first and the second partition (after C: and before D:).

Welcome to the Extend Volume Wizard. Hit Next to Continue.

Select Disks. Select unallocated disk space on your disk to be used to extend the needed disk partition. In my example, I select 10 GB of unallocated disk space that appeared after increasing the size of the virtual disk. You can select the amount of disk space in MB (it can be less or equal to the size of unallocated disk space).

Completing the Extend Volume Wizard. Hit Finish.

Now the partition size has been increased, and there is no unallocated disk space on the virtual disk. You can check the free space in the Disk Management window and in Windows Explorer.

Using Diskpart to Extend Windows Partitions

In modern versions of Windows, you can extend partitions, including a system partition, right in the operating system by using the Disk Management snap-in. However, this option is not available in Windows XP, Windows Server 2003, and older Windows versions. You might need to install old Windows versions such as Windows XP to run old applications that can run only on Windows XP. Another option is to connect old devices via USB (the USB Pass-through feature of VMware allows you to connect USB devices directly to a virtual machine) when devices don’t have drivers for newer versions of Windows. If you cannot install Windows XP on modern hardware due to ended support and missing drivers, you can still use this operating system on virtual machines. In this section, I explain how to increase the size of a virtual disk used by the Windows XP VM and how to extend a partition on this virtual disk after.

Note: As security patches are not released for Windows XP and Windows Server 2003 now, avoid connecting machines running these operating systems to a production network to avoid security issues. Attackers can use unpatched software vulnerabilities to initiate malware attacks. Windows XP is used as an example in this section of the blog post. Microsoft recommends that you use the latest versions of Windows.

I use the native command-line Windows tool that is available in Windows XP and all newer Windows versions and editions. I don’t use paid applications developed by third-party vendors. Many users have an ISO installation image of modern Windows versions, and this method is affordable. I use the Windows 10 installation image to boot from this virtual DVD image, run diskpart and resize a partition. The advantage of using diskpart in Windows 7 and newer versions of Windows is the support of 4-KB blocks and their alignment (native tools of Windows XP don’t support this feature). You can use this method to extend partitions on machines running other Windows versions.

Windows XP VM is running on an ESXi host. In this example, I edit the configuration of the VM running on the ESXi host by using VMware Workstation. VMware Workstation provides functionality for connecting to vCenter Servers and ESXi hosts and managing virtual machines. You can use this method to resize virtual disks and partitions of VMs running on VMware Workstation on your computer. Just keep in mind that the locations of VM files are different in this case.

Let’s start from the very beginning. Before increasing the size of a virtual disk in my VM running Windows XP, the disk partitioning scheme in Disk Management looks as on the following screenshot. There is one 10-GB virtual hard disk and one partition C: that fills all disk space. Windows is installed on this NTFS partition.

Expanding a virtual disk in VMware Workstation

Open VMware Workstation and click File > Connect to Server. Enter the IP address of the ESXi host on which your VM is residing or the IP address of vCenter Server that manages ESXi host with the needed VM. Enter administrative credentials for the selected server (by default, the root username is used for ESXi hosts and [email protected] for vCenter).

Select a VM whose virtual disk you want to expand in the list of VMs on the appropriate server. Shut down the VM. Then click VM > Settings in VMware Workstation.

In the Hardware tab, select the hard disk that you want to expand, then in the right section of the window, click the Expand button. My Windows XP VM has a thin provisioned 10-GB virtual disk.

Enter the new maximum disk size. I expand my virtual disk from 10 GB to 18 GB. VMware displays the following notification:

Expand increases only the size of a virtual disk. Sizes of partitions and file systems are not affected.

We’re aware of this. Click Expand to apply changes to the virtual disk.

When the disk is successfully expanded, a message is displayed with the reminder to repartition the disk. Hit OK to close this window.

Now in the hard disk options of VM hardware, you see that the size of the virtual disk has been increased to 18 GB.

If you power on the Windows XP VM and open the Disk Management snap-in, you notice that 8 GB of disk space is added as unallocated disk space after the first partition (disk C:). If you right-click a partition, you don’t see the Extend/Shrink options because there is not support of these features in Windows XP. Shut down Windows XP on this VM, and prepare the ISO installation image of a newer Windows version.

In VMware Workstation, open VM settings of the Windows XP VM. In hardware settings, click CD/DVD drive 1, and in the right pane of the window, select the needed Windows installation image. As my VM resides on an ESXi host, and I use VMware Workstation to manage this VM, I have multiple options for the CD/DVD drive of the VM. You can select the ISO image stored on a datastore of a remote server (ESXi server) or select the ISO image stored on a machine where VMware Workstation is installed. You can also insert an optical disc into a physical drive of the ESXi server or a computer running VMware Workstation.

I use the Windows 10 ISO image stored on a local machine for inserting into a virtual CD/DVD drive. Hit OK to save the VM configuration and close the window.

Using diskpart on the Windows installation medium

Power on the VM and press F2 quickly to open BIOS settings of the virtual machine. In the Boot tab, select CD-ROM Drive as the first boot device, save changes, and exit.

Boot from the Windows installation medium. I boot from the Windows 10 ISO image.

Press Shift+F10 at the stage of language selection to open the command prompt (CMD).

Type diskpart in the CMD window and press Enter. The prompt has been changed to

DISKPART>

Display a list of all disks connected to the current machine:

list disk

On the following screenshot, you see my virtual disk that was expanded to 18 GB, including 8 GB of free (unallocated) disk space. In the left column, numbers of disks are displayed. My disk is marked as Disk 0.

To select the disk, enter the command:

select disk 0

Once you have ensured that the needed disk is available, list available disk partitions:

list volume

In my case, I want to extend Volume 1, that is the NTFS partition on which Windows is installed.

Now select the partition whose size you want to increase:

select volume 1

To extend the partition to the maximum available size, enter the command:

extend

If you want to extend a partition to a custom size, enter a command like this one:

extend size=10240

Where 10240 means 10240 MB. If units are not defined in the command, megabytes are used by default. This command makes the selected partition larger by 10 gigabytes.

Check whether the selected partition is extended and list volumes (partitions) again. Run the command:

list volume

list partition

As you can see on the following screenshot, my partition has been extended successfully, and I can close the diskpart tool:

exit

Reboot the virtual machine, eject the ISO image from the virtual CD/DVD drive, set the virtual hard disk as the first boot device in BIOS of the VM.

Boot Windows XP installed on the virtual hard disk that was expanded. Open the Disk Management snap-in, and check disk partitions. The partition was extended successfully, and a guest Windows XP was loaded successfully from this extended partition. Similarly, you can extend partitions for Windows Vista, 7, 8, 10, Windows Server 2003, 2008, 2012, 2016, and 2019 when resizing partitions from the installed OS is not possible.

Using a Helper VM

Another method to extend a partition on a virtual disk is connecting this virtual disk to another VM on which partition management software is available. The first VM is the source VM, and the second VM is the helper VM. Let’s say that you have two virtual machines. Windows XP is installed on the first machine, and Windows 7 is installed on the second machine. You have expanded a virtual disk used by the Windows XP VM, and you want to extend a partition on that disk. This operation cannot be done by native Windows XP tools. At the same time, Windows 7 has the appropriate built-in tools with the functionality to extend partitions (Disk Management, diskpart). Windows XP and Windows 7 are used in this section as an example. You can use other/newer versions of Windows.

The workflow to increase the partition size on a disk used by the first VM is the following:

  • Power off both VMs.
  • Open VM settings of the second VM (on which Windows 7 is installed).
  • In the Hardware tab, click Add > Hard disk. Select to use an existing virtual hard disk, and define the location of the VMDK file of the appropriate virtual disk.
  • Power on the second VM, and boot from the virtual disk on which Windows 7 is installed.
  • Open the Disk Management snap-in in Windows 7, and find the virtual disk with the partition on which Windows XP is installed.
  • Right-click the partition of the disk that you want to extend, and click Extend Volume in the context menu.
  • Configure the volume parameters, and set the needed volume size.
  • When done, close the Disk Management snap-in, and shut down the Windows 7 VM.
  • Open virtual machine settings of the Windows 7 VM. Disconnect (remove) the virtual disk of the Windows XP VM.
  • Check VM settings of the Windows XP VM. Make sure that the expanded virtual disk is present in the VM configuration.
  • Power on the Windows XP VM, boot the operating system, and check the size of disk partitions.

Using VMware vCenter Converter

As an alternative to previous methods, you can use VMware vCenter Converter Standalone to extend virtual disks and adjust disk partition size on VMware VMs running on ESXi or VMware Workstation. Prepare enough free space because a new VM is created by VMware Converter, and data on the source VM is copied to the destination VM.

In this example, I expand a virtual disk of a Windows VM from 18 GB to 22 GB with automatic resize of a system partition (disk C:). As a result, I have V2V (virtual to virtual) conversion in this example.

Open VMware vCenter Converter Standalone, and click Convert machine.

Starting to convert a virtual machine in VMware vCenter Converter

Source System. Select the type of the source VM. You can select a VMware infrastructure VM (a VM residing on an ESXi host in vSphere) or a VMware Workstation VM. A source VM can be powered on or powered off. I select a powered-off Windows VM residing on an ESXi host. If you select a VM on an ESXi host, enter the credentials of the ESXi host or vCenter Server that manages the ESXi host with the VM. Hit Next at each step of the wizard to continue.

Selecting a source system

Source Machine. Select the source machine whose virtual disks you want to expand by using VM conversion.

Selecting a source virtual machine to convert for changing disk partition size

Destination System. Select the destination VM type – a VMware Infrastructure virtual machine or VMware Workstation virtual machine. Select the VMware product for compatibility. Enter the virtual machine name and location.

Selecting destination to store a converted VM

Options. Click Edit data to copy to expand disk options. In the drop-down menu, select volumes to copy and hit Advanced. In the following table, select the needed virtual disk, partition, and set the destination size. Select the Create optimized partition layout checkbox to extend the partition to fit the size of the expanded virtual disk.

Changing disk partition size for a VM in VMware Converter

Finish the wizard, and wait until a copy of the VM is created by VMware Converter. Then, if everything is correct, you can delete the source VM.

Conclusion

This blog post has explained how to extend partition size on virtual disks of VMware virtual machines running Linux and Windows after increasing the size of the virtual disks. You can use parted in most Linux distributions, the Disks utility is available in modern Ubuntu distributions. If you need to extend partitions that are used by the installed Linux operating system, boot from Live CD with GParted that is the universal tool for changing disk partition size. As for Windows, modern Windows versions have the Disk Management snap-in that can adjust disk partition size on the fly from the installed operating system. If you need more options than those Disk Management provides, use diskpart, which is the command line Windows tool for managing disk partitions. The diskpart tool is available when booting from the Windows installation medium. You can use the method that works best for you or combine parts of multiple methods explained in this blog post.

Before performing operations with virtual disks of virtual machines and disk partitions, it is recommended that you back up these virtual machines to avoid data loss. NAKIVO Backup & Replication is a universal data protection solution that supports VMware backup in vSphere. Download the Free Edition of NAKIVO Backup & Replication to protect your VMware vSphere environment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK