Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you handle libraries in /usr/lib/x86_64-linux-gnu/<subfolder> #68

Closed
blackliner opened this issue Jul 20, 2024 · 5 comments
Closed

Comments

@blackliner
Copy link

blackliner commented Jul 20, 2024

I am starting off with debian bookworm as a base image, and after utilizing this workaround (thanks @gfrankliu), I now have all required libraries installed.

One of them is libopenblas0, which is getting installed into /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblas.so.0. My program fails with ImportError: libopenblas.so.0: cannot open shared object file: No such file or directory, and I can fix it with these two lines in the containers entrypoint.sh:

echo "/usr/lib/x86_64-linux-gnu/openblas-pthread" > /etc/ld.so.conf.d/openblas.conf
ldconfig

Another way is to set LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/openblas-pthread, which is a bit more convinient with oci_image.

Is there a better way to tell ld about this additional location? How is dpkg doing it?

@gfrankliu
Copy link

Some deb packages come with "postinst" scripts. eg: , eg, libopenblas0-pthread_0.3.21+ds-4_amd64.deb, postinst script create those symlinks

#! /bin/sh
set -e

update-alternatives --install /usr/lib/x86_64-linux-gnu/libblas.so.3 \
                    libblas.so.3-x86_64-linux-gnu \
                    /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 100

update-alternatives --install /usr/lib/x86_64-linux-gnu/liblapack.so.3 \
                    liblapack.so.3-x86_64-linux-gnu \
                    /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 100

update-alternatives --install /usr/lib/x86_64-linux-gnu/libopenblas.so.0 \
                    libopenblas.so.0-x86_64-linux-gnu \
                    /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblas.so.0 100



exit 0

Since rules_distroless only takes the tar files, those libs aren't linked into /usr/lib/x86_64-linux-gnu, that's why your program can't find the shared libs in the standard directories.

I am not sure if there is already a feature request for this, or we will just have to live it with it, and add separate rules to create those symlinks.

@loosebazooka
Copy link
Member

Yeah, rules_distroless probably wont have a way of auto applying post-install scripts. You can maintain a custom rule to do it for you for the packages that need it.

@blackliner
Copy link
Author

Thanks, makes sense! Do you think there is a path forward using these postinst scripts with a dummy update-alternatives that just creates those symlinks?

For the short term solution, I would guess a similar approach to what you proposed previously should do it?

def symlink_files(name, srcs, from_dir, to_dir, **kwargs):
    """Create relative symlinks for all files in from_dir pointing to to_dir.
    
    For example, assume
        from_dir = /usr/bin
        to_dir   = /usr/local/bin
        touch      /usr/bin/foo.
    ls -al /usr/bin/foo
    lrwxrwxrwx 1 root root 20 /usr/bin/foo -> ../local/bin/foo
    """
    native.genrule(
        name = name,
        srcs = srcs,
        outs = [name + ".tar"],
        cmd = " ; ".join([
            # create a temporary directory and untar the files
            "SCRATCH=$$(mktemp -d )",
            "REALOUT=$$(realpath $@)",
            "mkdir -p $$SCRATCH/bundle",
            'for i in $(SRCS); do tar -xmf "$$i" -C $$SCRATCH/bundle ;done',
            "cd $$SCRATCH/bundle",
            "FROM_DIR=" + from_dir,
            "TO_DIR=" + to_dir,
            # remove leading / from the directories
            "FROM_DIR=$$(echo $$FROM_DIR | sed 's|^/||')",
            "TO_DIR=$$(echo $$TO_DIR | sed 's|^/||')",
            # find relative path from to_dir to from_dir
            "RELATIVE_DIR=$$(realpath --relative-to=$$TO_DIR $$FROM_DIR)",
            # create symlinks and tar the files
            "find $$FROM_DIR -type f,l -exec sh -c \"basename {} | xargs -I % ln -s $$RELATIVE_DIR/% $$TO_DIR/%\" \\;",
            "tar --sort=name --owner=root:0 --group=root:0 --mtime='UTC 2019-01-01' -cf $$REALOUT .",
            "rm -rf $$SCRATCH",
        ]),
        **kwargs
    )

usage:

symlink_files(
    name = "symlink_files",
    from_dir = "/usr/lib/x86_64-linux-gnu/openblas-pthread",
    to_dir = "/usr/lib/x86_64-linux-gnu",
    srcs = [
        "@bookworm//libopenblas0-pthread/amd64",
    ],
)

@blackliner
Copy link
Author

@loosebazooka should we close this then as won't fix, or keep it open as a feature request?

@loosebazooka
Copy link
Member

Yeah probably won't fix. The post install and symlink stories aren't great. But for the purposes of this project I don't see us dealing with them any time soon.

@blackliner blackliner closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants