Thursday, April 2

Raspberry Pi - Working with IMG files

One of the first steps to working with the Raspberry Pi is to transfer a version of linux stored in an IMG file to an micro or regular SD Card. Now this is easy if you have a card that is the same size or larger then the IMG file is.

My favorite distro for use on the Raspberry Pi is Minibian Wheezy, mainly for it's small starting size, I find it is a great base distro to build upon. At 488MB it is one of the smaller distros which is usually able to fit onto a single 512MB card.

The Problem:
My Kingston 512MB SD card which I was using to test my base IMG's died. After searching and finally being able to purchase another 512MB SD card, this time by KingMax, I thought I was off to the races.
The KingMax 512MB M-Series SD card actually has a total storage capacity of only 477MB. Exactly 11MB less then the IMG file I have for Minibian Wheezy (2015-02). I needed to trim the IMG file by 11MB so I can start using the KingMax card.

How to work with IMG files

  1. Copying the IMG file I want to work with to a USB key (FAT formated so it will work in Linux and Windows)
  2. I renamed the IMG file to 2015min.img just to save some typing
  3. Insert USB key into Raspberry PI, which is booted off a 4GB SD card with Minibian Wheezy already installed
  4. Mount the USB key
    1. mkdir /mnt/sda
      if you have not already made that directory
    2. mount /dev/sda1 /mnt/sda
      Mounts the USB key partition to directory /mnt/sda
    3. cd /mnt/sda
      switches are working directory to /mnt/sda (The USB Key)
    4. ls
      to confirm we are working with the USB key
    5. parted 2015min.img print
      displays the information about the IMG Disk file
      • Model:   (file)
      • Disk /mnt/sda/2015ming.img: 512MB
      • Sector size (logical/physical): 512B/512B
      • Partition Table: msdos
      • Number          Start           End           Size         Type   File system
      • 1                 8192B    50.0MB    50.0MB     primary    fat16
      • 2              50.0MB     512MB      462MB    primary    ext4
    6. fdisk -lu 2015min.img
      displays the same information in slightly different way
      • Disk 2015min.img: 511 MB, 511705088 bytes
      • 4 heads, 16 sectors/track, 15616 cylinders, total 999424 sectors
      • Units = sectors of 1 * 512 = 512 bytes
      • Sector size (logical/physical): 512 bytes / 512 bytes
      • I/O size (minimum/optimal): 512 bytes / 512 bytes
      • Disk identifier: 0x0004a452
      • Device            Boot     Start         End     Blocks    Id     System
      • 2015min.img        1         16     97727      48856      b     W95 FAT32
      • 2015min.img        2   97728   999423    450848    83     Linux
    7. Mounting a working with the linux partition for the IMG file
      1. Calculate the offset from the start of the image to the partition start
        sector size * Start = (in the case) 512 * 9772850036736
      2. losetup -o 50036736 /dev/loop0 2015min.img
        now the partition resides on /dev/loop0 and we can work with it
      3. mount /dev/loop0 /mnt/lo
        mount the linux partition from the IMG file
      4. df -h /dev/loop0
        displays Information about the filesystem
        • Filesystem         Size       Used       Avail    Use%     Mounted on
        • /dev/loop0   419MB   334MB    60MB       85%    /mnt/lo
      5. cd /mnt/lo
        switch working directory to /mnt/lo
      6. find . -type f -print0 | xargs -0 du -h | sort -hr | head -20
        Displays the top 20 largest files
        • This will show in this case that the top 3 files are located in the /var/cache folder
        • 39MB ./var/cache/apt/archives/raspberrypi-bootloader_1.20150214-1_armhf.deb
        • 18MB ./var/cache/apt/srcpkgcache.bin
        • 19MB ./var/cache/apt/pkgcache.bin
      7. rm -R -i var/cache/apt
        Recursivly deletes everything inside the /var/cache/apt directory
      8. mkdir var/cache/apt
        Recreates the apt directory (Just in case)
      9. df -h /dev/loop0
        Displays Information about the filesystem
        • Changes
        • Used: 254MB      Avail:  139M
      10. cd /mnt/sda
        Changes working directory
      11. umount /mnt/lo
        unmount the partition from IMG file
      12. losetup -d /dev/loop0
        disconnects the device from the IMG file
    8. Clean up and Resize IMG File
      1. losetup -o 50036736 /dev/loop0 2015min.img
      2. fsck -f /dev/loop0
        verify and clean partition
      3. resize2fs -P /dev/loop0
        returns the estimated minimum partition size: (in this case) 244201
      4. resize2fs /dev/loop0 300000
        resizes the partition to 300,000KB
      5. losetup -d /dev/loop0
        disconnect loop0 device from IMG file
      6. parted 2015min.img rm 2
        removes the 2nd partition from the 2015min.img file
      7. parted 2015min.img unit b mkpart primary ext4 50036736   350000000
        builds a new partition as #2 start: 50.0MB End: 350MB
      8. truncate -s 355MB 2015min.img
        truncates the file to the new files size of 355MB
      9. sync
        flushes file buffers
      10. umount /mnt/sda
        unmounts the usb key
After doing these steps I was able to transfer the IMG file over to the new KingMax "512MB" card, and after running resize2fs one last time on first boot. It now fills the card and ready to work.

Hope this helps somebody else ;)


Post a Comment