<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>thermcampos blog » linux</title><link href="http://thermcampos.blog/" rel="alternate"/><link href="http://thermcampos.blog/atom/linux/" rel="self"/><id>/atom/linux/</id><updated>2026-09-10T12:59:27Z</updated><author><name>R. Campos</name></author><subtitle>Welcome to my blog, personal corner and stuff</subtitle><entry><title>minimal initramfs script</title><link href="http://thermcampos.blog/post/minimal-initramfs-script/" rel="alternate"/><published>2026-09-10T12:59:27Z</published><updated>2026-09-10T12:59:27Z</updated><author><name>R. Campos</name><email>ricardompcampos@gmail.com</email></author><id>/post/minimal-initramfs-script/</id><summary type="html">&lt;!-- tags: linux,kernel --&gt;
&lt;p&gt;After several years having Debian as my main Linux &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Linux_distribution"&gt;distro&lt;/a&gt;, I've decided to go
back to &lt;a class="reference external" href="https://www.gentoo.org/"&gt;Gentoo&lt;/a&gt;, the distro who caught my attention since day one. However, my
root partition is encrypted. While Debian has an out-of-the-box solution that
makes everyone's life easier and almost transparent, Gentoo goes the opposite
way. &lt;em&gt;You&lt;/em&gt; have to connect the dots and make it work &lt;em&gt;yourself&lt;/em&gt;. I had to stop,
plan for a moment, and get ready.&lt;/p&gt;
&lt;p&gt;In this short post, I'll be sharing the steps I took, the shell scripts,
and how I wired up a minimal init script, that I called &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Initial_ramdisk"&gt;initramfs&lt;/a&gt;, although
it's not quite that, this is much smaller and single purposed, decrypt my
root partition.&lt;/p&gt;
</summary><content type="html">&lt;!-- tags: linux,kernel --&gt;
&lt;p&gt;After several years having Debian as my main Linux &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Linux_distribution"&gt;distro&lt;/a&gt;, I've decided to go
back to &lt;a class="reference external" href="https://www.gentoo.org/"&gt;Gentoo&lt;/a&gt;, the distro who caught my attention since day one. However, my
root partition is encrypted. While Debian has an out-of-the-box solution that
makes everyone's life easier and almost transparent, Gentoo goes the opposite
way. &lt;em&gt;You&lt;/em&gt; have to connect the dots and make it work &lt;em&gt;yourself&lt;/em&gt;. I had to stop,
plan for a moment, and get ready.&lt;/p&gt;
&lt;p&gt;In this short post, I'll be sharing the steps I took, the shell scripts,
and how I wired up a minimal init script, that I called &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Initial_ramdisk"&gt;initramfs&lt;/a&gt;, although
it's not quite that, this is much smaller and single purposed, decrypt my
root partition.&lt;/p&gt;
&lt;!-- read_more --&gt;
&lt;p&gt;Long story short, I needed to find a way to decrypt, mount and boot my new OS
having the root partition encrypted. Using Gentoo's live-cd I was able to
try the script partially, and confirm the decryption works. All I needed was
having a busybox environment with all crypt libs on it. Using Gentoo itself
for that, I compiled some packages with &lt;strong&gt;static linked&lt;/strong&gt; dependencies.&lt;/p&gt;
&lt;p&gt;Then build the initial script that's going to do all the heavy-lifting.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;, here are the scripts:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Create the initramfs directory, embed busybox and the crypt libs and tools on it&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="brush: bash"&gt;
#!/bin/sh
set -e
STAGE="${1:-/usr/src/initramfs}"

mkdir -p "$STAGE"/{bin,sbin,lib,dev,proc,sys,mnt/root,run}

cp /sbin/busybox "$STAGE/bin/"
"$STAGE/bin/busybox" --install -s "$STAGE/bin"

cp /sbin/cryptsetup "$STAGE/sbin/"
cp /sbin/lvm "$STAGE/sbin/"

for bin in /sbin/cryptsetup /sbin/lvm; do
    ldd "$bin" | awk '{print $3}' | grep -v '^$'
done | sort -u | while read -r lib; do
    mkdir -p "$STAGE$(dirname "$lib")"
    cp -L "$lib" "$STAGE$lib"
done

cp init "$STAGE/init"
chmod +x "$STAGE/init"

echo "Staged tree ready at $STAGE — cd there and run the cpio|gzip packaging step."
&lt;/pre&gt;

&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;create the &lt;em&gt;init&lt;/em&gt; script (it needs to be called only init) at /usr/src/initramfs&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="brush: bash"&gt;
#!/bin/sh

rescue_shell() {
    echo "Something went wrong v3. Dropping to a shell."
    exec /bin/sh
}

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mkdir -p /run/cryptsetup
export DM_DISABLE_UDEV=1

ROOT=$(sed -n 's/.*\broot=\([^ ]*\).*/\1/p' /proc/cmdline)

cryptsetup luksOpen /dev/nvme0n1p3 gentoo_root || rescue_shell
lvm vgchange -ay || rescue_shell

mount -o ro "$ROOT" /mnt/root || rescue_shell

exec switch_root /mnt/root /sbin/init
&lt;/pre&gt;

&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Finally generate the image that goes to /boot and grub&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="brush: bash"&gt;
#!/bin/bash

KERNEL="7.2.4-gentoo"

cd /usr/src/initramfs

find . | cpio -o -H newc | gzip &gt; /boot/initramfs-$KERNEL.img

grub-mkconfig -o /boot/grub/grub.cfg
&lt;/pre&gt;

&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Done. With luck, you should have a minimal setup ready to decrypt and boot
your OS. Feel free to get in touch and ask for help, if needed. I have these
scripts versioned on my personal repo, find then here: &lt;a class="reference external" href="https://github.com/thermcampos/gentoo-config-files/tree/main/kernel"&gt;https://github.com/thermcampos/gentoo-config-files/tree/main/kernel&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That's all. Thanks for reading.&lt;/p&gt;
</content></entry></feed>