Skip to content

Commit f1cb76d

Browse files
committed
[4/4] Upgrade GitLab to Amazon Linux 2023 (#6160)
Change resolve_container_path.py to work with new instance use of cgroup v2
1 parent a7a9a03 commit f1cb76d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

scripts/resolve_container_path.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import csv
12
import logging
23
import os
34
import sys
@@ -26,24 +27,31 @@
2627

2728
def resolve_container_path(container_path):
2829
container_path = os.path.realpath(container_path)
29-
proc_cgroup = '/proc/self/cgroup'
30+
mountinfo = '/proc/self/mountinfo'
3031
try:
31-
with open(proc_cgroup) as f:
32-
log.info('Found %s', proc_cgroup)
33-
# Entries in /proc/self/cgroup look like this (note the nesting):
34-
# 11:name=systemd:/docker/82c1bd2…23b5bcf/docker/6547bce…60ca5a7
35-
prefix, container_id = next(f).strip().split(':')[2].split('/')[-2:]
32+
with open(mountinfo) as f:
33+
log.info('Found %s', mountinfo)
34+
# Entries in /proc/self/mountinfo look like this:
35+
# 752 744 259:2 /docker/containers/dc61d93…ID…/hosts …
36+
prefix = '/docker/containers'
37+
contents = csv.reader(f, delimiter=' ')
38+
for line in contents:
39+
path = line[3]
40+
if path.startswith(prefix):
41+
log.info('Extracting the container ID from %s', path)
42+
parts = path.rsplit('/', maxsplit=2)[:-1]
43+
assert len(parts) == 2 and parts[0] == prefix, parts
44+
container_id = parts[1]
3645
except FileNotFoundError:
3746
log.info('Did not find %s', proc_cgroup)
3847
else:
39-
if prefix == 'docker':
40-
api = docker.client.from_env().api
41-
for mount in api.inspect_container(container_id)['Mounts']:
42-
if container_path.startswith(mount['Destination']):
43-
tail = os.path.relpath(container_path, mount['Destination'])
44-
host_path = os.path.normpath(os.path.join(mount['Source'], tail))
45-
log.info('Resolved %s to %s', container_path, host_path)
46-
return host_path
48+
api = docker.client.from_env().api
49+
for mount in api.inspect_container(container_id)['Mounts']:
50+
if container_path.startswith(mount['Destination']):
51+
tail = os.path.relpath(container_path, mount['Destination'])
52+
host_path = os.path.normpath(os.path.join(mount['Source'], tail))
53+
log.info('Resolved %s to %s', container_path, host_path)
54+
return host_path
4755
log.error('Failed to resolve container path %s', container_path)
4856
return None
4957

0 commit comments

Comments
 (0)