blog.lexruee.ch
Yet another blog on the Web.

The de facto standard GNU/Linux distribution for the Raspberry Pi is without any doubt Raspbian or Raspbian Lite, which is a Debian-based distribution optimized for the Broadcom SoC BCM283X.

But another really interesting ARM distribution for the RPi is Arch Linux ARM [1]. I was surprised to learn how easy it is to create your own image or customized distribution based on Arch Linux ARM [2]!

The remaining part of this blog post provides instructions and illustrates how a minimal Arch Linux image file for the RPi2 can be created with basic Linux CLI tools.

Minimal Arch Linux ARM Image for the RPi2

Create the image file

For creating the image file we can exploit my favourite command dd. The command below, for example, creates a file of the size of 2GiB:

dd if=/dev/zero of=./image.img bs=2G count=1

Setup a loop device

In this step we create a loopback device so we can format and partition the image disk file like a normal block device file or hard disk. In short, a loopback device allows files to be interpreted as real block devices.

The next command setups the image file image.img as a loop device:

sudo losetup -f image.img

To list the status of all loop devices and to find out the corresponding loop device for our image, we can run losetup with the option -a:

losetup -a

In my case the loop device for my image file is /dev/loop0.

Partiton the image

Now that we setup a loopback device for the image, we can finally partition it using fdisk like any other hard disk:

fdisk /dev/loop0

At the fdisk prompt, delete old partitions and create a new one:

  1. Type o. This will clear out any partitions on the drive.
  2. Type p to list partitions. There should be no partitions left.
  3. Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +100M for the last sector.
  4. Type t, then c to set the first partition to type W95 FAT32 (LBA).
  5. Type n, then p for primary, 2 for the second partition on the drive, and then press ENTER twice to accept the default first and last sector.
  6. Write the partition table and exit by typing w.

Once we have partioned the image file, we need to reload the partition table of the loop device. This can be done using partprobe which informs the OS of partition table changes.

sudo partprobe /dev/loop0

Format the partitions

In this step, we format the previously created partitions.

The first partition will contain the bootloader and must be formatted using the FAT filesystem:

sudo mkfs.vfat /dev/loop0p1

The second partiton will contain the root filesystem of the Arch system, which should be formatted using the ext4 filesystem:

sudo mkfs.ext4 /dev/loop0p2

Bootstrap the Arch system

In the following, we bootstrap the Arch system on the image file.

First we must create two empty directories so we can mount the two partitions of the image file:

mkdir boot root

Second, we mount both partitions as shown below:

mount /dev/loop0p1 boot
mount /dev/loop0p2 root

Third, we download and extract the Arch root filesystem to the mounted root partition (as root, not via sudo):

su
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-2-latest.tar.gz
bsdtar -xpf ArchLinuxARM-rpi-2-latest.tar.gz -C root
sync

Fourth, we need to move the boot files from the root filesystem to the first partition:

mv root/boot/* boot

Lastly, unmount the two partitions:

umount boot root

Testing the image on a RPi

Flash the image to a sdcard

Now that we have an Arch Linux image file, we can easily flash it to a sdcard. As before, we can use the handy dd command. In my case the sdcard is available under /dev/mmcblk0 on my system so the command looks as follows:

sudo dd if=./image.img of=/dev/mmcblk0 bs=4M status=progress && sync

Power on and login

Once the RPi is powered on, we can ssh into the Arch system, assuming we know the IP address. The default settings of an Arch Linux ARM are as follows:

  • Hostnane: alarmpi
  • User: alarm
  • Password: alarm
  • Root password: root

Final adjustments

As final adjustments, it's a good idea to resize the ext4 root partition so we don't run out of disk space. Otherwise, the root partition is limited to 1.9GiB.

Overall, extending a root partition at runtime is possible by simply using fdisk. So the steps are as follows:

  1. Delete the root partiton.
  2. Recreate the root partition.

Afterwards, the system should be rebooted and the root partition be resized using resize2fs.

sudo reboot
sudo resize2fs /dev/mmcblk0p2

References

© Alexander Rüedlinger, 2017-2018
Proudly powered by Pelican, which takes great advantage of Python.