Last active
August 23, 2022 21:30
-
-
Save codefionn/6f545d283b0b7f3678932c1190fd6305 to your computer and use it in GitHub Desktop.
Custom initramfs generator on Gentoo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| echo "Generating initramfs source structure" | |
| cd /usr/src/initramfs && rm -r * | |
| mkdir -p {bin,dev/mapper,etc,lib,lib64,mnt/root,proc,root,sbin,sys,run} | |
| cp --archive /dev/{null,console,tty} dev | |
| # Required executables | |
| cp \ | |
| /bin/busybox \ | |
| bin/ | |
| cp \ | |
| /sbin/cryptsetup \ | |
| sbin/ | |
| # Required dynamically link libraries | |
| cp \ | |
| /lib64/ld-linux-x86-64.so.2 /lib64/libpam.so.0* /lib64/libpam_misc.so.0* /lib64/libc.so.6 \ | |
| /lib64/libuuid.so.1 \ | |
| /lib64/libblkid.so.1 \ | |
| /lib64/libdevmapper.so.1.02 \ | |
| /lib64/libudev.so.1 \ | |
| /lib64/libm.so.6 \ | |
| lib64/ | |
| cp \ | |
| /usr/lib64/libcryptsetup.so.12 \ | |
| /usr/lib64/libpopt.so.0 \ | |
| /usr/lib64/libssl.so.1.1 \ | |
| /usr/lib64/libcrypto.so.1.1 \ | |
| /usr/lib64/libargon2.so.1 \ | |
| /usr/lib64/libjson-c.so.5 \ | |
| lib64/ | |
| # Required implicitly | |
| cp \ | |
| /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/libgcc_s.so \ | |
| /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/libgcc_s.so.1 \ | |
| lib64/ | |
| echo "Generating init script" | |
| # Write the init script | |
| cat > init << EOF | |
| #!/bin/busybox sh | |
| rescue_shell() { | |
| echo "Something went wrong. Sadly there's nothing you can do." | |
| exec poweroff -f | |
| #exec /bin/sh | |
| } | |
| # Mount the /proc and /sys/ filesystems | |
| mount -t devtmpfs none /dev | |
| mount -t proc none /proc | |
| mount -t sysfs none /sys | |
| # Print only very important messages | |
| sysctl -w kernel.printk="0 1 2" | |
| # Mount the rootfs | |
| echo "Mounting root ..." | |
| /sbin/cryptsetup luksOpen --allow-discards $(findfs UUID="b06abd50-20b8-4705-af9b-7fb7fce3b42f") cryptoroot || rescue_shell | |
| mount -t btrfs -o ro $(findfs UUID="0b836400-d4b4-43ca-859f-71f4fe20a0d1") /mnt/root || rescue_shell | |
| # Mount other file systems (without prompt) | |
| /sbin/cryptsetup luksOpen --key-file=/mnt/root/keyfile --allow-discards $(findfs UUID="4dd83704-ec34-4e42-a3cc-ca5d5fc0d74b") cryptoswap || rescue_shell | |
| swapon -d $(findfs UUID="ab6a7da2-6643-472b-ad4f-1599f82cab14") || rescue_shell | |
| # Cleanup | |
| umount /dev /proc /sys | |
| # Boot the real thing | |
| exec switch_root /mnt/root /usr/lib/systemd/systemd || rescue_shell | |
| EOF | |
| chmod +x init | |
| echo "Finished generating initramfs" | |
| echo "Packing into zstd cpio-archive" | |
| OUT=../initramfs.cpio.zstd | |
| [ -f "$OUT" ] && rm "$OUT" | |
| find . | cpio -o -H newc | zstd - --ultra -o $OUT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment