Hardened Kernel on Alpine — Edge / aarch64 / linux-virt 7.3.7

Building a Hardened Linux Kernel on Alpine

The required steps — build as a normal user, escalate with su. Every command has a copy button. Alpine Edge • aarch64 • custom virt-based flavor “virthardened” • kernel 7.3.7 + hardened1 patch • username fro, pkgrel 1.

What you are doing

Alpine's linux-virt kernel (a slim kernel for virtual machines) has no pre-built hardened version, so you rebuild it with the anthraxx linux-hardened patch applied, as your own flavor.

Why work in main/linux-lts? Alpine builds the linux-virt kernel from the linux-lts recipe — there is no separate linux-virt folder. One APKBUILD there produces both kernels, and both config files live beside it.

Before you start

ROOT vs USER. Commands tagged ROOT run as root (via su -c, which asks for the root password). Commands tagged USER run as your normal user. Never run the abuild commands as root — they only work as your normal user.
vi basics. It opens in command mode. Press i to type (insert mode), Esc to leave it. Search: /text then Enter (n = next match). Save & quit: Esc then :wq then Enter. Quit without saving: :q!. Delete one character: x.

Step 1 · Set up a package-building account ROOTUSER

Install the build tools and add yourself to the abuild group (as root):

su -c 'apk add alpine-sdk'
su -c "addgroup $(whoami) abuild"

Log out and back in so the group takes effect, then create your signing key as your normal user:

abuild-keygen -n -a

Install the public half of that key into /etc/apk/keys (as root). Without this, the last stage of the build fails with UNTRUSTED signature / Failed to create index:

su -c 'install -m644 /home/fro/.abuild/*.rsa.pub /etc/apk/keys/'
Must do: log out and back in after addgroup (or reboot the VM), or the build in Step 7 fails with permission errors.

Step 2 · Get Alpine's kernel recipe USER

Clone aports, switch to the Edge branch, make a working branch, and enter the kernel directory:

cd ~
git clone --depth=1 https://gitlab.alpinelinux.org/alpine/aports.git
cd aports
git checkout master
git checkout -b hardened-virt
cd main/linux-lts

Step 3 · Confirm the kernel version USER

The patch only applies to the exact version in the recipe. Expect pkgver=7.3.7:

grep -E '^pkgver|^pkgrel' APKBUILD
If pkgver is not 7.3.7: download the hardened patch whose version matches what it shows, or check out an aports commit where it is 7.3.7. The patch version and pkgver must be identical.

Step 4 · Download the hardened patch USER

Fetch the patch and its signature into the current directory:

BASE=https://github.com/anthraxx/linux-hardened/releases/download/v7.3.7-hardened1
wget -O 0006-linux-hardened.patch     "$BASE/linux-hardened-v7.3.7-hardened1.patch"
wget -O 0007-linux-hardened.patch.sig "$BASE/linux-hardened-v7.3.7-hardened1.patch.sig"

Confirm they arrived:

ls -l 0006-linux-hardened.patch 0007-linux-hardened.patch.sig

Step 5 · Remove the version suffix from the patch USER

The patch renames the kernel to end in -hardened1, which breaks Alpine's install hooks. Open the patch:

vi 0006-linux-hardened.patch

Type /hardened1 then Enter to jump to the line. It edits the kernel's top-level Makefile and looks like this:

reference — what you'll see

--- a/Makefile
+++ b/Makefile
 VERSION = 7
 PATCHLEVEL = 3
 SUBLEVEL = 7
-EXTRAVERSION =
+EXTRAVERSION = -hardened1

Put the cursor on the +EXTRAVERSION = -hardened1 line, press A (capital) to edit at the end of the line, Backspace to delete  -hardened1 so it reads exactly +EXTRAVERSION = (keep the leading +). Then Esc, :wq, Enter.

Do not edit 0007-linux-hardened.patch.sig — it is a signature and must stay exactly as downloaded.

Step 6 · Add the patch to the recipe USER

vi APKBUILD

6a. Find pkgrel (type /pkgrel then Enter) and set it to pkgrel=1 (one more than the stock value of 0).

6b. Find the source= block (type /source= then Enter). Add your two filenames on their own lines, before the .config lines (arrows mark the added lines — type only the filenames):

reference — edited source= block

source="https://.../linux-$_kernver.tar.xz
	0001-...patch
	0006-linux-hardened.patch        ← add
	0007-linux-hardened.patch.sig    ← add
	lts.aarch64.config
	virt.aarch64.config
	"

Indent to match the other entries, then save and quit (Esc, :wq, Enter).

Step 7 · Create your flavor, then build USER

First, a one-time setting so the compile uses all your cores (not one). Set the number to your VM's core count:

echo 'export JOBS=4'        >> ~/.abuild/abuild.conf
echo 'export MAKEFLAGS=-j4' >> ~/.abuild/abuild.conf

Create your own kernel flavor based on the virt config. You cannot reuse the built-in virt flavor directly (the recipe would add its config twice and abort with “duplicate found in $source”). Copy it to a new name:

cp virt.aarch64.config virthardened.aarch64.config
Do not add virthardened.aarch64.config to the source= list. The FLAVOR= prefix below adds it automatically; adding it yourself recreates the duplicate error.

Now record checksums and build only your flavor. FLAVOR=virthardened makes it the one and only flavor built (lts and virt are both skipped), and it must be set on both commands:

FLAVOR=virthardened abuild checksum
FLAVOR=virthardened abuild -r

Your package lands here:

ls ~/packages/main/aarch64/linux-virthardened-7.3.7-r1.apk
Slow build? On Apple Silicon, check your QEMU command includes -accel hvf and -cpu host (or enable the hypervisor option in UTM). Without hardware acceleration the aarch64 guest is software-emulated and many times slower. On an Intel Mac an aarch64 guest is always emulated, so a native build is inherently very slow.

Step 8 · Install the hardened kernel ROOT

Install the package you built directly. --allow-untrusted is needed because it is signed by your own key:

su -c 'apk add --allow-untrusted /home/fro/packages/main/aarch64/linux-virthardened-7.3.7-r1.apk'
Not sure where abuild saved it? Find it (as root), then pass the full path it prints to apk add --allow-untrusted:
su -c "find / -xdev -name 'linux-virthardened-*.apk' 2>/dev/null"
Fallback boot entry in case the hardened kernel misbehaves: also install the stock LTS kernel — a different package, so it keeps its own GRUB entry:
su -c 'apk add linux-lts'

Step 9 · Update GRUB ROOT

Installing the kernel package already rebuilds the initramfs. Regenerate the GRUB menu so the new kernel appears:

su -c 'grub-mkconfig -o /boot/grub/grub.cfg'

Check the kernel files are in place — you should see vmlinuz-virthardened and initramfs-virthardened:

ls /boot
If initramfs-virthardened is missing: get the exact module name with ls /lib/modules (such as 7.3.7-1-virthardened), build it by hand, then regenerate GRUB again:
su -c 'mkinitfs 7.3.7-1-virthardened'

Step 10 · Reboot and verify ROOT USER

su -c 'reboot'

When it returns, confirm you are running your kernel (as your normal user):

uname -a

It should show 7.3.7-1-virthardened with no -hardened1 suffix (because you removed EXTRAVERSION in Step 5). If the system does not boot, pick the stock linux-lts fallback entry at the GRUB menu, boot it, and rebuild after fixing the problem.