Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] EFI Stub + custom Initramfs: Attempted to kill Init
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3  Next  
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Mon Feb 26, 2024 11:00 am    Post subject: Reply with quote

NeddySeagoon,

NeddySeagoon wrote:
Does it ever ask you for your password, or is the init script skipped entirely?


No, it doesn't ask for my password. Seems like init script was skipped entirely.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Mon Feb 26, 2024 11:05 am    Post subject: Reply with quote

KZQN7737,

Fix the shebang line. Remove the extra sh.
_________________
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
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Mon Feb 26, 2024 11:06 am    Post subject: Reply with quote

Hu,

Hu wrote:
If the initramfs is embedded in the kernel, and you did not rebuild and reinstall the kernel, how would your adjustments to the script be incorporated into a subsequent boot?


Ok, TYSM, my doubts are vanished.

I thought that PERHAPS adjustments to init script are read by the kernel on boot every time it refers to CONFIG_INITRAMFS_SOURCE directory and this directory is being re-read. But I DID re-build when I made adjustments.

Though! Now I have a different result as I've re-built and adjusted the script according to NeddySeagoon's last post.

TYSM y'all for the help and explanations, without your help I wouldn't be able to make a difference!

There was a different result after the line "attached SCSI disk":
Code:

can't open /dev/tty2: No such file or directory
can't open /dev/tty3: No such file or directory
can't open /dev/tty4: No such file or directory

Endlessly.

With occasional breaks on
Code:
[xx.xxxxx] init (nnn) used greatest stack depth: yyyyy bytes left.


But I've searched on the Internet about that and was able to remove this by following:
https://stackoverflow.com/questions/55057641/cant-open-dev-tty2-3-4-no-such-file-or-directory

Now I have only this message:
Code:
[xx.xxxxx] init (nnn) used greatest stack depth: yyyyy bytes left.


I'm able to run console now. It means script fails and returns me into rescue_shell function without any echo message.

But it outputs:

Code:
Please press Enter to activate this console.
[333.143611] kworker/dying (10) used greatest stack depth: 13424 bytes left
can't run /bin/sh: No such file or directory


I think it searches for /bin/sh from line exec /bin/sh from rescue_shell function, but doesn't find it, because I have no such file in my initramfs tree. How could I properly add this file?

My system is openrc.

As you request for emerge --info, here it goes:
https://dpaste.com/9BL5P9EBP.txt

Hu wrote:
Your mega.nz links fail for me. mega.nz is known to have problems with JavaScript. I cannot see any pictures you host there.


Sorry about that.
Then enlighten me, what service can you recommend for pictures as an alternative, is there's a common practice on this forum for that matter? Really curious actually, not sarcastic or smth. I really wish to know more.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Mon Feb 26, 2024 11:36 am    Post subject: Reply with quote

KZQN7737,

It's maybe a bit late but you need to understand the PC Boot Process.

The kernel .config file is a build time thing only. It may be saved in /boot for posterity but it cannot be used.
Even if it could, until the system gets as far as completing localmount, the kernel filesystem tree does not exist.
By that time the initrd has done it's thing and is long gone.

We try to avoid posting images here. I would even go so far as to recommend mouting a USB stick by hand in the initrd console, saving text files there and posting them later.
However, that requires that you can get an initrd console in the first place. You don't have much choice but to post an image.
Personally, I prefer an image, or even a video, over a manually transcribed screen full of output as the typos can make it next to useless.

Code:
[333.143611] kworker/dying (10) used greatest stack depth: 13424 bytes left
can't run /bin/sh: No such file or directory


I don't think that comes from the initrd. Its far too late. The initrd will be done is a few seconds, or less.
Also busybox provides /bin/sh in the initrd. It does not need a PATH= any more to find its built ins either.

If your initrd init script is being run,

Code:
# Custom message.
echo "Hello World."
should appear in the console.
Then it will
Code:
# Attempt to open the encrypted root file system (Prompt for a password).
/sbin/cryptsetup --tries 5 luksOpen /dev/nvme0n1p2 cryptfs || rescue_shell "launching cryptsetup failed"

and drop you to a shell with two messages.
Code:
echo "$@"
echo "Something went wrong. Dropping you to a shell."
The first one being passed to rescue_shell() and the second being the hard coded
Code:
echo "Something went wrong. Dropping you to a shell."


Once the cleanup starts
Code:
# Clean up.
umount -l /proc || rescue_shell "umounting procfs failed"
umount -l /sys || rescue_shell "umounting sysfs failed"
umount -l /dev || rescue_shell "umounting devfs failed"
There is no console any more because
Code:
$ ls /dev/std* -l
lrwxrwxrwx 1 root root 17 Nov  4  2021 /dev/stderr -> ../proc/self/fd/2
lrwxrwxrwx 1 root root 17 Nov  4  2021 /dev/stdin -> ../proc/self/fd/0
lrwxrwxrwx 1 root root 17 Nov  4  2021 /dev/stdout -> ../proc/self/fd/1
as stdout has gone.
_________________
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
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21635

PostPosted: Mon Feb 26, 2024 4:00 pm    Post subject: Reply with quote

KZQN7737 wrote:
I thought that PERHAPS adjustments to init script are read by the kernel on boot every time it refers to CONFIG_INITRAMFS_SOURCE directory and this directory is being re-read. But I DID re-build when I made adjustments.
Such a re-read is impossible in the general case, since those files are on storage that is not available to the kernel until most of the initramfs has finished executing, at which point it is far too late to replace the active initramfs contents with the initramfs on your persistent storage.
KZQN7737 wrote:
As you request for emerge --info, here it goes:
https://dpaste.com/9BL5P9EBP.txt
Musl is unusual; glibc is much more common. You have picked an extremely complex setup for a user who has never been involved in Linux forums (LUKS, secure boot, LVM, EFI stub, embedded initramfs just to get booted; then a musl system once you manage to mount root). I am not surprised you are struggling. If you like being in the deep end of the pool, go ahead. We can keep trying to help you as long as you want to work on this. If you are tired of fighting this, I suggest you reduce your goals and get something simpler to work, then return to this project later, when you have both more experience and a better recovery environment that you can use to correct mistakes.
KZQN7737 wrote:
Then enlighten me, what service can you recommend for pictures as an alternative, is there's a common practice on this forum for that matter? Really curious actually, not sarcastic or smth. I really wish to know more.
I prefer text so that it can be copied, though I acknowledge Neddy's concern that requiring users to transcribe the data can lead to critical typos. When images are necessary, I have found imgur to be the least frustrating service. It doesn't handle JavaScript well (the default landing page requires JavaScript), but I know how to get the underlying image from a given imgur URL, so I can at least see what you are trying to show.
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Wed Feb 28, 2024 12:03 pm    Post subject: Reply with quote

Hu wrote:
Those files are on storage that is not available to the kernel until most of the initramfs has finished executing.


I'll try to recite and sum up in my own words to see if I got it right.
Kernel is not able to re-read init script on boot because:
1) init is stored in an encrypted partition (storage that is not available to the kernel).
Moreover,
2) rootfs is not mounted initially and thus init which is stored on rootfs is not able to be read on boot (storage that is not available to the kernel).
3) Only by including and embedding the initramfs in kernel (not talking about cpio archive, that's a whole new story) we can make init script and other initramfs parts available to the kernel.
4) Thus, as the init is embedded inside of the kernel, we need to re-build the kernel each time when we make an adjustment in anything related to initramfs.

Hu wrote:
At which point it is far too late to replace the active initramfs contents with the initramfs on your persistent storage.


The old active initramfs in the above mentioned potential scenario was not replaced, but re-read and only then replaced. I thought of complete replacement and kernel initramfs' "directory scanning" each time on boot, but contradiction of unavailable storage lets to be known. Nevermind.

Hu wrote:
We can keep trying to help you as long as you want to work on this.


Yes. I am willing to collaborate and learn for this endeavour. I am really curious, ready to soak in the experience. It would be a waste to turn back half-way there. How can I elaborate?
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Wed Feb 28, 2024 2:11 pm    Post subject: Reply with quote

KZQN7737 wrote:
4) Thus, as the init is embedded inside of the kernel, we need to re-build the kernel each time when we make an adjustment in anything related to initramfs.

YES ... but this is not all ... if you change only your init-script and then try to compile everything (kernel+embedded initramfs) again, you will not succeed !

Read this => https://wiki.gentoo.org/wiki/Custom_Initramfs#Integrated_initramfs_does_not_always_update

(and then the whole wiki article)
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Wed Feb 28, 2024 10:47 pm    Post subject: Reply with quote

Hi pietinger,
I'm grateful for the link.
I've re-read the whole article.
I've removed /usr/src/initramfs/usr/initramfs cpio, as it was told there and rebuilt everything.

I've modified my initramfs script a bit to see if something changes.

As I don't have /sbin/init and I wasn't able to find the package that does provide it, I've discovered that I have a /sbin/openrc init:
https://wiki.gentoo.org/wiki/Init_system
So I've put it inside of initramfs script instead of /sbin/init. Can I do so?

I was able to enter the shell inside of initramfs by replacing
Code:
exec /bin/sh
with
Code:
exec sh
in rescue_shell. Out of curiosity.

Now (once I press enter) it doesn't tell me:

Code:
can't run /bin/sh: No such file or directory


It dropped me to the shell once I pressed Enter without any messages:
Code:
#~

And no messages from echo.

The message before pressing Enter has changed.
From:
Code:
 [xx.xxxxxx] init (xxx) used greatest stack depth: yyyyy bytes left.


To:
Code:
 [xx.xxxxxx] sh (xxx) used greatest stack depth: yyyyy bytes left.


It's all that booting screen says before I enter the shell by pressing Enter key.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Wed Feb 28, 2024 11:52 pm    Post subject: Reply with quote

KZQN7737 wrote:
[...]As I don't have /sbin/init and I wasn't able to find the package that does provide it, I've discovered that I have a /sbin/openrc init:
https://wiki.gentoo.org/wiki/Init_system
So I've put it inside of initramfs script instead of /sbin/init. Can I do so?

There may be some confusion about /init and /sbin/init. So let me explain a few things:

1. IF you have a kernel WITHOUT initramfs, then the kernel MUST mount the root partition itself ... AND ... the last thing the kernel does is to start the /sbin/init program from the (mounted) ROOT partition.

2. BUT IF you have a kernel WITH initramfs (no matter if embedded or external), THEN the kernel will NEITHER mount the root partition NOR start /sbin/init. The LAST thing this kernel does is start /init in the root directory of your initramfs. Again, the kernel starts (hardcoded) /init and NOT /sbin/init.

It is THEN the task of THIS init-script to do everything you want ... AND THEN AT LAST to start the /sbin/init of the ROOT partition itself. This is usually done with the last command of an init-script:
Code:
exec switch_root /mnt/root /sbin/init

You see the init-script does as LAST JOB the same as kernel WOULD do without initramfs: Starting /sbin/init FROM root partition of your system.

Yes, package "sys-apps/openrc" automatically installs "sys-apps/sysvinit" and in this package is /sbin/init. You surely have it on your root partition ;-)

To help you further it would be useful if you (again) post your current init-script AND give the content of your initramfs. Example: "ls -alR /usr/src/initramfs"


P.S.: Maybe look also to the first example in: https://wiki.gentoo.org/wiki/Custom_Initramfs/Examples
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Thu Feb 29, 2024 1:10 pm    Post subject: Reply with quote

Ok, quick fact check up.

/sbin/init is the init inside of the "chrooted" root filesystem which is used to finish up the init PID 1 after switching to root partition.

/init is the init inside of init RAM FS which is used to prepare the system for the boot. Not necessary in simpler cases (when root is unencrypted, is partitioned without using logical volumes and no RAID 0/1/01/5/6 etc. is there).

I swear and bet that /sbin/init wasn't there until I've re-emerged sys-apps/openrc and sys-apps/sysvinit!

New /init (again):
https://bpa.st/G2WQ

ls -laR /usr/src/initramfs:
https://bpa.st/4PEQ

$ find UUID is there:
https://bpa.st/O4NA

Noticeable message on bootup:
Code:

[5.086734] Run /init as init process.
can't run '/etc/init.d/rcS': No such file or directory
[5.0911133] init (119) used greatest stack depth: 13960 bytes left
Please press Enter to activate this console.

Especially the rcS line. Indeed no such file on rootfs.

Note: I didn't press anything and it dropped me automatically now, no matter if the line is:
exec /bin/sh or exec sh.
Code:

[5.094446] sh (121) used greatest stack depth: 13912 bytes left

Last line is
Code:

[7.387734] sd 1:0:0:0: [sda] Attached SCSI removable disk

Unless I press enter the process is stuck with occasional breaks on:
Code:

[xx.xxxxx] sh (xxx) used greatest stack depth: xxxxx bytes left

Or
Code:
 [xx.xxxxx] kworker/dying (xx) used greatest stack depth: xxxxx bytes left
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Thu Feb 29, 2024 2:59 pm    Post subject: Reply with quote

Change the first line:
Code:
#!/bin/busybox

to:
Code:
#!/bin/busybox sh

_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Thu Feb 29, 2024 4:59 pm    Post subject: Reply with quote

pietinger,

I've never used
Code:
#!/bin/busybox sh
here ... Ewww I do.

Code:
#!/bin/busybox sh

rescue_shell() {
    echo "$@"
    echo "Something went wrong. Dropping you to a shell."
    # The symlinks are not required any longer
    # but it helps tab completion
    /bin/busybox --install -s
    exec /bin/sh
}

# Parse the root filesystem out of the kernel command line and mount it.
# allow the use of UUIDs or filesystem lables
uuidlabel_root() {
    for cmd in $(/bin/cat /proc/cmdline) ; do
        case $cmd in
        root=*)
            type=$(echo $cmd | /bin/cut -d= -f2)
            echo "Mounting rootfs from uuidlabel_root"
            if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then
                uuid=$(echo $cmd | /bin/cut -d= -f3)
                /bin/mount -o ro $(/bin/findfs "$type"="$uuid") /mnt/root
            else
                /bin/mount -o ro $(echo $cmd | /bin/cut -d= -f2) /mnt/root
            fi
            ;;
        esac
    done
}

...


KZQN7737,

Sorry for getting that wrong.
_________________
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
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Thu Feb 29, 2024 7:23 pm    Post subject: Reply with quote

KZQN7737,

I also have to apologize, because I didn't read the whole thread and therefore didn't see this (otherwise I would have protested earlier ;-) I have just made up for it).

Back to the content of your initramfs (unfortunately I haven't had time to take a closer look today):

You only use links here (most of them generated with busybox --install -s; which you don't actually need). I have to admit that I don't know if this works - i.e. if the script that is started during "make" follows these links and builds your initramfs correctly. Here we need an expert for creating an initramfs WITH a directory (instead a file-list)

(I myself only use the build with an initramfs-list => https://wiki.gentoo.org/wiki/User:Pietinger/Tutorials/Initramfs_Overview#Embedded_with_a_file-list )

What you have to check in any case is whether the programs you are using have been statically compiled. If this is not the case, you must also install the libraries used. Check it with "ldd" or "lddtree". Example of my cryptsetup which was NOT built statically:
Code:
# ldd /sbin/cryptsetup
        linux-vdso.so.1 (0x00007ffcae1cc000)
        libcryptsetup.so.12 => /usr/lib64/libcryptsetup.so.12 (0x00007f4a9c47d000)
        libpopt.so.0 => /usr/lib64/libpopt.so.0 (0x00007f4a9c46e000)
        libuuid.so.1 => /lib64/libuuid.so.1 (0x00007f4a9c464000)
        libblkid.so.1 => /lib64/libblkid.so.1 (0x00007f4a9c40c000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f4a9c24b000)
        libdevmapper.so.1.02 => /lib64/libdevmapper.so.1.02 (0x00007f4a9c1f1000)
        libssl.so.3 => /usr/lib64/libssl.so.3 (0x00007f4a9c14a000)
        libcrypto.so.3 => /usr/lib64/libcrypto.so.3 (0x00007f4a9bc00000)
        libargon2.so.1 => /usr/lib64/libargon2.so.1 (0x00007f4a9c141000)
        libjson-c.so.5 => /usr/lib64/libjson-c.so.5 (0x00007f4a9c12e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4a9c552000)
        libudev.so.1 => /lib64/libudev.so.1 (0x00007f4a9c0ec000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f4a9bb25000)
        libcap.so.2 => /usr/lib64/libcap.so.2 (0x00007f4a9c0de000)

_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Thu Feb 29, 2024 7:30 pm    Post subject: Reply with quote

P.S.: Maybe this is wrong also:
Code:
lrwxrwxrwx  1 root root      16 Feb 25 20:49 cryptsetup -> /sbin/cryptsetup

_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Thu Feb 29, 2024 7:55 pm    Post subject: Reply with quote

pietinger wrote:
Change the first line:
Code:
#!/bin/busybox

to:
Code:
#!/bin/busybox sh


WOW!!! FINALLY! MAGIC IS HAPPENING. SCRIPT IS WORKING
(though it's failing on proc mounting, holly-molly, finally messages of init script are printed!)

Sorry for caps. I got really excited.
In this thread earlier someone answered that I should get sh off shebang #!/bin/busybox line.
Why is that and why now it made the script's messages actually print?

Why did this message
Code:
can't run '/etc/init.d/rcS': No such file or directory

Disappear even though that script is not on the root fs?

I know my understanding of initramfs and how it is built was inaccurate and problematic, the directory tree and even the location of the init built back then, the theoretic part of the script works, the "embedding inside the kernel" thing.
Everything was wicked and I confused many things, that's the part of the learning =].

TYSM!!! I gained at least some proper understanding! XD

UPD: I wrote this post while there was only 1 pietinger's answer.
At the further posts after

pietinger wrote:
Change the first line:
Code:
#!/bin/busybox

to:
Code:
#!/bin/busybox sh


I'll look into tomorrow. Thanks for the tremendous help.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Thu Feb 29, 2024 8:27 pm    Post subject: Reply with quote

pietinger,

I don't like
Code:
busybox --install -s
because the functionality provided by busybox is different to that provided by the usual command.
Even worse, if you have installed some userspace commands
Code:
busybox --install -s
will overwrite them, which is probably not what you want.

To add to the interest, I believe that busybox will use its own internal command, anyway, if that's all there is.
No PATH= or symlinks required.

Code:
mkdir /tmp/busytest
cd /tmp/busytest
/tmp/busytest # mkdir proc dev sys
tmp/busytest # mount -t proc proc proc
/tmp/busytest # mount -t sysfs sys sys
/tmp/busytest # mount -o bind /dev dev
/tmp/busytest # mkdir bin
/tmp/busytest # cp /bin/busybox bin/
/tmp/busytest # chroot ./ /bin/busybox
chroot: failed to run command ‘/bin/busybox’: Permission denied
/tmp/busytest # mount -o remount,exec /tmp
/tmp/busytest # chroot ./ /bin/busybox sh
/ #

Now its just me and a statically compiled busybox.
Code:
/ # mount /dev/nvme0n1p1 /mnt/cdrom
mount: /etc/mtab: No such file or directory

but ...
Code:
/ # ls /mnt/cdrom/EFI/
Boot      SYSLINUX
it works.
As does ifconfig and hexedit /dev/nvme0n1p2
and
Code:
/ # $PATH
/ #

So PATH need not be set and symlinks are not required.
_________________
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
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1531
Location: South America

PostPosted: Thu Feb 29, 2024 10:06 pm    Post subject: Reply with quote

NeddySeagoon wrote:
To add to the interest, I believe that busybox will use its own internal command, anyway, if that's all there is.
No PATH= or symlinks required.

That's controlled by a build time option, CONFIG_FEATURE_SH_STANDALONE. Actually, it will use its internal applet even if there's something else.

With the savedconfig and make-symlinks USE flags unset, Gentoo's sys-apps/busybox does an emake allyesconfig during the src_configure() phase that leaves the option enabled it seems, so if the resulting busybox binary is what you copy to the initramfs, then that's consistent with this behaviour.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Thu Feb 29, 2024 10:46 pm    Post subject: Reply with quote

NeddySeagoon wrote:
I don't like
Code:
busybox --install -s
because the functionality provided by busybox is different to that provided by the usual command.

YESSS ... I dont like it too ... and I would never do it in my system ... and therefore I mentioned it in my answer to PO that he dont needs it ... IMHO it is only necessary if you would be dropped into the rescue-shell of an init-script (and there it will be executed; so need to do it before).
_________________
https://wiki.gentoo.org/wiki/User:Pietinger


Last edited by pietinger on Thu Feb 29, 2024 11:02 pm; edited 1 time in total
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Thu Feb 29, 2024 10:56 pm    Post subject: Reply with quote

KZQN7737,

KZQN7737 wrote:
WOW!!! FINALLY! MAGIC IS HAPPENING. SCRIPT IS WORKING
(though it's failing on proc mounting, holly-molly, finally messages of init script are printed!)

Sorry for caps. I got really excited.

:lol:

1. If you are happy its fully okay ;-)
2. Try it with:
Code:
mount -t proc none /proc  ...

instead of
Code:
mount -t procfs ...

8)

... AND mount "sysfs" instead "sys" :lol:

Because you are using:
Code:
mount -t devtmpfs none /dev || rescue shell "mounting devtmpfs failed"

you will need only the line for console (all other is not needed):
Code:
/usr/src/initramfs/dev:
total 16
drwxr-xr-x  4 root root 4096 Feb 28 21:24 .
drwxr-xr-x 13 root root 4096 Feb 29 13:22 ..
crw-------  1 root root 5, 1 Feb 28 21:12 console
drwxr-xr-x  2 root root 4096 Feb 22 09:27 mapper
crw-rw-rw-  1 root root 1, 3 Feb 28 21:12 null
brw-rw----  1 root disk 8, 1 Feb 28 21:12 sda1
crw-rw-rw-  1 root tty  5, 0 Feb 28 21:13 tty
lrwxrwxrwx  1 root root    9 Feb 26 11:57 tty2 -> /dev/null
lrwxrwxrwx  1 root root    9 Feb 26 11:57 tty3 -> /dev/null
lrwxrwxrwx  1 root root    9 Feb 26 11:57 tty4 -> /dev/null
drwxr-xr-x  2 root root 4096 Feb 22 09:27 vc

... and yes, you dont need the whole busybox stuff in your initramfs; only busybox itself and cryptsetup and lvm.static :P
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
sublogic
Apprentice
Apprentice


Joined: 21 Mar 2022
Posts: 222
Location: Pennsylvania, USA

PostPosted: Fri Mar 01, 2024 1:47 am    Post subject: Reply with quote

KZQN7737 wrote:
WOW!!! FINALLY! MAGIC IS HAPPENING. SCRIPT IS WORKING
(though it's failing on proc mounting, holly-molly, finally messages of init script are printed!)

Okay, that's progress. Before you fix the rest, in your initramfs /init script, change
Code:
rescue_shell() {
echo "$@"
echo "Something went wrong. Dropping you to a shell."
# The symlinks are not required any longer
# But it helps tab completion.
/bin/busybox --install -s
exec sh
}
to this excellent recommendation from Hu in post 8804109:
Code:
rescue_shell() {
    printf 'Starting rescue subshell: %s\nExit with code 5 to replace /init with a shell running as pid 1\n' "$*"
    /bin/sh
    if [[ $? -eq 5 ]]; then exec /bin/sh; fi
}


If something goes wrong, your init script prints an error message and drops you to a shell prompt so you have a chance to fix the problem manually. Then what ? With Hu's trick, you just type exit and your script resumes ... until the next problem, or until it boots. If you don't make the change, your script can't resume and you have to finish everything manually, all the way to the final exec switch_root.

As it is you'll be fine if the rescue shell is never invoked, but at line 34 your script has
Code:
rescue_shell "Stopping for diagnostics."
which is a good idea, but now you're in charge of cleaning up and doing the exec switch_root /mnt/root /sbin/init manually. So make the change, your life will be easier.

Long debate in topic 1165371: do you want a "fix and resume" rescue shell, or a "take over everything" rescue shell. Hu's suggestion lets you have it both ways. If you type exit 5 you get another shell prompt, this time of the "take over everything" kind.
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Fri Mar 01, 2024 8:36 pm    Post subject: Reply with quote

Hello everyone.
Glad that my post was able to provoke the expansion of topic regarding /bin/busybox --install -s.

pietinger wrote:

P.S.: Maybe this is wrong also:
Code:
lrwxrwxrwx  1 root root      16 Feb 25 20:49 cryptsetup -> /sbin/cryptsetup



Absolutely correct! For some reason I was creating a soft link for cryptsetup instead of copying it as an archive. So thanks for noticing!

On boot it resulted in an error which I haven't noticed from the first glance:

Code:
/init: line 21: /sbin/cryptsetup: Symbolic link loop


I understood instantly that it has to do with linking and troubleshooted. This cryptsetup softlink was pointing at rootfs' /sbin/cryptsetup, which is not able to be read because the storage is unavailable.

I just got confused as to why we create soft links for busybox.

And on the other hand, for the other executables such as cryptsetup or lvm.static when we'd like to include them in initramfs we copy them with -a flag. Same for devices in /dev/.

Have to note once more, all of programs I included are statically linked.
KZQN7737 wrote:

Code:

ldd: /bin/busybox: Not a valid dynamic program
ldd: /sbin/cryptsetup: Not a valid dynamic program


KZQN7737 wrote:

ldd: /sbin/lvm.static: Not a valid dynamic program


pietinger wrote:

(I myself only use the build with an initramfs-list => https://wiki.gentoo.org/wiki/User:Pietinger/Tutorials/Initramfs_Overview#Embedded_with_a_file-list )

Curious! On the other hand, I've heard only of the directory method. Likewise, it is more simple because no initramfs_list is required, from what I understood.

pietinger wrote:

Because you are using:
Code:

mount -t devtmpfs none /dev || rescue shell "mounting devtmpfs failed"

you will need only the line for console (all other is not needed) [...]


Because "none" devices are mounted into dev temporary filesystem, I suppose?

I'm cautious regarding /dev/tty2-4. Earlier I've got this:
KZQN7737 wrote:

Code:

can't open /dev/tty2: No such file or directory
can't open /dev/tty3: No such file or directory
can't open /dev/tty4: No such file or directory


Instead of proper booting, back when I didn't have them in /dev/.

pietinger wrote:

and yes, you dont need the whole busybox stuff in your initramfs; only busybox itself and cryptsetup and lvm.static

What about busybox shell (/bin/sh) and mount, umount, exec (not sure if it's in busybox?) utilities included in busybox that are used in /init? Should I simply rm everything besides these? Unbeliveable, but I'll try as an experiment =)

sublogic wrote:


change [...previous rescue shell() function placeholder...]
to this excellent recommendation from Hu in post 8804109 [...]


Done! I wonder about some parts of syntax of either functions and their inner work.

What exactly
Code:
 echo "$@"
does? Displays all the variables? Couldn't find the explanation of this particular syntax in the Internet.

Code:
 if [[ $? -eq 5 ]];

I see the conditional statement, athough the rest remains a mysterious elf language. I suppose -eq stands for PID or exit code.
Very impressive.

sublogic wrote:

As it is you'll be fine if the rescue shell is never invoked, but at line 34 your script has
Code:

rescue_shell "Stopping for diagnostics."

which is a good idea, but now you're in charge of cleaning up and doing the exec switch_root /mnt/root /sbin/init manually. So make the change, your life will be easier.

Got you. Got it commented.
Back to top
View user's profile Send private message
KZQN7737
n00b
n00b


Joined: 13 Feb 2024
Posts: 27

PostPosted: Fri Mar 01, 2024 9:25 pm    Post subject: Reply with quote

Nevertheless. Current state: Now some commands in the script, including "Hello world" are executed correctly.
Script fails on cryptsetup part with the message:
Code:

[5.0482271] Run /init as init process
+ mount -t proc none /proc
[5.0564321] busybox (122) used greatest stack depth: 13912 bytes left
+ mount -t sysfs none /sys
+ mount -t devtmpfs none /dev
+ echo 'Hello World.'
Hello World.
+ /sbin/cryptsetup --tries 5 luksOpen /dev/nvme0n1p2 cryptfs

Locking aborted. The locking path /run/cryptsetup is unusable (not a directory or missing).
Failed to acquire read lock on device /dev/nvme0n1p2.
Device /dev/nvne0n1p2 is not a valid LUKS device.
[5.0784651] cryptsetup (125) used greatest stack depth: 13472 bytes left
+ rescue_shell launching cryptsetup failed

printf 'Starting rescue subshell: %s\n Exit with code 5 to replace /init with a shell running as pid in' launching cryptsetup failed
Starting rescue subshell: launching cryptsetup failed Exit with code 5 to replace init with a shell running as pid 1

+ /bin/sh
/bin/sh: can't access tty: job control turned off

[6.3480951] sd 1:0:0:0: [sda] No Caching mode page found 6.3495431 sd 1:0:0:0: [sda] Assuming drive cache: write through
[6.721515] sda: sda1
[6.7239331] sd 1:0:0:0: [sda] Attached SCSI removable disk

# exit 5
+ '[[' 5 -eq 5]]
sh: missing ]]
+ /sbin/lvm.static vgscan --mknodes
[ 131.2578441] lvm.static (129) used greatest stack depth: 13288 bytes left
+ /sbin/lvm.static lvchange -a ly /dev/mapper/vg-root
Volume group "vg" not found
Cannot process volume group vg
[131.3228241] lvm.static (130) used greatest stack depth: 13088 bytes left
+ rescue_shell 'changing logical volume failed'
+ rescue_subshell: 'changing logical volume failed'
Exit with code 5 to replace init with a shell running as pid 1
+ /bin/sh
+ /bin/sh: can't access tty: job control turned off
+printf 'Starting rescue subshell: %s\n Exit with code 5 to replace /init with a shell running as pid in 'changing logical volume failed


Even though lsblk shows that nvme0n1p2 is crypto_LUKS.
https://bpa.st/APYA

and volume group vg exists:
https://bpa.st/XZMA

But I suppose that this error results from the previous, cryptsetup one. Unable to read LVM volumes inside of LUKS container, because for some reason "/dev/nvme0n1p2 is not a valid LUKS device".

I've checked syntax here: https://wiki.gentoo.org/wiki/Custom_Initramfs#DM-Crypt
And don't see what's the problem.

cryptsetup status /dev/nvme0n1p2 and cryptsetup status /dev/mapper/cryptfs:
https://bpa.st/HDYA

Current script:
https://bpa.st/ZEKA
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1531
Location: South America

PostPosted: Fri Mar 01, 2024 9:40 pm    Post subject: Reply with quote

KZQN7737 wrote:
Code:
Locking aborted. The locking path /run/cryptsetup is unusable (not a directory or missing).
Failed to acquire read lock on device /dev/nvme0n1p2.

cryptsetup needs a /run directory. Add an empty run/ subdirectory in /usr/src/initramfs, rebuild and reinstall the kernel with the new embedded initramfs, and try again.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Fri Mar 01, 2024 9:47 pm    Post subject: Reply with quote

KZQN7737

The word none in the mount commands below is not used. Its saved for diagnostics though.
Code:
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev


You get things like "none busy" which is not very useful.

I like to use proc, sys and dev respectively in place of none.

I don't think
Code:
Locking aborted. The locking path /run/cryptsetup is unusable (not a directory or missing).
matters.
If it makes you nervous, mount -t tmpfs on /run That may be /dev/shm

I get
Code:
/bin/sh: can't access tty: job control turned off
when my init script fails too.
I think its because we don't mount devpts on /dev/pts type devpts so that there are no ptys available.

-- edit --

Code:
/sbin/lvm.static vgscan --mknodes || rescue_shell "vgscan failed"
/sbin/lvm.static lvchange -a ly /dev/mapper/vg-root || rescue_shell "changing logical volume failed"
/sbin/lvm.static vgchange -ay || rescue_shell "vgchange failed"


Code:
/sbin/lvm.static vgscan --mknodes || rescue_shell "vgscan failed"
is not required. lvchange -ay will ho that.

I think that
Code:
/sbin/lvm.static lvchange -a ly /dev/mapper/vg-root || rescue_shell "changing logical volume failed
only activates vg-root.
If so,
Code:
/sbin/lvm.static vgchange -ay
will shout at you because vg-root is already active.
I don't know if that returns an error though.

Until recently, I ran root in LVM without an initrd, but I gave that up as an initrd is now required to mount a separate /usr.

In short, only
Code:
/sbin/lvm.static vgchange -ay || rescue_shell "vgchange failed"
is required.
_________________
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
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4157
Location: Bavaria

PostPosted: Fri Mar 01, 2024 11:13 pm    Post subject: Reply with quote

I will answer only your open questions; others was already answered by our experts.

KZQN7737 wrote:
I just got confused as to why we create soft links for busybox.

[...]

pietinger wrote:
and yes, you dont need the whole busybox stuff in your initramfs; only busybox itself and cryptsetup and lvm.static

What about busybox shell (/bin/sh) and mount, umount, exec (not sure if it's in busybox?) utilities included in busybox that are used in /init? Should I simply rm everything besides these? Unbeliveable, but I'll try as an experiment =)

It is very easy ... and you can try it in a simple terminal:

Start "busybox sh" (as root) and you will see a change of your prompt, because you are now IN the shell from busybox itself. Type now "mount". It works ! Because the shell from busybox knows all of its implemented internal commands. You will need these links ONLY if you are in an other shell (like bash) and want start the internal mount-command from busybox INSTEAD the normal mount command. YES, installing all of these busybox links OVERWRITES your normal used applications ... do you want this ?

So, the last question: WHY we do this in the rescue-shell function ? Because we have no normal system here and we want/can use only all the internal commands of busybox.

KZQN7737 wrote:
pietinger wrote:
(I myself only use the build with an initramfs-list => https://wiki.gentoo.org/wiki/User:Pietinger/Tutorials/Initramfs_Overview#Embedded_with_a_file-list )

Curious! On the other hand, I've heard only of the directory method. Likewise, it is more simple because no initramfs_list is required, from what I understood.

If you look in this article from me, you will see there are 4 options to build (manually) an initramsfs ... ;-)

KZQN7737 wrote:
Done! I wonder about some parts of syntax of either functions and their inner work.

What exactly
Code:
 echo "$@"
does? Displays all the variables? Couldn't find the explanation of this particular syntax in the Internet.

Code:
 if [[ $? -eq 5 ]];

I see the conditional statement, athough the rest remains a mysterious elf language. I suppose -eq stands for PID or exit code.

You can call a function in a shell script simply without any parameter OR with one (or more) parameter. If you look to the init-script from https://wiki.gentoo.org/wiki/Early_Userspace_Mounting you will see they test almost every command if it exits with 0 (== no error). If there was an error they call the function "rescue_shell" with an error message as 1st (and only) parameter:
Code:
        2|3)    if [ "$RC_UNAME" = Linux ]; then
                        echo "Filesystem repaired, but reboot needed"
                        reboot -f
                else
                        rescue_shell "Filesystem still have errors; manual fsck required"
                fi;;

THIS PARAMETER (the error message) shall now printed out to the display FROM the function rescue_shell ... and this is done with the echo command:
Code:
rescue_shell() {
    echo "$@"

"$@" is the representation of the transfered parameter.

Dont worry if in other script you will see a line like this:
Code:
mount $m || rescue_shell "Error while mounting $m"

This operator -> || is a logical "OR" and works like an IF...THEN...ELSE without THEN (hahaha)
If the first command was successful then I have a 1 and dont need to execute the second command. If the first command was NOT successful, then I must execute the second command == calling function rescue_shell.

-eq means "equal" ... the same as in some programming languages: "IF A == 5 THEN ..."

(TBH my collegue @Hu is the greatest master of bash I have ever seen ... and maybe I will get some shouting about my poor explanation ... but he can explain everything from bash nobody has ever heard before)
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
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
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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