Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Question about init conception
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Sun Jan 12, 2025 6:51 pm    Post subject: Question about init conception Reply with quote

Hi guys, I am actually doing test under virtualbox with an init system I am coding.

I just have one question.

Is it possible to compile an init system dynamically or not ?

Because if I compile my init dynamically, bascially it can't find the libraries. I have to compile it statically. Are there anyway to do ?
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Sun Jan 12, 2025 6:59 pm    Post subject: Reply with quote

How you start your init?

Usually init start after rootfs already mounted so runtime libraries usually available.

But as you already experienced, init is better statically linked to support all kinds of condition.
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Sun Jan 12, 2025 7:28 pm    Post subject: Reply with quote

So by your explanation, I guess my init should first mount the root filesystem ? I thought will be already mounted.
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22991

PostPosted: Sun Jan 12, 2025 7:29 pm    Post subject: Reply with quote

/sbin/init from sysvinit is dynamically linked, so yes, it is possible, and on a stock Gentoo system, it seems to work without user intervention.
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Sun Jan 12, 2025 7:34 pm    Post subject: Reply with quote

So how should I proceed ? Because there is not really documentation or anything about this.

So I just would like to make the rootfs available for my init system. How should I proceed exactly ?
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Sun Jan 12, 2025 7:40 pm    Post subject: Reply with quote

Fulgurance wrote:
So how should I proceed ? Because there is not really documentation or anything about this.

So I just would like to make the rootfs available for my init system. How should I proceed exactly ?


This is why I ask you how you start init.

Normally init (the program) reside on rootfs, kernel usually got rootfs mounted then call /sbin/init by default.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


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

PostPosted: Sun Jan 12, 2025 7:41 pm    Post subject: Re: Question about init conception Reply with quote

Fulgurance wrote:
Is it possible to compile an init system dynamically or not

"Init system" means several components these days. In particular, yes, the PID 1 program can be (and, as Hu said, on Gentoo is) dynamically linked if:
  • The filesystem that contains the expected location of the dynamic linker (/lib64/ld-linux-x86-64.so.2 for amd64 systems with GNU libc) is mounted by the time the kernel or initramfs' init file invokes the PID 1 program.
  • The filesystem(s) that contain the locations where the dynamic linker searches for the PID 1 program's shared libraries are mounted by the time the kernel or initramfs' init file invokes the PID 1 program.

For the rest of the init system's components it depends.
_________________
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 :)


Last edited by GDH-gentoo on Sun Jan 12, 2025 8:20 pm; edited 1 time in total
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Sun Jan 12, 2025 7:54 pm    Post subject: Reply with quote

So to make sure it's clear.

When the kernel finish to initialize all it need, it give the hand to the program located (by default) at /sbin/init, with the PID 1 right ?
So normally at this stage, rootfs should be mounted yet isn't it ? ( and I think it make sens because to call /sbin/init, root should be mounted)

So my question now is, why I get this error (screenshot):

https://global.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/6/67f17012d1d552d6614cd31ee22798ccad4aedf2.png

Because the rootfs is mounted, and this file exist: /lib64/ld-linux-x86-64.so.2 (I checked by booting bash as init process)

And why /proc/stat isn't mounted ?
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
zen_desu
Tux's lil' helper
Tux's lil' helper


Joined: 25 Oct 2024
Posts: 101

PostPosted: Sun Jan 12, 2025 8:08 pm    Post subject: Reply with quote

If an initramfs is not being used, and the kernel is able to mount the root and run the init, you must be sure your init is doing things such as mounting a devtmpfs on /dev/, procfs on /proc, sysfs on /sys, etc. The devtmpfs can be avoided if you add static nodes for devices.
_________________
µgRD dev
Wiki writer
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


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

PostPosted: Sun Jan 12, 2025 8:20 pm    Post subject: Reply with quote

In other words, if no initramfs is used, the only things that the PID 1 program can assume are:
  • That the rootfs, and only the rootfs, is mounted. Nothing else, not even the proc or devtmpfs filesystems.
  • That standard input, output and error are already open and redirected to the console.

_________________
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 :)


Last edited by GDH-gentoo on Sun Jan 12, 2025 8:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Sun Jan 12, 2025 8:26 pm    Post subject: Reply with quote

Fulgurance wrote:
So to make sure it's clear.

When the kernel finish to initialize all it need, it give the hand to the program located (by default) at /sbin/init, with the PID 1 right ?
So normally at this stage, rootfs should be mounted yet isn't it ? ( and I think it make sens because to call /sbin/init, root should be mounted)

So my question now is, why I get this error (screenshot):

https://global.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/6/67f17012d1d552d6614cd31ee22798ccad4aedf2.png

Because the rootfs is mounted, and this file exist: /lib64/ld-linux-x86-64.so.2 (I checked by booting bash as init process)

And why /proc/stat isn't mounted ?


How did you determine /lib64/ld-linux-x86-64.so.2 was missed in init start up? you screen shut does not show that.

rootfs (/) does not include /proc. /proc is just a mount point for pseudo file system proc. usually it is init job to invoke other program(s) to perform mounting of supporting pseudo file systems (for example sys, proc, devtmpfs, etc...

Not sure which "init" system you are using, but for example openrc on gentoo you can read /lib/rc/sh/init.sh for some idea.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


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

PostPosted: Sun Jan 12, 2025 8:41 pm    Post subject: Reply with quote

pingtoo wrote:
Not sure which "init" system you are using, [...]

The OP says Fulgurance is making one.
_________________
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
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Sun Jan 12, 2025 9:32 pm    Post subject: Reply with quote

GDH-gentoo wrote:
pingtoo wrote:
Not sure which "init" system you are using, [...]

The OP says Fulgurance is making one.


Yes, I understand. however I believe he is refer to the binary program because OP is specific about run time linked library.

I think at this OP is yet to understand the "init" as whole is not just about binary program. It is entire system that describe a life cycle of OSE (Operating System Environment)

I am not saying init system cannot a single binary program, it just that if you programmed entire life cycle of OSE in single binary you lost many opportunity for later enhancement to OSE. because otherwise the init binary need to reprogrammed for new changes. it is just not very well engineering design.
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Mon Jan 13, 2025 12:54 pm    Post subject: Reply with quote

pingtoo wrote:


How did you determine /lib64/ld-linux-x86-64.so.2 was missed in init start up? you screen shut does not show that.


I never said it was missing, in my previous post I said:

Fulgurance wrote:
Because the rootfs is mounted, and this file exist: /lib64/ld-linux-x86-64.so.2 (I checked by booting bash as init process)


That is my point, why my program get this error?

EDIT: So I think I understood the problem. Basically, the root filesystems when the kernel give the hand to the program PID 1 is in read only, so first it need to be remounted.
So basically I guess before I start any service, I need to make a small program that mount the necessary minimal things right ?

I know how to mount proc, but how do you mount /proc/stat ?
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


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

PostPosted: Mon Jan 13, 2025 1:58 pm    Post subject: Reply with quote

Fulgurance wrote:
EDIT: So I think I understood the problem. Basically, the root filesystems when the kernel give the hand to the program PID 1 is in read only, so first it need to be remounted.
So basically I guess before I start any service, I need to make a small program that mount the necessary minimal things right ?

The PID 1 program receiving a rootfs mounted read-only, and the init system remounting it read-write at some point during the boot sequence is a common setup, yes, but has nothing to do with the /proc/stat thing if you are asking because you think that it is connected.

Fulgurance wrote:
I know how to mount proc, but how do you mount /proc/stat ?

/proc/stat is a file, it will magically appear once the proc filesystem is mounted at /proc.
_________________
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
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Mon Jan 13, 2025 2:18 pm    Post subject: Re: Question about init conception Reply with quote

Quote:
but how do you mount /proc/stat ?
Do you have running example of what is actually mounted on /proc/stat?

I am not aware /proc/stat is a mount point for anything.

Assume /proc/stat is NOT a mount point then when you successfully mount /proc than you should automatically got /proc/stat.
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Mon Jan 13, 2025 2:42 pm    Post subject: Reply with quote

Basically it's simple, when I mount /proc, there is nothing. So how should I generate this file normally ?
So it's not a mount point, go it :wink:

So my guess now why I have this bug is, I think it's because of the language I am using. I started to code my program in Crystal (it's based on C). It look like the crystal language ask by default some libraries not available at boot time. A bit annoying
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22991

PostPosted: Mon Jan 13, 2025 3:16 pm    Post subject: Reply with quote

How exactly are you mounting /proc such that afterwards "there is nothing" there? If you mount procfs there, then there should be useful pseudo-files there.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Mon Jan 13, 2025 3:31 pm    Post subject: Reply with quote

Fulgurance wrote:
Basically it's simple, when I mount /proc, there is nothing. So how should I generate this file normally ?
So it's not a mount point, go it :wink:

So my guess now why I have this bug is, I think it's because of the language I am using. I started to code my program in Crystal (it's based on C). It look like the crystal language ask by default some libraries not available at boot time. A bit annoying


OK, Crystal could be problem. I am not sure if Crystal support static link concept. if it does then you can try that. other wise you may want to choose a programming language tool chain that support limited system library like C/C++ or Shell or Python/Perl etc...

I am not sure how a kernel NOT support Proc pseudo filesystem. So can you use the kernel that you plan for your init program but instead boot your init, just boot normally and share the "mount" command output. If the mount command output show /proc is mounted then please share the command (or script) you use that mount /proc.

if your command/script perform mount /proc and yet /proc is empty directory can you share "which mount" output. I am curous why the "mount" command can do such think without report error.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Mon Jan 13, 2025 3:48 pm    Post subject: Reply with quote

I just found CONFIG_PROC_FS is actually a thing. So proc (procfs) is actually configurable.

Fulgurance, is your kernel disable CONFIG_PROC_FS (CONFIG_PROC_FS=n)? that will explain why /proc is empty. But it does not explain why the "mount" command not report error. Or are you code you own mount command with system call mount(2) without checking return status?
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3867
Location: Rasi, Finland

PostPosted: Mon Jan 13, 2025 4:06 pm    Post subject: Reply with quote

Fulgurance wrote:
when I mount /proc
Please, post the exact command you used.
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Fulgurance
Veteran
Veteran


Joined: 15 Feb 2017
Posts: 1243

PostPosted: Mon Jan 13, 2025 4:17 pm    Post subject: Reply with quote

So I find a way to do it with a crystal program !

Basically I made a minimal bash script as /sbin/init:

Code:

#!/bin/bash

mount -o remount rw /
mount --types proc /proc /proc
mount --rbind /sys /sys
mount --rbind /dev /dev
mount --bind /run /run
source /etc/profile
zeroinit


And I compiled my custom small init with shared libraries in /sbin/zeroinit

:D

I don't know if it's a proper way, but it's a beginning. And /proc/stat is here now

Any advices now maybe ? I mean like for security?
_________________
My actual project: https://github.com/Fulgurance/ISM

Ingenius Software Manager is a tool to build and manage a Linux system from scratch.
It will able to manage a linux installation just with a given path to the futur root path
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3867
Location: Rasi, Finland

PostPosted: Mon Jan 13, 2025 5:02 pm    Post subject: Reply with quote

Fulgurance wrote:
And I compiled my custom small init with shared libraries in /sbin/zeroinit

:D

I don't know if it's a proper way
Yup. There are /lib and /lib64 for such libs that need to be present on the system before kernel runs init.

Your mount command doesn't seem to have any errors.

Fulgurance wrote:
And /proc/stat is here now
... so it started working?
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1387
Location: Richmond Hill, Canada

PostPosted: Mon Jan 13, 2025 5:10 pm    Post subject: Reply with quote

Fulgurance wrote:
So I find a way to do it with a crystal program !

Basically I made a minimal bash script as /sbin/init:

Code:

#!/bin/bash

mount -o remount rw /
mount --types proc /proc /proc
mount --rbind /sys /sys
mount --rbind /dev /dev
mount --bind /run /run
source /etc/profile
zeroinit


And I compiled my custom small init with shared libraries in /sbin/zeroinit

:D

I don't know if it's a proper way, but it's a beginning. And /proc/stat is here now

Any advices now maybe ? I mean like for security?

One of "init" duty for linux is reap zombie process. Not sure if you program that into your init program. in order for init to reap zombie process it will need to run as PID 1 (or you did some rearrange as sub-init). so your bash script last line should be "exec zeroinit"
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Mon Jan 13, 2025 5:31 pm    Post subject: Reply with quote

Fulgurance,

Lets back off a little.

You have a VM with a boot loader and a kernel and not much else.
What is in /dev is the VM root before anything is started.
You must have at least /dev/null and /dev/console or it all comes to a grinding halt.
The must be device special nodes too.

How does
Code:
mount --bind /run /run
work?
--bind makes an already mounted filesystem visible somewhere else ... but you have said nothing about mounting run anywhere.
The kernel has mounted root, then its all up to you.

/run is usually tmpfs, so that it can be rw, even an a read only root fs.

I don't know what the --rbind mounts do either, as those filesystems are not mounted anywhere yet.

Code:
#!/bin/bash
needs shared libraries.
Code:
 $ lddtree /bin/bash
/bin/bash (interpreter => /lib64/ld-linux-x86-64.so.2)
    libreadline.so.8 => /usr/lib64/libreadline.so.8
        libtinfow.so.6 => /lib64/libtinfow.so.6
    libtinfo.so.6 => /lib64/libtinfo.so.6
    libc.so.6 => /lib64/libc.so.6


Start off with a statically linked busybox

Where do you set $PATH ?
mount is actually /bin/mount, so it won't be found with an empty $PATH.

Hint: look at some initrd init scrips.
_________________
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
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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