Alpine Boot Process
I want to create a USB stick that will boot into Alpine
connect to the network using a static IP address and
gateway then connect to a VPN with OpenVPN... so how does
init
in Alpine work?
initramfs
Alpine, like other Linux, loads a kernel and a filesystem
into RAM then executes the init
file on that mini RAM
filesystem. For Alpine Linux 3.11.6 Standard that initial
filesystem is initramfs-lts
. This file is a gzip
compressed cpio archive.
The init
script in the initramfs shows that the boot
process calls nlplug-findfs
which searches for apkovls
on filesystems that can be applied to the run-from-RAM
tmpfs root created by the init
script.
Custom Bootable Drive
To begin boot into Alpine, these steps could be performed with another Linux installation but that would just complicate the tutorial.
From here the process is simple:
- Create a new partition table (with two partitons if you want UEFI)
- Copy the CDROM directories to the data partition
- Setup the boot sector (for BIOS booting) with syslinux
- Setup UEFI for syslinux
- Update the kernel command line arguments
- Boot the new OS to see that it works
- Configure system using setup-alpine
- Commit the changes using
lbu
Create new Partition Table
It should be possible to create a system that will boot using both UEFI and BIOS with a GPT partition table. Unfortunately I have not been able to do it so this first section is in two parts, one for BIOS and MBR and one for EFI and GPT.
Tools required for these steps are not all available on the Alpine Standard ISO but are available on Alpine Extended. I recommend using Alpine Extended since it has all the packages required in the ISO.
BIOS and MBR
Creating a bootable Alpine installation with BIOS and MBR
requires syslinux which is unfortunately not present in
Alpine Standard but easily added after setup-alpine
or
use Alpine Extended.
setup-alpine # if using Alpine Standard
apk add sfdisk e2fsprogs syslinux
sfdisk /dev/sda
label:dos
start=1M,size=255M,type=c,bootable
,,L
write
mkfs.vfat /dev/sda1
mkfs.ext2 /dev/sda2
modprobe vfat
modprobe ext4
mkdir /media/sda1
mkdir /media/sda2
mount /dev/sda1 /media/sda1
mount /dev/sda2 /media/sda2
ext2 partitions can be used as boot media by syslinux but vfat is fine and is similar to the GPT setup.
Install the kernel and syslinux configuration to sda1
mkdir -p /media/sda1/boot/syslinux
cp /media/cdrom/boot/syslinux/syslinux.cfg /media/sda1/boot/syslinux
cd /media/cdrom/boot
cp vmlinuz-lts initramfs-lts modloop-lts /media/sda1/boot
cd /media/sda1
syslinux --directory boot/syslinux --install /dev/sda1
Finally install the syslinux boot sector.
dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/mbr.bin of=/dev/sda
Booting this disk now should present an emergency boot prompt. To boot into Alpine the APKs must be copied to the drive and the kernel must be able to access the vfat file system.
Add the two kernel modules to the kernel arguments provided
by syslinux (/media/sda1/boot/syslinux/syslinux.cfg
):
TIMEOUT 20
PROMPT 1
DEFAULT lts
LABEL lts
MENU LABEL Linux lts
KERNEL /boot/vmlinuz-lts
INITRD /boot/initramfs-lts
FDTDIR /boot/dtbs-lts
APPEND modules=loop,squashfs,sd-mod,usb-storage,vfat,ext4 quiet nomodeset
And copy the apks to the media from the CDROM:
cp -r /media/cdrom/apks /media/sda1/
EFI and GPT
To support UEFI boot process the partition table
should be GPT, some hardware does not support UEFI booting
from a DOS/MBR partition. Unfortunately fdisk
included
with Alpine does not support GPT and so the sfdisk
utility or parted
must be installed. To install sfdisk
run setup-alpine
, choose none
when asked
about disks and leave the apk cache as /var/cache/apk
.
setup-alpine
apk add sfdisk e2fsprogs syslinux
sfdisk /dev/sda
label:gpt
1M,255M,U,*
,,L
write
mkfs.vfat /dev/sda1
mkfs.ext2 /dev/sda2
modprobe vfat
modprobe ext4
mkdir /media/sda1
mkdir /media/sda2
mount /dev/sda1 /media/sda1
mount /dev/sda2 /media/sda2
To allow UEFI to boot create the special UEFI boot directories and files:
mkdir -p /media/sda1/EFI/BOOT
cp /usr/share/syslinux/efi64/syslinux.efi /media/sda1/EFI/BOOT/bootx64.efi
cp /usr/share/syslinux/efi64/ldlinux.e64 /media/sda1/EFI/BOOT/ldlinux.e64
Just these files will allow UEFI to boot into syslinux. Of course there is no kernel or syslinux configuration so those files will need adding also.
mkdir -p /media/sda1/boot/syslinux
cd /media/cdrom/boot
cp vmlinuz-lts initramfs-lts modloop-lts /media/sda1/boot
cp syslinux/syslinux.cfg /media/sda1/boot/syslinux
By default the kernel included does not include support
for a VFAT file system or an EXT2 file system, to allow
the boot process to load the APKs from the VFAT partition
alter the kernel arguments in syslinux.cfg
. (Note:
the ext4 kernel module provides support for reading and
writing EXT2 file systems).
vi /media/sda1/boot/syslinux/syslinux.cfg
TIMEOUT 20
PROMPT 1
DEFAULT lts
LABEL lts
MENU LABEL Linux lts
KERNEL /boot/vmlinuz-lts
INITRD /boot/initramfs-lts
FDTDIR /boot/dtbs-lts
APPEND modules=loop,squashfs,sd-mod,usb-storage,vfat,ext4 quiet nomodeset
If you boot this now the system will boot to emergency rescue prompt. The last step is to copy the apk files from the CDROM.
cp -r /media/cdrom/apks /media/sda1
The system is now ready to boot from sda. Unmount the drives and poweroff the system then remove the CDROM the system should now boot using UEFI to the Alpine login prompt.
umount /media/sda1
umount /media/sda2
poweroff