0

I had made a complete image (at least I think I have) using dd if=/dev/sda of=/mnt/sda.img bs=32M where mnt is an external HD I use.

I came to restore that image this morning, so I booted up on a live USB and mounted the external HD and ran dd if=/mnt/sda.img of=/dev/sda bs=32M, I rebooted and it transpires that drive is missing it's MBR and partition table. I tried writing to the disk again with a much smaller blocksize, I think about 64k, however that made no difference.

Have I somehow failed to backup the whole disk? (i.e. have a lost the MBR?)

I ran file on sda.img and it identifies it as ext4 data.

EDIT: (in response to mpy)

It was in my understanding that the commands I ran should have imaged the whole disk, from the very first sector to the last.

sfdisk -l /mnt/sda.img yields

sfdisk: Disk sda.img: cannot get geometry

Disk sda.img: 7294 cylinders, 255 heads, 63 sectors/track

I have two images on this hard drive, one with linux installed, the other with win 7. The linux image isn't the same size, it's smaller, presumably dd didn't copy everything.

ls -l

..... 60003385344 May 29 14:19 sda.img

..... 64023257088 May 31 13:08 sda-win.img

I appreciate the fact I should have checked the image size before wiping my HD now... Would it be possible to try and restore this image (say, just assume the missing 4GB was just empty space)? Interestingly I can loop mount the image and have access to what looks like most of the data (I can't see anything missing)

3
  • 1
    In principle your MBR should be included in the image, too. (However, I usually omit the bs=... parameter completely). Can you include the output of sfdisk -l /mnt/sda.img? And, can you tell from the size of the image vs. whole hdd if some partitions are missing?
    – mpy
    Commented Jun 1, 2013 at 10:54
  • Please mount /mnt only read-only, so a typo in a command won't modify your backup. mount -o ro,remount /mnt
    – mpy
    Commented Jun 1, 2013 at 10:59
  • 1
    Question amended with response. I can also chroot into the mounted image, it looks like everything is working.
    – Will
    Commented Jun 1, 2013 at 12:46

1 Answer 1

1

If you can loop mount your image, this hints that the image only includes a partition. Usually you should get an error if you try to loop mount an image of a whole disk:

# mount -o loop sda.img /mnt/loop
mount: you must specify the filesystem type

This gets verified by file which also reported a ext4 filesystem for you. For a whole disk image file should identify a boot sector:

# file sda.img
sda.img: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, stage2 address 0x2000, stage2 segment 0x200, GRUB version 0.97; partition 1: ID=0x83, active, starthead 32, startsector 2048, 77607966 sectors; partition 2: ID=0x82, starthead 254, startsector 77611008, 4194304 sectors; partition 3: ID=0x83, starthead 254, startsector 81805312, 894967808 sectors, code offset 0x48, OEM-ID "      \320", Bytes/sector 190, sectors/cluster 124, reserved sectors 191, FATs 6, root entries 185, sectors 64514 (volumes <=32 MB) , Media descriptor 0xf3, sectors/FAT 20644, heads 6, hidden sectors 309755, sectors 2147991229 (volumes > 32 MB) , physical drive 0x7e, dos < 4.0 BootSector (0x0)

For reference, here is the output of sfdisk -l for a disk image (sda.img) and a partition image (sda1.img):

# sfdisk -l sda.img
Disk sda.img: cannot get geometry

Disk sda.img: 0 cylinders, 0 heads, 0 sectors/track
Warning: The partition table looks like it was made
  for C/H/S=*/255/63 (instead of 0/0/0).
For this listing I'll assume that geometry.
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
 sda.img1   *      0+   4830-   4831-  38803983   83  Linux
 sda.img2       4831+   5092-    262-   2097152   82  Linux swap / Solaris
 sda.img3       5092+  60801-  55710- 447483904   83  Linux
 sda.img4          0       -       0          0    0  Empty

# sfdisk -l sda1.img
Disk sda1.img: cannot get geometry

Disk sda1.img: 0 cylinders, 0 heads, 0 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 sda1.img: unrecognized partition table type
No partitions found

Hence, I assume, that your sda.img contains only a partition. That is better than nothing. Guessing(!) from the different sizes (64023257088-60003385344 = approx. 4 GB), I suppose you only lost your swap partition, which doesn't hurt.

So, what to do?!

  1. partition your HDD, with sda1 = 60003385344 bytes (sda2 = the rest)
  2. write back your image to sda1.
  3. restore the boot loader
    • how to do that depends on which one you used
    • for GRUB:
      1. mount sda1 (e.g. mkdir /root/part1 && mount /dev/sda1 /root/part1)
      2. chroot /root/part1
      3. update-grub (this is the Debian way, depends on your distro)
4
  • 1
    Thank you very much mpy, that was a very thorough and informative answer. I especially appreciate that you explained the reasoning behind your suggested actions, that's most laudable.
    – Will
    Commented Jun 1, 2013 at 14:23
  • 1
    I've just rebooted back into my old system :) Worked a treat, initially I formatted the sda1 with the wrong size, and had errors reinstalling grub as it suggested I had multiple filesystem(?) labels, I reformatted at the correct size and grub installed happily.
    – Will
    Commented Jun 1, 2013 at 15:46
  • @WillPrice: I'm really glad to hear that! Thanks for your feedback.
    – mpy
    Commented Jun 1, 2013 at 15:48
  • I will also add that now my system is back up and running, I've created another image that is identified as a x86 boot sector with file. I must have accidentally used sda1 instead.
    – Will
    Commented Jun 1, 2013 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .