3

I have already created an image with dd for the entry disk the image for windows 10, and I restore it from Ubuntu recovery mode

When I want to restore the windows I erase hdd and remove all partitions

wget -O- 'http://your_webhosting.com/Windows10template.img.gz' | gunzip | dd of=/dev/sda

Image size is 5GB compressed (gz format), 20 GB uncompressed

I had three partitions

 Device     Boot     Start        End    Sectors   Size Id Type
    /dev/sda1  *         2048    1026047    1024000   500M  7 HPFS/NTFS/exFAT
    /dev/sda2         1026048  208089944  207063897  98.8G  7 HPFS/NTFS/exFAT
    /dev/sda3       208089949 1953520064 1745430116 832.3G  f W95 Ext'd (LBA)
    /dev/sda5       208090012 1953520064 1745430053 832.3G  7 HPFS/NTFS/exFAT

All I needed was to restore the image which created from entry disk to sda1 and sda2 only, and keep sda5 (my own files)

I removed sda1 and sda2, and created a new partition sda1 100GB and restored the image with

wget -O- 'http://your_webhosting.com/Windows10template.img.gz' | gunzip | dd of=/dev/sda1

but nothing changed. My partitions are now like this

Device     Boot     Start        End    Sectors   Size Id Type
/dev/sda1            2048  195311615  195309568  93,1G 83 Linux
/dev/sda3       208089949 1953520064 1745430116 832,3G  f W95 Ext'd (LBA)
/dev/sda5       208090012 1953520064 1745430053 832,3G  7 HPFS/NTFS/exFAT

The normal and working method erased the hdd completely. I need a working method to restore it to one partition only or unallocated space

/dev/sda1

How to do this with dd?

UPDATE

$ fdisk -l Windows10template.img
Disk Windows10template.img: 20 GiB, 21474836480 bytes, 41943040 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: 0x266de740

Device                 Boot   Start      End  Sectors  Size Id Type
Windows10template.img1 *       2048  1026047  1024000  500M  7 HPFS/NTFS/exFAT
Windows10template.img2      1026048 41940991 40914944 19,5G  7 HPFS/NTFS/exFAT
  • 1
    I'm not exactly sure what you're asking. How (or, more precisely, of what) did you create the drive image? If it contains multiple partitions, what's the partition layout inside the image (use fdisk -l Windows10template.img)? In any case it would be best if you downloaded the drive image to a local file so you can 1) inspect it and 2) restore parts of it more easily. – David Foerster Jan 3 '17 at 10:10
  • edit the first post – ezak Jan 3 '17 at 10:16
  • the problem is image is large 20GB, I can't download it in local rescue mode (only give me 8GB and my rams). I need to restore from the img directly to the sda1 (should after restore its repartition sda1 to sda1 and sda2) – ezak Jan 3 '17 at 10:22
  • 1
    You can use regular storage in rescue mode via mount. It's possible to do all of this on streams (e. g. on the output of zcat) but it's going to much harder and involve manual offset and length calculations and typing which, when done incorrectly, may result in the loss of any or all current data on the drive. – David Foerster Jan 3 '17 at 10:36
2

Since the partition boundaries on the drive backup image line up with a subset of those of the current disk layout it would be relatively simple to cut out the relevant part out of the gunzip output and write it to the right parts of /dev/sda with dd, but I'm not going to write an answer that is going to help only you yet overwrite anybody else's data. Even for yourself there's a large risk to make a wrong offset calculation or to type in a wrong number to the same result.

Instead it would be much better to decompress the drive image onto a large enough storage medium and operate on that. You can do that even in recovery mode. But it would be much easier if you did this with a graphical user interface (e. g. from a live DVD/USB) – even if you end up doing most of this on a terminal.

I assume that there are at least 25 GB (enough to hold the compressed and the decompressed backup image) free on the file system on /dev/sda5.

  1. Mount the file system on /dev/sda5.

    • On the terminal that works with:

      udisksctl mount --block-device /dev/sda5 --options rw
      

      udisksctl will tell you where the drive was mounted.

    • Alternatively you can use the file manager or Gnome Disks to mount the file system.

    I'll assume that the mountpoint is located at /media/ubuntu/my-data. Adjust the following commands according to the actual mountpoint.

  2. Download the compressed drive image with a web browser or your favourite HTTP client onto the previously mounted file system, e. g.:

    wget -P /media/ubuntu/my-data 'http://your_webhosting.com/Windows10template.img.gz'
    
  3. Decompress the image and store the output on the same file system. You can use an archive manager like File-Roller or a command-line tool, e. g.:

    gunzip /media/ubuntu/my-data/Windows10template.img.gz
    
  4. Set up a loop device for the decompressed drive image:

    sudo losetup -f --show /media/ubuntu/my-data/Windows10template.img
    

    losetup will show you the path to the loop device. I'll assume it's /dev/loop7. Adjust the following commands according to the actual device path.

  5. Tell the kernel to detect the partition table inside on the loop device (backed by the drive image):

    sudo partprobe /dev/loop7
    

    The kernel will add additional block device nodes for the detected partitions, i. e. /dev/loop7p1 and /dev/loop7p2.

  6. Copy the previously exposed partitions individually to the target device. You can use a partition manager like Gnome Disks or GParted or dd. For the latter this will do:

    sudo dd if=/dev/loop7p1 of=/dev/sda1 bs=8M
    sudo dd if=/dev/loop7p2 of=/dev/sda2 bs=8M
    

    Double-check that you typed in the correct target device path and that the /dev/sda is actually what you think it is! Don't rely on the kernel to reassign the same name to a block device across system reboots!

    You can verify the identity of /dev/sda by looking at its partition layout (e. g. with fdisk /dev/sda or Gnome Disks) or use the unique and (mostly) stable device identifiers in /dev/disk/by-*.

  7. Verify that /dev/sda1 and /dev/sda2 contain the intended data.

  8. You're done. Reboot into Windows or do whatever.

  9. Clean up the leftover drive image whenever and as you like. Remove the loop device for it (if you didn't reboot in the mean time) or it won't actually be deleted until you do:

    sudo losetup -d /dev/loop7
    
4

dd is a very powerful but also very dangerous tool. Please backup whatever is new on the target drive (for example in partition /dev/sda5) before you start tampering with dd.

I think the least dangerous method would be to

  1. clone 'everything' from the backup image to an extra drive

  2. clone the first partition from the extra drive to the first partition of the target drive.

  3. But you have damaged the head of the drive (the first mibibyte), so you need to clone that part from the backup image to the target drive too. You can use count=N to copy only N blocks and the default block size is 512 bytes.

Another faster, but more risky method would be to clone the first part of the backup, from the head end until the end of the first partition.

  • sorry I don't now how to do this, I have edit the first post to show what img include, I want img1 as sda1 and img2 as sda2 . how to retore it with this way – ezak Jan 3 '17 at 10:45
  • Would it be possible for you to buy or borrow an external drive with at least 20 GiB, for example a USB 3 pendrive with 32 GB? It would make the process safer and easier compared to doing it directly from the compressed image 'soaring in the cloud'. – sudodus Jan 3 '17 at 11:06

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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