restore from entry disk image with dd, to unallocated space or specific partition
Solution 1:
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
clone 'everything' from the backup image to an extra drive
clone the first partition from the extra drive to the first partition of the target drive.
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.
Solution 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
.
-
Mount the file system on
/dev/sda5
.-
You can use the file manager or Gnome Disks to mount the file system.
-
Alternatively on the command line:
udisksctl mount --block-device /dev/sda5 --options rw
udisksctl
will tell you where the drive was mounted.
I'll assume that the mountpoint is located at
/media/ubuntu/my-data
. Adjust the following commands according to the actual mountpoint. -
-
Download the compressed drive image onto the previously mounted file system
-
using a web browser or
-
your favourite HTTP client,
-
e. g. on the command line:
wget -P /media/ubuntu/my-data 'http://your_webhosting.com/Windows10template.img.gz'
-
-
Decompress the image and store the output on the same file system.
-
You can use an archive manager like File-Roller (which your file manager should open automatically as the default action on a
.gz
file) or -
a command line tool, e. g.:
gunzip /media/ubuntu/my-data/Windows10template.img.gz
-
-
Set up a loop device for the decompressed drive image.
-
You should be able to tell your favourite graphical file manager to mount the image file (via its context menu) and to figure out the device path through the properties dialogue of the newly created loop device (accessible via its context menu).
-
Alternatively on the command line:
udisksctl loop-setup --file /media/ubuntu/my-data/Windows10template.img --read-only
udisksctl
will show you the path to the loop device.
I'll assume that the loop device path is
/dev/loopX
. Adjust the following commands according to the actual device path. -
-
Normally, in both cases,
udisksctl
will tell the kernel to detect the partition table inside the loop device (backed by the drive image) and to add additional block device nodes for the detected partitions, i. e./dev/loopXp1
and/dev/loopXp2
.If it didn't:
sudo partprobe /dev/loopX
-
Copy the previously exposed partitions individually to the target device.
-
You can use a graphical partition manager like Gnome Disks or GParted (which are the safer route since the nature of the target device is more obvious with some contextual graphical info which makes accidental selection and subsequent data loss less likely; they outright forbid alteration of partitions in use).
-
Alternatively on the command-line:
sudo dd if=/dev/loopXp1 of=/dev/sdX1 bs=8M sudo dd if=/dev/loopXp2 of=/dev/sdX2 bs=8M
Replace
loopX
andsdX
1 with the appropriate device names. Double-check that you typed in the correct target device path and that the/dev/sdX
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/sdX
by looking at its partition layout (e. g. withfdisk /dev/sdX
or Gnome Disks) or use the unique and (mostly) stable device identifiers in/dev/disk/by-*
.
-
-
Verify that
/dev/sdX1
and/dev/sdX2
contain the intended data. -
You're done. Reboot into Windows or do whatever.
-
Optional clean-up:
-
If you want to delete the drive image before the reboot in the previous step, remove the loop device first before you are allowed to do so.
-
You can use the file manager again or Gnome Disks, both of which should offer an eject button associated with the loop device.
-
Alternatively on the command-line:
udisksctl loop-delete --block-device /dev/loop7
-
-
Delete the leftover drive image whenever and as you like.
-
1sdX
is a placeholder that does not exist in any sane default *nix setup to avoid data loss by people who copy and paste the verbatim command into their terminal by mistake.