1

How can I save environment variables at boot time before the real filesystem has been mounted ?

I know there is /etc/environment but whithin initramfs that file is not accessible yet. I also thought of writing a file to store the value but when that file exits on the system it's override after the real filesystem is mounted

Ps : I just want to store the variable for later, not to read it at boot time

EDIT AFTER ANSWER

Approximately :

  • Create a kernel module (find help here and there) nammed mod.ko
  • Make sure the module is usable during boot time (by including it inside the kernel image with initramfs-tools or finding it within the filesystem)
  • Add the following lines to your script

If you don't have included the module in the kernel image

insmod /path/to/mod.ko

Else directly

echo "env_var=value" > /proc/name_of_your_process

where name_of_your_process is the name given inside the __init of your module (when doing entry = proc_create("name_of_your_process",0777,NULL,&my_fops);)

Then later during the startup process in a script

cat /proc/name_of_your_process
>> env_var=value
rmmod mod.ko
  • Depends on what your initramfs looks like, but my first try would be mkdir /run/envdump; mount -t tmpfs tmpfs /run/envdump -o size=16m; env >/run/envdump/output as an addition to linuxrc or whatever the script is your initramfs runs. There could be more to it and I cannot find out atm so this is just a comment ... – Bananguin May 30 at 18:46
  • That works I can create a file in /run. But I'm afraid that the file I create could be used by another process which need to create a file with the exact same name – Ben W May 31 at 9:30
  • However, I can create a directory on the filesystem (directory created by user), then mount a tmpfs over that directory (which will temporarily hide all files inside it) add my environment file inside the tmpfs, read that file at startup then write it into /etc/environment and finally umount the tmpfs. Of course, path to the user directory will be write into /etc/environment and read at boot time (since I have access to the RO /root filesystem) – Ben W May 31 at 10:06
  • So just to followup, you managed to store data inside of custom kernel module that never touched the root filesystem? Interesting. – Benjamin Jun 7 at 21:07
  • Yes. I just edit the /proc inside my kernel image. The value is then read at startup and the module is unloaded – Ben W Jun 8 at 5:27
0

When initramfs cleans up all the files it runs switch_root which will delete all of the files EXCEPT for the folders: /proc, /dev, /sys and /run. It may be possible to create a special file in one of those folders, but there is trickery involved.

See: Is it possible to create a directory and file inside /proc/sys?

Your easiest option of course, is to just add a script to write the variable to the filesystem after the root filesystem has been loaded but before init is run. This would go in the "local-bottom" folder. Is there any reason you can't do this?

  • Because the /root is mounted as RO and if possible I would like to avoid messing with filesystem (remount as rw) – Ben W May 31 at 9:27
0

Well, regular environment inheritance (just export it ...) would work in theory but... the problem is, whatever your initramfs is starting will probably clean up the environment on purpose, as that is usually done to make sure nothing odd is going on. Letting environment variables go unchecked can be a security hazard.

I know there is /etc/environment but whithin initramfs that file is not accessible yet.

The fun part about initramfs is that you can do whatever you want. The only limitation is your imagination.

You can totally make the file accessible. Mount, modify, umount. Or if you are using a traditional filesystem - without checksums - you can even modify it without mounting anything (or at least not in read-write mode). Same trick GRUB uses for grubenv.

If you don't want to muck with the filesystem or physical storage in general, you could set up a ram disk or tmpfs or loop device or ... there are countless ways for initramfs to leave something behind (you usually have to make an effort to clean up).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.