@@ -69,50 +69,58 @@ function run_docker() {
6969 fi
7070}
7171
72- # Sets container info by parsing /proc/self/mountinfo:
73- # - container_id: the current container id
74- # - container_root_on_host: the root directory of the container on the host filesystem
75- function set_container_info() {
76- local line upperdir docker_data_dir
77-
78- {
79- # The root overlay mount with upperdir is always on the first line
80- if read -r line; then
81- if [[ " ${line} " =~ ^[0-9]+[[:space:]][0-9]+[[:space:]][0-9]+:[0-9]+[[:space:]]/[[:space:]]/[[:space:]].* upperdir= (/[^,]+) ]]; then
82- upperdir=" ${BASH_REMATCH[1]} "
83- fi
72+ # Gets the current/parent container id on the host.
73+ function set_container_id() {
74+ local result
75+
76+ local mount_info_lines=()
77+ readarray -t mount_info_lines < /proc/self/mountinfo
78+ for line in " ${mount_info_lines[@]} " ; do
79+ if [[ " ${line} " =~ /([a-z0-9]{12,128})/resolv.conf" " ]]; then
80+ result=" ${BASH_REMATCH[1]} "
8481 fi
82+ done
83+ unset mount_info_lines
8584
86- # Find the resolv.conf mount to get container info
87- while read -r line; do
88- if [[ " ${line} " =~ [[:space:]](/[^[:space:]]+)/containers/([a-z0-9]{12,128})/resolv\. conf[[:space:]] ]]; then
89- docker_data_dir=" ${BASH_REMATCH[1]} "
90- readonly container_id=" ${BASH_REMATCH[2]} "
91- break
92- fi
93- done
94- } < /proc/self/mountinfo
95-
96- if [[ -z " ${container_id} " ]]; then
85+ # Sanity check
86+ if [[ " ${result} " =~ ^[a-z0-9]{12,128}$ ]]; then
87+ readonly container_id=" ${result} "
88+ else
9789 error " Could not get parent container id"
9890 fi
91+ }
9992
100- if [[ -n " ${mock_container_root_on_host} " ]]; then
101- readonly container_root_on_host=" ${mock_container_root_on_host} "
102- return
103- fi
104-
105- if [[ " ${upperdir} " == * " /overlay2/" * " /diff" ]]; then
106- readonly container_root_on_host=" ${upperdir% " /diff" } /merged"
107- elif [[ " ${upperdir} " == * " /io.containerd.snapshotter.v1.overlayfs/" * ]]; then
108- if [[ -z " ${docker_data_dir} " ]]; then
109- error " Could not determine docker data directory from mountinfo"
93+ # Gets the root directory of the current/parent container on the host
94+ # filesystem.
95+ function set_container_root_on_host() {
96+ local result
97+
98+ if [[ -z " ${mock_container_root_on_host} " ]]; then
99+ local docker_info
100+ docker_info=" $(
101+ " ${docker_path} " info --format ' {{.Driver}}|{{.DockerRootDir}}'
102+ ) "
103+
104+ local storage_driver=" ${docker_info%% " |" * } "
105+ local docker_data_dir=" ${docker_info#* " |" } "
106+
107+ if [[ " ${storage_driver} " == " overlay2" ]]; then
108+ result=" $(
109+ " ${docker_path} " inspect --format ' {{.GraphDriver.Data.MergedDir}}' " ${container_id} "
110+ ) "
111+ elif [[ " ${storage_driver} " == " overlayfs" ]]; then
112+ result=" ${docker_data_dir} /rootfs/overlayfs/${container_id} "
113+ else
114+ error " Unsupported storage driver: ${storage_driver} "
110115 fi
111- readonly container_root_on_host=" ${docker_data_dir} /rootfs/overlayfs/${container_id} "
116+ else
117+ result=" ${mock_container_root_on_host} "
112118 fi
113119
114120 # Sanity check
115- if [[ ! " ${container_root_on_host:- } " =~ ^(/[^/]+)+$ ]]; then
121+ if [[ " ${result} " =~ ^(/[^/]+)+$ ]]; then
122+ readonly container_root_on_host=" ${result} "
123+ else
116124 error " Could not get parent container root on host"
117125 fi
118126}
@@ -171,7 +179,8 @@ function fix_volume_arg() {
171179
172180 # Fetch data only once and if needed
173181 if [[ " ${container_data_fetched} " == false ]]; then
174- set_container_info
182+ set_container_id
183+ set_container_root_on_host
175184 set_parent_container_mounts
176185 container_data_fetched=true
177186 fi
0 commit comments