Consider a distro "foo" which, in its normal non-Bedrock usage, has a symlink at /sbin/init pointing to /usr/lib/systemd/systemd.
If it's added to Bedrock as a new stratum, there will be a symlink at /bedrock/strata/foo/sbin/init, but it will be pointing at /usr/lib/systemd/systemd rather than /bedrock/strata/foo/usr/lib/systemd/systemd. That symlink only works if you're chrooted to /bedrock/strata/foo.
If we know foo contains a "realpath" command, we could run:
Code:
chroot /bedrock/strata/foo realpath /sbin/init
to see where /sbin/init points, then append that to /bedrock/strata/foo to find the actual path we're interested in:
Code:
echo "/bedrock/strata/foo/$(chroot /bedrock/strata/foo realpath /sbin/init)"
However, we don't know if the stratum has realpath in it. We could copy a portable busybox realpath into the stratum:
Code:
cp /bedrock/libexec/busybox /bedrock/strata/foo/busybox
chroot /bedrock/strata/foo /busybox realpath /sbin/init
but I don't want to cause unnecessary disk writes.
Linux's /proc/<PID>/root points to a given process' root directory. In this case, we know PID 1 is running in the bedrock stratum and has /bedrock/libexec/busybox. So instead of copying it, mount proc and go through there:
Code:
mount -t proc proc /bedrock/strata/foo/proc
chroot /bedrock/strata/foo /proc/1/root/bedrock/libexec/busybox realpath /sbin/init
That's what this bit of code is doing. It's definitely convoluted.
There's work towards a proper test suite. Once that's in place with adequate tests to confirm changes here won't break things, I plan on making a small C utility which resolves symlinks as though they were chrooted. I'll then refactor code like this to use them, which should simplify things. Once
RESOLVE_IN_ROOT becomes available I can use it and improve performance in some parts of the code as well for people on new enough kernels.