VMware Cloud Community
Timbobaloba7
Contributor
Contributor

Shrinking a .vmdk file does not increase datastore space

Hi all,

I need to increase space on my datastore (physical disks maxed) and have followed the below procedure to shrink one of my VMs disks;

https://virtualman.wordpress.com/2016/02/24/shrink-a-vmware-virtual-machine-disk-vmdk/

Which completed successfully as the disk was 2.7TB and is now 1.5ish TB

pastedImage_1.png

But when i see the file size of the associated .vmdk it's still 2.7TB and datastore hasn't reduced;

pastedImage_2.png

Thoughts?

0 Kudos
6 Replies
a_p_
Leadership
Leadership

Although the steps may work if you are very careful, I wouldn't do this with a production machine, especially not if virtual disks larger than 2TB are involved.

Anyway, did you migrate the VM to another datastore, after modifying the metadata? Without doing this the data (flat.vmdk) file will not decrease in size.

André

0 Kudos
Timbobaloba7
Contributor
Contributor

thanks Andre,

yeah risky i know but had to be done.

i do have an iscsi NAS datastore but will take ages to move it.... so you're saying theres no other way but to move it to another datastore then move it back????

So if i just scp the .vmdk file in question to the NAS iscsi datastore then scp it back it will show as reduced?

0 Kudos
Timbobaloba7
Contributor
Contributor

jsut FYI i cannot leave the files on the iscsi NAS

0 Kudos
a_p_
Leadership
Leadership

What needs to be done is to cut off the freed space from the flat.vmdk file. This can be done by migrating the virtual disk from either the GUI, or by cloning it using the vmkfstools command line utility. Both will migrate the number of blocks which define the virtual disk's size in the descriptor .vmdk file. Simply coping the file will not help, except you'd use e.g. the dd command, and only coy the mentioned number of blocks.

Maybe continuum​ has an idea how to truncate the flat file without the need to migrate it?

André

0 Kudos
continuum
Immortal
Immortal

Yes - you can cut a vmdk to a new desired size in place - without the need of a copy / migration.
Assuming that you already adjusted the descriptorsize to the new size we will use that value for the next calculations.
So lets take this example .... only showing the relevant line of the descriptorfile.
RW 4508876800 VMFS "name-flat.vmdk"
Enter this value into a calculator and check if the size in sectors is a multiple of 1MB.
4508876800 sectors / 2048 = 2201600 MB.
Using flat.vmdk - sizes that are not a multiple of 1MB is allowed but not ideal at all.
So rather adjust the new descriptor and pick a value that is a multiple of 1MB.
In this case the value of 2201600 is fine.
To use dd in "scissor-mode" you must inject at least one block of data at a given skip-value and MUST not use the "conv=notrunc" option.
The dd command needs the following parameters:
bs= blocksize (we will use 1Mb here which corresponds to bs=1M)
if= inputfile (we can use either /dev/zero or a specially created file I call "cut.bin" of exactly 1MB)
of= outputfile (here we assign the flat.vmdk we want to cut)

seek= this is the offset in the outputfile where we want to cut MINUS 1MB)
I used to inject empty blocks created from /dev/zero in the past but learned a safer approach during last years ....

So I nowadays use a "cut.bin" file that I create in one extra step for safety.
To create that file I extract the last MB of the future size.
Future size is 2201600 MB so I create the cut.bin with this command:
dd if=name-flat.vmdk of=cut.bin bs=1M count=1 skip=$((2201600 - 1))
So now we have everything we need.
The final command now is this one:
dd if=cut.bin of=name-flat.vmdk bs=1M count=1 seek=$((2201600 - 1)) conv=notrunc
Now this command re-injects our premade cut.bin of 1MB at exactly the same offset from where we extracted it in the first place.
This command will not yet cut the vmdk - instead it does actually nothing visible - it only reinjects and does not change the size of name-flat.vmdk
Last chance to doublecheck your value is NOW !!!! if you are sure run

dd if=cut.bin of=name-flat.vmdk bs=1M count=1 seek=$((2201600 - 1))

If you miscalculated your values or forget to set bs to 1MB you just probably destroyed your flat.vmdk.

If your values were ok - you just cut your flat.vmdk to the new desired size in place in just one second.

DISCLAIMER: this command is destructive - even I may not be able to help you if you messed this up.
DO NOT USE THIS IF YOU DO NOT UNDERSTAND THE INSTRUCTIONS !!!!

If you are unsure about the parameters post your vmdk descriptorfile and ls -lah from inside the VM-directory.
Ulli


________________________________________________
Do you need support with a VMFS recovery problem ? - send a message via skype "sanbarrow"
I do not support Workstation 16 at this time ...

CriticMind
Contributor
Contributor

@continuum 

Had a similar issue with shrinking of the vmdk file. followed your instruction from other posts and changed the descriptor file and all went fine. now I need to cut a vmdk file to the new desired size and I managed to go through all the steps. but the last one doesn't go through. 

when i use this command:

dd if=cut.bin of=name_1-flat.vmdk bs=1M count=1 seek=$(('sector size in Megabytes' - 1))

I get Function not implemented while everything before that worked.

Any idea about it? 

Tags (3)
0 Kudos