|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# PROMPT: Create a bash script with the following: |
| 4 | +# If a CLI arg is not passed, fail with the mesage "Error: Pass the full path of the file/executable you want to include." |
| 5 | +# |
| 6 | +# If the CLI arg is neither a file nor a directory, fail with the message "Error: File not found." |
| 7 | +# |
| 8 | +# If the /tmp/distroless directory does not exist, create it. Then |
| 9 | +# cd into /tmp/distroless and create the POSIX standard Linux directories, using Ubuntu 22.04 as the exact model to copy. |
| 10 | +# set chmod 0777 for tmp var/{cache,log,tmp} |
| 11 | +# set chmod 0750 for root |
| 12 | +# Create ln -s for /bin /sbin from /usr/bin |
| 13 | +# Create ln -s for /lib /lib64 from /usr/lib |
| 14 | +# |
| 15 | +# Then if the CLI arg is a directory, use the following prompt: |
| 16 | +# If the SOURCE directory (via the CLI arg) is "/etc/php/8.3", |
| 17 | +# Copy this to /tmp/distroless/ while maintaining the relative directory path: |
| 18 | +# Example: /etc/php/8.3 -> /tmp/distroless/etc/php/8.3/ |
| 19 | +# |
| 20 | +# If it is not a directory: |
| 21 | +# cp -v "$1" "/tmp/distroless$1" |
| 22 | +# Test if the file is an executable (via bash's -x). |
| 23 | +# If it is an executable: |
| 24 | +# Then run `ldd "$1" | awk '{print $1}'` and use `cp` to copy the files to /usr/lib |
| 25 | +# Ignore any errors from the `cp` command, as they are likely false-positives. |
| 26 | +# |
| 27 | +# Include this exact prompt as a source code comment at the beginning of the Bash script. |
| 28 | +# Do not bother with inline comments. |
| 29 | +if [ -x "$1" ]; then |
| 30 | + cd /usr/lib/x86_64-linux-gnu |
| 31 | + cp -vf $(ldd "$1" | awk '{print $1}') /tmp/distroless/usr/lib/ |
| 32 | +fi |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +if [ -z "$1" ]; then |
| 37 | + echo "Error: Pass the full path of the file/executable you want to include." |
| 38 | + exit 1; |
| 39 | +fi |
| 40 | + |
| 41 | +if [ ! -f "$1" ] && [ ! -d "$1" ]; then |
| 42 | + echo "Error: File not found." |
| 43 | + exit 2; |
| 44 | +fi |
| 45 | + |
| 46 | +if [ ! -d /tmp/distroless ]; then |
| 47 | + mkdir -p /tmp/distroless |
| 48 | + cd /tmp/distroless |
| 49 | + mkdir -p dev etc home/user media mnt opt proc root run sys tmp usr var/{cache,lib,log,spool,tmp} |
| 50 | + mkdir -p /tmp/distroless/usr/{bin,lib,local/bin} |
| 51 | + |
| 52 | + chmod 0777 tmp var/{cache,log,tmp} |
| 53 | + chmod 0750 root |
| 54 | + |
| 55 | + ln -s usr/bin bin |
| 56 | + ln -s usr/bin sbin |
| 57 | + ln -s usr/lib lib |
| 58 | + ln -s usr/lib lib64 |
| 59 | +fi |
| 60 | + |
| 61 | +if [ -d "$1" ]; then |
| 62 | + |
| 63 | + cp -avf --parents "$1" /tmp/distroless |
| 64 | + |
| 65 | + for each in $(find "$1" -name \*.so\* -type f); do |
| 66 | + cd /usr/lib/x86_64-linux-gnu |
| 67 | + cp -v $(ldd $each | awk '{print $1}') /tmp/distroless/usr/lib |
| 68 | + done |
| 69 | + |
| 70 | + exit 0 |
| 71 | +fi |
| 72 | + |
| 73 | +cp -v "$1" "/tmp/distroless$1" |
| 74 | +if [ -x "$1" ]; then |
| 75 | + cd /usr/lib/x86_64-linux-gnu |
| 76 | + cp -vf $(ldd "$1" | awk '{print $1}') /tmp/distroless/usr/lib/ |
| 77 | +fi |
0 commit comments