How to reduce a dynamic disk's MAXIMUM size

This is for discussing general topics about how to use VirtualBox.
Post Reply
jfgafp
Posts: 7
Joined: 1. Mar 2011, 21:27
Primary OS: Mac OS X Leopard
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: WinXP, Ubuntu10

How to reduce a dynamic disk's MAXIMUM size

Post by jfgafp »

To reduce a dynamic disk's maximum size might seem at first useless, but it might well be quite interesting!

Consider the case of a dynamic disk which is initially given a large maximum size. Guests (as Windows) may allocate disk sectors which, even after being freed, remain occupying virtual disk space. Although this useless and potentially quite large space can be compacted, this requires a manual and annoying operation to be repeated regularly. On the other hand, if the dynamic disk's maximum size isn't much larger than the currently needed space (let's say no more than 50% larger), those used-then-freed sectors will be automatically reused when disk occupation grows, or else, at worst that useless disk space is reasonably bounded.

So, if one has a dynamic disk's maximum size much larger than currently needed (either because a too large size initially given, or after removal of large quantities of data), it is quite practical and space-efficient to reduce it. Of course, when the disk space needed grows (again) it is relatively easy to expand the maximum size and then the corresponding partition.

Unfortunately, I haven't been able to find a practical way of reducing the dynamic disk's maximum size for Windows and LInux guests.

Could anyone help? Thank you very much in advance.

- José

PS: I currently use VirtualBox 4.1.8 with Mac OS 10.6.8 as host on an Intel Core i3 iMac with Windows XP pro and Ubuntu 10 as guests.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: How to reduce a dynamic disk's MAXIMUM size

Post by mpack »

You don't need to reduce the maximum size of the drive, just reduce the size of the main partition (e.g. using GParted). Disk usage can't grow beyond the partition(s).

If you only shrink the partition then useless data sectors will remain on the drive in formerly partitioned areas, and VirtualBox will faithfully preserve these. One pass with CloneVDI (with "Compact" enabled) will get rid of them however.

One warning: severely restricting the natural size of a drive may lead to congestion, fragmentation of the guest filesystem, and hence poor performance.
jfgafp
Posts: 7
Joined: 1. Mar 2011, 21:27
Primary OS: Mac OS X Leopard
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: WinXP, Ubuntu10

Re: How to reduce a dynamic disk's MAXIMUM size

Post by jfgafp »

Thank you very much, mpack. I've been working on it, and here is what I did, my conclusions and remaining question.

I downloaded GParted Live CD as ISO file which I attached when needed to the VMs (it starts a graphical Debian, which runs GParted).

Regarding the WinXPpro guest (which had several snapshots), I installed SDelete from Microsoft and then fully cloned its current state, and started it (pressing fn+F8) in safe mode: configured to no-pagefile, defragmented (unique) partition C: and shut. Then I attached to the IDE controller of the original WinXPpro guest the clone VDI (shall be seen as E:) and started it: first in the console made chkdsk E: /F /R, then (again) defragmented E:, back in the console launched cd E: and then sdelete -z (the VDI size didn't change), then shut. From the Mac OS terminal, in the clone VM directory, I then run VBoxManage modifyhd <filename>.vdi --compact. Then started the clone VM with GParted and reduced the C: partition. Finally, the clone VDI file occupies a quite adjusted size and runs well (XP asked a reboot for the drivers). Configured back the pagefile.

Rearding the Ubuntu10 guest (also with several snapshots), I installed zerofree and then fully cloned its current state. But I wasn't able to go into "init 1" (recovery) mode, even after selecting it from the grub menu. What I did is to start the clone VM with GParted, reduced the /dev/sda1 partition, add a new (sda3) partition on the empty space, and on its terminal run

Code: Select all

sudo mount -o ro -t ext4 /dev/sda1 /mnt
sudo /mnt/usr/sbin/zerofree -v /dev/sda3
(the VDI size didn't change), then I removed sda3 and shut. Then compacted the VDI as before, although the resulting size doesn't seem as adjusted as in Windows, even a zerofree on sda1 and compact had no effect.

In both cases, renamed the clone VM and took a snapshot. With VirtualBox not running, I renamed the clone VDI file itself and correspondingly edited with emacs the XML line in the .vbox file. Of course, I kept the dynamic VIDs' maximum sizes. I'm planning to extend the partitions as needed, keeping their free space between a minimum of 25% and maximum of 50%, without having to delete snapshots (I want to preserve a small number of them in order to limit the impact of modifying VDIs on Time Machine backups).

I still wonder whether I could have applied the same zero+compact procedure to the original VMs without deleting their snapshots: would modifyhd compact through the snapshot chain?
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: How to reduce a dynamic disk's MAXIMUM size

Post by Perryg »

Snapshots rule out any way to change the size of the guest.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: How to reduce a dynamic disk's MAXIMUM size

Post by mpack »

I don't know why you went through that rigmarole with sdelete and modifyhd. CloneVDI could have done all that in a few minutes and in one pass; and I know you're familiar with CloneVDI, because I checked your previous posts before recommending a course of action in this thread! Still, if what you did works for you then that's all that matters. As Perry mentioned, I would avoid all use of snapshots if you want this kind of image manipulation to be problem free in future.
jfgafp
Posts: 7
Joined: 1. Mar 2011, 21:27
Primary OS: Mac OS X Leopard
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: WinXP, Ubuntu10

Re: How to reduce a dynamic disk's MAXIMUM size

Post by jfgafp »

I think you're right, mpack, CloneVDI would normally be quite easier, but in a Mac OS host that would mean, as you told me at the time, to install (and learn, what I possibly should!) Wine and try (*), so when I found the SDelete/zerofree way, plus your suggestion of using GParted and keeping the disk's maximum size, it seemed to me worth trying first. At the same time, I didn't like to go again through the process of copying the VDI file to external disk, back and forth the Windows machine, however I also discovered that SDelete and zerofree are quite slow (nearly 20 min for a dynamic disk with a maximum size of 256 GB, otherwise mostly empty).

Anyway, the most important corollary to me is: when creating a new machine, build a large dynamic disk with an initally small partition (I'd say 10GB, or double-size what you initially plan to use), and enlarge it as needed by attaching a GParted Live CD.

Again, thank you both for the advice. Best regards - José

(*) Since I felt I somehow owed it to you, I decided to try (later I found in CloneVDI's release notes that it was already known to run on Mac OS). I've installed WINE on my Mac OS, downloaded and unpacked CloneVDI, and run the Wine app to open CloneVDI.exe, the charset is almost readable, Wine asked for Gecko and while I was figuring out the CloneVDI window opened.

I first selected the VDI file of a newly cloned WinXPpro, marked to keep UUID and compression, it took less than 5 min!, then renamed the compacted disk to the non-compacted name on the same directory and the WinXPpro run perfectly! Only that the compacted disk is larger than the one I got before, but I suppose it's due to the fact that before using CloneVDI I hadn't neither defragmented the original VDI before cloning nor reduced the partition size (with GParted).

Finally, I have to admit that it's quite easy. So, I've also tried CloneVDI on my Ubuntu10: created a new clone from the original VM, started it with GParted Live CD, reduced the partition near to the minimal size twice (the used space reported by GParted gets reduced to about 50%, actually the correct value, after the first partition size reduction!?; the second partition reduction actually increases the VDI size!), applied CloneVDI and got a better compaction than I had before!!, renamed the destination VDI to the source name, restarted with GParted to double the partition size, and started the resulting Ubuntu10 VM, which run perfectly!
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: How to reduce a dynamic disk's MAXIMUM size

Post by mpack »

Well, your experiments reminded me of one reason why I recommended CloneVDI for your use case: sdelete/zerofree can only zero space that is part of a filesystem. If you shrink a partition and then use sdelete then the shrink area is not covered by the filesystem and hence not zeroed. CloneVDI does recognize and discard data from unpartitioned areas of the disk.

And running sdelete before shrinking the partition would almost certainly be of little help, since shrinking will likely involve moving files, leaving behind data footprints after shrinking.
Post Reply