Skip to content

Commit 186a22e

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 17b20c0 commit 186a22e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

scripts/resolve_container_path.py

Lines changed: 24 additions & 16 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,26 +27,33 @@
2627

2728
def resolve_container_path(container_path):
2829
container_path = os.path.realpath(container_path)
30+
mountinfo = '/proc/self/mountinfo'
2931
try:
30-
proc_cgroup = '/proc/self/cgroup'
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:
37-
log.info('Did not find %s', proc_cgroup)
46+
log.info('Did not find %s', mountinfo)
3847
pass
3948
else:
40-
log.info('cgroup prefix is %s', prefix)
41-
if prefix == 'docker':
42-
api = docker.client.from_env().api
43-
for mount in api.inspect_container(container_id)['Mounts']:
44-
if container_path.startswith(mount['Destination']):
45-
tail = os.path.relpath(container_path, mount['Destination'])
46-
host_path = os.path.normpath(os.path.join(mount['Source'], tail))
47-
log.info('Resolving %s to %s', container_path, host_path)
48-
return host_path
49+
log.info('cgroup v2 prefix is %s', prefix)
50+
api = docker.client.from_env().api
51+
for mount in api.inspect_container(container_id)['Mounts']:
52+
if container_path.startswith(mount['Destination']):
53+
tail = os.path.relpath(container_path, mount['Destination'])
54+
host_path = os.path.normpath(os.path.join(mount['Source'], tail))
55+
log.info('Resolving %s to %s', container_path, host_path)
56+
return host_path
4957
log.error('Cannot resolve container path: %s', container_path)
5058
return None
5159

0 commit comments

Comments
 (0)