|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euxo pipefail |
| 4 | + |
| 5 | +function get_container_id() { |
| 6 | + local cpuset_output |
| 7 | + cpuset_output=$(head -1 /proc/self/cpuset) |
| 8 | + basename "${cpuset_output}" |
| 9 | +} |
| 10 | + |
| 11 | +function get_container_root_dir() { |
| 12 | + local container_id="${1}" |
| 13 | + |
| 14 | + docker.orig inspect --format '{{.GraphDriver.Data.MergedDir}}' "${container_id}" |
| 15 | +} |
| 16 | + |
| 17 | +function set_container_volumes() { |
| 18 | + local container_id="${1}" |
| 19 | + |
| 20 | + local docker_output |
| 21 | + docker_output=$( |
| 22 | + docker.orig inspect \ |
| 23 | + --format '{{range .Mounts}}{{printf "%s:%s\n" .Source .Destination}}{{end}}' \ |
| 24 | + "${container_id}" |
| 25 | + ) |
| 26 | + |
| 27 | + readarray -t container_volumes <<<"${docker_output}" |
| 28 | +} |
| 29 | + |
| 30 | +container_id="$(get_container_id)" |
| 31 | +container_root_dir="$(get_container_root_dir "${container_id}")" |
| 32 | +set_container_volumes "${container_id}" |
| 33 | +readonly container_id container_root_dir container_volumes |
| 34 | + |
| 35 | +function fix_volume_arg() { |
| 36 | + local source="${volume_arg%%":"*}" |
| 37 | + local destination="${volume_arg#*":"}" |
| 38 | + |
| 39 | + # Fix relative paths as docker needs absolute paths |
| 40 | + # if [[ "${source}" == "./"* ]]; then |
| 41 | + # source="${PWD}/${source#"./"}" |
| 42 | + # fi |
| 43 | + |
| 44 | + if [[ "${source}" == "/"* ]]; then |
| 45 | + for container_volume in "${container_volumes[@]}"; do |
| 46 | + local container_volume_source="${container_volume%%":"*}" |
| 47 | + local container_volume_destination="${container_volume#*":"}" |
| 48 | + |
| 49 | + if [[ "${source}" == "${container_volume_destination}" ]]; then |
| 50 | + volume_arg="${container_volume_source}:${destination}" |
| 51 | + return |
| 52 | + fi |
| 53 | + |
| 54 | + if [[ "${source}" == "${container_volume_destination}/"* ]]; then |
| 55 | + volume_arg="${container_volume_source}${source#"${container_volume_destination}"}:${destination}" |
| 56 | + return |
| 57 | + fi |
| 58 | + |
| 59 | + # Check if there is some container_volume mounted within the source |
| 60 | + # Example: |
| 61 | + # First container mounts --volume "${PWD}/testfile:/home/rootless/testfile" |
| 62 | + # Second container mounts --volume /home/rootless:/wd |
| 63 | + # Then the second container should also mount --volume "${PWD}/testfile:/wd/testfile" (extra_arg) |
| 64 | + if [[ "${container_volume_destination}" == "${source}/"* ]]; then |
| 65 | + # Convert /home/rootless/testfile (container_volume_destination) to /wd/testfile (destination path) |
| 66 | + extra_args+=(--volume "${container_volume_source}:${destination}/${container_volume_destination#"${source}/"}") |
| 67 | + fi |
| 68 | + done |
| 69 | + |
| 70 | + volume_arg="${container_root_dir}${source}:${destination}" |
| 71 | + return |
| 72 | + fi |
| 73 | + |
| 74 | + volume_arg="${source}:${destination}" |
| 75 | +} |
| 76 | + |
| 77 | +original_args=("$@") |
| 78 | +fixed_args=() |
| 79 | + |
| 80 | +extra_args=() |
| 81 | +fix_next_arg=false |
| 82 | +for arg in "${original_args[@]}"; do |
| 83 | + if [[ "${fix_next_arg}" == true ]]; then |
| 84 | + fix_next_arg=false |
| 85 | + volume_arg="${arg}" |
| 86 | + fix_volume_arg |
| 87 | + arg="${volume_arg}" |
| 88 | + elif [[ "${arg}" == "-v" || "${arg}" == "--volume" ]]; then |
| 89 | + fix_next_arg=true |
| 90 | + elif [[ "${arg}" == "-v="* || "${arg}" == "--volume="* ]]; then |
| 91 | + dot_dot_volume="${arg%%"="*}" |
| 92 | + volume_arg="${arg#*"="}" |
| 93 | + fix_volume_arg |
| 94 | + arg="${dot_dot_volume}=${volume_arg}" |
| 95 | + fi |
| 96 | + |
| 97 | + fixed_args+=("${arg}" "${extra_args[@]}") |
| 98 | + extra_args=() |
| 99 | +done |
| 100 | + |
| 101 | +exec docker.orig "${fixed_args[@]}" |
0 commit comments