[TUTORIAL] Shrink Windows XP raw image for VM

gfngfn256

Distinguished Member
Mar 29, 2023
2,771
912
153
Firstly I would like to thank the Proxmox PVE staff for their excellent and tireless work in providing, such a robust system (been using for about a year now) - it just rocks!

Secondly thanks to the staff and all contributors for such an excellent forum platform. It is possibly one of the most useful - if not THE best - in the IT world! Kudos to all!


Recently I have felt that I should return something (small but possibly useful) to the community. I decided to share my notes that I have used on one of my projects.

Background: I have an old Laptop/Netbook from almost 20 years ago that runs Windows XP 32 bit (when it decides to turn on & waiting a few minutes to boot - I believe its one of the original Atom chips!).

My problem is I have many much-needed documents on it - that use a dedicated program within that environment, that won't run elsewhere.
What I did - Booted up above Laptop/Netbook with a live Linux USB (it has no cd/dvd) and used Linux DD to save a complete block raw image of its HD - this took a LONG time on my HD!

After that I spun up a new VM in Proxmox PVE using this image as its HD (referred in this post as VM <VMID> ), got everything working (not very hard) -
[Had to re-authenticate Windows XP by telephone - because of hardware changes!)

The Windows XP VM <VMID> now boots up in second/s - probably faster than the laptop/netbook by about a factor of 100x! THANKS PROXMOX PVE!!!

However after cleaning up the running Windows XP by deleting unnecessary files, I quickly realized I was using about 30% of the actual disk space (150GB).
Since I like to backup the Windows XP image often, this massive file is quite unnecessary, so I decided to take matters into my own hands. Here is the procedure. YMMV.


---------------------------------------------------------------------------------
USE AT YOUR OWN RISK - BACKUP AND THEN BACKUP!
---------------------------------------------------------------------------------



How to: Shrink Windows XP (C: Boot Drive) NTFS image.raw drive (for VM <VMID> on Proxmox)

1. Defragment HD (from running within) Windows XP VM <VMID> & shutdown VM

2. First Backup the original image (in tmux since this can be a lengthy process):

Code:
tmux
cp /path/to/location/images/<VMID>/image.raw /path/to/backup/location/image.raw

3. Add a SystemRescueCd CD/DVD disc image file (iso) to VM <VMID> and make sure it appears in VM <VMID> boot options

4. Start VM <VMID> by pressing ESC to enter SystemRescueCD GUI boot

5. Run Gparted (and apply changes!) to dev/path/to/WindowsXP/partition
Resize accordingly ONLY AT THE END OF PARTITION - IN FREE SPACE AREA
(I also left a bit of available space).

6. Reboot VM <VMID> to Windows XP - chkdsk was automatically run because of partition change. I rebooted again for software changes.

7. Check everything works. Your C: drive should now only show the new shrinked size.

8. Shutdown Windows XP VM <VMID>

9. In Proxmox host SSH (or shell from GUI):

Code:
fdisk -l '/path/to/location/images/<VMID>/image.raw'


Disk /path/to/location/images/<VMID>/image.raw: xxx.xx GiB, xxxxxxxxxxxxxxxx bytes,  xxxxxxxxxx sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: xxxxxxxxxxxx


Device                                      Boot    Start       End   Sectors   Size Id Type
/path/to/location/images/<VMID>/image.raw1            xx  xxxxxxxxx  xxxxxxxx   x.xG xx xxxxxxxx
/path/to/location/images/<VMID>/image.raw2 *    xxxxxxxx YYYYYYYYY xxxxxxxxx xxx.xG  x HPFS/NTFS/exFAT

10. Find from above, the End number of the boot partition - in example above thats image.raw2 "YYYYYYYYY" and copy this number as follows:

Code:
truncate --size=$[(YYYYYYYYY+1)*512] '/path/to/location/images/<VMID>/image.raw'

11. Run this to tell Proxmox PVE of changed file (no changes will yet be made):

Code:
qm rescan --vmid <VMID> --dryrun

12. Check output looks OK, and then run this to make changes:

Code:
qm rescan --vmid <VMID>

That's it you should be done - you will now see that the image file has shrunk accordingly.
I believe it also boots faster!

Enjoy YMMV

AND again:

---------------------------------------------------------------------------------
USE AT YOUR OWN RISK - BACKUP AND THEN BACKUP!
---------------------------------------------------------------------------------
 
Last edited: