Shrink VDI Disk Size to Parition Size

Discussions related to using VirtualBox on Linux hosts.
KJ
Posts: 4
Joined: 3. Jul 2013, 00:39

Shrink VDI Disk Size to Parition Size

Post by KJ »

I'm running Ubuntu 12.04 with Virtualbox 4.2. I'm running a Windows 7 Guest with the following configuration:

Code: Select all

Dynamic VDI (Real Size 320GB, Dynamic to 490GB)
Single Parition (C: 320 GB)
Unallocated Space (170 GB)
I want to shrink the VDI down to 320GB and remove the Unallocated Space to just be left with this:

Code: Select all

Dynamic VDI (Real Size 320GB, Dynamic to 320GB)
Single Parition (C: 320 GB)
NO Unallocated Space
What would be the best way to accomplish this in my current configuration?
Last edited by KJ on 5. Jul 2013, 16:32, edited 1 time in total.
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Shrink VDI to Parition Size

Post by noteirak »

Reduce the VDI size to 320GB using CloneVDI + Wine per example, as shrinking a VDI is not supported.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
KJ
Posts: 4
Joined: 3. Jul 2013, 00:39

Re: Shrink VDI to Parition Size

Post by KJ »

There is no option in ClondeVDI to reduce the size ONLY to increase the drive and/or partition. Also: Compact option did nothing to the drive. The drive is still 490GB. What I'm trying to do is to move this configuration to another system with a 320GB hard drive which is giving me problems because this drive is 490GB.

EDIT: CloneVDI DID reduce the size of the PHYSICAL vdi file from 276GB to 83.5GB; however, this is not what I'm trying to do. I'm trying to reduce the LOGICAL size of the drive in Windows 7 to be 320GB instead of 490GB.
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: Shrink VDI to Parition Size

Post by mpack »

KJ wrote:EDIT: CloneVDI DID reduce the size of the PHYSICAL vdi file from 276GB to 83.5GB; however, this is not what I'm trying to do. I'm trying to reduce the LOGICAL size of the drive in Windows 7 to be 320GB instead of 490GB.
Why? The guest OS can't use unpartitioned space, hence the VDI cannot grow larger than the partition size - so now that CloneVDI has discarded anything that was previously stored there, you shouldn't have a problem.

ps. If your goal is to ensure that the maxed out drive will still fit on the host drive then I assume you realize that the virtual drive will need to be smaller than the host drive, so you may need to reduce that partition size again.
KJ
Posts: 4
Joined: 3. Jul 2013, 00:39

Re: Shrink VDI to Parition Size

Post by KJ »

Partition size is not my problem....disk size is. The partition size is correct but the disk size is not. This is as is shown in Windows 7 disk management.
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: Shrink VDI Disk Size to Parition Size

Post by mpack »

The disk size is correct, it shows you the logical size of the disk, and it's harmless that it is larger than needed to accomodate the partition. But please yourself, I believe there's nothing to be gained by us repeating what has already been said above.

Basically, there is no supported way to shrink the capacity of a disk, except to create a new smaller disk, attach to VM, and CloneZilla the boot sectors and partitions onto it.
KJ
Posts: 4
Joined: 3. Jul 2013, 00:39

Re: Shrink VDI Disk Size to Parition Size

Post by KJ »

mpack: Thanks for the answer. That's really all I wanted as a straight answer of how to accomplish this. I'll Clonezilla it.
WFV
Posts: 94
Joined: 7. Mar 2016, 13:28
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: ArchLinux; Fedora; Ubuntu; Win7pro64; WinXP
Contact:

Re: Shrink VDI Disk Size to Parition Size

Post by WFV »

CloneVDI doesn't create a smaller vdi. When you use the clone as an existing hard disk, it will be the same dynamic size as the original. If you try to create a new hard disk dynamically set smaller, and point it to the cloned vdi, it will tell you that you can't use that file, it is already being used - even though it isn't loaded in VirtualBox anywhere. I have a 320GB dynamic guest that has 24GB WinXP installed in it. I'll never grow it to 320 and want to shrink the guest to 40GB (the vdi is 24gb but when you use it for a new instance, it reports that it is 320GB 24 used...)
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: Shrink VDI Disk Size to Parition Size

Post by mpack »

Yes, that is what the above topic already said.

The suggestion was to use a partition tool such as GParted to shrink the partition. That leaves unpartitioned sectors with data in them, which you get rid of by using CloneVDI to clone and compact the drive. The logical size of the drive will be unchanged, but it will take minimum space on the host and the potiential for regrowth is now limited by the smaller partition.
WFV
Posts: 94
Joined: 7. Mar 2016, 13:28
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: ArchLinux; Fedora; Ubuntu; Win7pro64; WinXP
Contact:

Re: Shrink VDI Disk Size to Parition Size

Post by WFV »

Thank you mpack. I was experimenting but ran into some troubles in the process. I was able to boot the XP clone with a live linux CD, and dd'd the partition to an ".img" file, (saved to a usb), then copied it to the host, and vboxmanagered it to a vdi, obviously it wasn't going to boot without its mbr and partition table. I've since tried to do about the same but instead of dd'ing the partition, dd bs=512 count=xxxxxxxx if=/dev/sda of=/path/to/file.img (in this case I mount a clean usb 64GB large enough to put a 40gb image on), "xxxxxxxx" = the end of sda1 as viewed in fdisk -l, plus 1 (example sda1 ended at 84710744, so count=84710745) but my hardware started having trouble dd would get 1 to 2GB into the write and then halt on IO errors, which I could also see in dmesg on the Linux host (not linux live boot). So right now loathe to go back and try it again, as DVD's finally recovered. My thinking is this: boot into the XP guest with live linux CD and dd as per above, to a USB mounted in the live linux session.

Code: Select all

dd bs=512 count=84710745 if=/dev/sda of=/mnt/sdb1/xp.img
- prior to starting dd I created a directory "sdb1" in /mnt of the live linux session, and mounted sdb1 (the usb) there. If I could get this, I'd now have a 40GB image of the XP vbox guest starting from beginning that would include the mbr, partition table, and C drive partition with XP on it. Then vboxmanager it to a 40GB vdi, create a new 40GB machine (guest) with this vdi, and maybe it will boot???
You're probably thinking, wouldn't it be easier to just re-install XP? yes at this point, but the one I'd like to shrink has the last of the XP updates on it and figured I give it a whirl to see if it could be done. Might try one more time, will post results back one way or other.
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: Shrink VDI Disk Size to Parition Size

Post by mpack »

No, in fact I'm thinking: why are using using that archaic dd thing instead of the tool which was suggested, which was CloneVDI?
WFV
Posts: 94
Joined: 7. Mar 2016, 13:28
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: ArchLinux; Fedora; Ubuntu; Win7pro64; WinXP
Contact:

Re: Shrink VDI Disk Size to Parition Size

Post by WFV »

I used CloneVDI, it created the clone but is still same size as the original. I needed to use CloneVDI to break-away from a working snapshot that has a bad base.
1. CloneVDI snapshot to unlinked VDI, still made a 320gb dynamic vdi even though the actual guest install is limited at 40GB (23GB used) and VirtualBox for reasons already stated doesn't shrink VDI's (basically same as a hard drive itself cannot literally be shrank).
2. boot into the new machine with a live linux CD (in this case I'm using 32bit Slacko-Puppy CD as others like Mint and Ubuntu wouldn't boot).
3. mount a 64GB usb formatted to NTFS (in the live linux session in the vbox guest)
4. dd to it as mentioned, why? because dd still does these things very well, and with the intent to get just the 40GB part of the VDI from sector 0 through the end of the C: partition (theoretically should give me mbr, partition table, and C drive).
5. unmount the USB when dd is done, shutdown the guest session.
6. Use that image on the host, vboxmanager it to a vdi
7. install the new vdi as a new 40GB guest machine in VirtualBox
8. Delete the original 320GB vdi.
I don't know that this will work, but thought it worth the try until I started running into IO problems. As mentioned before, I initially dd'd the C partition only and was able to make a vdi with it via vboxmanager but doesn't boot for obvious reason it didn't have the boot manager and partition table section in the image, I could insert the XP-install CD and add run mbrfix, but that's useless without the partition table and file allocation table, the CD would see <C:\Windows> and that the partition had 23gb used, but DIR /P would turn up empty. Same with Windows Repair - however, I'm thinking that if I get a good image from the beginning, the other Windows XP tools will work if needed. If it works, it may be a viable work-around. If not ( and I don't crash my host in the process) then I'll uninstall the old XP, and just do a new install from the ground up in a smaller VDI session as I no longer have use for tying up 320GB of the host.
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: Shrink VDI Disk Size to Parition Size

Post by mpack »

If you want CloneVDI to compact the disk, tick the "Compact" checkbox. If you don't do that then yes, the file size might well be unchanged.
WFV
Posts: 94
Joined: 7. Mar 2016, 13:28
Primary OS: Linux other
VBox Version: OSE other
Guest OSses: ArchLinux; Fedora; Ubuntu; Win7pro64; WinXP
Contact:

Re: Shrink VDI Disk Size to Parition Size

Post by WFV »

I will try that again, I thought I did check the compact option, but when I created new vdi with the clone (or actually VirtualBox creates in the process) it still lists as a 320GB guest. I'm running VirtualBox 5.0.14 on an Arch Linux host. My host is latest stable release, and latest rev of VirtualBox I can install in Arch presently although I see VBox on Oracle is now at 5.0.16, Arch will probably have the 16 version in a week or two.
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: Shrink VDI Disk Size to Parition Size

Post by mpack »

Make sure you aren't using snapshots, unless you know how this works with snapshots. Many people seem to assume that they can modify the base disk of a snapshot chain and somehow that will affect the latest snapshot state that the VM sees.
Post Reply