Article Index

Step 1: prepare a new image

  1. Create a 512 MB raw file to contain the image (of course you can make a larger image, but you can also enlarge your partitions once the image is on an SD card):
    dd if=/dev/zero of=/root/image.raw bs=33554432 count=16
  2. Attach the image to a loop device. This way, the system can access it as a block device, just like a real SD card:
    losetup /dev/loop0 /root/image.raw
  3. Now create two partitions. The first partitions should be a vfat (vat32) partition of around 60 MB. The second partition should be a ext4 partition of the remaining space. Note that the first partition has to be a vfat partition (although you can choose to make it bigger). You may choose to create more partitions or use a different filesystem for the second partition, but for now I recommend using this configuration as it is the same as the normal Raspbian image. It is easiest to use fdisk to do this interactively from the command line. Alternatively it might be possible to run gparted and do this in in a GUI. Please use google to figure out how to create the partitions. I use sfdisk from the command line, you could try what I did:
    sfdisk --head 4 --sectors 16 --unit B /dev/loop0 <<EOS
    1024 64512 c *
    65536
    EOS
  4. Make the system detect your newly created partitions:
    kpartx -a /dev/loop0
  5. This will make your partitions available under /dev/mapper/loop0p1 and /dev/mapper/loop0p2. Now we must create the file systems on those partitions. You will need to install the package 'dosfstools' (apt-get install dosfstools) before you can create the vfat file system:
    mkfs -t vfat /dev/mapper/loop0p1
    mkfs -t ext4 /dev/mapper/loop0p2
  6. Create the mount points and mount the file systems. Also, we will bind /proc and /dev in the new partition and create a tmp directory. These will be needed while installing packages:
    mkdir /target
    mount /dev/mapper/loop0p2 /target
    mkdir /target/boot
    mount /dev/mapper/loop0p1 /target/boot
    mkdir /target/dev
    mount --bind /dev /target/dev
    mount --bind /dev/pts /target/dev/pts
    mkdir /target/proc
    mount --bind /proc /target/proc
    mkdir /target/tmp
    chmod 1777 /target/tmp
    mkdir -p /target/var/log
    mkdir /target/root
    chmod 700 /target/root

Now, your newly created image is ready to be used. However, if you reboot your Raspberry Pi, it will not be attached to the loop device and no partitions will be mounted. You can copy the commands from step 2 and 4 and all 'mount' commands from step 6 to a bash script (keep them in order!). This way, you can simply run this script to prepare everything for installation. You can 'detach' the image file by doing the reverse; unmount everything, release the partition mapping and release the loop device. After that you can move the image file around like a normal file:

umount /target/proc
umount /target/dev/pts
umount /target/dev
umount /target/boot
umount /target
kpartx -d /dev/loop0
losetup -d /dev/loop0

Add comment


Security code
Refresh