Laptop I want to buy comes with preinstalled windows 10. I want to install linux on this laptop but I want to have option to recover windows (reset to factory settings). Because of that I want to make bit by bit image of whole disk (all partitions, MBR, etc). Of course I must have an option to "flash" this image to disk so laptop is reverted to "factory settings" with preinstalled windows. What is the best option to create such image and how do I later recover such image?

I will boot linux from usb and attach much bigger hard drive throught usb. I will store backup on this usb hard drive. I was thinking about dd command but it won't take in account empty space so it will produce huge image.

closed as unclear what you're asking by Kusalananda, Wildcard, countermode, Anthon, user34720 Jan 23 '17 at 9:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Do you have access to at least the same amount of storage that the laptop's hard drive is large? If you do, it's easy; if you don't, it's rather more involved. – a CVn Jan 21 '17 at 13:32
  • I will boot linux from usb and attach much bigger hard drive throught usb. I will store backup on this usb hard drive. I was thinking about dd command but it won't take in account empty space so it will produce huge image. – Trismegistos Jan 21 '17 at 13:34
  • Your comment is inconsistent with your question. You say you want a bit by bit image of the WHOLE disk—all partitions—but then refer to "free space" which is a filesystem-level concept. Unless you meant space outside of any partition, but that would still be part of the bit-by-bit image of the whole disk. Do you want bit level duplication or not? – Wildcard Jan 22 '17 at 12:22
  • 1
    What @Wildcard means is that a proper image includes empty space. Unless you are working for Microsoft. Obviously you can zip the image, free space is very compressable. – jiggunjer Jan 22 '17 at 13:22
  • 1
    I'm voting to close this question as off-topic because it asks about creating an image of one or more windows partitions. There is software for this; if you want to know exactly how, SuperUser might be the place to ask. – countermode Jan 23 '17 at 7:14

You can try using built-in Windows backup tools to create a system image of your Windows OS. Here's a tutorial on it on Windows Central.

You could also shrink a partition and create a new one on which you could install Linux and dual-boot, but for begginer users it might be risky and could result in a complete data loss if you partition wrong.

  • Also the Windows tool is limited to 2TB images. – jiggunjer Jan 22 '17 at 13:26

Most computers with OEM preinstalled Windows come with a tool to create "recovery disks", either USB or CD/DVD. If yours does, that's the best option. You can then optionally make a copy of that recovery media to a file on disk, or back them up in any other normal way.

If yours doesn't, then you will have to resort to brute force. Since you have sufficient disk space to make a full copy of the hard disk, that's probably the best bet. To do that, you would:

  • Boot from a live Linux media, and make sure that the file system on the internal hard disk is not mounted, but that the target drive is. For the purposes of this brief guide, I will assume that you have unmounted the file system(s) on the internal drive, and mounted the file system on the target drive on /mnt.
  • Run ddrescue to copy the internal hard disk, including partition tables etc., to a file on the target media. ddrescue is preferable to dd because it has far better handling of any read problems it might encounter. (This is mostly a concern with marginal media, but when the plan is to create an image to be used much later to restore the computer to its factory condition, being cautious doesn't hurt.) A good start might be running sudo ddrescue -b 4096 -c 1024 -p /dev/sda /mnt/laptop-copy.sda in a terminal. (The values for -b and -c aren't critical, but can be used to improve performance.) See man 1 ddrescue for a description of these options.
  • If desired, split the output file into chunks using something like split. For example, you could use something like split -a 2 -d -n 100 /mnt/laptop-copy.sda to split the image into 100 equal-size chunks. These can later be recombined using cat.
  • If desired, compress the output file(s). Note that depending on the actual disk contents, compression might not be terribly effective. Something like gzip -1 /mnt/laptop-copy.sda* should be plenty enough; the data will either be trivially compressible, or hardly compressible at all.

To restore the laptop to its factory software condition, simply reverse these steps:

  • Make sure the chunks or image file is available (obviously).
  • Uncompress the chunks, if applicable, using gunzip.
  • Recombine the chunks, if applicable, using cat.
  • Write the data out to the drive, using something like sudo ddrescue -b 4096 -c 1024 /mnt/laptop-copy.sda /dev/sda.

You can create a compressed image:

gzip < /dev/hdd > hdd.img.gz

To restore:

gunzip < hdd.img.gz > /dev/hdd

If it's never been used, fresh out of the factory, free space shouldn't be randomized and the resulting compressed image should be quite small.

If free space is zero, you could also do a sparse image (on a filesystem that supports sparse files):

dd bs=4K conv=sparse if=/dev/hdd of=hdd.sparse.img

To restore: (must not use sparse on restore)

dd bs=1M if=hdd.sparse.img of=/dev/hdd

To check how much storage space that file actually uses:

du -h hdd.sparse.img

The advantage of the sparse image is that it's uncompressed and appears with the original size (while not actually using space for zeroes), so you could mount it read-only or try to boot it in a virtual machine. If VM is your goal you might also be interested in qemu-img.

Doing it in-place would be interesting. It would involve caching as much of the data in RAM, then create a filesystem (at offset 1MiB) no larger than what's already been cached, then fill it with the above, and grow the filesystem along with it. Afterwards you can create a partition that starts at 1MiB and whatever size it ended up with to act as your recovery partition. This is the involved solution, and dangerous if the process is interrupted.

You could do it blindly like thus:

gzip --verbose < /dev/hdd > /dev/hdd

but that makes assumptions like, compressed always smaller than uncompressed data, and won't write gzip header before reading data and such things. It might work (example below) but there's no guarantees.

# md5sum /dev/loop0
dd409e37f092ce049c396b99b32366fb  /dev/loop0
# xz --verbose < /dev/loop0 > /dev/loop0
  100 %   1,027.4 KiB / 8,192.0 KiB = 0.125                                    
# unxz --verbose < /dev/loop0 | md5sum
  --- %   1,027.4 KiB / 8,192.0 KiB = 0.125                                    
unxz: (stdin): Compressed data is corrupt
  --- %   1,027.4 KiB / 8,192.0 KiB = 0.125                                    
dd409e37f092ce049c396b99b32366fb  -

And then you still have to figure out what to do with it. In this example you'd have to move 1028 KiB elsewhere (shift by 1MiB, create partition of this size, then you have your recovery partition).

Might also consider doing it backwards (so the image ends up at end of disk).

  • "free space should actually be zero" That's actually a big if. I have personally seen what -- without having examined it in detail -- appeared to be the opposite case. – a CVn Jan 21 '17 at 13:55
  • If it's not zero then that's just your bad luck. It will still work regardless - with a much larger image - but if a bit perfect copy is desired then that's that. – frostschutz Jan 21 '17 at 17:13
  • Of course it will still work; I'm just pointing out that there are cases where the free space hasn't been wiped clean. As for why that is, I haven't investigated it. – a CVn Jan 21 '17 at 21:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.