Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I imaged a 16 GB SD card using the dd command. The card however, does not have more than 4 GB of data on it across 2 partitions (FAT32 & ext3).

I would like to transfer this image on to a 8GB SD card. How do I go about shrinking the image?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Assuming Linux, something like this could be done:

  1. Attach the image to a loop device using losetup. Do not mount any partitions.

    0 GB                              8 GB                             16 GB
    |--------fat32--------|--------------------ext3--------------------|
    
  2. Using gparted, shrink the first partition to less than 50% of original size.

    0 GB                              8 GB                             16 GB
    |--fat32---|          |--------------------ext3--------------------|
    
  3. Move the second partition to the newly created free space, shrink to <50%.

    0 GB                              8 GB                             16 GB
    |--fat32---|---------ext3---------|                                |
    
  4. Ensure that the second partition ends before the 8 GB.

  5. Detach the loop device.

  6. Truncate the image file to 8 GB.

Note: Usually, you have to first shrink the filesystem, then adjust the partition size to match the filesystem. Gparted does this for you in a single "Resize" step, but other tools may not.

share|improve this answer
    
Don't forget: After you do such a 'resize', you won't be able to just DD everything back. "DD" is not for simple backup. It's for byte-by-byte backup. Keeping every information of the device. If you want a simple backup from a device, use .tar.gz, .tar.bz2 (on Linux, these two will keep the permissions). But not DD if you only care about the files and the space. –  Shiki Feb 23 '12 at 14:27
    
@Shiki: The goal is not to backup, but to move entire volumes (incl. filesystems, partition tables, etc.) to a smaller disk. –  grawity Feb 23 '12 at 14:37
    
Oh I'm really sorry, you're right. ( I skipped that part. :( ) –  Shiki Feb 23 '12 at 14:38

First off, you can't just shrink the disk image. There's too much complicated stuff going on inside.

I'm going to suggest you mount the dd image then either copy files across to your new filesystem, or research further on shrinking your specific filesystems rather than the disk.

First off, do you have a copy of the original partition table?

If not, try: fdisk -l -u /path/to/image

Then hopefully you can use the values in the above to do something like:

dd if=/path/to/image of=/path/to/output bs=BLOCKSIZE skip=STARTBLOCK count=TOTALBLOCKS

(replacing the above capitals as required)

From there, mount the image:

mount -o loop /path/to/output /path/to/mountlocation

and either copy your data or learn about standard shrinking of your particular file systems and further dd'ing.

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.