LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   dd command help needed here (https://www.linuxquestions.org/questions/linux-general-1/dd-command-help-needed-here-675826/)

neocontrol 10-12-2008 08:18 AM

dd command help needed here
 
Hi,

I'm trying to save usb drive that has a corrupted partition table. And here's my idea of how to get it fixed, but I'm not sure if what I want to do will work.

My plan is...

take a backup of the usb drive with this command.

dd if=/dev/sdb of=/tmp/dead_usb_file bs=512

And then I thought I would reformat / partition the usb drive - in windows....

And then put back the file I saved, skipping the first 512, since I believe that is what holds the mbr and partition layout.

dd if=/tmp/dead_usb_file of=/dev/sdb bs=512 skip=1..............

My issue comes from the last command, as looking at the dd documentation it looks like I can skip the first chunk out of the input file, but how do I also skip the first chunk of the usb device since it has a new mbr / partition.

I have also thought about just trying to repartition / rebuild a mbr but I haven't looked into that one enough yet.

Thanks for any insight.

glalejos 10-12-2008 08:48 AM

Hi,

I'm not sure whether the partition table occupies only the first 512 bytes of the device, but I think is a good guess. In this case I would try to backup up only the partition you want to recover (i.e. skip the first 512 bytes when dumping the contents of the USB drive to your hard drive) and then try to mount it using the loopback device.

If you succeed in doing this, all you have to do is format your USB drive properly, mount it and then copy the files from the mounted loopback device to the USB drive using cp (or whatever method you prefer).

Good luck!,

Guillermo

jschiwal 10-12-2008 09:45 AM

The partition probably starts on block 63. This is from a 2GB pendrive that I never reformatted.
Code:

sudo /sbin/fdisk -lu /dev/sdd

Disk /dev/sdd: 2020 MB, 2020539904 bytes
32 heads, 63 sectors/track, 1957 cylinders, total 3946367 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x44a84755

  Device Boot      Start        End      Blocks  Id  System
/dev/sdd1              63    3945311    1972624+  6  FAT16

Code:

dd --help
Usage: dd [OPERAND]...
  or:  dd OPTION
Copy a file, converting and formatting according to the operands.

  bs=BYTES        force ibs=BYTES and obs=BYTES
  cbs=BYTES      convert BYTES bytes at a time
  conv=CONVS      convert the file as per the comma separated symbol list
  count=BLOCKS    copy only BLOCKS input blocks
  ibs=BYTES      read BYTES bytes at a time
  if=FILE        read from FILE instead of stdin
  iflag=FLAGS    read as per the comma separated symbol list
  obs=BYTES      write BYTES bytes at a time
  of=FILE        write to FILE instead of stdout
  oflag=FLAGS    write as per the comma separated symbol list
  seek=BLOCKS    skip BLOCKS obs-sized blocks at start of output
  skip=BLOCKS    skip BLOCKS ibs-sized blocks at start of input
  status=noxfer  suppress transfer statistics

You use seek to change where the writing starts and skip to change where the reading starts. Working with a block size of 512 is a good idea.
If you have the room, then make a copy of the backup file. Use that file as the destination for restoring the mbr and see if you can mount it.

However, if the partition part of the image on the dead_usb_file file is OK, you could just try mounting it using losetup & mount. You don't need to mess with dd.
Code:

sudo /sbin/losetup -sf /temp/dead_usb_file.bu -o $((512*63))
mkdir /tmp/deadusb
sudo mount -t vfat /dev/loop0 /tmp/deadusb -o ro,loop

Use the loop device that losetup finds. I assumed loop0 in my example.

If you can mount the partition image part, then you can simply copy the files back to the reformatted pendrive.

neocontrol 10-12-2008 10:18 AM

Thanks for the replies. Partition is toasted. That was the error I was getting when I was plugging in the usb drive. It is 2GB as well.

I'll try using seek to the same spot and fixing the mbr.

I'll let you know how it goes next.


All times are GMT -5. The time now is 07:04 PM.