Jump to content
We're Hiring! Full Stack Developer ×

Can I shrink a Windows.qcow image that already has a VM installed on it?


JustinChase

Recommended Posts

I use an unassigned SSD to store/run my 10GB docker image and also a 50GB Windows VM.  I picked 50GB originally just because I had the room, but now I'm wanting to install another VM on the same drive, and I don't have enough room to do so.  I have almost 20GB of free space on the current windows install, so there's plenty of room available, but I dont' know if I can, nor how to shrink the qcow image.

 

If it's not possible, could I just copy the VM and docker images onto the array, then format/clear/wipe the SSD, then recreate the images but smaller?  If so, is there a guide for doing this?  I hate to screw up my otherwise good Windows VM.

 

Thanks for any help.

Link to comment
  • 3 weeks later...

not done it myself but id imagine the steps you'd need to take would be:

 

  • BACKUP YOUR QCOW IMAGE FILE
  • boot your exisitng windows VM from a gparted ISO and resize the partition

Add an ISO:

disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='mnt/user/Software/GParted.iso'/>
      <backingStore/>
      <target dev='hda' bus='usb'/>
      <readonly/>
      <alias name='usb-disk0'/>
    </disk>

  • Turn off the VM
    • resize the qcow image from the unraid command line:

    qemu-img resize /path/to/img.qcow -10GB

    • Boot the VM

     

    Id be careful to ensure that the size of the partitions inside of the qcow files are smaller than what you are re-sizing the file to. Give yourself a GB or so of wiggle room, then just expand the partition once you've re sized.

     

    Not done any of the above before, but if you take a backup, you can revert if things go pear shaped.

Link to comment

not done it myself but id imagine the steps you'd need to take would be:

 

  • BACKUP YOUR QCOW IMAGE FILE
  • boot your exisitng windows VM from a gparted ISO and resize the partition

Add an ISO:

disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='mnt/user/Software/GParted.iso'/>
      <backingStore/>
      <target dev='hda' bus='usb'/>
      <readonly/>
      <alias name='usb-disk0'/>
    </disk>

  • Turn off the VM
    • resize the qcow image from the unraid command line:

qemu-img resize /path/to/img.qcow -10GB

  • Boot the VM

 

Id be careful to ensure that the size of the partitions inside of the qcow files are smaller than what you are re-sizing the file to. Give yourself a GB or so of wiggle room, then just expand the partition once you've re sized.

 

Not done any of the above before, but if you take a backup, you can revert if things go pear shaped.

 

something like this...

 

From within the Windows VM, shrink the partition down to a bit under the final size I desire, using Computer Management > Disk Management.  If unable to shrink enough, use defrag to move files to front of disk.  (not sure this is possible/needed if VM is on an SSD.)

 

Shut down VM.

 

Copy VM (like so)

cp /mnt/disks/KINGSTON_SNVP325S264GB_40BS106CT72Z-part1/Windows8.1.qcow2 /mnt/disks/KINGSTON_SNVP325S264GB_40BS106CT72Z-part1/Windows8.1_backup.qcow2

 

Resize VM (like so)

qemu-img resize /mnt/disks/KINGSTON_SNVP325S264GB_40BS106CT72Z-part1/Windows8.1.qcow2 -{however many}GB

 

Sound about right?

 

I've done some searching, and found this as a guide...

 

On Linux

 

We need to fill the disk of the guest with zero’s (or any other character) so that the disk image can be re-compressed.

 

In a terminal, run the below command until you run out of disk space. Before running this, be sure to stop any applications running on the guest otherwise errors may result.

dd if=/dev/zero of=/mytempfile

 

Once the command errors out (this may take a while depending on your disk image size and physical disk speed) delete the file.

 

rm -f /mytempfile

 

Shutdown the guest and follow the steps below under All OS’s.

On Windows

 

You will need to download a tool called sdelete from Microsoft which is will fill the entire disk with zeros which can be re-compressed later.

 

Download: http://technet.microsoft.com/en-gb/sysinternals/bb897443.aspx

 

Once you have downloaded and extracted sdelete, open up a command prompt and enter the following. This assumes that sdelete was extracted into c:\ and c:\ is the disk you would like to use to reclaim space

 

c:\sdelete.exe -z c:

 

Once this completes (this may take a while depending on your disk image size and physical disk speed), shutdown the guest and follow the below steps under All OS’s.

All OS’s

 

The rest of the process is done on the host so open up a terminal window and SSH to your host. Move to the directory where the disk image is stored and run the below commands.

 

Make sure you have shut down the virtual machine which is using the qcow2 image file before running the below commands.

 

mv original_image.qcow2 original_image.qcow2_backup
qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2

 

The above commands move the original image file, and then re-compress it to it’s original name. This will shrink the qcow2 image to consume less physical disk space.

 

You can now start the guest and check that everything is in working order. If it is, you can remove the original_image.qcow2_backup file.

 

It seems like there might be 2 separate things I need to do then.  The first one is to make the image smaller, using the steps just above, then use the qemu-img resize command to make the allocation smaller.

 

From what I've read, I guess I wouldn't need to make the allocation smaller, as I'm allowed to over-allocate, but doing so puts me at risk of filling the disk more than it's physically capable of, which would cause bad things to happen.

 

So, I think I'd prefer to lower the allocation of the current VM, then allocate another image for the second VM I wish to create.

 

hmmm...  I hope backing up the VM will be enough to recover if this all goes horribly wrong!  :o

Link to comment

I really dont know if the following would work but it could maybe?!

1. create a new vm disk.

2. Attatch it to vm as second drive

3. Run something like a drive cloning software like acronis true image and clone the main os drive to the smaller drive.

 

like i say never tried it but dont see why it wouldnt work so long as the software can "hot image" ie clone without restarting the pc

 

Link to comment

billington.mark,

 

I'm a bit confused with the boot existing windows VM from the GParted ISO.  How do you boot the GParted ISO to access the windows VM?  The windows VM is a img file.

 

I was able to get a GParted Live ISO mounted as a CD ROM in the windows VM but can't start GParted to resize from within Windows.  I'm missing something.

 

Thanks,

moose

 

not done it myself but id imagine the steps you'd need to take would be:

 

  • BACKUP YOUR QCOW IMAGE FILE
  • boot your exisitng windows VM from a gparted ISO and resize the partition

Add an ISO:

disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='mnt/user/Software/GParted.iso'/>
      <backingStore/>
      <target dev='hda' bus='usb'/>
      <readonly/>
      <alias name='usb-disk0'/>
    </disk>

  • Turn off the VM
    • resize the qcow image from the unraid command line:

qemu-img resize /path/to/img.qcow -10GB

  • Boot the VM

 

Id be careful to ensure that the size of the partitions inside of the qcow files are smaller than what you are re-sizing the file to. Give yourself a GB or so of wiggle room, then just expand the partition once you've re sized.

 

Not done any of the above before, but if you take a backup, you can revert if things go pear shaped.

 

EDIT:

 

I was able to figure it out with a bit more searching.  The solution to force the GParted ISO to boot was to add the "<boot order='1'/>" line below.  Once the GParted ISO booted I was able to resize (reduce) the Win7 hard drive size.

 

    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='mnt/user/ISOs/GParted/gparted-live-0.24.0-2-amd64.iso'/>
      <target dev='hdd' bus='usb'/>
      <readonly/>
      <boot order='1'/>
    </disk>

 

Link to comment
  • 2 weeks later...

Well, I haven't had much luck with this, plus I want to upgrade my windows 8 to windows 10, and the 3 times I've tried to update from within the VM, all efforts have failed, so I can't even upgrade my windows install.

 

So, with that said, I'm just going to create a new VM and install windows 10 directly into a smaller image.

 

Thanks for the help anyway.

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...