Files
collection/image-files.md
2024-11-18 15:35:36 +01:00

2.0 KiB

image files

create image file

To create an image file, use:

dd if=/dev/zero of=<image-file> bs=<block-size> count=<count>

# example: generate image with size of exactly 7.8 Gb
dd if=/dev/zero of=test.img bs=1000000 count=7800 conv=fsync

resize image

One can change an images size with truncate --size=<size> <image-file>. this is usually used to increase image size in order to increase the file system size (i.e. make more space for rootfs). If the goal is to decrease the memory footprint, extra care has to be taken to not cripple the filesystem.

resize partition

One can resize a partition (if there is no data afterwards) by deleting and remaking it with fdisk. Make sure to choose the same start sector!.

mount image files

To mount an image file use:

mount -o loop <path/to/image-file> <mount-point>

If the image contains multiple partitions one needs to setup a loop device first:

losetup --find --show <path/to/image-file> -P

Afterwards the partitions of the image should be shown as loopXpY and can be mounted like any other device.

setup image for Raspberry PI

This is a quick step through the setup of a raspbian image:

  1. download image
  2. truncate image (see resize image)
  3. resize partition with fdisk (see resize partition)
  4. resize filesystem on said partition with resize2fs (or similar)
  5. setup loop device and mount boot partition (see mount image)
  6. add empty file ssh to boot partition
  7. add userconf.txt to boot partition, to set up initial user & password
  8. disable resize script in boot/cmdline.txt (something with firstboot)
  9. unmount partition and detach loop device