Alpine runs in memory on the Pi, which is great for performance, security, and for minimizing wear on the SD-card. The downside is that it has no persistent storage. Everything done on such a system is cleared out on reboot.
There are some measures that can be taken to have persistent storage on a Pi with Alpine Linux.
Create a /home directory on the SD-card
When a 32 GB SD card is used then the OS uses the first 16 GB,
leaving about 15 GB for a /home
partitition.
Assuming the SD-card is accesible under /dev/mmcblk0
,
then the first partition used for Alpine boot is /dev/mmcblk01
,
and a new partition /dev/mmcblk02
can be added for /home
.
Then install the file system utilities e2fsprogs
for ext4
and
format the new disk partition:
# apk add e2fsprogs
# mkfs.ext4 -L AlpineHome /dev/mmcblk0p2
Make the SD-card accessible by updating file /etc/fstab
as follows:
# <fs> <mountpoint> <type> <opts> <dump> <pass>
/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,errors=remount-ro 0 0
/dev/mmcblk0p2 /home ext4 rw,noatime,errors=remount-ro 0 0
Optionally the mount points for /dev/cdrom
and /dev/usbdisk
could be
commented out if they are not needed..
Saving system settings
In order to save configuration settings and be able to use installed
package after a reboot, Alpine makes use of the
local backup utility
lbu
.
Lbu needs to know where to save its backup.
Run lbu-setup
to configure the backup location, in our case it is
mmcblk0p1
.
The result is reflected in file /etc/lbu/lbu.conf
.
Basically what lbu
does is analyze the difference between the system
setup on disk and the actual running setup in memory.
The detected changes are written to a gzipped tar file, that is unpacked
at boot.
Typically the changes made are in the /etc
directory.
If other directories or files needs to be monitored for change too,
then they need to be added with lbu add <path>
.
Where path can be a file or a directory.
The result can be found in /etc/apk/protected_paths.d/lbu.lst
.
Saving installed packges
When Alpine boots, it should reinstall all the packages added earlier.
For that to work, a local package cache should be set up.
This is done though the command setup-apkcache
.
Basically what is does is setup a soft link from /etc/apk/cache
towards a directory on persistent storage.
Assuming the SD-card is mounted at /media/mmcblk0p1
:
# mkdir /media/mmcblk0p1/cache
# setup-apkcache
Enter apk cache directory (or '?' or 'none') [/media/mmcblk0p1/cache]
# lbu commit
When the Pi boots, it will reinstall the packages.