[Solved] shrink vhd of windows guest on mac host

Discussions related to using VirtualBox on Mac OS X hosts.
Post Reply
henry1
Posts: 7
Joined: 17. Nov 2012, 00:29

[Solved] shrink vhd of windows guest on mac host

Post by henry1 »

I have a windows 7 guest vm on mac host.

The win vm disk is dynamic and 100GB is allocated. Now inside win vm I saw the disk is about 50GB, while in Mac I saw the VHD file is about 80GB. I am trying to shrink the VHD file. After searching online, this is what I got:

In win7:
defragment c:
sdelete.exe -z -r c:
shut down windows 7

In Mac:
quit virtualbox
VBoxManage modifyhd my.vhd –compact

Then the vhd file should be shrink to close 50GB.

Is this the correct step?

I also have a snapshot of the win7, the snapshot folder which contains a vhd file of 50GB and sav file about 1.67GB. Is it possible that I can move the snapshot file to external drive to free some space (and move them back if I need to use the snapshot)?

Thanks.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: shrink vhd of windows guest on mac host

Post by mpack »

Why on earth are you using VHD format on a Mac host?? VDI is the native format of VirtualBox. Nobody uses VHD, not even Microsoft. Also, as the user manual tells you, the compaction feature is only available for VDI format.

I suggest that you follow your sdelete procedure, but instead of running the compact command, run "VBoxManage clonehd <my.vhd> <my.vdi> --format VDI". I believe this will give you a compacted VDI.

I will note however that this may be pointless: the VDI can't stay at the minimum size - it's bound to grow to its natural equilibrium size as you use it. And hopefully you know not to run defrag tools, sdelete etc without thinking about it, primarily that this will force the VDI to grow, perhaps to the full 100GB.
henry1
Posts: 7
Joined: 17. Nov 2012, 00:29

Re: shrink vhd of windows guest on mac host

Post by henry1 »

I think it was a VM created long time ago, forgot why it is in VHD.

So what is the best way to do on the guest, before convert it to vdi as you suggested?

Thanks.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: shrink vhd of windows guest on mac host

Post by mpack »

henry1 wrote: So what is the best way to do on the guest, before convert it to vdi as you suggested?
I don't understand the question. If any additional steps was required then I'd have mentioned them already. Likewise if a better alternative existed.

You already outlined your own intended procedure, all you need do is replace the one VBoxManage command but otherwise follow the same procedure.
henry1
Posts: 7
Joined: 17. Nov 2012, 00:29

Re: shrink vhd of windows guest on mac host

Post by henry1 »

Thank you.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: shrink vhd of windows guest on mac host

Post by mpack »

One correction: since you are cloning instead of modifying in-place, you will of course have to replace the VHD with the VDI in the VM settings, or create a new VM around the VDI. In the former case you need to release then remove the VHD, add your cloned VDI as a new attachment. I would advise making a backup of the VM before you start.
henry1
Posts: 7
Joined: 17. Nov 2012, 00:29

Re: shrink vhd of windows guest on mac host

Post by henry1 »

I created a new VM using vdi file. Looks running fine now. I will keep the old one for some time before I get rid of it.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: shrink vhd of windows guest on mac host

Post by mpack »

Good idea, and well done.
Chalky
Posts: 2
Joined: 29. May 2018, 03:35

Re: shrink vhd of windows guest on mac host

Post by Chalky »

henry1 wrote:I created a new VM using vdi file. Looks running fine now. I will keep the old one for some time before I get rid of it.
Hi there, did it shrink down OK though?
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: [Solved] shrink vhd of windows guest on mac host

Post by socratis »

Yes, it does shrink if there's room to shrink.
For more details on the procedure, take a look at the FAQ How to resize a Virtual Drive.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
SysMerlin
Posts: 3
Joined: 11. May 2018, 05:40

Re: [Solved] shrink vhd of windows guest on mac host

Post by SysMerlin »

Hi

I managed to shrink a vhdx on windows 10 using diskpart only (no need for Hyper-V) module following (except I selected the vhdx).
The vhdx was in dynamic format and I detached the disk before (offline)

file compact.bat

Code: Select all

echo select vdisk file="C:\file.vhdx" > %TMP%\vhd.txt
echo compact vdisk >> %TMP%\vhd.txt
type  %TMP%\vhd.txt
pause

:: BatchGotAdmin        
:-------------------------------------        
REM  --> Check for permissions  
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"  
REM --> If error flag set, we do not have admin.  
if '%errorlevel%' NEQ '0' (    echo Requesting administrative privileges...    goto UACPrompt) else ( goto gotAdmin )  
:UACPrompt  
    echo Set UAC = CreateObject^("Shell.Application"^) > "%TMP%\getadmin.vbs"  
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%TMP%\getadmin.vbs"  
    "%TMP%\getadmin.vbs"  
    exit /B
:gotAdmin  


DISKPART /S %TMP%\vhd.txt
pause
Reference:
Using diskpart
https://www.daniel-mitchell.com/blog/co ... -vhd-file/
https://blogs.msdn.microsoft.com/7/2009 ... windows-7/

Using Hyper-V module on windows 10
https://www.considerednormal.com/2014/0 ... -easy-way/
and
https://blogs.technet.microsoft.com/can ... indows-10/
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: [Solved] shrink vhd of windows guest on mac host

Post by socratis »

@SysMerlin
Thank you for the script! However, given that this thread is in the "OSX Hosts" section, I'm not sure if a lot of people are going to be using it straight as is. I'll keep it in mind however for future VHD/VHDX questions...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: [Solved] shrink vhd of windows guest on mac host

Post by mpack »

I'm not convinced that post belongs on these forums at all. I see a lot of external links, I see no mention of VirtualBox. Also VHDX is not officially supported by VirtualBox, which kinda limits the relevance of the info (the OP's question is about VHD, a completely unrelated format).
Post Reply