-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-extract.py
executable file
·233 lines (192 loc) · 9.54 KB
/
docker-extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
# ========================================================================== #
# #
# docker-extract - extract docker image to directory. #
# #
# Partially based on https://github.com/larsks/undocker and contains #
# many major fixes for image spec v1.2. #
# #
# Copyright (C) 2019-2023 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import os
import tarfile
import shutil
import json
import contextlib
import argparse
import logging
# =====
_logger = logging.getLogger("docker-extract")
# =====
def _get_image_manifest(image_tar: tarfile.TarFile) -> dict:
"""
Get the manifest of the Docker image.
Args:
image_tar (tarfile.TarFile): The Docker image tarfile.
Returns:
dict: The image manifest.
"""
_logger.debug(":: Reading image manifest")
with contextlib.closing(image_tar.extractfile("manifest.json")) as manifest_file:
manifest = json.load(manifest_file) # type: ignore
assert len(manifest) == 1, "Can't process manifest.json:\n" + json.dumps(manifest, indent=" ")
return manifest[0]
def _get_image_layers(image_tar: tarfile.TarFile) -> list[str]:
"""
Get the layers of the Docker image.
Args:
image_tar (tarfile.TarFile): The Docker image tarfile.
Returns:
list[str]: The list of layer paths.
"""
layers = _get_image_manifest(image_tar)["Layers"]
for layer_path in layers:
_logger.debug(":: Layer: %s", layer_path)
return layers
def _extract_rootfs(image_tar: tarfile.TarFile, layers: list[str], root_path: str) -> None:
"""
Extract the root filesystem of the Docker image.
Args:
image_tar (tarfile.TarFile): The Docker image tarfile.
layers (list[str]): The list of layer paths.
root_path (str): The output directory for the root filesystem.
"""
# https://github.com/moby/moby/blob/master/image/spec/v1.2.md
if os.getuid() != 0:
raise RuntimeError("You must be a root")
if os.path.exists(root_path):
raise RuntimeError(f"{root_path}: is already exists")
os.mkdir(root_path)
for layer_path in layers:
_logger.debug(":: Extracting layer: %s", layer_path)
with contextlib.closing(image_tar.extractfile(layer_path)) as layer_file:
with _TarFile(fileobj=layer_file, errorlevel=1) as layer_tar:
members = layer_tar.getmembers()
for member in members:
# https://github.com/moby/moby/blob/8e610b2b55bfd1bfa9436ab110d311f5e8a74dcb/pkg/archive/whiteouts.go
# https://github.com/moby/moby/blob/8e610b2b55bfd1bfa9436ab110d311f5e8a74dcb/pkg/archive/diff.go
# https://github.com/sylabs/singularity/blob/8a0e3008b29db85d1e23677d98978cafba1fdbe7/src/docker-extract.c#L21
member_path = os.path.join(root_path, member.path) # type: ignore
assert not member_path.startswith(".wh."), member_path
if member_path.endswith("/.wh..wh..opq"):
whiteout = os.path.dirname(member_path)
_logger.debug(":: Removing opaque: %s", whiteout)
try:
shutil.rmtree(whiteout)
except FileNotFoundError:
pass
elif "/.wh." in member_path:
whiteout = member_path.replace("/.wh.", "/")
if os.path.isdir(whiteout) and not os.path.islink(whiteout):
_logger.debug(":: Removing whiteout D: %s", whiteout)
shutil.rmtree(whiteout)
else:
_logger.debug(":: Removing whiteout F: %s", whiteout)
os.unlink(whiteout)
layer_tar.extractall(
path=root_path,
members=[member for member in members if "/.wh." not in member.path], # type: ignore
numeric_owner=True,
)
class _TarFile(tarfile.TarFile):
"""
Custom TarFile class that overrides some methods for handling file extraction.
"""
def makefile(self, tarinfo: tarfile.TarInfo, targetpath: str) -> None:
if os.path.lexists(targetpath) and not os.path.isfile(targetpath):
os.unlink(targetpath)
super().makefile(tarinfo, targetpath)
def makefifo(self, tarinfo: tarfile.TarInfo, targetpath: str) -> None:
self.__remove(targetpath)
super().makefifo(tarinfo, targetpath)
def makedev(self, tarinfo: tarfile.TarInfo, targetpath: str) -> None:
self.__remove(targetpath)
super().makedev(tarinfo, targetpath)
def makelink(self, tarinfo: tarfile.TarInfo, targetpath: str) -> None:
self.__remove(targetpath)
super().makelink(tarinfo, targetpath)
def __remove(self, targetpath: str) -> None:
if os.path.lexists(targetpath):
if os.path.isdir(targetpath) and not os.path.islink(targetpath):
shutil.rmtree(targetpath)
else:
os.unlink(targetpath)
# =====
def _hook_set_hostname(root_path: str, hostname: str) -> None:
"""
Set the hostname in the root filesystem.
Args:
root_path (str): The path to the root filesystem.
hostname (str): The hostname to set.
"""
path = os.path.join(root_path, "etc/hostname")
_logger.info(":: Setting up /etc/hostname: %s ...", hostname)
with open(path, "w") as file:
file.write(hostname)
def _hook_set_resolv_symlink(root_path: str, target_path: str) -> None:
"""
Create a symlink from /etc/resolv.conf to the specified path.
Args:
root_path (str): The path to the root filesystem.
target_path (str): The target path for the symlink.
"""
_logger.info(":: Setting up symlink /etc/resolv.conf -> %s ...", target_path)
path = os.path.join(root_path, "etc/resolv.conf")
try:
os.unlink(path)
except FileNotFoundError:
pass
os.symlink(target_path, path)
# =====
def main() -> None:
"""
Main function for extracting a Docker image to a directory.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--list-layers", action="store_true", help="List layers in an image")
parser.add_argument("--list-tags", action="store_true", help="List tags contained in archive")
parser.add_argument("--debug", action="store_const", const=logging.DEBUG, dest="log_level")
parser.add_argument("--remove-root", action="store_true", help="Remove destination directory before exporting")
parser.add_argument("--root", default="rootfs", help="Output directory (defaults to 'rootfs')")
parser.add_argument("--set-hostname", default="", help="Set /etc/hostname")
parser.add_argument("--set-resolv-symlink", default="", help="Symlink /etc/resolv.conf to specified path")
parser.add_argument("input")
parser.set_defaults(log_level=logging.INFO)
options = parser.parse_args()
logging.basicConfig(level=options.log_level, format="%(message)s")
with tarfile.open(options.input) as image_tar:
with contextlib.closing(image_tar.extractfile("repositories")) as repos_file:
repos = json.load(repos_file) # type: ignore
if options.list_tags:
print("\n".join(_get_image_manifest(image_tar)["RepoTags"]))
elif options.list_layers:
print("\n".join(_get_image_layers(image_tar)))
else:
if options.remove_root:
if os.path.exists(options.root):
_logger.info(":: Removing an old rootfs %r ...", options.root)
shutil.rmtree(options.root)
_logger.info(":: Extracting rootfs to %r ...", options.root)
_extract_rootfs(image_tar, _get_image_layers(image_tar), options.root)
if options.set_hostname:
_hook_set_hostname(options.root, options.set_hostname)
if options.set_resolv_symlink:
_hook_set_resolv_symlink(options.root, options.set_resolv_symlink)
_logger.info(":: Success!")
# =====
if __name__ == "__main__":
main()