Posted on

Making a Reduced Size IMG for backups or sharing

Here is a technique for creating an image of an SD card (or any other filesystem) and then reducing the size of that image to exclude unallocated space in the image file system. If you are doing this on a Raspberry Pi you will need to ensure your SD card is large enough to hold both the original and the shrunk image. Alternatively, you could connect an external storage device to hold the images.

Simple Image Creation

Start by creating an image of the SD card with the following command:

sudo dd if=/dev/sdc of=./myImage.img

Where /dev/sdc is the location of the SD card you want to image and ./myImage.img is the desired name and location of the image file.

Better Image Creation

A more sophisticated version of dd can be found by installing dc3dd:

sudo apt-get install -y dc3dd

In addition to creating an image file, dc3dd also provides a progress report and confirmation that the job was completed successfully.

To create an image with dc3dd tool enter:

sudo dc3dd if=/dev/sdc of=./myImage.img

 

Establish the used space and truncate the image

Next you need to know the position of the last allocated sector in the image. To find that type:

fdisk -l myImage.img

The two important details you need from the output are: Units (normally 512 bytes) and the last used sector.

To eliminate the unallocated space from the image use the truncate command as follows:

sudo truncate –size=$[(8447999+1)*512] myImage.img

NB: There are two dashes immediately prior to size in the above – some browsers show this as a single long dash – grrr!

Where 8447999 is the final sector on your image, 512 is the block-size and myImage.img is the  name of the image file you want to shrink. The bracketed section simply adds 1 to the number of blocks in the wanted section and multiplies the total by 512 to calculate the size of the wanted section in bytes. All data above that value is truncated.

That’s it!

What if there’s no unallocated space on the SD Card?

If you have expanded the filesystem on the SD Card but not all the space is in use, you first need to create unallocated space that can be removed later with the truncate command.

To do this, run gparted to view the SD Card’s file system and use the Partition menu to resize the target partition and convert the unused space into unallocated space. Once complete, you can use the truncate command as above to remove it.

4 thoughts on “Making a Reduced Size IMG for backups or sharing

  1. Hi Mike,
    Can you see why this is not working

    pi@raspberrypi:~ $ sudo dc3dd if=/dev/sdb of=/dev/sda/heyu.img

    dc3dd 7.2.646 started at 2018-07-26 09:27:18 +0100
    compiled options:
    command line: dc3dd if=/dev/sdb of=./dev/sda/heyu.img
    device size: 31116288 sectors (probed), 15,931,539,456 bytes
    sector size: 512 bytes (probed)
    [!!] opening `./dev/sda/heyu.img’: No such file or directory
    0 bytes ( 0 ) copied ( 0% ), 0 s, 0 K/s
    input results for device `/dev/sdb’:
    0 sectors in
    0 bad sectors replaced by zeros
    output results for file `./dev/sda/heyu.img’:
    0 sectors out
    dc3dd failed at 2018-07-26 09:27:18 +0100

    1. Neil,

      In the command line there’s an unwanted . before the output file. Also you haven’t said what partition to use for the output file. A simpler way to do this is to use the file manager to navigate to the folder where you want to store the image. When you get there, choose tools – open a terminal session and it will open a terminal session in the correct folder. The command to generate the image then becomes:

      sudo dc3dd if=/dev/sdb of=heyu.img

      If you want to reduce the size of the image, I suggest you use the pishrink script(Google it). Copy the pishrink.sh file to the same folder as the image and enter the following:

      sudo pishrink.sh heyu.img heyuSmall.img

      This creates a new, smaller, image. When copied to a blank SD card the image will automatically expand to fill the SD card when it boots.

      Mike – G4WNC

      Regards,

      Mike – G4WNC

  2. Hi Mike
    thanks
    I did wonder about the . I did try with and without..it was there in your original text…

    I created an image on Win10 machine but now cant copy it to the Pi..either it is too big for the USB FAT32 files system or if I try and drag it across the network to a smb share…I do not have enough space on the currently running Pi

    using gparted now to resize the partition to try again

  3. A little confused by what you said about I had not specified a partition for the output file
    I used
    sudo dc3dd if=/dev/sdb of=/dev/sda/heyu.img
    I also tried
    sudo dc3dd if=/dev/sdb of=/dev/sda/sda1/heyu.img

    both with and without the extra ‘ . ‘

    I tried multiple times.

    You gave me:

    sudo dc3dd if=/dev/sdb of=heyu.img

    where does that put the output file

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.