Project

General

Profile

Support #492

Trimming Excessively Large Disk Image Files

Added by Daniel Curtis about 10 years ago. Updated almost 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Target version:
-
Start date:
12/11/2014
Due date:
% Done:

100%

Estimated time:
1.00 h
Spent time:

Description

Zero The Free Space

  • Install zerofree:
    apt-get install zerofree
    
  • Fill free space of the image with zero:
    zerofree /dev/sdb2
    

Acquiring The Image

  • While modifying the stock image of Raspbian, I decided to take a backup image of my drive using dd:
    dd if=/dev/sdb of=modified-raspbian.img
    
  • Then shrunk main partition image to the value I desired using gparted:
    gparted modified-raspbian.img
    

However the image of the entire drive left a lot of unallocated space left in the image. After searching online I found that all I needed to do was to use dd to extract just the raspbian image apart from the unallocated space. The parameter for dd that does this is count=.

Find Image Size Information

  • Get the needed block information by running the following:
    fdisk -u -l modified-raspbian.img
    
    • Example output:
      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
      

Trim The Image

  • In the following example the last partition ends on 103, and the unit size is 8225280 bytes. So simply run command:
    dd if=modified-raspbian.img of=modified-raspbian-stripped.img bs=8225280 count=103
    
    • NOTE: I had problems with a couple of images being corrupt after this process, so I found that adding 1 to the count argument will correct this.
      dd if=modified-raspbian.img of=modified-raspbian-stripped.img bs=8225280 count=104
      

Resources

Also available in: Atom PDF