<

Go Premium for a chance to win a PS4. Enter to Win

x

HOW TO: Shrink a VMware Virtual Machine Disk (VMDK) in 15 minutes

Published on
255,284 Points
215,384 Views
44 Endorsements
Last Modified:
Awarded
Andrew Hancock (VMware vExpert / EE MVE^2)
MVE^2, Expert of the Year 2016-2011, Scribe 2016-2012, Author of the Year 2016,2013-2012 VMware vExpert 2016-2011 27 years of experience.
HOW TO: Shrink a VMware Virtual Machine Disk (VMDK) in 15 minutes

In my previous Experts Exchange Articles, most have featured Basic and Intermediate VMware and Virtualisation Topics.

If you would like to read my Basic VMware articles,  they are listed here for your convenience.  

During this series of articles VMware released VMware vSphere 5.5 and VMware vSphere Hypervisor ESXi 5.5. These articles are also applicable to VMware vSphere Hypervisor ESXi 5.x and 5.5. For consistency, I have used VMware vSphere Hypervisor ESXi 5.1 through this series.


Increasing the size of a VMware Virtual Machine Disk (VMDK) is straightforward, and is supported in the vSphere Client, by selecting the virtual hard disk, and increasing the size using the up arrow. or typing in a new value. Please see my previous Experts Exchange article:


Resize - VMDK.jpg
However, although there is a down arrow present, you cannot shrink the disk in this way, it's not supported. The only supported method of shrinking a VMware virtual machine disk (VMDK) is to use VMware vCenter Converter Standalone and create a virtual to virtual (V2V) conversion, a similar process to a Physical to Virtual (P2V) conversion.

Please see my previous Experts Exchange articles


V2V Disadvantages
V2V Conversions can be slow.
You need to ensure, you have enough datastore storage space for the new virtual machine disk.
The virtual machine is converted, so there is a small risk, the conversion can cause issues with the converted virtual machine.
The converted machine can contain a different MAC address.
V2V are prone to failure.

V2V Advantages
Support end-user method of re-sizing a virtual machine disk.
GUI Interface when using VMware vCenter Converter Standalone

After reading and understanding this article, you will be able to reduce the size of virtual machine disks in less than five minutes. I will let the reader decide if this is a faster method than the traditional practice of using a V2V, to change the size of a VMware Virtual Machine Disk (VMDK). On occasion we come across an installation, where an Administrator has assigned a 2TB virtual disk, and does not have the space to create a V2V!

In this article we will show you HOW TO: Shrink a VMware Virtual Machine Disk (VMDK) in 15 minutes

I feel this is a little more advanced, so I've not included it in the Basic VMware article series.

Before your start the following procedure, please ensure you have a valid and tested Full Backup of your virtual machine. This does not mean a VMware Snapshot.

Also check to see the virtual machine is NOT running on a Snapshot disk, If unsure please refer to my Experts Exchange Article


The following procedure is split into two parts

Shrink the Operating System partition - This is covered in Step 1.
Shrink the VMware Virtual Machine Disk (VMDK) - This is covered in Step 2

1. Login to the Virtual Machine and shrink the OS Partition

Before we can shrink the VMware virtual machine disk (VMDK), we need to shrink the OS partition. (to avoid file system corruption). In this example I am using Windows 2008 R2, which has a shrink function. If you are using another OS, please see the other 3rd party partition utilities which are available, they are listed in my Experts Exchange article Using an RDP (Remote Desktop Protocol) connection or connect via the Console, using the vSphere Client, login to the virtual machine as an Administrator.
Logon to the Virtual MachinePress Control-Alt-Delete to login to the virtual machine.
Right Click My Computer and Select ManageRight Click My Computer and Select Manage
Select Disk ManagementSelect Disk Management, and select the partition you need to shrink.
Right Click VolumeRight Click the Volume/Partition to shrink, and select Shrink.
Shrink Disk QueryThe above dialogue will briefly appear whilst the file system is queried.
Shrink C:the above dialogue will appear. Enter a size to reduce the OS partition.

In this example the VMware virtual machine disk (VMDK) is 40GB, and we would like to reduce the size of the VMware virtual machine disk (VMDK) to 20GB. The Disk Management utility scans the available file system, and reports a maximum size the OS partition can be reduce by, this is based on current file system usage.

Enter the figure 19.5 (GB) x 1024 = 19968
Shrink SizeOS Partition size after Shrink Operation.
Disk Manager after Shrink OperationAs can be clearly seen in the above screenshot, there is now an unallocated 19.5GB space on the virtual disk, in Step 2 the VMware virtual machine disk (VMDK) will be "chopped", removing this unallocated storage space, and finally reducing the virtual machine disk (VMDK) to 20GB. Providing that we DO NOT affect the existing partitions, this is a safe operation. So in effect the "cut" will be made in the unallocated storage space, after the OS partition.

2. Reducing the size of the VMware Virtual Machine Disk (VMDK)


Login and connect to the VMware vSphere Host ESXi server which hosts the virtual machine.

see my previous Experts Exchange articles


Power OFF the Virtual Machine, and change to the datastore path where the VMware virtual machine disk (VMDK) is located.

cd /vmfs/volumes/<datastore name>/<VM foldername>
VM Folder PathWe need to edit the *.vmdk, which is the descriptor file, which contains the variables for the size of the *.-flat.vmdk. Using cat, this is what the descriptor file contains
VMDK Descriptor fileThe number highlighted above, under the heading #Extent description, after the letters RW, defines the size of the VMware virtual disk (VMDK).

this number - 83886080, and it's calculated as follows:

40 GB = 40 * 1024 * 1024 * 1024 / 512  =  83886080

We wanted to reduce the size of the VMware virtual machine disk (VMDK) from 40 GB to 20 GB. So the value we need to enter into the descriptor file is:-

20 GB = 20 * 1024 * 1024 * 1024 / 512  =  41943040

Using vi, edit the descriptor file, and change the number from 83886080 to 41943040, and save the file.
VMDK Edited with viMigrate or Copy the virtual machine to another datastore, if you do not have the migrate option, see my Experts Exchange article here


After the virtual machine disk (VMDK) has been moved, you will notice the disk size reflects the desired size of 20GB.
Size of virtual disk, as viewed from vSphere ClientSize of virtual disk as viewed from consoleAfter restarting the virtual machine, and checking with Disk Management, you will notice the 19.5GB unallocated storage space, has been removed, and disappeared.
Size of virtual disk as viewed from Disk Management in the OS
Congratulations, you have successfully Shrunk a VMware Virtual Machine Disk (VMDK)

****************************************************************************
Thank you for reading my article, please leave valuable feedback. If you liked my VMware article and would like to see more Articles from me, please click the Yes button near the: Was this article helpful? at the bottom of this article just below and to the right of this information. Thank You. Do not forget if you have a question about this article or another VMware, Virtualisation, Windows Server 2012 question, why not post a Question for me and the other Experts Exchange Experts in the VMware, Virtualisation, Windows 2008, Windows 2012 Zones. I look forward to hearing from you. - Andy :- twitter @einsteinagogo
****************************************************************************
44
Comment
45 Comments
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Todd

Okay, moved what do you think of the placement now?

Andy
0
 
 

Administrative Comment

by:lherrou
Andrew,

Congrats, the page editors agree that your article is worthy of the Experts Exchange Approved award for your excellent work.

LHerrou
Experts Exchange Editor
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Todd

That's excellent news.

I've got many more......

Andy
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Todd Reibling
Andrew,
I ran through your instructions, but some reason the disk space is not reduced in my edit settings > provisioned size.

Any ideas what I am doing wrong?  I even went back and did a cat of the .vmdk file and the settings are as they should be.  I reduced the disk number from 209715200 to 104857600.
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@Todd, PLease post a question, and myself or other experts will be glad to assist.
0
 

Expert Comment

by:Todd Reibling
Andrew,

I ran through your instructions and yet the setting for the hard drive hasn't decreased.  Do you know why the number is not able to be decreased in the disk provisioning section?

http://screencast.com/t/yGxmU8fLzu

http://screencast.com/t/6QU5d88NA
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@Todd, Please post a question to the VMware Zone, and myself or other experts will be glad to assist you with your issue, and work through the issue.
0
 

Expert Comment

by:itezcouk
Hi, there seems to be a contradiction to what you are saying in the article and the steps involved...

"On occasion we come across an installation, where an Administrator has assigned a 2TB virtual disk, and does not have the space to create a V2V!"

but one of the tasks is to move the vm to another datastore.

"Migrate or Copy the virtual machine to another datastore, if you do not have the migrate option, see my Experts Exchange article here"

I'm in the exact situation that Im unable to complete a storage VMotion due to not having enough space on the san (vm is 8TB) we have 500gb free
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@itezcouk

With only 500GB available, your options are limited, please see this article

HOW TO: Shrink or Reduce a VMware Virtual Machine Disk (VMDK) using VMware vCenter Converter Standalone v5.5.2

Please post a new question if you want to discuss these options.
0
 

Expert Comment

by:Varun Tiwari
very good and helpful article
0
 

Expert Comment

by:Raja Inayat
Hi Sir Andrew

I am stuck in a problem of  vendor's pre-configured Datastore Disk which occupied all 1TB storage under Thick provisioning. I want to reduce the size of this  Datastore file which I cant as I dont find any shrink option as the Hardisk Size is un-editable please see attached screen shot.  Kindly guide me as I am new to VMware machine System. My Regards: Raja Inayat Assistant Professor, Department of Physics, University of Peshawar.
1
 

Expert Comment

by:Raja Inayat
Please see attached file

VMWARE-Veeam.png
1
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@Raja Inayat Please post a question to the VMware Zone.
0
 

Expert Comment

by:hasayeret
Andrew, just so I understand: if I do the SSH/Putty way which is the none convertor way, do I still need to copy the new edited VM to a different datastore or can the shrink still happen within the datastore without moving the VM anywhere?

Thanks!!!
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@hasayeret Please post questions in the VMware Zone, so they can be answered by experts. tagging questions onto the article comment page, is not going to get your Questions Answered, as no other members or Experts, get the benefit.

So take the time, to post a question, in the VMware Zone.

Thanks
0
 
LVL 20

Expert Comment

by:compdigit44
Excellent article as always!!!!

Two questions, in the example 40 GB = 40 * 1024 * 1024 * 1024 / 512  =  83886080
what can you break down the numbers? Why are you multiplying 1024 twice? Also when the VMDK file reduce is the white space freed up on the datastore?
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Hi compdigit44

I'm glad you asked.

The formula to find the R value is actually

R = Gx1024x1024x(1024/512) 

Open in new window


or

R = 2097152*G 

Open in new window

 

You can also work out R (approx value), if you do not know the size of disk from cylinders, heads and sectors (this is a bit old school, when we worked in the physical realm, and knew heads, cylinders and sectors counts of disks, and had to write BIOS in PC to match MFM and RLL disks (SCSI) as well. (this is circa early 8o's)

R = (  (  ( cylinders + 1 ) x512 x sectors x heads ) /1024  )  - 8  )  x  2  ) 

Open in new window


R = ( ( ( 5221 +1) x 512 x 63 x 255 ) /1024 ) -8 ) x 2)

Open in new window


R = 83891414

(R = 83886080) (5334 diff!)

so close...

The space should be given back to the datastore, depending upon SANs, you may need to use a unmap command and reclaim the space. But as this process requires you to Storage vMotion and move the virtual machine disk from one datastore to another, it's just like any other storage vMotion so the space will be made available instantly, and the new virtual machine disk, on the new datastore will be smaller. (because of your adjustment).
1
 
LVL 20

Expert Comment

by:compdigit44
Very interesting do the values in the equation are the cylinders, heads and sectors now since we are talking about a virtual hd VMware much use some default values here since it is not directly present the HD to the VM correct? Are there any VMware KB's on this..

Once again great feedback!!!
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
virtual cylinders, virtual heads and virtual sectors.

This is not really VMware, but related to file systems, disk partitions, from early Unix and DOS, and VMware just used them to define their virtual disk geometry.

I'll see if there are any KBs.
0
 

Expert Comment

by:vpadala
Hi Andrew,

This  is very help full to me  i am able to decrease the drive size with the above procedure .
But here we are getting another issue like when we trying to increase hard disk again using vsphere client it throwing an error i am attaching error screen shot .
ESX version 6 and we are using vsan 6.

Virtual machine working without any issue
error.JPG
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@vpadala

Please post a question in the VMware Topic Area and myself or other Experts will be glad to assist.
0
 

Expert Comment

by:kanky
Andrew, great info, nowere in the network is the same info you placed here. Im stuck at the step to use the vi command line. Could you set me an example of what exact line should i place on the vi editor, to do this type of change from xxx Gigas to xx Gigas? Thanks in advance
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@kanky

Please post a question in the VMware Topic Area, and myself or other experts will gladly assist you.

This way other members benefit, from the Answer.
0
 

Expert Comment

by:kanky
Andrew, no worries, I wa sable to find the soluton. Moving the VM, hoping it shrinks as stated. Thanks in advance
0
 
LVL 13

Expert Comment

by:George Sas
Great article thank you.

One thing to add maybe. No need to move the machine from one host to the other, if you reboot your VM, the disk size will be the modified one.
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@George

Thanks for your comments

Many methods are available..... removing and re-adding the disk removing and re-adding the VM!

.....
0
 
LVL 13

Expert Comment

by:George Sas
@Andrew

I just mentioned this because at the end you say :
"Migrate or Copy the virtual machine to another datastore, if you do not have the migrate option, see my Experts Exchange article here"

Just wanted to point out that there is no need to move it.
I am not a VM expert and I was just about to migrate it when I realized I have no space (that's why I want to shrink it). So I was looking for an extra disk or something when I thought I can just try and reboot and see if this does the trick :)
Anyway , your article saved me some precious time and space. Thanks again.
1
 
LVL 8

Expert Comment

by:Senior IT System Engineer
Thanks Andrew for the great article :-)

Yes, I still need to manually perform offline svMotion the VM after the VMDK descriptor change, because the VM Edit Settings HDD still showing the old HDD size.

Note:
I'm on ESXi 6.0U2 Essentials Plus edition.
0
 

Expert Comment

by:IT Q
Thanks for the interesting article.

We would like to reduce a 2TB vmdk (ESXi 5.1) but we noticed that the size is 4294967294 instead of 4294967296 as expected by multiplying 2048 * 1024 * 1024 * 1024/512.
Why two sectors are missing?
Wanting to reduce vmdk to 1900 GB how should we consider the difference of the two sectors?
Thanks again.
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@IT Q, please post a question to the VMware Topic area, and we can discuss.
1
 

Expert Comment

by:Jack Howarth
Great article! How do you begin to edit the descriptor file, what is the command?
The way I tried said "VI out of memory", so I'm assuming I'm doing it wrong.

Many thanks,
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
@Jack Please post a question and myself or other Experts will be glad to answer.
0
 

Expert Comment

by:Tecnologia Tecnologia
Hello @andrew i did itall and worked it, but i don't know how to say to the vpshere client that can use that new space for another virtual machine. However that the shirk virtuales machine reflects the updated size, the file *.vmk in the datastore it still occupies the same amount of mb. Can you tell me if you can back the new free space that was shrinked to the original datastore?
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Please post a question.
0
 

Expert Comment

by:Paul Kien
Good Job! Thanks for that! Works for me!
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
no problems.
0
 

Expert Comment

by:Ben Stephenson
I have to shrink a volume for a client at some point so I have been doing research and came across this article. Out of all the methods I have seen, this definitely looks like the best (and easiest way). Hopefully it goes well when I perform the work! Question out of curiosity. What is the importance of moving the VM (or vmdk at least) to a different datastore, or at least coping and pasting the files per the other article you referenced? Will the vmdk not actually change sizes until that happens, or will VMware not update the provisioned size? Either way, why is that the case? Thanks again for posting this article!
0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Will the vmdk not actually change sizes until that happens, or will VMware not update the provisioned size? Either way, why is that the case? Thanks again for posting this article!

Correct. Because it's a hack!

If you want more help, with ref. to this Article, please post a question, and we can discuss.

Thanks for reading.
0
 

Expert Comment

by:Balaji Kubendran
Great Work, i could see the VM size reduced what i want, That's perfect. Thanks.

but i have seen a problem with exporting my VM to OVA / OVF format, OVA file shows corrupted or no error while exporting.
1
 

Expert Comment

by:simeon king
Thanks very much! This will come in handy, one of my colleagues added a 1.5TB to the wrong drive .
I have tested this method on a test VM, and made a discovery you should all hear!
THERE IS NO NEED TO MOVE THE VM TO ANOTHER DATASTORE!
Once you shut down the VM remove the HDD from the VM that you wish to shrink. Make sure you DON'T delete from disk!
Perform the changes to the VMDK (as above)
In the datastore where the VMDK is located, create a new folder (I put it in the root). Then move the VMDK to the new location on the datastore (using the datastore browser).
Then before you start the VM add a new HDD and point it back to the VMDK you just shrunk, that is in the new location on the same datastore.
No need for extra storage!
0
 

Expert Comment

by:Jörg Müller
Hi,
your article helped a lot.

In your example you didn't change the geometrical parameters (LBA, so 90s :-)) of your shrinked 20GB vmdk. They still are those of a 40GB vmdk.
Will vmware change them automatically when you "move" your vmdk using vmotion? Or is it necessary to manually recalculate the number of cylinders beofore i restart the vm?
You may calculate the cylinders using the simple formula:
Cylinders = INT ( R / (heads x sectors)) = INT ( G * 2,097,152 / (heads x sectors))

Open in new window

Example: Your 40GB vmdk file uses  the following settings:
R: 40 x 1024 x 1024 x 1024 / 512 = 40 x 1024 x 1024 x 2 = 40 x 2,097,152 = 83,886,080
Cylinders: 5221, Heads: 255, Sectors: 63
Calculated Cylinders: 83,886,080/255/63 = 5221,667

Open in new window

So for a 20 GB vmdk the calculated values should be:
R: 20 x 2,097,152 = 41,943,040
Calculated Cylinders: 41,943,040/255/63 = 2610,833
Cylinders: 2610, Heads: 255, Sectors: 63

Open in new window

But you didn't change that...
Or is vmware ignoring the virtual disk geometry settings and they are only "informal"?

Thank you for your time
Jörg Müller
0
 

Expert Comment

by:Jörg Müller
@Balaji Kubendran

May be you should recalculate the number of cylinders of the virtual hard disk according to the formula :
Cylinders = INT ( R / (heads x sectors)) = INT ( G * 2,097,152 / (heads x sectors))

Open in new window

0
 
LVL 124

Author Comment

by:Andrew Hancock (VMware vExpert / EE MVE^2)
thanks for reading...

we are only interested in the extent description value, as this is virtual disk, does not matter how you will get to the figure you want to shrink it by....

cylinders, heads, sectors have no real meaning in a virtual disk,
0
 
LVL 8

Expert Comment

by:A Q Choudary
Big yes to above article
0
 

Expert Comment

by:Ahsan_h
Hi,

Your article provides details for windows. How can the same be achieved for Linux?

Thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Join & Write a Comment

Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…
This video shows you how easy it is to boot from ISO images for virtual machines with the ISO images stored on a local datastore on the ESXi host.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month