myspaghetti / macos-virtualbox

Push-button installer of macOS Catalina, Mojave, and High Sierra guests in Virtualbox on x86 CPUs for Windows, Linux, and macOS
GNU General Public License v2.0
13.51k stars 1.12k forks source link

Shrink/Compact bigSur.vdi #383

Closed crbyxwpzfl closed 3 years ago

crbyxwpzfl commented 3 years ago

I want to shrink the current vdi size. VBox provides the VBoxManage modifymedium --compact command to shrink a vdi. For this I need to nullify/zero free space of the vdi. Usually you can use Diskutility to split the VmName-Data partition and then erase(/nullify/zero) the new partition.

The problem: I can not boot after I partition the VmName-Data volume with Diskutility. It stops after the countdown for startup.nsh and leaves me in the UEFI interactive shell.

Further Information: Splitting the vdi with Diskutility works without errors and the Vm keeps working perfectly fine until shutdown or reboot. I tried pointing UEFI to boot.efi manually by typing exit into the UEFI shell then going into Boot Maintenance Manager and here Boot From File but it will not boot this way as well. I created the vm with Catalina and upgraded to BigSur.

Worst case if I can not shrink the vdi I need to create a new vm and copy the files. Therefor I totally understand if this an unsupported feature. Either way Im happy for any help... ...to get it to boot again. (...on other methods to zero free space in macOS Bigsur.) (...shrinking a bigSUR.vdi in general.)

myspaghetti commented 3 years ago

People have reported success in shrinking APFS VDIs on the VirtualBox forum. Use diskutil secureErase 0 then compact the VDI.

crbyxwpzfl commented 3 years ago

Thanks for the help!!

diskutil secureErase freespace 0 "device ID of volume" gave me this error Error with secure disk erase: Secure erase by writing a run of bytes to an APFS Volume makes no sense due to its possibly-unbounded size (-69489)

I did not realise this before but secureErase essentially just writes a file of zeros. This file then gets released as freespace to the host by VBoxManage modifymedium --compact.

For me dd if=/dev/zero of=file.of.zeros bs=67108864 count=1250 worked to create the file. The option bs is the block size here (64mb=67108864b). The option count specifies the number of blocks to write to the file (here 1250blocks). (So my file.of.zeros is roughly 64mb*1250=80gb large)

Step by step what I did: 1. run this in terminal of virtual bigSur but change bs and count to specify the amount of free space you want to Release. dd if=/dev/zero of=file.of.zeros bs=67108864 count=1250 2. shutdown the vm. 3. run this in comand promt. VBoxManage modifymedium --compact "path\to\bigSur.vdi" 4. start the vm and delete file.of.zeros

This comment was very helpful for me. https://discussions.apple.com/thread/250795166?answerId=251571051022#251571051022