problem: you have a windows PC with interesting software on it, but it has a 500GB disk, of which only 18GB is used. how do you produce a disk image from this that doesn't suck ass to work with?
you can dd it, but nobody wants you to upload a 500GB image to internet archive. you can zip it, sure, but if the drive experienced fragmentation, etc. then there may be junk data that will cause the image to be much bigger than it has to be. 18GB is bad enough; if the zip ends up being 48GB zipped due to random junk sectors, that's super un-ideal. even worse, you still need 500GB of free space to extract it before you can so much as look at the contents. ugh.
okay, Just Use Ntfsclone. mmm, well, problems there too. by default, ntfsclone... just dds the partition, apparently? it's not actually clear why this mode exists, nobody seems to have an explanation. most of the tool's magic is in the confusingly named --save-image flag, which enables its secret sauce. they just call this "the special image format."
on its face this does exactly what you want. if the drive has 18GB of actual files on it, "ntfsclone --save-image [destination].ntfsclone /dev/[source]" will produce an 18GB image with no wasted space even if there's junk data on the disk.
new problem: nothing can read it. the "special image format" is completely unique to ntfsclone and nobody has bothered to implement support for it. you can't mount it as a loopback device, 7zip won't open it, etc. The only possible thing you can do with one of these is write it back to a block device with ntfsclone. ugh.
i've been trying to figure out what to do about this. copying the files themselves isn't an option because they never have installers; installed programs are useless without the OS and it's incredibly complex profile and registry data structures, which can't be easily identified or extracted. so you need to copy the OS in a replicatable, bootable state, but nobody wants an image file that's completely uninspectable in this year of our lord.
so here's my new idea:
- use ntfsresize to shrink the partition to the size of its contents plus a small margin; 20GB, say
- suppose the partition in question is sda1.
- ntfsresize --info /dev/sda1 - this will tell you how much space is in use
- ntfsresize -s 20G /dev/sda1 - shrinks the partition
- use fdisk to shrink the partition
- fdisk /dev/sda
- delete the OS partition. it needs to be the last partition on the disk. if it isn't, you're fucked, give up.
- create new partition and specify the size as +20G
- set the type to 7 for NTFS
- write
- use dd to save the MBR to a file
- dd if=/dev/sda of=machine.mbr.img bs=512 count=63 - this is not a magic number, it's the same for all drives.
- use dd to save the partition itself to another file
- **dd if=/dev/sda1 of=/destination/machine.part1.img bs=4M status=progress
- zip the result
now you have an image that's as small as possible, and which you can open in 7zip. to restore it:
- dd the MBR to your destination disk
- dd if=machine.mbr.img of=/dev/sdb
- refresh the partition table
- blockdev --rereadpt
- dd the disk image to the partition
- dd if=machine.part1.img of=/dev/sdb1 bs=4M status=progress
- boot into Windows from the newly imaged disk
- resize the partition to fill the disk using Disk Management
I've tested this and it seems to work. This was on Windows 8, but should work fine with Vista or newer. XP lacks the ability to resize partitions as I recall; I was unable to get ntfsresize to play ball, for some reason it gets mad about the backup file map not matching, I don't know how to fix that and neither does anyone else it seems. So XP/2000 are still wildcards, but for anything newer this should work.
