Raspberry Pi: Difference between revisions

From Alpine Linux
No edit summary
No edit summary
 
(56 intermediate revisions by 28 users not shown)
Line 1: Line 1:
{{warning | 11 Feb 2021 - There is currently a known bug upstream <br />[https://github.com/raspberrypi/firmware/issues/1529 kernel/initramfs cannot be loaded from subdirectory with same name as volume label]. Since the kernel is installed to <code>boot/</code>, you must not use a label named <code>boot</code> for the fat32 partition. }}
{{TOC right}}
{{TOC right}}


This tutorial will help you install Alpine Linux on your Raspberry Pi.
This tutorial explains how to install Alpine Linux on a Raspberry Pi. Alpine Linux will be installed in [[Installation#Diskless_Mode|diskless mode]], hence, [[Alpine local backup|Alpine Local Backup (lbu)]] is required to save modifications between reboots.
 
For scenarios where there is not expected to be significant changes to disk after setup (like running a static HTTP server), this is likely preferable, as running the entire system from memory will improve performance (by avoiding the slow SD card) and improve the SD card life (by reducing the writes to the card, as all logging will happen in RAM). Diskless installations still allow you to install packages, save local files, and tune the system to your needs.
 
If any of the following apply, then installation in [[Classic install or sys mode on Raspberry Pi|sys-mode installation]] is likely more appropriate.
 
* There will be constant changes to the disk after initial setup (for example, if you expect people to login and save files to their home directories)
* Logs should persists across reboots
* Plan to install packages which consume more space than can be loaded into RAM
* Plan to install kernel modules (such as ZFS)
 
== Compability list ==
As of Alpine 3.19:
* '''armhf''' (defconfig bcmrpi) - Raspberry Pi 1, Zero, ZeroW, cm1
* '''armv7''' (defconfig bcm2709) - Raspberry Pi 2, 3, 3+, Zero2W, cm3, cm3+
* '''aarch64''' (defconfig bcm2711) - Raspberry Pi 3, 3+, 4, 400, Zero2W, cm3, cm3+, cm4, 5


== Preparation ==
== Preparation ==


# [http://alpinelinux.org/downloads/ Download] the Alpine for Raspberry Pi tarball. You should be safe using the '''armhf''' build on all versions of Raspberry Pi (including Pi Zero and Compute Modules); but it may perform less optimally on recent versions of Raspberry Pi. The '''armv7''' build is compatible with Raspberry Pi 2 Model B. The '''aarch64''' build should be compatible with Raspberry Pi 2 Model v1.2, Raspberry Pi 3 and Compute Module 3, and Raspberry Pi 4 model B.
# [https://alpinelinux.org/downloads/ Download] the Alpine for Raspberry Pi tarball. Use the compability list above when choosing image/file to download.  
# [https://wiki.alpinelinux.org/wiki/Create_a_Bootable_USB#Format_USB_stick Create a bootable FAT32 partition on your SD card.] You can use a partitioning tool such as [https://en.wikipedia.org/wiki/GNOME_Disks gnome-disks] or [http://linux.die.net/man/8/fdisk fdisk].
# [[Create_a_Bootable_Device#Manually_copying_Alpine_files|Create a bootable FAT32 partition on your SD card.]] The partitioning and formatting part of the instructions on the linked page could be done using a graphical partitioning tool such as [https://en.wikipedia.org/wiki/GNOME_Disks gnome-disks], just make sure the partition type is <code>W95 FAT32 (LBA)</code>. (The current type can be found in the "Type" column in the output of <code>fdisk -l</code>.)
# Create a filesystem on the partition with <code>mkdosfs -F 32 /dev/sdX1</code> (Replace sdX1 with the correct reference to the partition you just created.)
# Extract the tarball to the root of the bootable FAT32 partition.
# Mount the partition and extract the tarball contents unto it.


Optionally create a '''usercfg.txt''' file on the partition to configure low-level system settings. Specifications can be found [https://www.raspberrypi.org/documentation/configuration/config-txt here]. Some interesting values include:
To setup a headless system, a bootstrapping configuration overlay file [https://github.com/macmpi/alpine-linux-headless-bootstrap  headless.apkovl.tar.gz] may be added to enable basic networking, so that following configuration steps can be performed under <code>ssh</code>. Pi Zero may be configured with simple USB ethernet-gadget networking with another computer sharing its internet connection.
 
It is recommended to create a '''usercfg.txt''' file on boot partition to configure low-level system settings, as '''config.txt''' may be replaced during bootloader/system upgrades: details can be found [https://www.raspberrypi.com/documentation/computers/config_txt.html here].<br>
However, note some [https://www.raspberrypi.com/documentation/computers/config_txt.html#include settings] can only be set in '''config.txt''', and will have no effect when specified in '''usercfg.txt''' (e.g. <code>gpu_mem</code>). Some interesting values include:
* To enable the UART console: <code>enable_uart=1</code>
* To enable audio: <code>dtparam=audio=on</code>
* To enable audio: <code>dtparam=audio=on</code>
* If you see black edges around your screen after booting the Pi, you can add <code>disable_overscan=1</code>
* By default system will use legacy video driver: some [https://www.raspberrypi.com/documentation/computers/legacy_config_txt.html#legacy-video-options options] may be used to adjust displays modes (e.g. if you see black edges around your screen after booting the Pi, you can add <code>disable_overscan=1</code>). Alternatively Linux DRM-KMS driver may be used (see below).
 
* If you plan to install on a Pi Compute Module 4 with I/O board, you may need to add: <code>otg_mode=1</code>
If you need WiFi to work, you have to [https://github.com/RPi-Distro/firmware-nonfree/tree/master/brcm download] the latest Broadcom drivers to your SD card. (Replace /mnt/sdcard with the correct mount point.)


  git clone --depth 1 https://github.com/RPi-Distro/firmware-nonfree.git
Recent versions include Broadcom firmware files. If you're using an older Alpine version, see [[#Wireless_support_with_older_Alpine_images|section below]].
  cp firmware-nonfree/brcm/* /mnt/sdcard/firmware/brcm


== Installation ==
== Installation ==


Alpine Linux will be installed as [[Installation#Installation_Handbook|diskless mode]], hence you need to use [[Alpine local backup|Alpine Local Backup (lbu)]] to save your modifications between reboots.  Follow these steps to install Alpine Linux:
Follow these steps to install Alpine Linux in Diskless Mode:


# Insert the SD card into the Raspberry Pi and turn it on
# Insert the SD card into the Raspberry Pi and power it on
# Login into the Alpine system as root.  Leave the password empty.
# Login into the Alpine system as root.  Leave the password empty.
# Check the current date and time by issuing <code>date</code>. Correct if necessary by issuing <code>date -s YYYYMMDDhhmm</code>.
# Type <code>setup-alpine</code>
# Type <code>setup-alpine</code>
# Once the installation is complete, commit the changes by typing <code>lbu commit -d</code>
# Once the installation is complete, commit the changes by typing <code>lbu commit -d</code>
Line 34: Line 53:
=== Update the System ===
=== Update the System ===


Upon installation, make sure that your system is up-to-date:
After installation, make sure your system is up-to-date:


{{cmd|apk update
{{cmd|apk update
Line 43: Line 62:
{{cmd|lbu commit -d}}
{{cmd|lbu commit -d}}


=== Clock-related error messages ===
Note: this does not upgrade the kernel. In order to upgrade the kernel, a full upgrade of the Alpine Linux version must be performed as described in [[Upgrading Alpine#Upgrading Alpine Linux on other removable media (such as CF/USB)|upgrading Alpine Linux for removable media]].


During the booting time, you might notice errors related to the hardware clockThe Raspberry Pi does not have
=== Linux Kernel Graphics Modes ===
a hardware clock and therefore you need to disable the hwclock daemon and enable swclock:
By default system configuration will use legacy video driver: this driver has some limitations and is lacking support.<br>
It is recommended to enable Linux DRM-KMS driver by adding the following to '''usercfg.txt''':
  # Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2
# Don't have the firmware create an initial video= setting in cmdline.txt.
# Use the kernel's default instead.
disable_fw_kms_setup=1
Note: This overlay disables legacy video [https://www.raspberrypi.com/documentation/computers/legacy_config_txt.html#legacy-video-options options].


{{cmd|rc-update add swclock boot    # enable the software clock
Install the Mesa drivers (Pi4 and Pi5):
rc-update del hwclock boot    # disable the hardware clock}}


Since Raspberry Pi does not have a clock, the Alpine Linux needs to know what the time is by using a
{{cmd|apk add {{pkg|mesa-dri-gallium|arch=a*}}}}
[https://en.wikipedia.org/wiki/Network_Time_Protocol Network Time Protocol (NTP)] daemon.  Make sure that you a
NTP daemon installed and running.  If you are not sure, then you can install NTP client by running the following
command:


{{cmd|setup-ntp}}
Then reboot:


The Busybox NTP client might be the most lightweight solution.  Save the changes and reboot, once the NTP software is
{{cmd|lbu_commit -d; reboot}}
installed and running:


{{cmd|lbu commit -d
=== Wireless drivers ===
reboot}}
As of Alpine 3.17, Wifi and Bluetooth drivers are available within install image: they are part of <code>linux-firmware-brcm</code> (and linked dependencies).<br>
Since kernel 6.1.25 (i.e. Alpine 3.18), onboard bluetooth is enabled & autoprobed by default (it may be disabled by setting [https://github.com/raspberrypi/rpi-firmware/tree/master/overlays krnbt] off).


After reboot, make sure that the <code>date</code> command outputs the correct date and time.
== Troubleshooting ==
<code>raspinfo</code> utility can be used as a first step to diagnose issues: it will make a log report of essential Pi system configuration, and is often used as a reference to submit questions or bug reports within Raspberry Pi community (Forums, Github, etc).<br>
It can be installed with <code>raspberrypi-utils-raspinfo</code> subpackage.


=== WiFi on boot ===
=== Long boot time when running headless ===
If you have already [[Connecting_to_a_wireless_access_point|configured WiFi]] during the setup, the connection will not return on reboot.
You will need to start up a service to automatically connect to the wireless access point.
# Run <code>rc-update add wpa_supplicant boot</code> to connect to the wireless access point on boot.
# Run it manually with <code>/etc/init.d/wpa_supplicant start</code>.


=== Enable OpenGL (Raspberry Pi 3) ===
If no peripherals are connected, the system might hang for an exceptionally long period of time while it attempts to accumulate entropy.


Remount the boot partition writeable (ie. /media/mmcblk0p1):
If this is the case, simply plugging in any USB device should work around this issue, since it increases the amount of entropy available to the kernel via interrupts.


{{cmd|mount -o remount,rw /media/mmcblk0p1}}
=== apk indicating 'No space left on device' ===


Add the following lines to /media/mmcblk0p1/config.txt
Note some models of the Raspberry Pi such as the 3A+ only have 512M of RAM, which on fresh Alpine deployment will only leave around 200M for tmpfs root. It's important to keep this limitation in mind when using these boards.


dtoverlay=vc4-kms-v3d
=== Clock-related error messages ===
gpu_mem=128


256MB gpu_mem is also possible
During the booting time, you might notice errors related to the hardware clock.  Many Raspberry Pi do not have a hardware clock, or may have one without battery, in which case the behaviour depends on whether you physically cut power or not.


Install mesa-dri-vc4:
Also, <code>setup-alpine</code> will adapt the installation to the type of Raspberry Pi you use, which means for example that on a RPI5, you will see <code>hwclock></code> service, whereas on a RPI4 it will use <code>swclock</code>. In doubt, make sure to do the installation on the target hardware.
{{cmd|apk add mesa-dri-vc4}}


Reboot:
If you still experience clock errors, add the following line to '''/etc/chrony/chrony.conf''':


{{cmd|lbu_commit -d; reboot}}
<code>makestep 0.1 3</code>


== Persistent storage ==
== Persistent storage ==
=== Traditional disk-based (sys) installation ===
{{Merge|Classic install or sys mode on Raspberry Pi|There's an existing page for sys-installations on RasPi.}}
It is also possible to switch 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 <code>mmcblk0p1</code>) , and a second partition to hold the root filesystem (here it'll be <code>mmcblk0p2</code>). Boot and configure your diskless system as above, then create a root filesystem:
{{cmd|apk add {{pkg|e2fsprogs|arch=a*}}
mkfs.ext4 /dev/mmcblk0p2}}
Now do a disk install via a mountpoint. The <code>setup-disk</code> script will give some errors about syslinux/extlinux, but you can ignore them.
The Raspberry Pi doesn't need them to boot.
{{cmd|<nowiki>mkdir /stage
mount /dev/mmcblk0p2 /stage
setup-disk -o /media/mmcblk0p1/MYHOSTNAME.apkovl.tar.gz /stage
# (ignore errors about syslinux/extlinux)</nowiki>}}
Add a line to <code>/stage/etc/fstab</code> to mount the Pi's boot partition again:
{{cmd|/dev/mmcblk0p1 /media/mmcblk0p1 vfat defaults 0 0}}
Now add a <code>root=/dev/mmcblk0p2</code> parameter to the Pi's boot command line, either <code>cmdline-rpi2.txt</code> or <code>cmdline-rpi.txt</code> depending on model:
{{cmd|<nowiki>mount -o remount,rw /media/mmcblk0p1
sed -i '$ s/$/ root=\/dev\/mmcblk0p2/' /media/mmcblk0p1/cmdline-rpi2.txt</nowiki>}}
You might also consider <code>overlaytmpfs=yes</code> here, which 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.
N.B. <b>the contents of /boot will be ignored when the Pi boots</b>. 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.
{{cmd|<nowiki>echo /media/mmcblk0p1/boot /boot none defaults,bind 0 0 >> /etc/fstab</nowiki>}}


=== Loopback image with overlayfs ===
=== Loopback image with overlayfs ===


The install is in diskless mode and forces everything into memory, if you want additional storage we need to create loop-back storage onto the SD mounted with overlayfs.
When you install Alpine in diskless mode, the entire system is loaded into memory at boot. If you want additional storage (for example, if you need more space than offered by your RAM) we need to create loop-back storage onto the SD card mounted with overlayfs.


First make the SD card writable again and change fstab to always do so:
First, make the SD card writable again and change fstab to always do so:
{{cmd|mount /media/mmcblk0p1 -o rw,remount
{{cmd|mount /media/mmcblk0p1 -o rw,remount
sed -i 's/vfat\ ro,/vfat\ rw,/' /etc/fstab}}
sed -i 's/vfat\ ro,/vfat\ rw,/' /etc/fstab}}
Line 120: Line 173:
mount -a}}
mount -a}}


Make the overlay folders, we are doing /usr here, but you can do /home or anything else:
Make the overlay folders, we are using the /usr directory here, but you can use /home or anything else.
{{Warning|Overlay workdir needs to be an empty directory on the same filesystem mount as the upper directory. So each overlay must use its own workdir.}}
 


{{cmd|mkdir /media/persist/usr  
{{cmd|mkdir /media/persist/usr  
mkdir /media/persist/.work
mkdir /media/persist/.work_usr
echo "overlay /usr overlay lowerdir&#61;/usr,upperdir&#61;/media/persist/usr,workdir&#61;/media/persist/.work 0 0" >> /etc/fstab  
echo "overlay /usr overlay lowerdir&#61;/usr,upperdir&#61;/media/persist/usr,workdir&#61;/media/persist/.work_usr 0 0" >> /etc/fstab  
mount -a}}
mount -a}}


Line 132: Line 187:
/dev/mmcblk0p1 /media/mmcblk0p1 vfat rw,relatime,fmask&#61;0022,dmask&#61;0022,errors&#61;remount-ro 0 0
/dev/mmcblk0p1 /media/mmcblk0p1 vfat rw,relatime,fmask&#61;0022,dmask&#61;0022,errors&#61;remount-ro 0 0
/media/mmcblk0p1/persist.img /media/persist ext4 rw,relatime,errors&#61;remount-ro 0 0
/media/mmcblk0p1/persist.img /media/persist ext4 rw,relatime,errors&#61;remount-ro 0 0
overlay /usr overlay lowerdir&#61;/usr,upperdir&#61;/media/persist/usr,workdir&#61;/media/persist/.work 0 0}}
overlay /usr overlay lowerdir&#61;/usr,upperdir&#61;/media/persist/usr,workdir&#61;/media/persist/.work_usr 0 0}}


Now commit the changes: (optionally remove the e2fsprogs, but it does contain repair tools)
Now commit the changes: (optionally remove the e2fsprogs, but it does contain repair tools)
{{cmd|lbu_commit -d}}
{{cmd|lbu_commit -d}}


Remember with this setup, if you install things and you have done this overlay for /usr, you must not commit the 'apk add', otherwise while it boots it will try and install it to memory and not to the persist storage.
Remember, with this setup if you install things and you have done this overlay for /usr, you must not commit the 'apk add', otherwise, while it boots it will try and install it to memory, not to the persistent storage.
 
If you do want to install something small at boot you can use `apk add` and `lbu commit -d`.
 
If it is something a bit bigger then you can use `apk add` but then not commit it, it will be persistent (in /user), but do check everything you need is in that directory and not in folders you have not made persistent.
 
=== Traditional disk-based (sys) installation ===
 
{{Warning|This isn't yet supported by the Alpine setup scripts for Raspberry Pi. It requires manual intervention, and might break.}}


It is also possible to switch 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.
If you do want to install something small at boot, you can use <code>apk add</code> and <code>lbu commit -d</code>.


Split your SD card into two partitions: the FAT32 boot partition described above (in this example it'll be <code>mmcblk0p1</code>) , and a second partition to hold the root filesystem (here it'll be <code>mmcblk0p2</code>). Boot and configure your diskless system as above, then create a root filesystem:
If it is something a bit bigger, then you can use <code>apk add</code> but then not commit it. It will be persistent (in <code>/user</code>), but be sure to check everything you need is in that directory and not in folders you have not made persistent.


{{cmd|apk add e2fsprogs
== Netboot ==
mkfs.ext4 /dev/mmcblk0p2}}


Now do a disk install via a mountpoint. The <code>setup-disk</code> script will give some errors about syslinux/extlinux, but you can ignore these: the Raspberry Pi doesn't need this to boot anyway.
=== Netbooting Raspberry Pi 4 ===


{{cmd|<nowiki>mkdir /stage
The Raspberry Pi 4 bootloader can be configured to boot from the network [https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-4-bootloader-configuration]. Configure the bootloader with at least
mount /dev/mmcblk0p2 /stage
setup-disk -o /media/mmcblk0p1/MYHOSTNAME.apkovl.tar.gz /stage
# (ignore errors about syslinux/extlinux)</nowiki>}}


Add a line to <code>/stage/etc/fstab</code> to mount the Pi's boot partition again:
{{cmd|<nowiki>BOOT_ORDER=0xf142
TFTP_PREFIX=1</nowiki>}}


{{cmd|/dev/mmcblk0p1 /media/mmcblk0p1 vfat defaults 0 0}}
and optionally also {{cmd|<nowiki>TFTP_IP=x.x.x.x</nowiki>}} where <code>x.x.x.x</code> is the IP address of your TFTP server.


Now add a <code>root=/dev/mmcblk0p2</code> parameter to the Pi's boot command line, either <code>cmdline-rpi2.txt</code> or <code>cmdline-rpi.txt</code> depending on model:
If not configuring <code>TFTP_IP</code> in the bootloader, you'll need to configure your DHCP server to advertise the TFTP server IP address. This varies depending on your DHCP server; use the following details if applicable:


{{cmd|<nowiki>mount -o remount,rw /media/mmcblk0p1
# Vendor class: <code>PXEClient:Arch:00000:UNDI:002001</code>
sed -i '$ s/$/ root=\/dev\/mmcblk0p2/' /media/mmcblk0p1/cmdline-rpi2.txt</nowiki>}}
# Filename: <code>/</code>


You might also consider <code>overlaytmpfs=yes</code> here, which will cause the underlying SD card root filesystem to be mounted read-only, with an overlayed tmpfs for modifications which will be discarded on shutdown.
The minimal set of files that your TFTP server needs to host are:


Beware, though, that <b>the contents of /boot will be ignored when the Pi boots</b>: 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 so that manually copy the files to boot partition is not needed.
# <code>bcm2711-rpi-4-b.dtb</code> (from [https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/bcm2711-rpi-4-b.dtb raspberrypi/firmware/master/boot/bcm2711-rpi-4-b.dtb])
# <code>cmdline.txt</code> (see below)
# <code>config.txt</code> (see below)
# <code>fixup4.dat</code> (from [https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/fixup4.dat raspberrypi/firmware/master/boot/fixup4.dat], alternatively <code>fixup4cd.dat</code> for the cut down version)
# <code>initramfs-rpi4</code> (from [https://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/initramfs-rpi4 alpine/edge/releases/aarch64/netboot/initramfs-rpi4])
# <code>start4.elf</code> (from [https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/start4.elf raspberrypi/firmware/master/boot/start4.elf], alternatively <code>start4cd.elf</code> for the cut down version)
# <code>vmlinuz-rpi4</code> (from [https://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/vmlinuz-rpi4 alpine/edge/releases/aarch64/netboot/vmlinuz-rpi4])


{{cmd|<nowiki>echo /media/mmcblk0p1/boot /boot none defaults,bind 0 0 >> /etc/fstab</nowiki>}}
<code>config.txt</code>:
{{cmd|<nowiki>[pi4]
kernel=vmlinuz-rpi4
initramfs initramfs-rpi4
arm_64bit=1
</nowiki>}}


=== Persistent Installation on Raspberry Pi 3 ===
<code>cmdline.txt</code>:
{{cmd|<nowiki>modules=loop,squashfs console=ttyAMA0,115200 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/edge/main modloop=http://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/modloop-rpi4</nowiki>}}


See this page : https://wiki.alpinelinux.org/wiki/Classic_install_or_sys_mode_on_Raspberry_Pi
Instead of using the <nowiki>http://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/</nowiki> base URL above, pinning to a specific point in time is preferred. Raspberry Pi 4 netboot files are available from https://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot-20230329/ onward.


See https://web.archive.org/web/20171125115835/https://forum.alpinelinux.org/comment/1084#comment-1084
With the above configured the Raspberry Pi 4 should be able to boot from the network without an SD card.


== Troubleshooting ==
=== Wireless support with older Alpine images ===


=== Long boot time when running headless ===
In Alpine 3.14, the WiFi drivers for the Raspberry Pi were moved from <code>linux-firmware-brcm</code> to the <code>linux-firmware-cypress</code> package (source?). Since the images seem to be an outdated version of the former, Wi-Fi will work during installation, but after the first update it will break.
Use the ethernet interface to download the required packages:


If no peripherals are connected the system might hang for an exceptionally long period of time while it attempts to accumulate entropy.
{{cmd|apk add {{pkg|linux-firmware-cypress|arch=a*}}}}


If this is the case simply plugging in any USB device should work around this issue.
And reboot.


'''Alternatively''', installing haveged, the random numbers generator, would speed up the process :
If you need Wi-Fi, you'll need to [https://github.com/RPi-Distro/firmware-nonfree/tree/master/brcm download] the latest Broadcom drivers to your SD card.
(Replace /mnt/sdcard with the correct mount point.)


   apk update
   git clone --depth 1 https://github.com/RPi-Distro/firmware-nonfree.git
  apk add haveged
   cp firmware-nonfree/brcm/* /mnt/sdcard/firmware/brcm
  rc-update add haveged boot
   lbu commit -d
  service haveged start


(Tested on a raspberry pi zero W headless mode, no USB connected, Alpine 3.10.3)
== Enable the Serial Console ==
Besides having <code>enable_uart=1</code> in ''usercfg.txt'', the kernel command-line option <var>console</var> needs to be changed to <code>console=serial0,115200</code> in ''cmdline.txt''.


=== apk indicating 'No space left on device' ===
From a Linux desktop, connect to it with something like this:


Note some models of the Raspberry Pi such as the 3A+ only have 512M of RAM, which on fresh Alpine deployment will only leave around 200M for tmpfs root. It's important to keep this limitation in mind when using these boards.
{{cmd|cu -l /dev/ttyUSB0 -s 115200}}


== See Also ==
== See Also ==
Line 209: Line 265:
* [[Raspberry Pi 3 - Setting Up Bluetooth]]
* [[Raspberry Pi 3 - Setting Up Bluetooth]]
* [[Raspberry Pi 3 - Configuring it as wireless access point -AP Mode]]
* [[Raspberry Pi 3 - Configuring it as wireless access point -AP Mode]]
* [[Raspberry Pi 3 - Browser Client]]
* [[Linux Router with VPN on a Raspberry Pi]]
* [[Linux Router with VPN on a Raspberry Pi]]
* [[Create a bootable SDHC from a Mac]]
* [[Create a bootable SDHC from a Mac]]
* Build custom Raspberry Pi images based on Alpine via [https://github.com/tolstoyevsky/pieman Pieman]
* Build custom Raspberry Pi images based on Alpine via [https://github.com/tolstoyevsky/pieman Pieman]
 
* [[Tutorials and Howtos#Raspberry Pi]]


[[Category:Installation]]
[[Category:Installation]]
[[Category: Raspberry]]
[[Category: Raspberry]]

Latest revision as of 16:02, 31 October 2024

Warning: 11 Feb 2021 - There is currently a known bug upstream
kernel/initramfs cannot be loaded from subdirectory with same name as volume label. Since the kernel is installed to boot/, you must not use a label named boot for the fat32 partition.


This tutorial explains how to install Alpine Linux on a Raspberry Pi. Alpine Linux will be installed in diskless mode, hence, Alpine Local Backup (lbu) is required to save modifications between reboots.

For scenarios where there is not expected to be significant changes to disk after setup (like running a static HTTP server), this is likely preferable, as running the entire system from memory will improve performance (by avoiding the slow SD card) and improve the SD card life (by reducing the writes to the card, as all logging will happen in RAM). Diskless installations still allow you to install packages, save local files, and tune the system to your needs.

If any of the following apply, then installation in sys-mode installation is likely more appropriate.

  • There will be constant changes to the disk after initial setup (for example, if you expect people to login and save files to their home directories)
  • Logs should persists across reboots
  • Plan to install packages which consume more space than can be loaded into RAM
  • Plan to install kernel modules (such as ZFS)

Compability list

As of Alpine 3.19:

  • armhf (defconfig bcmrpi) - Raspberry Pi 1, Zero, ZeroW, cm1
  • armv7 (defconfig bcm2709) - Raspberry Pi 2, 3, 3+, Zero2W, cm3, cm3+
  • aarch64 (defconfig bcm2711) - Raspberry Pi 3, 3+, 4, 400, Zero2W, cm3, cm3+, cm4, 5

Preparation

  1. Download the Alpine for Raspberry Pi tarball. Use the compability list above when choosing image/file to download.
  2. Create a bootable FAT32 partition on your SD card. The partitioning and formatting part of the instructions on the linked page could be done using a graphical partitioning tool such as gnome-disks, just make sure the partition type is W95 FAT32 (LBA). (The current type can be found in the "Type" column in the output of fdisk -l.)
  3. Extract the tarball to the root of the bootable FAT32 partition.

To setup a headless system, a bootstrapping configuration overlay file headless.apkovl.tar.gz may be added to enable basic networking, so that following configuration steps can be performed under ssh. Pi Zero may be configured with simple USB ethernet-gadget networking with another computer sharing its internet connection.

It is recommended to create a usercfg.txt file on boot partition to configure low-level system settings, as config.txt may be replaced during bootloader/system upgrades: details can be found here.
However, note some settings can only be set in config.txt, and will have no effect when specified in usercfg.txt (e.g. gpu_mem). Some interesting values include:

  • To enable the UART console: enable_uart=1
  • To enable audio: dtparam=audio=on
  • By default system will use legacy video driver: some options may be used to adjust displays modes (e.g. if you see black edges around your screen after booting the Pi, you can add disable_overscan=1). Alternatively Linux DRM-KMS driver may be used (see below).
  • If you plan to install on a Pi Compute Module 4 with I/O board, you may need to add: otg_mode=1

Recent versions include Broadcom firmware files. If you're using an older Alpine version, see section below.

Installation

Follow these steps to install Alpine Linux in Diskless Mode:

  1. Insert the SD card into the Raspberry Pi and power it on
  2. Login into the Alpine system as root. Leave the password empty.
  3. Check the current date and time by issuing date. Correct if necessary by issuing date -s YYYYMMDDhhmm.
  4. Type setup-alpine
  5. Once the installation is complete, commit the changes by typing lbu commit -d

Type reboot to verify that the installation was indeed successful.

Post Installation

Update the System

After installation, make sure your system is up-to-date:

apk update apk upgrade

Don't forget to save the changes:

lbu commit -d

Note: this does not upgrade the kernel. In order to upgrade the kernel, a full upgrade of the Alpine Linux version must be performed as described in upgrading Alpine Linux for removable media.

Linux Kernel Graphics Modes

By default system configuration will use legacy video driver: this driver has some limitations and is lacking support.
It is recommended to enable Linux DRM-KMS driver by adding the following to usercfg.txt:

# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2

# Don't have the firmware create an initial video= setting in cmdline.txt.
# Use the kernel's default instead.
disable_fw_kms_setup=1

Note: This overlay disables legacy video options.

Install the Mesa drivers (Pi4 and Pi5):

apk add mesa-dri-gallium

Then reboot:

lbu_commit -d; reboot

Wireless drivers

As of Alpine 3.17, Wifi and Bluetooth drivers are available within install image: they are part of linux-firmware-brcm (and linked dependencies).
Since kernel 6.1.25 (i.e. Alpine 3.18), onboard bluetooth is enabled & autoprobed by default (it may be disabled by setting krnbt off).

Troubleshooting

raspinfo utility can be used as a first step to diagnose issues: it will make a log report of essential Pi system configuration, and is often used as a reference to submit questions or bug reports within Raspberry Pi community (Forums, Github, etc).
It can be installed with raspberrypi-utils-raspinfo subpackage.

Long boot time when running headless

If no peripherals are connected, the system might hang for an exceptionally long period of time while it attempts to accumulate entropy.

If this is the case, simply plugging in any USB device should work around this issue, since it increases the amount of entropy available to the kernel via interrupts.

apk indicating 'No space left on device'

Note some models of the Raspberry Pi such as the 3A+ only have 512M of RAM, which on fresh Alpine deployment will only leave around 200M for tmpfs root. It's important to keep this limitation in mind when using these boards.

Clock-related error messages

During the booting time, you might notice errors related to the hardware clock. Many Raspberry Pi do not have a hardware clock, or may have one without battery, in which case the behaviour depends on whether you physically cut power or not.

Also, setup-alpine will adapt the installation to the type of Raspberry Pi you use, which means for example that on a RPI5, you will see hwclock> service, whereas on a RPI4 it will use swclock. In doubt, make sure to do the installation on the target hardware.

If you still experience clock errors, add the following line to /etc/chrony/chrony.conf:

makestep 0.1 3

Persistent storage

Traditional disk-based (sys) installation

This material is proposed for merging ...

It should be merged with Classic install or sys mode on Raspberry Pi. There's an existing page for sys-installations on RasPi. (Discuss)

It is also possible to switch 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

You might also consider overlaytmpfs=yes here, which 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.

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

Loopback image with overlayfs

When you install Alpine in diskless mode, the entire system is loaded into memory at boot. If you want additional storage (for example, if you need more space than offered by your RAM) we need to create loop-back storage onto the SD card mounted with overlayfs.

First, make the SD card writable again and change fstab to always do so:

mount /media/mmcblk0p1 -o rw,remount sed -i 's/vfat\ ro,/vfat\ rw,/' /etc/fstab

Create the loop-back file, this example is 1 GB:

dd if=/dev/zero of=/media/mmcblk0p1/persist.img bs=1024 count=0 seek=1048576

Install the ext utilities:

apk add e2fsprogs

Format the loop-back file:

mkfs.ext4 /media/mmcblk0p1/persist.img

Mount the storage:

echo "/media/mmcblk0p1/persist.img /media/persist ext4 rw,relatime,errors=remount-ro 0 0" >> /etc/fstab mkdir /media/persist mount -a

Make the overlay folders, we are using the /usr directory here, but you can use /home or anything else.

Warning: Overlay workdir needs to be an empty directory on the same filesystem mount as the upper directory. So each overlay must use its own workdir.



mkdir /media/persist/usr mkdir /media/persist/.work_usr echo "overlay /usr overlay lowerdir=/usr,upperdir=/media/persist/usr,workdir=/media/persist/.work_usr 0 0" >> /etc/fstab mount -a

Your /etc/fstab should look something like this:

/dev/cdrom /media/cdrom iso9660 noauto,ro 0 0 /dev/usbdisk /media/usb vfat noauto,ro 0 0 /dev/mmcblk0p1 /media/mmcblk0p1 vfat rw,relatime,fmask=0022,dmask=0022,errors=remount-ro 0 0 /media/mmcblk0p1/persist.img /media/persist ext4 rw,relatime,errors=remount-ro 0 0 overlay /usr overlay lowerdir=/usr,upperdir=/media/persist/usr,workdir=/media/persist/.work_usr 0 0

Now commit the changes: (optionally remove the e2fsprogs, but it does contain repair tools)

lbu_commit -d

Remember, with this setup if you install things and you have done this overlay for /usr, you must not commit the 'apk add', otherwise, while it boots it will try and install it to memory, not to the persistent storage.

If you do want to install something small at boot, you can use apk add and lbu commit -d.

If it is something a bit bigger, then you can use apk add but then not commit it. It will be persistent (in /user), but be sure to check everything you need is in that directory and not in folders you have not made persistent.

Netboot

Netbooting Raspberry Pi 4

The Raspberry Pi 4 bootloader can be configured to boot from the network [1]. Configure the bootloader with at least

BOOT_ORDER=0xf142 TFTP_PREFIX=1

and optionally also

TFTP_IP=x.x.x.x

where x.x.x.x is the IP address of your TFTP server.

If not configuring TFTP_IP in the bootloader, you'll need to configure your DHCP server to advertise the TFTP server IP address. This varies depending on your DHCP server; use the following details if applicable:

  1. Vendor class: PXEClient:Arch:00000:UNDI:002001
  2. Filename: /

The minimal set of files that your TFTP server needs to host are:

  1. bcm2711-rpi-4-b.dtb (from raspberrypi/firmware/master/boot/bcm2711-rpi-4-b.dtb)
  2. cmdline.txt (see below)
  3. config.txt (see below)
  4. fixup4.dat (from raspberrypi/firmware/master/boot/fixup4.dat, alternatively fixup4cd.dat for the cut down version)
  5. initramfs-rpi4 (from alpine/edge/releases/aarch64/netboot/initramfs-rpi4)
  6. start4.elf (from raspberrypi/firmware/master/boot/start4.elf, alternatively start4cd.elf for the cut down version)
  7. vmlinuz-rpi4 (from alpine/edge/releases/aarch64/netboot/vmlinuz-rpi4)

config.txt:

[pi4] kernel=vmlinuz-rpi4 initramfs initramfs-rpi4 arm_64bit=1

cmdline.txt:

modules=loop,squashfs console=ttyAMA0,115200 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/edge/main modloop=http://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/modloop-rpi4

Instead of using the http://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot/ base URL above, pinning to a specific point in time is preferred. Raspberry Pi 4 netboot files are available from https://dl-cdn.alpinelinux.org/alpine/edge/releases/aarch64/netboot-20230329/ onward.

With the above configured the Raspberry Pi 4 should be able to boot from the network without an SD card.

Wireless support with older Alpine images

In Alpine 3.14, the WiFi drivers for the Raspberry Pi were moved from linux-firmware-brcm to the linux-firmware-cypress package (source?). Since the images seem to be an outdated version of the former, Wi-Fi will work during installation, but after the first update it will break. Use the ethernet interface to download the required packages:

apk add linux-firmware-cypress

And reboot.

If you need Wi-Fi, you'll need to download the latest Broadcom drivers to your SD card. (Replace /mnt/sdcard with the correct mount point.)

 git clone --depth 1 https://github.com/RPi-Distro/firmware-nonfree.git
 cp firmware-nonfree/brcm/* /mnt/sdcard/firmware/brcm

Enable the Serial Console

Besides having enable_uart=1 in usercfg.txt, the kernel command-line option console needs to be changed to console=serial0,115200 in cmdline.txt.

From a Linux desktop, connect to it with something like this:

cu -l /dev/ttyUSB0 -s 115200

See Also