<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pawciobiel</id>
	<title>Alpine Linux - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pawciobiel"/>
	<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/wiki/Special:Contributions/Pawciobiel"/>
	<updated>2026-04-30T19:37:33Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31896</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31896"/>
		<updated>2025-12-30T09:52:52Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: rephrase configuration&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.23 (linux-lts - 6.18):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.23-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
It is a good idea to use custom `FLAVOR` in order to change the name of your kernel and package (custom flavor or revision) so you newly build kernel will not override the existing linux-lts and it&#039;s modules. Flavor build will only use your config and omit files that are not for your cpu architectures or virt* configs.(Virt configs provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
This will also speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture x86, x86_64...you use.)&lt;br /&gt;
&lt;br /&gt;
First make a copy of your ARCH config file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp lts.x86_64.config lts-my_custom.x86_64.config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your custom config file and add your changes. When you are done with your edits you need to run checksum to update APKBUILD:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;FLAVOR=lts-my_custom abuild checksum&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then commit your changes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;git commit -a -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
There are multiple ways to prepare and change kernel config. The basic one is to just edit your custom config file. You can also attempt to create a config tailored for your currently running system and modules.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
make localmodconfig&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;pre&amp;gt;make localyesconfig&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;make menuconfig&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.&lt;br /&gt;
&lt;br /&gt;
When you are done updating the {{path|config-NAME.ARCH}}, you need to update sums:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abuild checksum&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Please note that kernel build process may not use some of the settings you set in `abuild.conf` so in order to customize compiler or linker and/or it&#039;s flags you may need to edit APKBUILD.&lt;br /&gt;
If you have your config ready, first try building with:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FLAVOR=lts-my_custom abuild -rK 2&amp;gt;&amp;amp;1 | tee build1.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you use &amp;lt;code&amp;gt;FLAVOR&amp;lt;/code&amp;gt; or removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs in APKBUILD there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
If the build was successful the kernel packages are located in &amp;lt;code&amp;gt;~/packages/main/ARCH&amp;lt;/code&amp;gt;&lt;br /&gt;
You probably already know how to install a package in your Alpine Linux...&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
In case your new kernel may be missing a module and can&#039;t boot it is generally a good idea to keep the default &amp;lt;code&amp;gt;linux-lts&amp;lt;/code&amp;gt; so make sure you have it installed... &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case something goes wrong with a boot process it is also a good idea to have a bootable rescue Alpine USB ready.&lt;br /&gt;
&lt;br /&gt;
Once you have the default lts kernel and rescue USB &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31895</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31895"/>
		<updated>2025-12-30T09:46:48Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: rephrase testing description&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.23 (linux-lts - 6.18):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.23-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
It is a good idea to use custom `FLAVOR` in order to change the name of your kernel and package (custom flavor or revision) so you newly build kernel will not override the existing linux-lts and it&#039;s modules. Flavor build will only use your config and omit files that are not for your cpu architectures or virt* configs.(Virt configs provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
This will also speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture x86, x86_64...you use.)&lt;br /&gt;
&lt;br /&gt;
First make a copy of your ARCH config file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp lts.x86_64.config lts-my_custom.x86_64.config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your custom config file and add your changes. When you are done with your edits you need to run checksum to update APKBUILD:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;FLAVOR=lts-my_custom abuild checksum&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then commit your changes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;git commit -a -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Please note that kernel build process may not use some of the settings you set in `abuild.conf` so in order to customize compiler or linker and/or it&#039;s flags you may need to edit APKBUILD.&lt;br /&gt;
If you have your config ready, first try building with:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FLAVOR=lts-my_custom abuild -rK 2&amp;gt;&amp;amp;1 | tee build1.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you use &amp;lt;code&amp;gt;FLAVOR&amp;lt;/code&amp;gt; or removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs in APKBUILD there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
If the build was successful the kernel packages are located in &amp;lt;code&amp;gt;~/packages/main/ARCH&amp;lt;/code&amp;gt;&lt;br /&gt;
You probably already know how to install a package in your Alpine Linux...&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
In case your new kernel may be missing a module and can&#039;t boot it is generally a good idea to keep the default &amp;lt;code&amp;gt;linux-lts&amp;lt;/code&amp;gt; so make sure you have it installed... &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case something goes wrong with a boot process it is also a good idea to have a bootable rescue Alpine USB ready.&lt;br /&gt;
&lt;br /&gt;
Once you have the default lts kernel and rescue USB &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31894</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31894"/>
		<updated>2025-12-30T09:38:00Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Updated installing&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.23 (linux-lts - 6.18):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.23-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
It is a good idea to use custom `FLAVOR` in order to change the name of your kernel and package (custom flavor or revision) so you newly build kernel will not override the existing linux-lts and it&#039;s modules. Flavor build will only use your config and omit files that are not for your cpu architectures or virt* configs.(Virt configs provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
This will also speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture x86, x86_64...you use.)&lt;br /&gt;
&lt;br /&gt;
First make a copy of your ARCH config file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp lts.x86_64.config lts-my_custom.x86_64.config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your custom config file and add your changes. When you are done with your edits you need to run checksum to update APKBUILD:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;FLAVOR=lts-my_custom abuild checksum&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then commit your changes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;git commit -a -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Please note that kernel build process may not use some of the settings you set in `abuild.conf` so in order to customize compiler or linker and/or it&#039;s flags you may need to edit APKBUILD.&lt;br /&gt;
If you have your config ready, first try building with:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FLAVOR=lts-my_custom abuild -rK 2&amp;gt;&amp;amp;1 | tee build1.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you use &amp;lt;code&amp;gt;FLAVOR&amp;lt;/code&amp;gt; or removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs in APKBUILD there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
If the build was successful the kernel packages are located in &amp;lt;code&amp;gt;~/packages/main/ARCH&amp;lt;/code&amp;gt;&lt;br /&gt;
You probably already know how to install a package in your Alpine Linux...&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31893</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31893"/>
		<updated>2025-12-30T09:35:28Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Update building with FLAVOR&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.23 (linux-lts - 6.18):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.23-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
It is a good idea to use custom `FLAVOR` in order to change the name of your kernel and package (custom flavor or revision) so you newly build kernel will not override the existing linux-lts and it&#039;s modules. Flavor build will only use your config and omit files that are not for your cpu architectures or virt* configs.(Virt configs provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
This will also speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture x86, x86_64...you use.)&lt;br /&gt;
&lt;br /&gt;
First make a copy of your ARCH config file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp lts.x86_64.config lts-my_custom.x86_64.config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your custom config file and add your changes. When you are done with your edits you need to run checksum to update APKBUILD:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;FLAVOR=lts-my_custom abuild checksum&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then commit your changes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;git commit -a -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Please note that kernel build process may not use some of the settings you set in `abuild.conf` so in order to customize compiler or linker and/or it&#039;s flags you may need to edit APKBUILD.&lt;br /&gt;
If you have your config ready, first try building with:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FLAVOR=lts-my_custom abuild -rK 2&amp;gt;&amp;amp;1 | tee build1.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you use &amp;lt;code&amp;gt;FLAVOR&amp;lt;/code&amp;gt; or removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs in APKBUILD there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31892</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31892"/>
		<updated>2025-12-30T09:24:23Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: updated description with FLAVOR build&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.23 (linux-lts - 6.18):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.23-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
It is a good idea to use custom `FLAVOR` in order to change the name of your kernel and package (custom flavor or revision) so you newly build kernel will not override the existing linux-lts and it&#039;s modules. Flavor build will only use your config and omit files that are not for your cpu architectures or virt* configs.(Virt configs provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
This will also speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture x86, x86_64...you use.)&lt;br /&gt;
&lt;br /&gt;
First make a copy of your ARCH config file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp lts.x86_64.config lts-my_custom.x86_64.config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your custom config file and add your changes. When you are done with your edits you need to run checksum to update APKBUILD:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;FLAVOR=lts-my_custom abuild checksum&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then commit your changes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;git commit -a -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31891</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31891"/>
		<updated>2025-12-30T09:09:18Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: updated release branch to 23&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.23.0&lt;br /&gt;
| 3.23-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.23 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.23-stable origin/3.23-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
_flavor=lts-custom&lt;br /&gt;
pkgname=linux-$_flavor&lt;br /&gt;
pkgrel=0&lt;br /&gt;
pkgdesc=&amp;quot;Linux ${_flavor} kernel (optimized)&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy your config from {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD lts.ARCH.config -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Kernels]]&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
* [[Kernel_live_patching|Kernel Live Patching (KLP)]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31361</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31361"/>
		<updated>2025-11-02T20:23:14Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.22.0&lt;br /&gt;
| 3.22-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.22 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.22-stable origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
_flavor=lts-custom&lt;br /&gt;
pkgname=linux-$_flavor&lt;br /&gt;
pkgrel=0&lt;br /&gt;
pkgdesc=&amp;quot;Linux ${_flavor} kernel (optimized)&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy your config from {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD lts.ARCH.config -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31360</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31360"/>
		<updated>2025-11-02T20:22:23Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: /* Building */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.22.0&lt;br /&gt;
| 3.22-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.22 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.22-stable origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
_flavor=lts-custom&lt;br /&gt;
pkgname=linux-$_flavor&lt;br /&gt;
pkgrel=0&lt;br /&gt;
pkgdesc=&amp;quot;Linux ${_flavor} kernel (optimized)&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy your config from {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD lts.ARCH.config -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31359</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31359"/>
		<updated>2025-11-02T20:06:45Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: update code example with custom flavor&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.22.0&lt;br /&gt;
| 3.22-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.22 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.22-stable origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
_flavor=lts-custom&lt;br /&gt;
pkgname=linux-$_flavor&lt;br /&gt;
pkgrel=0&lt;br /&gt;
pkgdesc=&amp;quot;Linux ${_flavor} kernel (optimized)&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy your config from {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD lts.ARCH.config -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31358</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31358"/>
		<updated>2025-11-02T20:00:52Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: add example about flavor&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.22.0&lt;br /&gt;
| 3.22-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.22 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.22-stable origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
_flavor=lts-custom&lt;br /&gt;
pkgname=linux-$_flavor&lt;br /&gt;
pkgrel=0&lt;br /&gt;
pkgdesc=&amp;quot;Linux ${_flavor} kernel (optimized)&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy your config from {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD lts.ARCH.config -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31357</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31357"/>
		<updated>2025-11-02T19:51:05Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: 3.17 -&amp;gt; 3.22&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.22.0&lt;br /&gt;
| 3.22-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.22 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.22-stable origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy it {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31356</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31356"/>
		<updated>2025-11-02T19:47:57Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: reword and add info about virt configs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures or virt* configs that are for virtualized environments.(They provide optimized kernel configurations for running Alpine as a guest in virtual machines.)&lt;br /&gt;
Setting custom name/flavor will prevent overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and commenting out unused configs will speed up the process of applying kernel patches by applying it for your architecture only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy it {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31355</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31355"/>
		<updated>2025-11-02T19:43:48Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Review and update building&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures. This will avoid overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and speed up the process of applying kernel patches applying it for your arch only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy it {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, make sure you have ccache installed.  This should reduce compile time on multiple builds.&lt;br /&gt;
You may also want to read up on compile flags - perhaps you want your build to be optimized for speed (or size which is the default).&lt;br /&gt;
Review &amp;lt;code&amp;gt;/etc/abuild.conf&amp;lt;/code&amp;gt; and set:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
PACKAGER_PRIVKEY=&amp;quot;/home/MY_USER_HERE/.abuild/my-email@mydomain.com-ID.rsa&amp;quot;&lt;br /&gt;
USE_CCACHE=1&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have your config ready, first try building with &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; which install most of the dependencies and keep buildtime temp dirs and files (srcdir/pkgdir/deps).&lt;br /&gt;
&lt;br /&gt;
If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.&lt;br /&gt;
Unless you removed or commented out &amp;lt;code&amp;gt;virt.*.config&amp;lt;/code&amp;gt; configs&lt;br /&gt;
there should be two sets one for -lts and the other for the -virt.&lt;br /&gt;
Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after &lt;br /&gt;
the second set so you can further customize the config.&lt;br /&gt;
Then you go into the {{path|src/linux-VER}} and edit the config file.&lt;br /&gt;
Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31354</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31354"/>
		<updated>2025-11-02T19:28:49Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: update info about localmodconfig and move build related info to building&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures. This will avoid overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and speed up the process of applying kernel patches applying it for your arch only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy it {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to create a config for your currently running system and modules first.&lt;br /&gt;
&lt;br /&gt;
To create a kernel config based on your currently running PC, use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This command reads your current kernel&#039;s configuration from /proc/config.gz (if available) or /boot/config-* and creates a .config file that includes only the modules currently loaded on your system. This results in a minimal configuration tailored to your hardware.&lt;br /&gt;
Alternatively, you can use:&lt;br /&gt;
&amp;lt;code&amp;gt;make localyesconfig&amp;lt;/code&amp;gt;&lt;br /&gt;
This is similar to &#039;&#039;localmodconfig&#039;&#039;, but instead of building features as modules (m), it builds them directly into the kernel (y), which can result in a larger kernel image but eliminates the need for separate module files.&lt;br /&gt;
Note: Before running either command, make sure you have all the hardware you want to support actively in use (USB devices plugged in, network cards active, etc.) so their modules are loaded and included in the configuration.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, you may want to remove as many modules as possible.  This will reduce the time to compile greatly.  Also, you may want to use [https://github.com/ccache/ccache/ ccache] for faster recompiles especially if you are searching for the minimal set of options or modules to use or include.&lt;br /&gt;
&lt;br /&gt;
You should then do an &amp;lt;code&amp;gt;abuild -r&amp;lt;/code&amp;gt; to attempt to build it.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31353</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31353"/>
		<updated>2025-11-02T19:14:49Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: update to 3.22 and rephrase&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.22 (linux-lts - 6.12):&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.22-stable}}&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.22-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &lt;br /&gt;
&amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  &lt;br /&gt;
Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.&lt;br /&gt;
&lt;br /&gt;
You may want to change APKBUILD to change the name of your kernel and package (custom flavor or revision) and comment out or remove lines with config files that are not for your cpu architectures. This will avoid overriding your default &#039;&#039;linux-lts&#039;&#039; kernel modules and speed up the process of applying kernel patches applying it for your arch only. (Obviously ARCH in the following example is whatever architecture (x86, x86_64, ...) you use.)&lt;br /&gt;
&lt;br /&gt;
When you are done with your edits to the APKBUILD file copy it {{path|lts.ARCH.config}} to {{path|.config}}.  You can then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make localmodconfig&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to run &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
Then add your changes, eg &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt;. &lt;br /&gt;
And commit &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the &#039;&#039;&#039;ARCH&#039;&#039;&#039;itecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to build the kernel first.  To do that, you do &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; to install most of the dependencies.  If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.  Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.  There should be two sets one for -lts and the other for the -virt.  Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after the second set so you can further customize the config.  Then you go into the {{path|src/linux-VER}} and edit the config file.  Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, you may want to remove as many modules as possible.  This will reduce the time to compile greatly.  Also, you may want to use [https://github.com/ccache/ccache/ ccache] for faster recompiles especially if you are searching for the minimal set of options or modules to use or include.&lt;br /&gt;
&lt;br /&gt;
You should then do an &amp;lt;code&amp;gt;abuild -r&amp;lt;/code&amp;gt; to attempt to build it.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31352</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31352"/>
		<updated>2025-11-02T18:43:11Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: typo yo -&amp;gt; you&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.17:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.17-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.  When you are done with your edits either by editing directly the APKBUILD and copying the {{path|lts.ARCH.config}} as {{path|.config}} in the {{path|linux-lts}} folder.  You will then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.  Then, do &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt; where ARCH is whatever architecture (x86, x86_64, ...) you use.  Then, you need to do &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the ARCHitecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to build the kernel first.  To do that, you do &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; to install most of the dependencies.  If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.  Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.  There should be two sets one for -lts and the other for the -virt.  Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after the second set so you can further customize the config.  Then you go into the {{path|src/linux-VER}} and edit the config file.  Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before you do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, you may want to remove as many modules as possible.  This will reduce the time to compile greatly.  Also, you may want to use [https://github.com/ccache/ccache/ ccache] for faster recompiles especially if you are searching for the minimal set of options or modules to use or include.&lt;br /&gt;
&lt;br /&gt;
You should then do an &amp;lt;code&amp;gt;abuild -r&amp;lt;/code&amp;gt; to attempt to build it.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31351</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31351"/>
		<updated>2025-11-02T18:42:37Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Add some example info about updating initramfs and bootloader&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.17:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.17-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.  When you are done with your edits either by editing directly the APKBUILD and copying the {{path|lts.ARCH.config}} as {{path|.config}} in the {{path|linux-lts}} folder.  You will then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.  Then, do &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt; where ARCH is whatever architecture (x86, x86_64, ...) you use.  Then, you need to do &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the ARCHitecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to build the kernel first.  To do that, you do &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; to install most of the dependencies.  If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.  Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.  There should be two sets one for -lts and the other for the -virt.  Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after the second set so you can further customize the config.  Then you go into the {{path|src/linux-VER}} and edit the config file.  Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before yo do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, you may want to remove as many modules as possible.  This will reduce the time to compile greatly.  Also, you may want to use [https://github.com/ccache/ccache/ ccache] for faster recompiles especially if you are searching for the minimal set of options or modules to use or include.&lt;br /&gt;
&lt;br /&gt;
You should then do an &amp;lt;code&amp;gt;abuild -r&amp;lt;/code&amp;gt; to attempt to build it.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.&lt;br /&gt;
&lt;br /&gt;
Normally during the installation using with &#039;&#039;apk&#039;&#039; there are tools that will update initramfs and a boot loader automatically for you. For easier debugging you may want to change boot loader config and remove &amp;lt;code&amp;gt;quiet&amp;lt;/code&amp;gt; from linux command line in &amp;lt;code&amp;gt;/etc/default/grub&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/update-extlinux.conf&amp;lt;/code&amp;gt;.&lt;br /&gt;
Or perhaps you want to change other kernel options in &amp;lt;code&amp;gt;/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt; - please remember to generate initramfs and update your bootloader manually, eg:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
# mkinitfs&lt;br /&gt;
# grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31350</id>
		<title>Custom Kernel</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Custom_Kernel&amp;diff=31350"/>
		<updated>2025-11-02T18:11:03Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: add info about kernel-hardening-checker&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Draft}}&lt;br /&gt;
&lt;br /&gt;
This process of building a &#039;&#039;&#039;custom configured kernel&#039;&#039;&#039; assumes you are running on Alpine Linux utilizing abuild &amp;amp; aports.&lt;br /&gt;
&lt;br /&gt;
== But why? ==&lt;br /&gt;
&lt;br /&gt;
You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.&lt;br /&gt;
&lt;br /&gt;
The lts kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices.  You can tweak the kernel for desktop use and low latency and responsiveness.&lt;br /&gt;
&lt;br /&gt;
You should disable modules to increase security.  By default, Alpine will install modules but not disable most of them.  Disabling modules will reduce an DMA attack but not eliminate it completely.  If you have a newer processor with VT-d, you can mitigate as long as you:&lt;br /&gt;
&lt;br /&gt;
Leave &amp;lt;code&amp;gt;CONFIG_INTEL_IOMMU_DEFAULT_ON=y&amp;lt;/code&amp;gt; or pass &amp;lt;code&amp;gt;intel_iommu=on&amp;lt;/code&amp;gt; as a kernel parameter and disable kernel logging so the attacker doesn&#039;t gain DMAR address information through dmesg.[https://blog.frizk.net/2016/11/disable-virtualization-based-security.html]  Also remove references to the kernel version to calculate the IOMMU addresses.[https://link.springer.com/content/pdf/10.1186/s13173-017-0066-7.pdf]&lt;br /&gt;
&lt;br /&gt;
You may also want to harden your kernel by adding at least some of the config changes recommended by &amp;lt;code&amp;gt;kernel-hardening-checker&amp;lt;/code&amp;gt; [https://github.com/a13xp0p0v/kernel-hardening-checker/]&lt;br /&gt;
&lt;br /&gt;
To increase the security of the boot process, if you have a TPM, you could set &amp;lt;code&amp;gt;CONFIG_INTEL_TXT=y&amp;lt;/code&amp;gt; (Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)) (which is not enabled in the hardened kernel by default), then you would need the SINIT module (provided only by Intel)[https://software.intel.com/en-us/articles/intel-trusted-execution-technology], a possibly compiled TrustedGrub2[https://github.com/Rohde-Schwarz-Cybersecurity/TrustedGRUB2], trousers[https://sourceforge.net/projects/trousers/?source=navbar], tboot[https://sourceforge.net/projects/tboot/].  These packages are not in aports and it is unknown if these tools work on musl.  It&#039;s not recommended for Edge.  Also, there would be trigger packages to generate hashes for the kernel and the mkinitfs updates.&lt;br /&gt;
&lt;br /&gt;
== Setting up the Alpine Build System ==&lt;br /&gt;
&lt;br /&gt;
First, you need to follow the steps in [[Creating_an_Alpine_package#Setup_your_system_and_account|Setup your system and account for building packages]].  You also need to configure your {{path|/etc/apk/repositories}} so that they search locally for your apks.  See [[Creating_an_Alpine_package#Testing_the_package_locally|Testing the package locally]] for details.&lt;br /&gt;
&lt;br /&gt;
After setting up accounts and repos, change your shell&#039;s current working directory to &#039;&#039;&#039;aports&#039;&#039;&#039; that you just cloned.&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ cd aports}}&lt;br /&gt;
&lt;br /&gt;
== Working with aports ==&lt;br /&gt;
&lt;br /&gt;
We will try using an existing lts kernel just tweaking the {{path|lts.ARCH.config}} file.  &lt;br /&gt;
&lt;br /&gt;
=== Switching to the proper release version ===&lt;br /&gt;
&lt;br /&gt;
You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
! Alpine version&lt;br /&gt;
! Remote branch&lt;br /&gt;
|-&lt;br /&gt;
| Edge&lt;br /&gt;
| master&lt;br /&gt;
|-&lt;br /&gt;
| 3.17.0&lt;br /&gt;
| 3.17-stable&lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following is required to get access to the {{path|APKBUILD}} released for that version of Alpine and which you will create a commit for.&lt;br /&gt;
&lt;br /&gt;
If you are on 3.17 do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b 3.17-stable origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
If you are on Edge do:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout master}}&lt;br /&gt;
&lt;br /&gt;
=== Creating your config ===&lt;br /&gt;
&lt;br /&gt;
You can use {{pkg|linux-lts}} but what you should do is create a local branch by doing:&lt;br /&gt;
&lt;br /&gt;
For Alpine Edge:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel}}&lt;br /&gt;
&lt;br /&gt;
For Alpine 3.17:&lt;br /&gt;
&lt;br /&gt;
{{cmd|$ git checkout -b my-custom-kernel origin/3.17-stable}}&lt;br /&gt;
&lt;br /&gt;
Doing it this way, you do less work in maintaining.  All you need to do is keep &#039;&#039;master&#039;&#039; or &#039;&#039;3.17-stable&#039;&#039; in sync[https://help.github.com/articles/syncing-a-fork/][https://help.github.com/articles/configuring-a-remote-for-a-fork/] and merge any conflicts.  &lt;br /&gt;
&lt;br /&gt;
First switch to the branch by doing &amp;lt;code&amp;gt;git checkout my-custom-kernel&amp;lt;/code&amp;gt;.  Then, you need to navigate to the {{path|main/linux-lts}} folder where you should see a APKBUILD and some config- files.  When you are done with your edits either by editing directly the APKBUILD and copying the {{path|lts.ARCH.config}} as {{path|.config}} in the {{path|linux-lts}} folder.  You will then move the {{path|.config}} back overriding the {{path|lts.ARCH.config}} generated by &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt; (discussed below in the &#039;&#039;Configuring kernel&#039;&#039; section).  After generating your config, you need to &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.  Then, do &amp;lt;code&amp;gt;git add APKBUILD lts.ARCH.config&amp;lt;/code&amp;gt; where ARCH is whatever architecture (x86, x86_64, ...) you use.  Then, you need to do &amp;lt;code&amp;gt;git commit APKBUILD config-NAME.ARCH -m &amp;quot;Enabled these options ....&amp;quot;&amp;lt;/code&amp;gt; for your customization the ARCHitecture of your system.  You do this so that git can keep your code separate from Alpine&#039;s and so your changes float forward between kernel updates.&lt;br /&gt;
&lt;br /&gt;
== Adding custom patches ==&lt;br /&gt;
&lt;br /&gt;
Custom patches should be added to &#039;&#039;sources=&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After you added the URL, you need to produce a checksum by doing &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().&lt;br /&gt;
&lt;br /&gt;
== Configuring kernel ==&lt;br /&gt;
&lt;br /&gt;
Attempt to build the kernel first.  To do that, you do &amp;lt;code&amp;gt;abuild -rK&amp;lt;/code&amp;gt; to install most of the dependencies.  If it complains about a dependency like {{pkg|elfutils-dev}} use &amp;lt;code&amp;gt;-rKd&amp;lt;/code&amp;gt;.  Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel.  There should be two sets one for -lts and the other for the -virt.  Just {{Key|Ctrl}}+{{Key|C}} out of the compilation process after the second set so you can further customize the config.  Then you go into the {{path|src/linux-VER}} and edit the config file.  Copy the {{path|.config}} file overriding the {{path|lts.ARCH.config}} in the srcdir.&lt;br /&gt;
&lt;br /&gt;
The alternative is to use the kernel configuration menu in the build-NAME folder, but before yo do that you need to &amp;lt;code&amp;gt;sudo apk add {{pkg|ncurses-dev}}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you are done using the menu in the build-NAME folder by doing &amp;lt;code&amp;gt;make menuconfig&amp;lt;/code&amp;gt;, you want to remove &amp;lt;code&amp;gt;ncurses-dev&amp;lt;/code&amp;gt;.  When you are done, it will be stored in &#039;&#039;.config&#039;&#039; which you need to again override the {{path|lts.ARCH.config}} file.  When you are done updating the {{path|config-NAME.ARCH}}, you need to do &amp;lt;code&amp;gt;abuild checksum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The options in the kernel config are typically defaults.  If your device is old, it may be set to n by default.&lt;br /&gt;
&lt;br /&gt;
=== Vanilla targets and tuning ===&lt;br /&gt;
&lt;br /&gt;
{|cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot; class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!ARCH&lt;br /&gt;
!Processor Type / CPU Selection / System Type&lt;br /&gt;
!Code Generation / Instruction Extensions&lt;br /&gt;
!Timer Frequency&lt;br /&gt;
!Preemption Model&lt;br /&gt;
!Bitness&lt;br /&gt;
|-&lt;br /&gt;
|s390x&lt;br /&gt;
|IBM zEnterprise 114 and 196&lt;br /&gt;
|IBM zBC12 and zEC12 (&amp;lt;code&amp;gt;-march=zEC12 -mtune=zEC12&amp;lt;/code&amp;gt;)&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc64le&lt;br /&gt;
|Server processors&lt;br /&gt;
|POWER8 (&amp;lt;code&amp;gt;-mcpu=power8&amp;lt;/code&amp;gt;), AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;), VSX&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|ppc&lt;br /&gt;
|&lt;br /&gt;
512x/52xx/6xx/7xx/74xx/82xx/83xx/86xx&lt;br /&gt;
* Apple PowerMac based machines&lt;br /&gt;
|AltiVec (&amp;lt;code&amp;gt;-Wa,-maltivec&amp;lt;/code&amp;gt; to assembler or &amp;lt;code&amp;gt;-maltivec -mabi=altivec&amp;lt;/code&amp;gt;) on &amp;gt;=74xx&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|No Forced Preemption (Server)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|x86_64&lt;br /&gt;
|Generic-x86-64&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|x86&lt;br /&gt;
|586/K5/5x86/6x86/6x86MX&lt;br /&gt;
|(-mtune=generic ; SIMD assembly modules enabled based on simple compile test and/or presence of CPU flag)&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|armv7&lt;br /&gt;
|&lt;br /&gt;
* ARMv7 based platforms (Cortex-A, PJ4, Scorpion, Krait)&lt;br /&gt;
* Freescale i.MX family -- Cortex A (i.MX51, i.MX53, i.MX6 Quad/DualLite, i.MX6 SoloLite, i.MX6 SoloX, i.MX6 UltraLite, i.MX7 Dual)&lt;br /&gt;
* Qualcomm -- (MSM8X60, MSM8960, MSM8974)&lt;br /&gt;
* Allwinner SoCs -- (A10 (sun4i), A10s / A13 (sun5i), A31 (sun6i), A20 (sun7i), sun8i Family, (sun9i))&lt;br /&gt;
* ARM Ldt Versatile Express family -- &lt;br /&gt;
|Either &amp;lt;code&amp;gt;-march=armv7-a&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-march=armv5t -Wa,-march=armv7-a&amp;lt;/code&amp;gt; based on a compile test. &amp;lt;code&amp;gt;-mfpu=vfp&amp;lt;/code&amp;gt;&lt;br /&gt;
|1000 Hz&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|32&lt;br /&gt;
|-&lt;br /&gt;
|aarch64&lt;br /&gt;
|&lt;br /&gt;
* Allwinner sunxi 64-bit SoC Family&lt;br /&gt;
* Broadcom BCM2835 family&lt;br /&gt;
* Marvell Berlin SoC Family&lt;br /&gt;
* ARMv8 based Samsung Exynos SoC family&lt;br /&gt;
* ARMv8 based Freescale Layerscape SoC family&lt;br /&gt;
* Hisilicon SoC Family&lt;br /&gt;
* Mediatek MT65xx &amp;amp; MT81xx ARMv8 SoC&lt;br /&gt;
* Marvell EBU SoC Family&lt;br /&gt;
* Qualcomm Platforms&lt;br /&gt;
* Rockchip Platforms&lt;br /&gt;
* AMD Seattle SoC Family&lt;br /&gt;
* Altera&#039;s Stratix 10 SoCFPGA Family&lt;br /&gt;
* NVIDIA Tegra SoC Family&lt;br /&gt;
* Spreadtrum SoC platform&lt;br /&gt;
* Cavium Inc. Thunder SoC Family&lt;br /&gt;
* ARMv8 software model (Versatile Express)&lt;br /&gt;
* AppliedMicro X-Gene SOC Family&lt;br /&gt;
* Xilinx ZynqMP Family&lt;br /&gt;
|&lt;br /&gt;
|1000 HZ&lt;br /&gt;
|Voluntary Kernel Preemption (Desktop)&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
If you do desktop multitasking, you may want to switch to Voluntary Kernel Preemption (Desktop) or Preemptible Kernel (Low-Latency Desktop) and up the Timer Frequency.  If you run a dedicated render farm node or a dedicated bitcoin miner use No Forced Preemption (Server) and decrease the Timer Frequency.&lt;br /&gt;
&lt;br /&gt;
Optimized modules (most are already compiled as modules):&lt;br /&gt;
* raid6 -- altivec, avx512, ssse3, avx2, mmx, sse, sse2, neon&lt;br /&gt;
* some operations of raid5 -- mmx (32 bit), sse (64 bit), avx&lt;br /&gt;
For Kernel API:&lt;br /&gt;
* 32-bit memcpy -- 3dnow&lt;br /&gt;
* 32-bit memory page clearing and copying -- sse (Athlon/K7 only), mmx&lt;br /&gt;
From x86/crypto, arm/crypto, powerpc/crypto:&lt;br /&gt;
* CAMELLIA -- avx2, avx, aes-ni&lt;br /&gt;
* CHACHA20 -- avx2, neon&lt;br /&gt;
* CAST5 -- avx&lt;br /&gt;
* CAST6 -- avx&lt;br /&gt;
* TWOFISH -- avx&lt;br /&gt;
* SERPENT -- avx2, avx, sse2&lt;br /&gt;
* SHA1 -- avx2, ssse3, neon, spe&lt;br /&gt;
* SHA2 -- avx2&lt;br /&gt;
* SHA256 -- ssse3, neon, spe&lt;br /&gt;
* SHA512 -- avx2, ssse3, neon&lt;br /&gt;
* POLY1305 -- avx2&lt;br /&gt;
* GHASH -- pclmulqdq (part of aes-ni), vmx (power8)&lt;br /&gt;
* AES -- aes-ni, neon, vmx (power8), spe&lt;br /&gt;
* CRC32 -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
* CRCT10DIF -- pclmulqdq, sse, neon, vmx (power8)&lt;br /&gt;
&lt;br /&gt;
=== Fast reboots with kexec ===&lt;br /&gt;
{{main|kexec}}&lt;br /&gt;
&lt;br /&gt;
If you want to reboot the kernel fast avoiding the POST test, you need {{ic|doas apk add {{pkg|kexec-tools}}}} and enable kexec in the kernel:&lt;br /&gt;
&lt;br /&gt;
  Processor type and features&lt;br /&gt;
    [*] kexec system call&lt;br /&gt;
&lt;br /&gt;
=== Hibernation to prevent data loss ===&lt;br /&gt;
&lt;br /&gt;
  Power management and ACPI options&lt;br /&gt;
    [*] Hibernation (aka &#039;suspend to disk&#039;)&lt;br /&gt;
&lt;br /&gt;
Hibernation should be used if you have a laptop.  You don&#039;t want the laptop to suddenly shut off resulting in data loss, you want it to save your work based on a percentage of battery life (this requires special script).  When hibernation resumes, should lock and ask for credentials.  Depending on your needs, the hibernated image can be encrypted/decrypted which again requires additional customization to scripts.&lt;br /&gt;
&lt;br /&gt;
Hibernation with an unsanitized swap file is generally insecure because data and unlocked memory pages are swapped out in plaintext.  To increase the security either disable swap or use an encrypted swap.  The swap file/partition is typically used as the hibernation resume image.&lt;br /&gt;
&lt;br /&gt;
== Building ==&lt;br /&gt;
&lt;br /&gt;
Before building, you may want to remove as many modules as possible.  This will reduce the time to compile greatly.  Also, you may want to use [https://github.com/ccache/ccache/ ccache] for faster recompiles especially if you are searching for the minimal set of options or modules to use or include.&lt;br /&gt;
&lt;br /&gt;
You should then do an &amp;lt;code&amp;gt;abuild -r&amp;lt;/code&amp;gt; to attempt to build it.&lt;br /&gt;
&lt;br /&gt;
== Installing ==&lt;br /&gt;
&lt;br /&gt;
To install it you do a {{ic|doas apk add linux-NAME}} where NAME is your custom kernel name.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
Before you test, you should install the lts kernel too, using &amp;lt;code&amp;gt;apk add {{pkg|linux-lts}}&amp;lt;/code&amp;gt;.  You may be missing a module and can&#039;t boot, so you use the other kernel as the fallback boot kernel.  Don&#039;t forget to update your bootloader configuration.&lt;br /&gt;
&lt;br /&gt;
To test, first you should make a bootable Alpine USB image.  Then, when you have your rescue USB done, you &amp;lt;code&amp;gt;reboot&amp;lt;/code&amp;gt; the computer.&lt;br /&gt;
&lt;br /&gt;
To test it, you basically do trial and error.  Sometimes your config is missing something if you want to have a bare minimum setting.&lt;br /&gt;
&lt;br /&gt;
If you are curious about correctness testing, some kernel modules or components do preform self tests at the beginning of the boot process.  The tools may have test suites that you run with the make command.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [https://wiki.archlinux.org/title/Kernel Archwiki Kernels]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel Gentoo Wiki Kernel]&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Kernel/Configuration Gentoo Wiki Kernel Configuration]&lt;br /&gt;
* [[How to build the Alpine Linux kernel]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Kernel]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31262</id>
		<title>Hyprland</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31262"/>
		<updated>2025-10-21T20:57:29Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Update info about the setup&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page is about [https://hyprland.org Hyprland], a [[wayland]] based tiling compositor with all the eyecandy, powerful plugins and much more. &lt;br /&gt;
&lt;br /&gt;
Refer to [https://wiki.hyprland.org/Getting-Started/Master-Tutorial/ hyprland Tutorial] to get started on using Hyprland.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
{{:Include:Desktop prerequisites}}&lt;br /&gt;
* Install [[Alpine_setup_scripts#setup-wayland-base|wayland-base]].This enables [[elogind]] as [[Seat manager|seat manager]], enables [[Repositories#Community|community repository]] and enables [[eudev]].&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
To run the {{Pkg|hyprland|arch=}} nicely, it is likely that you will need at least those packages: {{Cmd|# apk add hyprland hyprland-plugin-manager hyprland-protocols \&lt;br /&gt;
hyprland-wallpapers wayland xwayland elogind elogind-openrc \&lt;br /&gt;
polkit-elogind polkit-openrc openrc-user-pam eudev \&lt;br /&gt;
eudev-openrc dbus dbus-openrc dbus-daemon-launch-helper pipewire \&lt;br /&gt;
pipewire-openrc pipewire-pulse pipewire-pulse-openrc \&lt;br /&gt;
wireplumber wireplumber-openrc&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Optionally {{Cmd|# apk add waybar hyprland-wallpapers \&lt;br /&gt;
hyprutils hyprcursor wlroots grim slurp \&lt;br /&gt;
wofi kitty dolphin&lt;br /&gt;
}}&lt;br /&gt;
Obviously there are other choices: `rofi`, `alacritty`, etc...&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
As a bare minimum: {{Cmd|#setup-wayland-base}}&lt;br /&gt;
&lt;br /&gt;
{{Pkg|openrc|arch=}} comes with &amp;quot;user services&amp;quot; so we can use it&lt;br /&gt;
to start required services when you login.&lt;br /&gt;
&lt;br /&gt;
1) add user to &#039;&#039;seat&#039;&#039; group - required for elogind&lt;br /&gt;
&lt;br /&gt;
2) add user to &#039;&#039;audio&#039;&#039;, &#039;&#039;video&#039;&#039;, &#039;&#039;pipewire&#039;&#039;, &#039;&#039;input&#039;&#039; groups&lt;br /&gt;
&lt;br /&gt;
3) create a symbolic link for user service {{Cmd|# /etc/init.d/user.$YOUR_USERNAME_HERE -&amp;gt; /etc/init.d/user}}&lt;br /&gt;
&lt;br /&gt;
4) enable linger for your user&lt;br /&gt;
{{Cmd|# loginctl enable-linger $YOUR_USERNAME_HERE}}&lt;br /&gt;
&lt;br /&gt;
5) Add required services to your user services&lt;br /&gt;
{{Cmd|$ ls -la /etc/user/init.d/}}&lt;br /&gt;
{{Cmd|$ rc-status --user}}&lt;br /&gt;
{{Cmd|$ for service in dbus wireplumber pipewire pipewire-pulse; do rc-update --user add &amp;quot;$service&amp;quot; &amp;amp;&amp;amp; rc-service --user &amp;quot;$service&amp;quot; start; done}}&lt;br /&gt;
&lt;br /&gt;
== Configuration ==&lt;br /&gt;
&lt;br /&gt;
The default config is in {{Cmd|/usr/share/hypr/hyprland.conf}}&lt;br /&gt;
and based on that you may want to customize yours {{Cmd|~/.config/hypr/hyprland.conf}}&lt;br /&gt;
&lt;br /&gt;
=== consistent icon theme ===&lt;br /&gt;
&lt;br /&gt;
Depending on what applications you are running, you only need the HYPRCURSOR_THEME environment, it should work for all modern applications. But since you are reading this, you probably found an incompatible application. You have the option to disable hyprcursors, then you need only one theme. Otherwise you need to download a theme that is available as hyprcursors and xcursors (e.g. rose-pine-hyprcursor).&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;# Set the theme for most applications&lt;br /&gt;
env = HYPRCURSOR_THEME,$your_hyprcursor_theme&lt;br /&gt;
env = HYPRCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some Qt applications&lt;br /&gt;
env = XCURSOR_THEME,$your_xcursor_theme&lt;br /&gt;
env = XCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some GTK applications&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-theme $your_xcursor_theme&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-size 24&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
If you use gsettings make sure you have installed {{pkg|gsettings-desktop-schemas}} package.&lt;br /&gt;
&lt;br /&gt;
== Plugins ==&lt;br /&gt;
&lt;br /&gt;
=== hyprland-plugin-manager ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.22 on the &#039;&#039;hyprland-plugin-manager&#039;&#039; package is available. It installs the complete development-tree of hyprland, since it requires to compile hyprland. It is possible to uninstall &#039;&#039;hyprland-plugin-manager&#039;&#039; after the plugin is compiled.&lt;br /&gt;
&lt;br /&gt;
=== hyrpland-plugins ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.23 (not yet released) on or on &#039;&#039;edge&#039;&#039; it is possible to install the [https://github.com/hyprwm/hyprland-plugins official plugins] as a [https://pkgs.alpinelinux.org/package/edge/community/x86_64/hyprland-plugins package]. Checkout the subpackages to install individual plugins.&lt;br /&gt;
&lt;br /&gt;
=== loading plugins ===&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;plugin = /usr/lib/libhyprexpo.so&lt;br /&gt;
plugin {&lt;br /&gt;
    hyprexpo {&lt;br /&gt;
            columns = 3&lt;br /&gt;
            gap_size = 0&lt;br /&gt;
            workspace_method = first 1&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This is also useful if you have uninstalled &#039;&#039;hyprland-plugin-manager&#039;&#039; and you can&#039;t load the plugin via it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
=== Black/Magenta checkerboard background: Hyprland failed to load 1 essential asset ===&lt;br /&gt;
&lt;br /&gt;
As of Hyprland 0.45.0, upstream shows a scary message (and an ugly graphic) if you don&#039;t have their wallpapers installed.&lt;br /&gt;
&lt;br /&gt;
The ugly graphic goes away when you&#039;ve launched a [https://wiki.hyprland.org/Useful-Utilities/Wallpapers/ wallpaper utility], but the scary message remains.&lt;br /&gt;
&lt;br /&gt;
The canonical way to solve this is to install {{pkg|hyprland-wallpapers}} package using the command:&lt;br /&gt;
&lt;br /&gt;
{{Cmd|# apk add hyprland-wallpapers}}&lt;br /&gt;
&lt;br /&gt;
Or you can just have some file at {{Path|/usr/share/hypr/wall0.png}} or {{Path|/usr/local/share/hypr/wall0.png}} and modify your config file to have a line as follows: {{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc {&lt;br /&gt;
        force_default_wallpaper = 0&lt;br /&gt;
    }&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== Hyprland crashes almost immediately ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 has a bug where the default configuration will crash if Xwayland is not installed. You can either install {{pkg|xwayland}}, or  disable in your config:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;xwayland:enabled = false&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This should be fixed in the next update.&lt;br /&gt;
&lt;br /&gt;
=== warning message about hyprland-qtutils ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 and newer suggest you install hyprland-qtutils, which is currently not available in alpine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Your system does not have hyprland-qtutils installed. This is a runtime dependency for some dialogs. Consider installing it.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can disable the check in your config like so:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc:disable_hyprland_qtutils_check = true&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== DBUS_SESSION_BUS_ADDRESS unset ===&lt;br /&gt;
&lt;br /&gt;
If you&#039;re using login manager, create a copy of {{ic|/usr/share/wayland-sessions/hyprland.desktop}} that starts Hyprland within D-Bus session:&lt;br /&gt;
&lt;br /&gt;
 Exec=dbus-run-session -- Hyprland&lt;br /&gt;
&lt;br /&gt;
Also make sure that D-Bus is started as a system service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
 &lt;br /&gt;
* [https://wiki.hyprland.org Official Hyprland wiki]&lt;br /&gt;
&lt;br /&gt;
[[Category:compositor]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31261</id>
		<title>Hyprland</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31261"/>
		<updated>2025-10-21T20:30:37Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: update info about configuration file&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page is about [https://hyprland.org Hyprland], a [[wayland]] based tiling compositor with all the eyecandy, powerful plugins and much more. &lt;br /&gt;
&lt;br /&gt;
Refer to [https://wiki.hyprland.org/Getting-Started/Master-Tutorial/ hyprland Tutorial] to get started on using Hyprland.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
{{:Include:Desktop prerequisites}}&lt;br /&gt;
* Install [[Alpine_setup_scripts#setup-wayland-base|wayland-base]].This enables [[elogind]] as [[Seat manager|seat manager]], enables [[Repositories#Community|community repository]] and enables [[eudev]].&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
To run the {{Pkg|hyprland|arch=}} nicely, it is likely that you will need at least those packages: {{Cmd|# apk add hyprland hyprland-plugin-manager hyprland-protocols \&lt;br /&gt;
hyprland-wallpapers wayland xwayland elogind elogind-openrc \&lt;br /&gt;
polkit-elogind polkit-openrc openrc-user-pam eudev \&lt;br /&gt;
eudev-openrc dbus dbus-openrc dbus-daemon-launch-helper pipewire \&lt;br /&gt;
pipewire-openrc pipewire-pulse pipewire-pulse-openrc \&lt;br /&gt;
wireplumber wireplumber-openrc&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Optionally {{Cmd|# apk add waybar hyprland-wallpapers \&lt;br /&gt;
hyprutils hyprcursor wlroots grim slurp \&lt;br /&gt;
wofi kitty dolphin&lt;br /&gt;
}}&lt;br /&gt;
Obviously there are other choices: `rofi`, `alacritty`, etc...&lt;br /&gt;
&lt;br /&gt;
== Configuration ==&lt;br /&gt;
&lt;br /&gt;
=== consistent icon theme ===&lt;br /&gt;
&lt;br /&gt;
The default config is in &#039;&#039;/usr/share/hypr/hyprland.conf&#039;&#039;&lt;br /&gt;
and based on that you may want to customize yours &#039;&#039;~/.config/hypr/hyprland.conf&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Depending on what applications you are running, you only need the HYPRCURSOR_THEME environment, it should work for all modern applications. But since you are reading this, you probably found an incompatible application. You have the option to disable hyprcursors, then you need only one theme. Otherwise you need to download a theme that is available as hyprcursors and xcursors (e.g. rose-pine-hyprcursor).&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;# Set the theme for most applications&lt;br /&gt;
env = HYPRCURSOR_THEME,$your_hyprcursor_theme&lt;br /&gt;
env = HYPRCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some Qt applications&lt;br /&gt;
env = XCURSOR_THEME,$your_xcursor_theme&lt;br /&gt;
env = XCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some GTK applications&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-theme $your_xcursor_theme&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-size 24&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
If you use gsettings make sure you have installed {{pkg|gsettings-desktop-schemas}} package.&lt;br /&gt;
&lt;br /&gt;
== Plugins ==&lt;br /&gt;
&lt;br /&gt;
=== hyprland-plugin-manager ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.22 on the &#039;&#039;hyprland-plugin-manager&#039;&#039; package is available. It installs the complete development-tree of hyprland, since it requires to compile hyprland. It is possible to uninstall &#039;&#039;hyprland-plugin-manager&#039;&#039; after the plugin is compiled.&lt;br /&gt;
&lt;br /&gt;
=== hyrpland-plugins ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.23 (not yet released) on or on &#039;&#039;edge&#039;&#039; it is possible to install the [https://github.com/hyprwm/hyprland-plugins official plugins] as a [https://pkgs.alpinelinux.org/package/edge/community/x86_64/hyprland-plugins package]. Checkout the subpackages to install individual plugins.&lt;br /&gt;
&lt;br /&gt;
=== loading plugins ===&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;plugin = /usr/lib/libhyprexpo.so&lt;br /&gt;
plugin {&lt;br /&gt;
    hyprexpo {&lt;br /&gt;
            columns = 3&lt;br /&gt;
            gap_size = 0&lt;br /&gt;
            workspace_method = first 1&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This is also useful if you have uninstalled &#039;&#039;hyprland-plugin-manager&#039;&#039; and you can&#039;t load the plugin via it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
=== Black/Magenta checkerboard background: Hyprland failed to load 1 essential asset ===&lt;br /&gt;
&lt;br /&gt;
As of Hyprland 0.45.0, upstream shows a scary message (and an ugly graphic) if you don&#039;t have their wallpapers installed.&lt;br /&gt;
&lt;br /&gt;
The ugly graphic goes away when you&#039;ve launched a [https://wiki.hyprland.org/Useful-Utilities/Wallpapers/ wallpaper utility], but the scary message remains.&lt;br /&gt;
&lt;br /&gt;
The canonical way to solve this is to install {{pkg|hyprland-wallpapers}} package using the command:&lt;br /&gt;
&lt;br /&gt;
{{Cmd|# apk add hyprland-wallpapers}}&lt;br /&gt;
&lt;br /&gt;
Or you can just have some file at {{Path|/usr/share/hypr/wall0.png}} or {{Path|/usr/local/share/hypr/wall0.png}} and modify your config file to have a line as follows: {{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc {&lt;br /&gt;
        force_default_wallpaper = 0&lt;br /&gt;
    }&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== Hyprland crashes almost immediately ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 has a bug where the default configuration will crash if Xwayland is not installed. You can either install {{pkg|xwayland}}, or  disable in your config:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;xwayland:enabled = false&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This should be fixed in the next update.&lt;br /&gt;
&lt;br /&gt;
=== warning message about hyprland-qtutils ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 and newer suggest you install hyprland-qtutils, which is currently not available in alpine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Your system does not have hyprland-qtutils installed. This is a runtime dependency for some dialogs. Consider installing it.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can disable the check in your config like so:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc:disable_hyprland_qtutils_check = true&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== DBUS_SESSION_BUS_ADDRESS unset ===&lt;br /&gt;
&lt;br /&gt;
If you&#039;re using login manager, create a copy of {{ic|/usr/share/wayland-sessions/hyprland.desktop}} that starts Hyprland within D-Bus session:&lt;br /&gt;
&lt;br /&gt;
 Exec=dbus-run-session -- Hyprland&lt;br /&gt;
&lt;br /&gt;
Also make sure that D-Bus is started as a system service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
 &lt;br /&gt;
* [https://wiki.hyprland.org Official Hyprland wiki]&lt;br /&gt;
&lt;br /&gt;
[[Category:compositor]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31260</id>
		<title>Hyprland</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Hyprland&amp;diff=31260"/>
		<updated>2025-10-21T20:22:58Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Update installation with most recent packages on alpine 3.22&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page is about [https://hyprland.org Hyprland], a [[wayland]] based tiling compositor with all the eyecandy, powerful plugins and much more. &lt;br /&gt;
&lt;br /&gt;
Refer to [https://wiki.hyprland.org/Getting-Started/Master-Tutorial/ hyprland Tutorial] to get started on using Hyprland.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
{{:Include:Desktop prerequisites}}&lt;br /&gt;
* Install [[Alpine_setup_scripts#setup-wayland-base|wayland-base]].This enables [[elogind]] as [[Seat manager|seat manager]], enables [[Repositories#Community|community repository]] and enables [[eudev]].&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
To run the {{Pkg|hyprland|arch=}} nicely, it is likely that you will need at least those packages: {{Cmd|# apk add hyprland hyprland-plugin-manager hyprland-protocols \&lt;br /&gt;
hyprland-wallpapers wayland xwayland elogind elogind-openrc \&lt;br /&gt;
polkit-elogind polkit-openrc openrc-user-pam eudev \&lt;br /&gt;
eudev-openrc dbus dbus-openrc dbus-daemon-launch-helper pipewire \&lt;br /&gt;
pipewire-openrc pipewire-pulse pipewire-pulse-openrc \&lt;br /&gt;
wireplumber wireplumber-openrc&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Optionally {{Cmd|# apk add waybar hyprland-wallpapers \&lt;br /&gt;
hyprutils hyprcursor wlroots grim slurp \&lt;br /&gt;
wofi kitty dolphin&lt;br /&gt;
}}&lt;br /&gt;
Obviously there are other choices: `rofi`, `alacritty`, etc...&lt;br /&gt;
&lt;br /&gt;
== Configuration ==&lt;br /&gt;
&lt;br /&gt;
=== consistent icon theme ===&lt;br /&gt;
&lt;br /&gt;
Depending on what applications you are running, you only need the HYPRCURSOR_THEME environment, it should work for all modern applications. But since you are reading this, you probably found an incompatible application. You have the option to disable hyprcursors, then you need only one theme. Otherwise you need to download a theme that is available as hyprcursors and xcursors (e.g. rose-pine-hyprcursor).&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;# Set the theme for most applications&lt;br /&gt;
env = HYPRCURSOR_THEME,$your_hyprcursor_theme&lt;br /&gt;
env = HYPRCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some Qt applications&lt;br /&gt;
env = XCURSOR_THEME,$your_xcursor_theme&lt;br /&gt;
env = XCURSOR_SIZE,24&lt;br /&gt;
# Set the theme for some GTK applications&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-theme $your_xcursor_theme&lt;br /&gt;
exec-once = gsettings set org.gnome.desktop.interface cursor-size 24&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
If you use gsettings make sure you have installed {{pkg|gsettings-desktop-schemas}} package.&lt;br /&gt;
&lt;br /&gt;
== Plugins ==&lt;br /&gt;
&lt;br /&gt;
=== hyprland-plugin-manager ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.22 on the &#039;&#039;hyprland-plugin-manager&#039;&#039; package is available. It installs the complete development-tree of hyprland, since it requires to compile hyprland. It is possible to uninstall &#039;&#039;hyprland-plugin-manager&#039;&#039; after the plugin is compiled.&lt;br /&gt;
&lt;br /&gt;
=== hyrpland-plugins ===&lt;br /&gt;
&lt;br /&gt;
From Alpine v3.23 (not yet released) on or on &#039;&#039;edge&#039;&#039; it is possible to install the [https://github.com/hyprwm/hyprland-plugins official plugins] as a [https://pkgs.alpinelinux.org/package/edge/community/x86_64/hyprland-plugins package]. Checkout the subpackages to install individual plugins.&lt;br /&gt;
&lt;br /&gt;
=== loading plugins ===&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;plugin = /usr/lib/libhyprexpo.so&lt;br /&gt;
plugin {&lt;br /&gt;
    hyprexpo {&lt;br /&gt;
            columns = 3&lt;br /&gt;
            gap_size = 0&lt;br /&gt;
            workspace_method = first 1&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This is also useful if you have uninstalled &#039;&#039;hyprland-plugin-manager&#039;&#039; and you can&#039;t load the plugin via it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
=== Black/Magenta checkerboard background: Hyprland failed to load 1 essential asset ===&lt;br /&gt;
&lt;br /&gt;
As of Hyprland 0.45.0, upstream shows a scary message (and an ugly graphic) if you don&#039;t have their wallpapers installed.&lt;br /&gt;
&lt;br /&gt;
The ugly graphic goes away when you&#039;ve launched a [https://wiki.hyprland.org/Useful-Utilities/Wallpapers/ wallpaper utility], but the scary message remains.&lt;br /&gt;
&lt;br /&gt;
The canonical way to solve this is to install {{pkg|hyprland-wallpapers}} package using the command:&lt;br /&gt;
&lt;br /&gt;
{{Cmd|# apk add hyprland-wallpapers}}&lt;br /&gt;
&lt;br /&gt;
Or you can just have some file at {{Path|/usr/share/hypr/wall0.png}} or {{Path|/usr/local/share/hypr/wall0.png}} and modify your config file to have a line as follows: {{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc {&lt;br /&gt;
        force_default_wallpaper = 0&lt;br /&gt;
    }&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== Hyprland crashes almost immediately ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 has a bug where the default configuration will crash if Xwayland is not installed. You can either install {{pkg|xwayland}}, or  disable in your config:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;xwayland:enabled = false&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
This should be fixed in the next update.&lt;br /&gt;
&lt;br /&gt;
=== warning message about hyprland-qtutils ===&lt;br /&gt;
&lt;br /&gt;
Hyprland 0.46 and newer suggest you install hyprland-qtutils, which is currently not available in alpine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Your system does not have hyprland-qtutils installed. This is a runtime dependency for some dialogs. Consider installing it.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can disable the check in your config like so:&lt;br /&gt;
&lt;br /&gt;
{{Cat|~/.config/hypr/hyprland.conf|&amp;lt;nowiki&amp;gt;misc:disable_hyprland_qtutils_check = true&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== DBUS_SESSION_BUS_ADDRESS unset ===&lt;br /&gt;
&lt;br /&gt;
If you&#039;re using login manager, create a copy of {{ic|/usr/share/wayland-sessions/hyprland.desktop}} that starts Hyprland within D-Bus session:&lt;br /&gt;
&lt;br /&gt;
 Exec=dbus-run-session -- Hyprland&lt;br /&gt;
&lt;br /&gt;
Also make sure that D-Bus is started as a system service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
 &lt;br /&gt;
* [https://wiki.hyprland.org Official Hyprland wiki]&lt;br /&gt;
&lt;br /&gt;
[[Category:compositor]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18579</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18579"/>
		<updated>2021-01-15T21:51:20Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Add note about volumes within main encrypted partition&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you must add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot. You may also need: &amp;lt;code&amp;gt;usb&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;lvm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID of sda2&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
&lt;br /&gt;
We can also double check if &amp;lt;code&amp;gt;modules&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; are set correctly, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modules=sd-mod,usb-storage,ext4,cryptsetup,keymap,cryptkey,kms,lvm&lt;br /&gt;
root=UUID=&amp;lt;UUID of /dev/mapper/vg0-root&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: Because we didn&#039;t mount &amp;lt;code&amp;gt;/dev&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;/proc&amp;lt;/code&amp;gt; inside our &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; chroot, some errors may occur when we run &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command. But you can most likely ignore these.&lt;br /&gt;
&lt;br /&gt;
Write the MBR (without partition table) to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
 * GPT partition table on a motherboard that runs BIOS instead of UEFI&lt;br /&gt;
 * running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings&lt;br /&gt;
&lt;br /&gt;
== I see &amp;quot;can not mount /sysroot&amp;quot; during boot ==&lt;br /&gt;
&lt;br /&gt;
 * incorrect device UUID&lt;br /&gt;
 * missing module in &amp;lt;code&amp;gt;/mnt/etc/update-extlinux.conf&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/mnt/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required.&lt;br /&gt;
{{Note|This does not apply for volumes&lt;br /&gt;
within your main encrypted partition &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;}}&lt;br /&gt;
For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18578</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18578"/>
		<updated>2021-01-15T21:39:22Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: add info about can not mount /sysroot&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you must add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot. You may also need: &amp;lt;code&amp;gt;usb&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;lvm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID of sda2&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
&lt;br /&gt;
We can also double check if &amp;lt;code&amp;gt;modules&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; are set correctly, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modules=sd-mod,usb-storage,ext4,cryptsetup,keymap,cryptkey,kms,lvm&lt;br /&gt;
root=UUID=&amp;lt;UUID of /dev/mapper/vg0-root&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: Because we didn&#039;t mount &amp;lt;code&amp;gt;/dev&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;/proc&amp;lt;/code&amp;gt; inside our &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; chroot, some errors may occur when we run &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command. But you can most likely ignore these.&lt;br /&gt;
&lt;br /&gt;
Write the MBR (without partition table) to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
 * GPT partition table on a motherboard that runs BIOS instead of UEFI&lt;br /&gt;
 * running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings&lt;br /&gt;
&lt;br /&gt;
== I see &amp;quot;can not mount /sysroot&amp;quot; during boot ==&lt;br /&gt;
&lt;br /&gt;
 * incorrect device UUID&lt;br /&gt;
 * missing module in &amp;lt;code&amp;gt;/mnt/etc/update-extlinux.conf&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/mnt/etc/mkinitfs/mkinitfs.conf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18577</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18577"/>
		<updated>2021-01-15T21:26:09Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Update info about cryptroot and UUID in GPT with GRUB&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you must add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot. You may also need: &amp;lt;code&amp;gt;usb&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;lvm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID of sda2&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
&lt;br /&gt;
We can also double check if &amp;lt;code&amp;gt;modules&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; are set correctly, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modules=sd-mod,usb-storage,ext4,cryptsetup,keymap,cryptkey,kms,lvm&lt;br /&gt;
root=UUID=&amp;lt;UUID of /dev/mapper/vg0-root&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: Because we didn&#039;t mount &amp;lt;code&amp;gt;/dev&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;/proc&amp;lt;/code&amp;gt; inside our &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; chroot, some errors may occur when we run &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command. But you can most likely ignore these.&lt;br /&gt;
&lt;br /&gt;
Write the MBR (without partition table) to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
This can be because you are using a GPT partition table on a motherboard that runs BIOS instead of UEFI, or you are running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings.&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18576</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18576"/>
		<updated>2021-01-15T21:19:05Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Expand info about syslinux package and devs UUIDs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you must add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot. You may also need: &amp;lt;code&amp;gt;usb&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;lvm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID of sda2&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the ID of the device/partition that contains encrypted volumes, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter uses the name of the mapping we have already configured a few lines above.&lt;br /&gt;
&lt;br /&gt;
We can also double check if &amp;lt;code&amp;gt;modules&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; are set correctly, eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
modules=sd-mod,usb-storage,ext4,cryptsetup,keymap,cryptkey,kms,lvm&lt;br /&gt;
root=UUID=&amp;lt;UUID of /dev/mapper/vg0-root&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: Because we didn&#039;t mount &amp;lt;code&amp;gt;/dev&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;/proc&amp;lt;/code&amp;gt; inside our &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; chroot, some errors may occur when we run &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command. But you can most likely ignore these.&lt;br /&gt;
&lt;br /&gt;
Write the MBR (without partition table) to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system. The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
This can be because you are using a GPT partition table on a motherboard that runs BIOS instead of UEFI, or you are running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings.&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18575</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18575"/>
		<updated>2021-01-15T20:58:19Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Expand note about modules list in mkinitfs.conf&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you must add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot. You may also need: &amp;lt;code&amp;gt;usb&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;lvm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter sets the name of the mapping previously set in &amp;lt;code&amp;gt;crypttab&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: If an error occurs in the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command you can most likely ignore it.&lt;br /&gt;
&lt;br /&gt;
Write the MBR to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system. The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
This can be because you are using a GPT partition table on a motherboard that runs BIOS instead of UEFI, or you are running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings.&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18574</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18574"/>
		<updated>2021-01-15T20:46:42Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: Add a small info about possible layouts.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
Depending on your motherboard and bios features and configuration&lt;br /&gt;
we can either use partition table in MBR (legacy BIOS)&lt;br /&gt;
or GUID Partition Table (GPT).&lt;br /&gt;
Here we will describe those two with example layouts.&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI is only possible with GPT).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you should also add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot.}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter sets the name of the mapping previously set in &amp;lt;code&amp;gt;crypttab&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: If an error occurs in the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command you can most likely ignore it.&lt;br /&gt;
&lt;br /&gt;
Write the MBR to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system. The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
This can be because you are using a GPT partition table on a motherboard that runs BIOS instead of UEFI, or you are running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings.&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18573</id>
		<title>LVM on LUKS</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=LVM_on_LUKS&amp;diff=18573"/>
		<updated>2021-01-15T20:27:03Z</updated>

		<summary type="html">&lt;p&gt;Pawciobiel: fix typo fro&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart from the bootloader&#039;s partition). We will have an LVM container installed inside an encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt (which is managed by the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; command) and its LUKS subsystem is used.&lt;br /&gt;
&lt;br /&gt;
Note that your &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; partition must be non-encrypted to work with Syslinux. When using GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection from [https://en.wikipedia.org/wiki/Evil_maid_attack Evil Maid attacks], but Syslinux doesn&#039;t support this.&lt;br /&gt;
&lt;br /&gt;
== Storage Device Name ==&lt;br /&gt;
&lt;br /&gt;
To find your storage device&#039;s name, you could either install &amp;lt;code&amp;gt;util-linux&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;apk add util-linux&amp;lt;/code&amp;gt;) and find your device using the &amp;lt;code&amp;gt;lspci&amp;lt;/code&amp;gt; command, or you could make an educated guess by using BusyBox&#039;s &amp;lt;code&amp;gt;blkid&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; commands, and running &amp;lt;code&amp;gt;ls /dev/sd*&amp;lt;/code&amp;gt; if you are installing to a USB, SATA or SCSI device, &amp;lt;code&amp;gt;ls /dev/fd*&amp;lt;/code&amp;gt; for floppy disks and &amp;lt;code&amp;gt;ls /dev/hd*&amp;lt;/code&amp;gt; for IDE (PATA) devices.&lt;br /&gt;
&lt;br /&gt;
The following documentation uses the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device as installation destination. If your environment uses a different device name for your storage device, use the corresponding device names in the examples.&lt;br /&gt;
&lt;br /&gt;
= Setting up Alpine Linux Using LVM on Top of a LUKS Partition =&lt;br /&gt;
&lt;br /&gt;
To install Alpine Linux in logical volumes running on top of a LUKS encrypted partition, you cannot use the [[Installation|official installation]] procedure. The installation requires several manual steps you must run in the Alpine Linux Live CD environment.&lt;br /&gt;
&lt;br /&gt;
== Preparing the Temporary Installation Environment ==&lt;br /&gt;
&lt;br /&gt;
Before you begin to install Alpine Linux, prepare the temporary environment:&lt;br /&gt;
&lt;br /&gt;
Boot the latest Alpine Linux Installation CD. At the login prompt, use the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; user without a password to log in. Now we will follow the [[Setup-alpine]] script and make our changes along the way.&lt;br /&gt;
&lt;br /&gt;
Run the scripts in this order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-keymap&lt;br /&gt;
# setup-hostname&lt;br /&gt;
# setup-interfaces&lt;br /&gt;
# rc-service networking start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are configuring static networking (you didn&#039;t configure any interfaces to use DHCP), run &amp;lt;code&amp;gt;setup-dns&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you are using Wi-Fi you may need to do run &amp;lt;code&amp;gt;rc-update add wpa_supplicant boot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# passwd&lt;br /&gt;
# setup-timezone&lt;br /&gt;
# rc-update add networking boot&lt;br /&gt;
# rc-update add urandom boot&lt;br /&gt;
# rc-update add acpid default&lt;br /&gt;
# rc-service acpid start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit your {{Path|/etc/hosts}} to look like this, replacing &amp;lt;hostname&amp;gt; with your hostname and &amp;lt;domain&amp;gt; with your TLD (if you don&#039;t have a TLD, use &#039;localdomain&#039;:&lt;br /&gt;
{{Tip|The default text editor in BusyBox is &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt; (pronounced &#039;&#039;vee-eye&#039;&#039;).}}&lt;br /&gt;
{{Cat|/etc/hosts|127.0.0.1       &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain&lt;br /&gt;
::1             &amp;lt;hostname&amp;gt; &amp;lt;hostname&amp;gt;.&amp;lt;domain&amp;gt; localhost localhost.localdomain}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Note|In order to setup GRUB with UEFI, you are required to use the edge branch with the main and community repository. The reason for this is that &amp;lt;code&amp;gt;efibootmgr&amp;lt;/code&amp;gt; is not available in the stable branch. If you do not want to switch completely over to edge you can do something called repository pinning. You will need to do this after the &amp;lt;code&amp;gt;setup-apkrepos&amp;lt;/code&amp;gt; step.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-apkrepos&lt;br /&gt;
# apk update&lt;br /&gt;
# setup-sshd&lt;br /&gt;
# setup-ntp&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will deviate from the install script.&lt;br /&gt;
&lt;br /&gt;
Install the following packages required to set up LVM and LUKS:&lt;br /&gt;
&lt;br /&gt;
{{Note|The &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt; partition editor is needed for advanced partitioning and GPT disklabels. BusyBox &amp;lt;code&amp;gt;fdisk&amp;lt;/code&amp;gt; is a very stripped-down version with minimal functionality}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add lvm2 cryptsetup e2fsprogs parted&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optionally, if you want to overwrite your storage with random data first, install &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt;, which is a random number generator based on hardware events and has a higher throughput than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add haveged&lt;br /&gt;
# rc-service haveged start&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Partition Layout ==&lt;br /&gt;
&lt;br /&gt;
=== BIOS/MBR with DOS disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be partitioning the storage device with a non-encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; partition for use with the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and the MSDOS MBR partition table. Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | Boot partition         | ext4                  |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 100MB partition to boot off, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel msdos&lt;br /&gt;
(parted) mkpart primary ext4 0% 100M&lt;br /&gt;
(parted) name 1 boot&lt;br /&gt;
(parted) set 1 boot on&lt;br /&gt;
(parted) mkpart primary ext4 100M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To view your partition table, type &amp;lt;code&amp;gt;print&amp;lt;/code&amp;gt; while still in &amp;lt;code&amp;gt;parted&amp;lt;/code&amp;gt;. Your results should look something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;(parted) print&lt;br /&gt;
Model: ATA TOSHIBA ******** (scsi)&lt;br /&gt;
Disk /dev/sda: 1000GB&lt;br /&gt;
Sector size (logical/physical): 512B/4096B&lt;br /&gt;
Partition Table: msdos&lt;br /&gt;
Disk Flags:&lt;br /&gt;
&lt;br /&gt;
Number  Start   End     Size    Type     File system  Flags&lt;br /&gt;
 1      1049kB  99.6MB  98.6MB  primary  ext4         boot&lt;br /&gt;
 2      99.6MB  1000GB  1000GB  primary  ext4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UEFI with GPT disklabel ===&lt;br /&gt;
&lt;br /&gt;
We will be encrypting the whole disk but the EFI system partition mounted at &amp;lt;code&amp;gt;/boot/efi&amp;lt;/code&amp;gt;. This means that GRUB2 will decrypt the LUKS volume and load the kernel from there, preventing someone with physical access to your computer from maliciously installing a rootkit (or bootkit) in your boot partition while your computer is not already unlocked. The partitioning scheme will look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| Partition name            | Partition purpose      | Filesystem type       |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&lt;br /&gt;
| /dev/sda1                 | EFI system partition   | fat32                 |&lt;br /&gt;
| /dev/sda2                 | LUKS container         | LUKS                  |&lt;br /&gt;
| |-&amp;gt; /dev/mapper/lvmcrypt  | LVM container          | LVM                   |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/root       | Root partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/boot       | Boot partition         | ext4                  |&lt;br /&gt;
|  |-&amp;gt; /dev/vg01/swap       | Swap partition         | swap                  |&lt;br /&gt;
+---------------------------+------------------------+-----------------------+&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Warning|This will delete your previous partitioning table and make your data very hard to recover. If you want to dual boot, stop here and ask an expert.}}&lt;br /&gt;
&lt;br /&gt;
Create an approx. 200MB EFI system partition, then assign the rest of the space to your LUKS partition.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# parted -a optimal&lt;br /&gt;
(parted) mklabel gpt&lt;br /&gt;
(parted) mkpart primary fat32 0% 200M&lt;br /&gt;
(parted) name 1 esp&lt;br /&gt;
(parted) set 1 esp on&lt;br /&gt;
(parted) mkpart primary ext4 200M 100%&lt;br /&gt;
(parted) name 2 crypto-luks&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optional: Overwrite LUKS Partition with Random Data ==&lt;br /&gt;
&lt;br /&gt;
This should be done if your hard drive wasn&#039;t encrypted previously. It helps purge old, non-encrypted data and makes it harder for an attacker to work out how much data you have on your drive if they have access to the encrypted contents.&lt;br /&gt;
&lt;br /&gt;
We will be using &amp;lt;code&amp;gt;haveged&amp;lt;/code&amp;gt; as it is considerably faster than &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; when generating pseudo-random numbers (it&#039;s almost as high as &amp;lt;code&amp;gt;/dev/zero&amp;lt;/code&amp;gt; in throughput), and is (supposedly) very close to truly random.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# haveged -n 0 | dd of=/dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Encrypting the LVM Physical Volume Partition == &lt;br /&gt;
&lt;br /&gt;
To encrypt the partition which will later contain the LVM PV, you could either use the default settings (aes-xts-plain64 cipher with 256-bit key and Argon2 hashing with iter-time 2000ms), or you could use these settings which have added security with the trade-off being a non-noticeable decrease in performance in modern computers:&lt;br /&gt;
&lt;br /&gt;
Default settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optimized for security:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup -v -c serpent-xts-plain64 -s 512 --hash whirlpool --iter-time 5000 --use-random luksFormat /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11 and GRUB2 with encrypted /boot, the following should be used instead (because GRUB2 does not yet support LUKS2 containers):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksFormat --type luks1 /dev/sda2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the Logical Volumes and File Systems ==&lt;br /&gt;
&lt;br /&gt;
Open the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2 lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the PV on &amp;lt;code&amp;gt;lvmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# pvcreate /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the &amp;lt;code&amp;gt;vg0&amp;lt;/code&amp;gt; LVM VG in the &amp;lt;code&amp;gt;/dev/mapper/lvmcrypt&amp;lt;/code&amp;gt; PV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# vgcreate vg0 /dev/mapper/lvmcrypt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for BIOS/MBR ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== LV Creation for UEFI/GPT ===&lt;br /&gt;
&lt;br /&gt;
This will create a 2GB swap partition, a 2GB boot partition and a root partition which takes up the rest of the space. This setup is for those who do not need to use the hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition slightly larger than the size of your RAM (change the size after &amp;lt;code&amp;gt;# lvcreate -L&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvcreate -L 2G vg0 -n swap&lt;br /&gt;
# lvcreate -L 2G vg0 -n boot&lt;br /&gt;
# lvcreate -l 100%FREE vg0 -n root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The LVs created in the previous steps are automatically marked active. To verify, enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# lvscan&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Mounting the File Systems ==&lt;br /&gt;
&lt;br /&gt;
Format the &amp;lt;code&amp;gt;root&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;boot&amp;lt;/code&amp;gt; LVs using the ext4 file system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/vg0/root&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Format the swap LV:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkswap /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the root LV to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t ext4 /dev/vg0/root /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next format your boot partition, create a mount point and mount it:&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using BIOS and MBR:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkfs.ext4 /dev/sda1&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/sda1 /mnt/boot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you&#039;re using UEFI and GPT:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add dosfstools&lt;br /&gt;
# mkfs.fat -F32 /dev/sda1&lt;br /&gt;
# mkfs.ext4 /dev/vg0/boot&lt;br /&gt;
# mkdir -v /mnt/boot&lt;br /&gt;
# mount -t ext4 /dev/vg0/boot /mnt/boot&lt;br /&gt;
# mkdir -v /mnt/boot/efi&lt;br /&gt;
# mount -t vfat /dev/sda1 /mnt/boot/efi&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, activate your swap partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# swapon /dev/vg0/swap&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing Alpine Linux ==&lt;br /&gt;
&lt;br /&gt;
In this step you will install Alpine Linux in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory, which contains the mounted file system structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# setup-disk -m sys /mnt/&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The installer downloads the latest packages to install the base installation. Additionally, the installer automatically creates the entries for the mount points in {{Path|/etc/fstab}} file, which is currently mounted in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
{{Note|The automatic writing of the master boot record (MBR) fails in this step. You will write the MBR later manually to the disk.}}&lt;br /&gt;
&lt;br /&gt;
The swap LV is not automatically added to the &amp;lt;code&amp;gt;fstab&amp;lt;/code&amp;gt; file. To add it manually, add the following line to the {{Path|/mnt/etc/fstab}} file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg0/swap    swap    swap    defaults    0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the {{Path|/mnt/etc/mkinitfs/mkinitfs.conf}} file and append the &amp;lt;code&amp;gt;cryptsetup&amp;lt;/code&amp;gt; module to the &amp;lt;code&amp;gt;features&amp;lt;/code&amp;gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;features=&amp;quot;... cryptsetup&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using GRUB with an encrypted &amp;lt;code&amp;gt;/boot&amp;lt;/code&amp;gt; you should also add the &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; feature so that Alpine can use a keyfile for decryption on boot.&lt;br /&gt;
&lt;br /&gt;
{{Note|Alpine Linux uses the &amp;lt;code&amp;gt;en-us&amp;lt;/code&amp;gt; keyboard mapping by default when prompting for the password to decrypt the partition at boot time. If you changed the keyboard mapping in the temporary environment and want to use it at the boot password prompt, be sure to also add the &amp;lt;code&amp;gt;keymap&amp;lt;/code&amp;gt; feature to the list above.}}&lt;br /&gt;
&lt;br /&gt;
{{Note|Check the output of &amp;lt;code&amp;gt;mkinitfs -L&amp;lt;/code&amp;gt; and add the features necessary for your system to boot. You may need to add &amp;lt;code&amp;gt;kms&amp;lt;/code&amp;gt; in order to see a password prompt at boot.}}&lt;br /&gt;
&lt;br /&gt;
Rebuild the initial RAM disk:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mkinitfs -c /mnt/etc/mkinitfs/mkinitfs.conf -b /mnt/ $(ls /mnt/lib/modules/)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command uses the settings from the &amp;lt;code&amp;gt;mkinitfs.conf&amp;lt;/code&amp;gt; file set in the &amp;lt;code&amp;gt;-c&amp;lt;/code&amp;gt; parameter to generate the RAM disk. The command is executed in the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and the RAM disk is generated using the modules for the installed kernel. Without setting the kernel version using the &amp;lt;code&amp;gt;$(ls /mnt/lib/modules/&amp;lt;/code&amp;gt;) option, &amp;lt;code&amp;gt;mkinitfs&amp;lt;/code&amp;gt; tries to generate the RAM disk using the kernel version installed in the temporary environment, which can differ from the latest one installed by the &amp;lt;code&amp;gt;setup-disk&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
== Installing a bootloader ==&lt;br /&gt;
&lt;br /&gt;
To get the UUID of your storage device into a file for later use, use this command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# blkid -s UUID -o value /dev/sda2 &amp;gt; ~/uuid&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Tip|To easily read the UUID into a file so you don&#039;t have to type it manually, open the file in &amp;lt;code&amp;gt;vi&amp;lt;/code&amp;gt;, then type &amp;lt;code&amp;gt;:r /root/uuid&amp;lt;/code&amp;gt; to load the UUID onto a new line.}}&lt;br /&gt;
&lt;br /&gt;
=== Syslinux with BIOS ===&lt;br /&gt;
&lt;br /&gt;
Install the Syslinux package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/mnt/etc/update-extlinux.conf}} and append the following kernel options to the &amp;lt;code&amp;gt;default_kernel_opts&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;default_kernel_opts=&amp;quot;... cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system, and the &amp;lt;code&amp;gt;cryptdm&amp;lt;/code&amp;gt; parameter sets the name of the mapping previously set in &amp;lt;code&amp;gt;crypttab&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; utility operates only on the &amp;lt;code&amp;gt;/boot/&amp;lt;/code&amp;gt; directory, temporarily change the root to the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; directory and update the boot loader configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt/&lt;br /&gt;
# update-extlinux&lt;br /&gt;
# exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: If an error occurs in the &amp;lt;code&amp;gt;update-extlinux&amp;lt;/code&amp;gt; command you can most likely ignore it.&lt;br /&gt;
&lt;br /&gt;
Write the MBR to the &amp;lt;code&amp;gt;/dev/sda&amp;lt;/code&amp;gt; device:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=440 count=1 conv=notrunc if=/mnt/usr/share/syslinux/mbr.bin of=/dev/sda&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Grub with UEFI ===&lt;br /&gt;
&lt;br /&gt;
To avoid having to type your decryption password twice every boot (once for GRUB and once for Alpine), add a keyfile to your LUKS partition. The filename is important.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This keyfile is stored encrypted at rest (it is in your LUKS partition), so it&#039;s existence does not reduce the security of the system.&lt;br /&gt;
&lt;br /&gt;
Mount the required filesystems for the Grub EFI installer to the installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# mount -t proc /proc /mnt/proc&lt;br /&gt;
# mount --rbind /dev /mnt/dev&lt;br /&gt;
# mount --make-rslave /mnt/dev&lt;br /&gt;
# mount --rbind /sys /mnt/sys&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then chroot in and use &amp;lt;code&amp;gt;grub-install&amp;lt;/code&amp;gt; to install Grub.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# chroot /mnt&lt;br /&gt;
# source /etc/profile&lt;br /&gt;
# export PS1=&amp;quot;(chroot) $PS1&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install &amp;lt;code&amp;gt;GRUB2&amp;lt;/code&amp;gt; for EFI and (optionally) remove syslinux:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# apk add grub grub-efi efibootmgr&lt;br /&gt;
# apk del syslinux&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit {{Path|/etc/default/grub}} and add the following kernel options to the &amp;lt;code&amp;gt;GRUB_CMDLINE_LINUX_DEFAULT&amp;lt;/code&amp;gt; parameter, replacing &amp;lt;UUID&amp;gt; with the UUID of the encrypted partition (in this case, &amp;lt;code&amp;gt;/dev/sda2&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;cryptroot=UUID=&amp;lt;UUID&amp;gt; cryptdm=lvmcrypt cryptkey&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cryptroot&amp;lt;/code&amp;gt; parameter sets the name of the device that contains the root file system. The &amp;lt;code&amp;gt;cryptkey&amp;lt;/code&amp;gt; parameter indicates the existence of the file &amp;lt;code&amp;gt;/crypto_keyfile.bin&amp;lt;/code&amp;gt; you created previously.&lt;br /&gt;
&lt;br /&gt;
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;GRUB_PRELOAD_MODULES=&amp;quot;luks cryptodisk part_gpt lvm&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using at least Alpine v3.11, &amp;lt;code&amp;gt;GRUB_ENABLE_CRYPTODISK=y&amp;lt;/code&amp;gt; should also be added to {{Path|/etc/default/grub}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# (chroot) grub-install --target=x86_64-efi --efi-directory=/boot/efi&lt;br /&gt;
# (chroot) grub-mkconfig -o /boot/grub/grub.cfg&lt;br /&gt;
# (chroot) exit&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Unmounting the Volumes and Partitions ==&lt;br /&gt;
&lt;br /&gt;
Unmount the &amp;lt;code&amp;gt;/mnt/&amp;lt;/code&amp;gt; partitions, deactivate the LVM volumes, close the LUKS partition and reboot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cd&lt;br /&gt;
# umount -l /mnt/dev&lt;br /&gt;
# umount -l /mnt/proc&lt;br /&gt;
# umount -l /mnt/sys&lt;br /&gt;
# umount /mnt/boot/efi&lt;br /&gt;
# umount /mnt/boot&lt;br /&gt;
# swapoff /dev/vg0/swap&lt;br /&gt;
# umount /mnt&lt;br /&gt;
# vgchange -a n&lt;br /&gt;
# cryptsetup luksClose lvmcrypt&lt;br /&gt;
# reboot&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Troubleshooting =&lt;br /&gt;
&lt;br /&gt;
== General Procedure ==&lt;br /&gt;
&lt;br /&gt;
In case your system fails to boot, you can verify the settings and fix incorrect configurations.&lt;br /&gt;
&lt;br /&gt;
Reboot and do the steps in [[#Preparing_the_Temporary_Installation_Environment|Prepare the temporary installation environment]] again.&lt;br /&gt;
&lt;br /&gt;
Setup the LUKS partition and activate the LVs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# cryptsetup luksOpen /dev/sda2&lt;br /&gt;
# vgchange -ay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#Creating_and_Mounting_the_File Systems|Mount the file systems]]&lt;br /&gt;
&lt;br /&gt;
Verify that you run the steps described in the [[#Installing_Alpine_Linux|Installing Alpine Linux]] section correctly. Update the configuration if necessary, unmount the partitions, then reboot.&lt;br /&gt;
&lt;br /&gt;
== System can&#039;t find boot device ==&lt;br /&gt;
&lt;br /&gt;
This can be because you are using a GPT partition table on a motherboard that runs BIOS instead of UEFI, or you are running an MSDOS/MBR/Syslinux install without enabling legacy boot mode in the UEFI settings.&lt;br /&gt;
&lt;br /&gt;
== Secure boot ==&lt;br /&gt;
&lt;br /&gt;
If secure boot complains of an unsigned bootloader, you can either disable it or adapt [https://wiki.archlinux.org/index.php/Secure_Boot this] guide to sign GRUB. If you&#039;re using Syslinux, then secure boot should be automatically disabled when you enable legacy boot mode.&lt;br /&gt;
&lt;br /&gt;
= Hardening =&lt;br /&gt;
&lt;br /&gt;
* To harden, you should disable DMA[https://old.iseclab.org/papers/acsac2012dma.pdf] and install a hardened version of AES (TRESOR[https://www1.informatik.uni-erlangen.de/tresor] or Loop-Amnesia[http://moongate.ydns.eu/amnesia.html]) since by default cryptsetup with luks uses AES by default.&lt;br /&gt;
* Disable DMA in the BIOS and set the password for the BIOS according to Wikipedia.[https://en.wikipedia.org/wiki/DMA_attack]&lt;br /&gt;
* Blacklist kernel modules that use DMA and any unused expansion modules (FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug modules) that use DMA.&lt;br /&gt;
&lt;br /&gt;
= Mounting additional encrypted filesystems at boot =&lt;br /&gt;
&lt;br /&gt;
If you would like other encrypted LUKS partitions to be decrypted and mounted automatically during boot, for example if you have &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; on a separate physical drive, some extra steps are required. For the purposes of these instructions we will say &amp;lt;code&amp;gt;/dev/sdb1&amp;lt;/code&amp;gt; contains an LVM volume that should be mounted at &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Create a keyfile and add it to the LUKS partition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# dd bs=512 count=4 if=/dev/urandom of=/root/crypt-home-keyfile.bin&lt;br /&gt;
# cryptsetup luksAddKey /dev/sdb1 /root/crypt-home-keyfile.bin&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alpine, like Gentoo, uses the &amp;lt;code&amp;gt;dmcrypt&amp;lt;/code&amp;gt; service rather than &amp;lt;code&amp;gt;/etc/crypttab&amp;lt;/code&amp;gt;. Add the following lines to &amp;lt;code&amp;gt;/etc/conf.d/dmcrypt&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;target=crypt-home&lt;br /&gt;
source=&#039;/dev/sdb1&#039;&lt;br /&gt;
key=&#039;/root/crypt-home-keyfile.bin&#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add an entry to &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, changing &amp;lt;code&amp;gt;vg1&amp;lt;/code&amp;gt; to the name of your LVM volume group:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/dev/vg1/home /home ext4 rw,relatime 0 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enable the dmcrypt and lvm services to start on boot:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;# rc-update add dmcrypt boot&lt;br /&gt;
# rc-update add lvm boot&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a reboot the partition should be decrypted and mounted automatically.&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
*[[Bootloaders]]&lt;br /&gt;
*[[Alpine setup scripts]]&lt;br /&gt;
*[[Installing on GPT LVM]]&lt;br /&gt;
*[[Setting up LVM on GPT-labeled disks]]&lt;br /&gt;
*[[Setting up disks manually]]&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Syslinux&lt;br /&gt;
*https://wiki.gentoo.org/wiki/GRUB2&lt;br /&gt;
*https://wiki.archlinux.org/index.php/Syslinux&lt;br /&gt;
*https://wiki.archlinux.org/index.php/GRUB&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Sakaki&#039;s_EFI_Install_Guide&lt;br /&gt;
*https://battlepenguin.com/tech/alpine-linux-with-full-disk-encryption/&lt;br /&gt;
*https://wiki.gentoo.org/wiki/Dm-crypt&lt;br /&gt;
&lt;br /&gt;
[[Category:Storage]]&lt;br /&gt;
[[Category:Security]]&lt;/div&gt;</summary>
		<author><name>Pawciobiel</name></author>
	</entry>
</feed>