|
22 | 22 | import subprocess |
23 | 23 | from typing import Final, Sequence |
24 | 24 |
|
| 25 | +from absl import logging |
| 26 | + |
| 27 | +from google3.pyglib import gfile |
| 28 | +import pathlib |
| 29 | + |
| 30 | + |
25 | 31 | LD_BINARY_NAME: Final[str] = "ld-linux-x86-64.so.2" |
26 | 32 | _LD_BINARY_PATH: Final[pathlib.Path] = pathlib.Path("/lib64") / LD_BINARY_NAME |
27 | 33 |
|
@@ -79,3 +85,52 @@ def get_shared_libraries( |
79 | 85 | ) |
80 | 86 |
|
81 | 87 | return _parse_ld_trace_output(result.stdout.decode()) |
| 88 | + |
| 89 | + |
| 90 | +def copy_shared_libraries( |
| 91 | + libraries: Sequence[SharedLibrary], dst_path: pathlib.Path |
| 92 | +) -> None: |
| 93 | + """Copies the shared libraries to the shared directory.""" |
| 94 | + for lib in libraries: |
| 95 | + try: |
| 96 | + logging.info("Copying %s => %s", lib.name, lib.path) |
| 97 | + gfile.Copy(lib.path, dst_path / lib.path.name, overwrite=True, mode=0o755) |
| 98 | + except gfile.GOSError: |
| 99 | + logging.exception("Could not copy %s to %s", lib.path, dst_path) |
| 100 | + raise |
| 101 | + |
| 102 | + |
| 103 | +def patch_binary_rpath_and_interpreter( |
| 104 | + binary_path: os.PathLike[str], |
| 105 | + lib_mount_path: pathlib.Path, |
| 106 | +): |
| 107 | + """Patches the binary rpath and interpreter.""" |
| 108 | + subprocess.run( |
| 109 | + [ |
| 110 | + "patchelf", |
| 111 | + "--set-rpath", |
| 112 | + lib_mount_path.as_posix(), |
| 113 | + "--force-rpath", |
| 114 | + binary_path, |
| 115 | + ], |
| 116 | + check=True, |
| 117 | + ) |
| 118 | + |
| 119 | + subprocess.run( |
| 120 | + [ |
| 121 | + "patchelf", |
| 122 | + "--set-interpreter", |
| 123 | + (lib_mount_path / LD_BINARY_NAME).as_posix(), |
| 124 | + binary_path, |
| 125 | + ], |
| 126 | + check=True, |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def get_library_mount_path(binary_id: str) -> pathlib.Path: |
| 131 | + return pathlib.Path("/tmp") / (binary_id + "_lib") |
| 132 | + |
| 133 | + |
| 134 | +def report_progress(stage: str, is_done: bool = False) -> None: |
| 135 | + """Reports progress of a stage of the snapshotting process.""" |
| 136 | + logging.info("%s%s", stage, "..." if not is_done else "") |
0 commit comments