13

I have 8GB USB drive attached to my system which looks like this:

[root@host]# fdisk -l /dev/sdb

Disk /dev/sdb: 8462 MB, 8462008320 bytes
255 heads, 63 sectors/track, 1028 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5c0894d9

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1           9       72261    e  W95 FAT16 (LBA)
/dev/sdb2              10         103      755055   83  Linux
[root@host]# 

So basically my FAT partition is around 70 MB, ext2 partition is around 740MB and rest of the space (~ 7 GB) is unallocated . Now when I dd my USB hard drive via:

dd if=/dev/sdb of=myimage.img bs=1M

the output file (myimage.img) is around 8GB which is the normal operation of dd.

Question: What I am looking for is a way to directly clone my USB hard drive without the unallocated space so that my result file is around 1 GB uncompressed instead of 8 GB. The reason I am asking is because the output file (myimage.img) is being used by a simulator program to boot the image. The simulator can handle 8 GB files but I don't want to waste my disk space.

modest
  • 231
  • 1
  • 2
  • 4
  • 1
    This is somewhat similar problem with interesting solution: http://serverfault.com/q/281628/141604 – week Nov 08 '12 at 00:46

2 Answers2

15

If I understand correctly, you want to create an image from the start of the disk to the end of the last partition.

The parameter for dd that does this is count=. Your last partition ends on 103, and count will need to be 1 extra (104) and your unit size is 8225280 bytes (according to the fdisk -l output). So you could simply modify your command this way:

dd if=/dev/sdb of=myimage.img bs=8225280 count=104

I would, however, suggest that you run fdisk -u -l /dev/sdb instead. Cylinders are not really relevant in this age anymore, so you are better off if you see the sector count to avoid any rounding errors. Then you will have to run:

dd if=/dev/sdb of=myimage.img count=...

where count will be set to the number you got from fdisk -u -l at the end of the last partition plus one, instead of what used to be 104. The default block size for dd is 512 bytes, which is also the unit that fdisk -u -l will use in the output.

The backup partition table of a GPT partition is stored at the end of the disk, but since you are not using GPT you'll be fine.

matt.baker
  • 103
  • 2
chutz
  • 7,569
  • 1
  • 28
  • 57
2

You just need to tell dd to only read the part of the drive you're interested in. The parameter you're looking for is count.

So take your cylinder size of 8,225,280 and multiply times the number of cylinders in use of 103 and you get 847,203,840 bytes. Since you are using a one megabyte block size, convert that bytes to megabytes which is 808.

So your command is dd if=... of=... bs=1M count=808

longneck
  • 22,793
  • 4
  • 50
  • 84