Re: s6-linux-init, alpine linux, and initramfs

From: Laurent Bercot <ska-skaware_at_skarnet.org>
Date: Tue, 31 Jan 2017 21:35:55 +0000

>I had to mount /dev and to resort to busybox's switch_root because the
>one built using execline tools had trouble executing. Despite using
>executables from the actual root filesystem, it had issue spawing tools
>at some point in the loop. I guess this is because they are not
>statically compiled.

  Yes, obviously, all the binaries in your initramfs need to be
statically
compiled. Or you could put a libc.so and a dynamic linker in your
initramfs' /lib, if you prefer. musl makes it easy enough (only one
.so),
but it also makes it easy to statically compile, so, your choice.


>The "root=" seems a bit tricker: it is only available through parsing
>/proc/cmdline. I think something like this will do it:

  It won't do it in every case: parsing /proc/cmdline is hazardous and
much more difficult than it appears. (There could be quotes, and quoted
spaces, in the elements.) We had a discussion about this on the Alpine
development IRC channel, and it appeared that you can't do it safely
with less than 50 lines of shell.

  So you could:
  - ignore the problem and hope you'll never need funky options
  - add a /bin/sh to your rootfs and the code to parse /proc/cmdline
safely
  - find another solution.

  The simplest solution, if you control the kernel command line, is to
duplicate the root= argument with something the kernel will let through:
root=/dev/sda1 rootfs=/dev/sda1
  The kernel keeps the "root" argument, but puts "rootfs" in the
environment, which you can easily read from.


>I have seen alpine's intit script use KOPT_* variables (like KOPT_root,
>KOPT_quiet and so on) but I have found no reference to them nor have I
>found them in the environment of process 1.

  The Alpine initramfs is a bit more complex than my simple skeleton, for
several reasons. One of the reasons is that they want to parse
/proc/cmdline the right way, so they have that giant block of shell
code;
it's that piece of code that exports the elements of /proc/cmdline as
KOPT_something.
  Once it has all the information it needs and has found its rootfs, the
initramfs script doesn't need environment variables anymore, so it
cleans up its environment. You should do the same: be as transparent as
possible, do not leak into /sbin/init anything it doesn't strictly need.
/sbin/init may have the environment variables set by the kernel, but it
definitely shouldn't have any variables set by your initramfs script.

--
  Laurent
Received on Tue Jan 31 2017 - 21:35:55 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC