Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
custom initramfs
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 12:41 pm    Post subject: custom initramfs Reply with quote

I was trying to create a custom init that

1. unlocks a LUKS volume
2. copies squashfs image into memory
3. closes LUKS volume

I generated an init and it appears that it is loading, so I think the file format is correct, but the system reboots and I'm unable to debug. The contents of the init are:

1. lddtree -l /usr/bin/{cat,cp,cryptsetup,dd,find,grep,insmod,ls,mkdir,modprobe,mount,printf,rm,sh,sleep,switch_root,umount}
2. kernel modules at their respective locations
3. /init

The /init script is a simple shell script which loads /proc, /dev, /sys, and /run. Then the kernel modules are loaded based on what is loaded on the machine when the init is generated. From there, I basically call cryptsetup, mount, umount, and cryptsetup again.

Since I'm using sh instead of bash, I included the printf command (and any necessary libraries).

I'm wondering what I may be missing since it reboots fairly quickly. I don't see any output and I call printf early on to help with debugging.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 55015
Location: 56N 3W

PostPosted: Thu Mar 13, 2025 12:50 pm    Post subject: Reply with quote

o5gmmob8,

We need an least the the init script and the file you feed to
Code:
/usr/src/linux/usr/gen_init_cpio /root/initramfs/initramfs_list > /boot/<initramfs_name>

to assemble the cpio archive.

Early reboots sound like illegal instruction exceptions.

Have you seen https://wiki.gentoo.org/wiki/Initramfs_-_make_your_own ?
Its not exactly what you need but its a start.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 1:02 pm    Post subject: Reply with quote

Hi NeddySeagoon,

Yes, I saw many pages, some of which appear out-of-date.

Here is my init script:

Code:
#!/bin/sh

export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib64:/usr/lib/gcc/x86_64-pc-linux-gnu/14
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

printf '### init ###\n'
OVERLAYFS_SIZE=
LUKS_DEVICE_UUID=

# setup some basic mounts, is this needed?
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mount -t tmpfs -o rw,nosuid,nodev,relatime,mode=755 none /run

printf '### mounted filesystems ###\n'

# load modules
modprobe -a KERNEL_MODULES

printf '### loaded modules ###\n'

cryptsetup luksOpen /dev/disk/by-uuid/$LUKS_DEVICE_UUID luks-$LUKS_DEVICE_UUID

printf '### unlocked device ###\n'

mkdir -p /run/root-volume /run/root-image
mount /dev/mapper/luks-$LUKS_DEVICE_UUID /run/root-volume

# copy squashfs image into memory
printf 'Copying image into memory\n'
dd if=/run/root-volume/root-squashfs.img of=/run/root-image/root-squashfs.img
printf 'Copied image into memory\n'

umount /run/root-volume
cryptsetup luksClose luks-$LUKS_DEVICE_UUID

# mount squashfs image to new root
mkdir -p /mnt/overlayfs/rw /mnt/overlayfs/work /mnt/root
mount /run/root-image/root-squashfs.img /mnt/root

mount -t tmpfs -o size=$OVERLAYFS_SIZE tmpfs /mnt/overlay
mount -t overlay -o lowerdir=/mnt/root,upperdir=/mnt/overlay/rw,workdir=/mnt/overlay/work overlay /mnt/root

mkdir -p /mnt/root/rw
mount -o bind /mnt/overlayfs/rw /mnt/root/rw

umount /dev /proc /sys
exec /sbin/switch_root /mnt/root /sbin/init


printf '### init done ###\n'



I'm using this script to generate an init:

Code:
#!/bin/sh

_REQUIRED_ARGUMENTS="LUKS_DEVICE_UUID:LUKS Device UUID"

# @see: https://wiki.gentoo.org/wiki/Custom_Initramfs#Prerequisites
_info "Preparing init"
rm -rf /tmp/init && mkdir -p /tmp/init
cd /tmp/init

KERNEL_VERSION=$(uname -r)

: ${OVERLAYFS_SIZE:=1G}

mkdir -p {dev,etc,mnt/root,proc,root,sys,run,usr/bin,usr/lib,usr/lib64,usr/sbin}
cp --archive /dev/{null,console,tty} dev/

ln -s usr/bin bin
ln -s usr/sbin sbin
ln -s usr/lib lib
ln -s usr/lib64 lib64

#mkdir -p lib/modules/$KERNEL_VERSION

# copy binaries and dependencies
_info "Copying dependencies"
tar cp -C / $(lddtree -l /usr/bin/{cat,cp,cryptsetup,dd,find,grep,insmod,ls,mkdir,modprobe,mount,printf,rm,sh,sleep,switch_root,umount} | tr '\n' ' ') 2>/dev/null | tar xp -C /tmp/init

_info "Copying init"
cp $_CONF_APPLICATION_LIBRARY_PATH/init.sh init
chmod +x init

_info "Copying kernel modules"
# kernel modules
for module_name in $(lsmod | awk {'print$1'} | sort -u); do
  module_file=$(find /lib/modules/$KERNEL_VERSION -type f -name $module_name.ko)
  [ -z "$module_file" ] && {
    _warn "No module file found for $module_name"
    continue
  }

  tar cp -C / $module_file 2>/dev/null | tar xp -C /tmp/init
done

tar cp -C / /lib/modules/$KERNEL_VERSION/modules.* 2>/dev/null | tar xp -C /tmp/init

# kernel modules to load
$_CONF_GNU_SED -i "s/KERNEL_MODULES/$(lsmod | awk {'print$1'} | sort -u | tr '\n' ' ')/" init
$_CONF_GNU_SED -i "s/^OVERLAYFS_SIZE=.*$/OVERLAYFS_SIZE=$OVERLAYFS_SIZE/" init
$_CONF_GNU_SED -i "s/^LUKS_DEVICE_UUID=.*$/LUKS_DEVICE_UUID=$LUKS_DEVICE_UUID/" init

# generate init
find . -print0 | cpio --null --create --verbose --format=newc | zstd --ultra -o /tmp/initramfs.cpio.zstd
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 55015
Location: 56N 3W

PostPosted: Thu Mar 13, 2025 1:11 pm    Post subject: Reply with quote

o5gmmob8,

Code:
/dev/disk/by-*
is a set oy symbolic links created by udev
Do you have udev in the initrd and started?

You could do with a rescue_shell() function, so that when something fails, you get a shell to poke about. e.g.

Code:
/sbin/mdadm --assemble /dev/md1 --uuid=de8f2cbc-17ca3275-0b69db3c-b9f91a6b || rescue_shell "The host RAID set failed to assemble"

_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 1:15 pm    Post subject: Reply with quote

Hi NeddySeagoon,

No, I don't have udev, let me see about that. I suppose I can add the rescue shell, but my thought process was I have a printf statement very early, I should see that, right?
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 55015
Location: 56N 3W

PostPosted: Thu Mar 13, 2025 1:25 pm    Post subject: Reply with quote

o5gmmob8,

You may need some sleep statements too, to have time to read the print statements.

The rescue shell will do noting in normal operation, as it would never be called. It may save debug time though.

Code:
cryptsetup luksOpen /dev/disk/by-uuid/$LUKS_DEVICE_UUID luks-$LUKS_DEVICE_UUID || rescueshell "luksOpen failed"

would have allowed to to poke about in /dev and maybe spot that /dev/disk/by-* was missing.

It also allows you to complete the boot manually.

Is there any reason not to use busybox in the initrd?
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 1:28 pm    Post subject: Reply with quote

Hi NeddySeagoon,

I suppose I'm trying to understand what is actually required to make an init :). The less 'stuff' there is in it, the easier it is for me to understand.

I saw the notes on busybox and historically, I think all of my inits probably utilized that.

Good points about the rescue shell, I can include that earlier on to have an idea that something did go wrong.

Let me explore those options and report back.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 55015
Location: 56N 3W

PostPosted: Thu Mar 13, 2025 1:35 pm    Post subject: Reply with quote

o5gmmob8,

Its just the kernel, the initrd and the init script.
Everything must be provided.

busybox gives you cut down versions of most of the commands you need and some that that you don't.
e.g. busybox mount did not understand filesystem UUID, so I needed the user space mount.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 2:00 pm    Post subject: Reply with quote

Ok, now I'm getting somewhere.

I think you're right, I installed busybox (not static because I didn't want to rebuild other libraries) and was able to capture a segfault. It is unable to load a library.

So, I think I will stick with busybox and statically compile it to simplify that. It only requires a rebuild of a few other packages, so it isn't a huge deal.
Back to top
View user's profile Send private message
o5gmmob8
Guru
Guru


Joined: 17 Oct 2003
Posts: 555

PostPosted: Thu Mar 13, 2025 3:30 pm    Post subject: Reply with quote

Ok, my cryptsetup is not static and if I build it with static-libs, then it wants me to remove udev support which I suppose I can do locally.

Hmm, getting somewhere.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum