Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
This is somewhat similar problem with interesting solution: serverfault.com/q/281628/141604 –  week Nov 8 '12 at 0:46

2 Answers 2

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 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=103

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 as the end of the last partition instead of what used to be 103. The default block size for dd is 512 bytes, which is also the unit that fdisk -u -l will use in the output.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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