RF Eclectica Wiki

Try to learn something about everything, and everything about something - Thomas Huxley

User Tools

Site Tools


public:computers:rpi_image

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

  • unmount the loop device:
    sudo umount /mnt
  • use gparted (need to be at a machine with a graphical session) to shrink the partition to a more suitable size, leaving some room for safety
  • dispose of the loop device
    sudo losetup -d loop0
  • The *.img file is still the same size, but the partitions within the file are smaller, the rest of the space needs to be stripped from the *.img file with truncate
  • Use fdisk to inspect the image file to see where the partitions start and end
  • gm4slv@laptop:~/piimg $ sudo fdisk -l igate.img
    Disk igate.img: 4.25 GiB, 4563402752 bytes, 8912896 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
    Disklabel type: dos
    Disk identifier: 0x38ea48c4
     
    Device     Boot  Start     End Sectors  Size Id Type
    igate.img1        8192  532479  524288  256M  c W95 FAT32 (LBA)
    igate.img2      532480 8912895 8380416    4G 83 Linux
  • The second partition ends at block 8912895 so you shrink the filesystem to this point (plus one). This requires the calculation of the actual number of bytes, based on the block size of 512 bytes.
  • Either calculate it yourself, or put the calculation in the command:
    •  sudo truncate --size=$[(8912895+1)*512] name_of_image.img

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

public/computers/rpi_image.txt · Last modified: 20/05/22 16:41 BST by 127.0.0.1