Table of Contents

02 : Computers

R Pi Image files

Various tips and tricks for working with Raspberry Pi Disk Images

Taking an image

  1. remove SD card from Pi
  2. insert into PC
  3. unmount the image partitions if they get auto-mounted
  4. take the image using dd
    sudo dd if=/dev/mmblahblah of=name_of_image.img bs=4M
  5. wait….. and wait….
  6. this can now be either
    1. saved as it is (possibly a big file)
    2. compressed with gzip
      gzip name_of_image.img
  7. an alternative is to make sure the image is as small as it needs to be, appropriate to the space actually needed by the installed OS.

Using ''losetup'' to work with the image file

The image can be used as a loop device and then mounted as a normal disk to allow access to the files etc.

To access the image as a loop device you let losetup find the partitions automatically and convert them to to loop partitions loop0p1 (the /boot partition) and loop0p2 the root partition. You can then mount whichever one you need.

gm4slv@laptop:~/piimg $ sudo losetup -Pf igate.img
gm4slv@laptop:~/piimg $ lsblk
NAME      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0       7:0    0   4.3G  0 loop
├─loop0p1 259:0    0   256M  0 part
└─loop0p2 259:1    0     4G  0 part
sda         8:0    0 298.1G  0 disk
├─sda1      8:1    0 235.1G  0 part /
├─sda2      8:2    0     1K  0 part
├─sda3      8:3    0  50.9G  0 part
└─sda5      8:5    0  12.1G  0 part [SWAP]
sr0        11:0    1  1024M  0 rom
gm4slv@laptop:~/piimg $ sudo mount /dev/loop0p2 /mnt
gm4slv@laptop:~/piimg $ cd /mnt
gm4slv@laptop:/mnt $ ls
bin  boot  dev  etc  home  lib  lost+found  media  mnt  opt  proc  root  run  sbin  share  srv  sys  tmp  usr  var
gm4slv@laptop:/mnt $ cd etc/
gm4slv@laptop:/mnt/etc $ cat hostname
igate
gm4slv@laptop:/mnt/etc $

This allows you to change files (one example is to set a fixed IP address in /etc/dhcpcd.conf) as required.

Shrinking the image

If the OS had been expanded after install to fill the whole SD Card space there is now a lot of pointless wasted space in the image that

  1. takes up disk space to store
  2. makes copying to a new SD card slow
  3. makes it impossible to copy to a smaller card

The image can be shrunk to a more appropriate size.

The gist of it is here

Voila, a smaller image on file which uses just enough space for the already known to be working OS

Page Updated: 20/05/22 16:41 BST