Classic install or sys mode on Raspberry Pi

From Alpine Linux

This page documents the traditional or sys mode installation of Alpine Linux on a Raspberry Pi.

Recommended installation method

  1. Create SD card based install media.
  2. Run setup-alpine script until step 11(i.e SSH) of the base configuration. Do not skip network configuration as Internet access is mandatory to complete sys mode installation.
  3. In Disk & Install section, answer the questions as follows:
    No disks available. Try boot media /media/mmcblk0? (y/n) [n], change the default option and type y.
    Which disk(s) would you like to use? (or '?' for help or 'none') [none], type device name mmcblk0 as per displayed information.
    How would you like to use it? ('sys', 'data', 'crypt', 'lvm' or '?' for help) [?], type sys.
    WARNING: Erase the above disk(s) and continue? (y/n) [n] change the default option and type y to proceed with the installation.
  4. At the end of Installation, you can reboot to boot into the newly installed Alpine Linux and configure further.

The above steps are shown below as screengrab for reference. Depending on the size of your install media and pi model, the information may vary.

 Disk & Install
----------------
No disks available. Try boot media /media/mmcblk0? (y/n) [n]  y
 * WARNING: you are stopping a sysinit service
 * Caching service dependencies ...                                                                                                [ ok ]
 * Unmounting /.modloop ...                                                                                                        [ ok ]
Available disks are:
  mmcblk0	(64.1 GB  )

Which disk(s) would you like to use? (or '?' for help or 'none') [none]  mmcblk0

The following disk is selected:
  mmcblk0	(64.1 GB  )

How would you like to use it? ('sys', 'data', 'crypt', 'lvm' or '?' for help) [?]  sys
100% ████████████████████████████████████████████████████████████████████████████████████████████████████████████
/usr/sbin/update-raspberrypi-bootloader: WARNING: no kernel found

WARNING: The following disk(s) will be erased:
  mmcblk0	(64.1 GB  )

WARNING: Erase the above disk(s) and continue? (y/n) [n]  y
Partition #2 contains a ext4 signature.
Creating file systems...
mkfs.fat 4.2 (2021-01-31)
Installing system on /dev/mmcblk0p2:
100% ████████████████████████████████████████████████████████████████████████████████████████████████████████████
==> initramfs: creating /boot/initramfs-rpi for 6.12.8-0-rpi
/usr/sbin/update-raspberrypi-bootloader: INFO: replaced config.txt and saved config.txt.old
Configured kernel vmlinuz-rpi / initramfs-rpi

Installation is complete. Please reboot.

Manual installation

The following steps were used in the past. They are not needed since Alpine Linux v3.21.

Create an MBR partition table with two partitions on an 8 GB (or larger) class 10 sd-card:

  • First one, a fat32 type, of 256MB. You may have to set boot and lba flags
  • The second one, an ext4 type, occupying the remaining space on the media
  • Eject and re-insert your SD card to ensure recognition of all the partitions.
  • Go into the first partition (fat32). Untar the archive with tar:

    tar zxvf ~/Download/alpine-rpi-3.21.3-armhf.tar.gz

Eject the SD card properly. Insert it into the Raspberry Pi. Plug in a usb keyboard as well as the HDMI and network cables. Power on. When the command prompt displays, log in as root. (no password)

Execute the following commands. Make sure there is an internet connection available otherwise setting up the apk mirrors will fail.

setup-alpine 

Set the keyboard map, the timezone, how to connect to the network (dhcp is the best method), say none at save config and save cache.

apk update

If the extra space in the sd card was left empty, a partition must be created now:

apk add cfdisk e2fsprogs  # or the tool of your choice
cfdisk /dev/mmcblk0       # create the new partition with the free space
mkfs.ext4 /dev/mmcblk0p2  # create the ext4 filesystem in the new partition

Raspberry Pi has no hardware clock, so synchronize with an ntp server:

apk add chrony
service chronyd restart
Warning: 22 June 2021 - There is a bug in Alpine 3.12.x and older that causes setup-disk to fail on ext4 mounts on Raspberry Pi. The work around is marked in the instructions below.
#12353


mount /dev/mmcblk0p2 /mnt             # The second partition, in ext4 format, where Alpine Linux is installing in sys mode
export FORCE_BOOTFS=1                 # work around for issue 12353
setup-disk -m sys /mnt
mount -o remount,rw /media/mmcblk0p1  # An update in the first partition is required for the next reboot.

You may get some warning about syslinux when you run setup-disk. You can safely ignore this.

Update boot partition (keep alpine-rpi* image layout)

From here we can either update boot partition to keep the alpine-rpi* image layout or to keep the system partition/setup-alpine's layout (see next section). We only need one of the two. Not both.

Clean up the boot folder in the first partition to drop unused files:

rm -f /media/mmcblk0p1/boot/*  
cd /mnt       # We are in the second partition 
rm boot/boot  # Drop the unused symbolic link

Move the image and initramfs for Alpine Linux into the right place:

mv boot/* /media/mmcblk0p1/boot/  
rm -Rf boot
mkdir media/mmcblk0p1   # It's the mount point for the first partition on the next reboot

Don't worry about the error when you execute the following:

ln -s media/mmcblk0p1/boot boot

Update boot partition (keep system partition/setup-alpine layout)

It turns out that the system partition created by setup-alpine has a working boot layout. To keep this, perform the following steps instead of the steps in the previous chapter.

Clean up the boot / first partition to drop unused files:

rm -Rf /media/mmcblk0p1/*
cd /mnt       # We are in the second partition 
rm boot/boot  # Drop the unused symbolink link

Move the boot folder created by setup-alpine into the right place:

cd /media/mmcblk0p1
mkdir boot
cd /mnt
mv boot/* /media/mmcblk0p1/  
rm -Rf boot
mkdir media/mmcblk0p1   # It's the mount point for the first partition on the next reboot

Don't worry about the error when you execute the following:

ln -s media/mmcblk0p1 boot

End of update boot partition - continue here in both cases

Update /etc/fstab:

echo "/dev/mmcblk0p1 /media/mmcblk0p1 vfat defaults 0 0" >> etc/fstab
sed -i '/cdrom/d' etc/fstab   # Of course, you don't have any cdrom or floppy on the Raspberry Pi
sed -i '/floppy/d' etc/fstab
cd /media/mmcblk0p1

If you want to activate the edge repository:

sed -i '/edge/s/^#//' etc/apk/repositories   # But enable the repository for community if you want vim, mc, php, apache, nginx, etc.

For the next boot, indicate that the root filesystem is on the second partition. If the cmdline.txt file contains a line that starts with /root, then use sed:

sed -i 's/$/ root=\/dev\/mmcblk0p2 /' /media/mmcblk0p1/cmdline.txt  
reboot

That works on Raspberry Pi 3B and 1B, but if you have the 1B version, you'll need to be very, very patient (several tens of minutes). If a hard disk is connected via usb, you can replace the /dev/mmcblk0p2 above with /dev/sda1, for example.

Convert diskless to sys mode

It is also possible to switch a diskless installation to a fully disk-based installation. This is not yet formally supported, but can be done somewhat manually. This frees all the memory otherwise needed for the root filesystem, allowing more installed packages.

Split your SD card into two partitions: the FAT32 boot partition described above (in this example it'll be mmcblk0p1) , and a second partition to hold the root filesystem (here it'll be mmcblk0p2). Boot and configure your diskless system as above, then create a root filesystem:

apk add e2fsprogs mkfs.ext4 /dev/mmcblk0p2

Now do a disk install via a mountpoint. The setup-disk script will give some errors about syslinux/extlinux, but you can ignore them. The Raspberry Pi doesn't need them to boot.

mkdir /stage mount /dev/mmcblk0p2 /stage setup-disk -o /media/mmcblk0p1/MYHOSTNAME.apkovl.tar.gz /stage # (ignore errors about syslinux/extlinux)

Add a line to /stage/etc/fstab to mount the Pi's boot partition again:

/dev/mmcblk0p1 /media/mmcblk0p1 vfat defaults 0 0

Now add a root=/dev/mmcblk0p2 parameter to the Pi's boot command line, either cmdline-rpi2.txt or cmdline-rpi.txt depending on model:

mount -o remount,rw /media/mmcblk0p1 sed -i '$ s/$/ root=\/dev\/mmcblk0p2/' /media/mmcblk0p1/cmdline-rpi2.txt

N.B. the contents of /boot will be ignored when the Pi boots. It will use the kernel, initramfs, and modloop images from the FAT32 boot partition. To update the kernel, initfs or modules, you will need to manually (generate and) copy these to the boot partition or you could use bind mount, in which case, copying the files to boot partition manually, is not needed.

echo /media/mmcblk0p1/boot /boot none defaults,bind 0 0 >> /etc/fstab

Overlay root filesystem

Overlay root filesystem can be enabled by adding the option overlaytmpfs=yes to the /boot/cmdline.txt file. This will cause the underlying SD card root filesystem to be mounted read-only, with an overlayed tmpfs for modifications which will be discarded at shutdown. This option will avoid constant writing to the sd-card on a pi.

The /boot/cmdline.txt file appears as follows

Contents of /boot/cmdline.txt

root=UUID=afaa7f11-8191-4fbc-9e45-62113f61e1b4 modules=sd-mod,usb-storage,ext4 quiet rootfstype=ext4 overlaytmpfs=yes

The below output shows how the filesystem is mounted when Overlay root filesystem is enabled:

# df -m

Filesystem           1M-blocks      Used Available Use% Mounted on
...
/dev/mmcblk0p2           59555       326     56172   1% /media/root-ro
root-tmpfs                 225        13       211   6% /media/root-rw
overlayfs                  225        13       211   6% /
...

# mount
...
/dev/mmcblk0p2 on /media/root-ro type ext4 (ro,relatime)
root-tmpfs on /media/root-rw type tmpfs (rw,relatime,mode=755)
overlayfs on / type overlay (rw,relatime,lowerdir=/media/root-ro,upperdir=/media/root-rw/root,workdir=/media/root-rw/work,uuid=on)
...

Removing and adding back the overlaytmpfs=yes option in the /boot/cmdline.txt file using a toggle script will allow easy maintenance on the pi and any software/configuration can be easily added/removed.

See also