Debian Codegraph @

docs/estargz.md

raw
1 # eStargz: Standard-Compatible Extension to Container Image Layers for Lazy Pulling
2
3 This doc describes the extension to gzip layers of container images (`application/vnd.oci.image.layer.v1.tar+gzip` of [OCI Image Specification](https://github.com/opencontainers/image-spec/) and `application/vnd.docker.image.rootfs.diff.tar.gzip` of [Docker Image Specification](https://github.com/moby/moby/blob/master/image/spec/v1.2.md)) for *lazy pulling*.
4 The extension is called *eStargz*.
5
6 eStargz is a *backward-compatible extension* which means that images can be pushed to the extension-agnostic registry and can run on extension-agnostic runtimes.
7
8 This extension is based on stargz (stands for *seekable tar.gz*) proposed by [Google CRFS](https://github.com/google/crfs) project (initially [discussed in Go community](https://github.com/golang/go/issues/30829)).
9 eStargz extends stargz for chunk-level verification and runtime performance optimization.
10
11 Notational convention follows [OCI Image Specification](https://github.com/opencontainers/image-spec/blob/v1.0.1/spec.md#notational-conventions).
12
13 ## Overview
14
15 Lazy pulling is a technique of pulling container images aiming at the faster cold start.
16 This allows a container to startup without waiting for the entire image layer contents to be locally available.
17 Instead, necessary files (or chunks for large files) in the layer are fetched *on-demand* during running the container.
18
19 For achieving this, runtimes need to fetch and extract each file in a layer independently.
20 However, layer without eStargz extension doesn't allow this because of the following reasons,
21
22 1. The entire layer blob needs to be extracted even for getting a single file entry.
23 2. Digests aren't provided for each file so it cannot be verified independently.
24
25 eStargz solves these issues and enables lazy pulling.
26
27 Additionally, it supports prefetching of files.
28 This can be used to mitigate runtime performance drawbacks caused by the on-demand fetching of each file.
29
30 This extension is a backward-compatible so the eStargz-formatted image can be pushed to the registry and can run even on eStargz-agnostic runtimes.
31
32 ## The structure
33
34 ![The structure of eStargz](/docs/images/estargz-structure.png)
35
36 eStargz is a gzip-compressed tar archive of files and a metadata component called *TOC* (described in the later section).
37 In an eStargz-formatted blob, each non-empty regular file and each metadata component MUST be separately compressed as gzip.
38 This structure is inherited from [stargz](https://github.com/google/crfs).
39
40 Therefore, the gzip headers MUST locate at the following locations.
41
42 - The top of the blob
43 - The top of the payload of each non-empty regular file tar entry except *TOC*
44 - The top of *TOC* tar header
45 - The top of *footer* (described in the later section)
46
47 Large regular files in an eStargz blob MAY be chunked into several smaller gzip members.
48 Each chunked member is called *chunk* in this doc.
49
50 Therefore, gzip headers MAY locate at the following locations.
51
52 - Arbitrary location within the payload of non-empty regular file entry
53
54 An eStargz-formatted blob is the concatenation of these gzip members, which is a still valid gzip blob.
55
56 ## TOC, TOCEntries and Footer
57
58 ### TOC and TOCEntries
59
60 eStargz contains a regular file called *TOC* which records metadata (e.g. name, file type, owners, offset etc) of all file entries in eStargz, except TOC itself.
61 Container runtimes MAY use TOC to mount the container's filesystem without downloading the entire layer contents.
62
63 TOC MUST be a JSON file contained as the last tar entry and MUST be named `stargz.index.json`.
64
65 The following fields contain the primary properties that constitute a TOC.
66
67 - **`version`** *int*
68
69 This REQUIRED property contains the version of the TOC. This value MUST be `1`.
70
71 - **`entries`** *array of objects*
72
73 This property MUST contain an array of *TOCEntry* of all tar entries and chunks in the blob, except `stargz.index.json`.
74
75 *TOCEntry* consists of metadata of a file or chunk in eStargz.
76 If metadata in a TOCEntry of a file differs from the corresponding tar entry, TOCEntry SHOULD be respected.
77
78 The following fields contain the primary properties that constitute a TOCEntry.
79 Properties other than `chunkDigest` are inherited from [stargz](https://github.com/google/crfs).
80
81 - **`name`** *string*
82
83 This REQUIRED property contains the name of the tar entry.
84 This MUST be the complete path stored in the tar file.
85
86 - **`type`** *string*
87
88 This REQUIRED property contains the type of tar entry.
89 This MUST be either of the following.
90 - `dir`: directory
91 - `reg`: regular file
92 - `symlink`: symbolic link
93 - `hardlink`: hard link
94 - `char`: character device
95 - `block`: block device
96 - `fifo`: fifo
97 - `chunk`: a chunk of regular file data
98 As described in the above section, a regular file can be divided into several chunks.
99 TOCEntry MUST be created for each chunk.
100 TOCEntry of the first chunk of that file MUST be typed as `reg`.
101 TOCEntry of each chunk after 2nd MUST be typed as `chunk`.
102 `chunk` TOCEntry MUST set *offset*, *chunkOffset* and *chunkSize* properties.
103
104 - **`size`** *uint64*
105
106 This OPTIONAL property contains the uncompressed size of the regular file.
107 Non-empty `reg` file MUST set this property.
108
109 - **`modtime`** *string*
110
111 This OPTIONAL property contains the modification time of the tar entry.
112 Empty means zero or unknown.
113 Otherwise, the value is in UTC RFC3339 format.
114
115 - **`linkName`** *string*
116
117 This OPTIONAL property contains the link target.
118 `symlink` and `hardlink` MUST set this property.
119
120 - **`mode`** *int64*
121
122 This REQUIRED property contains the permission and mode bits.
123
124 - **`uid`** *uint*
125
126 This REQUIRED property contains the user ID of the owner of this file.
127
128 - **`gid`** *uint*
129
130 This REQUIRED property contains the group ID of the owner of this file.
131
132 - **`userName`** *string*
133
134 This OPTIONAL property contains the username of the owner.
135
136 - **`groupName`** *string*
137
138 This OPTIONAL property contains the groupname of the owner.
139
140 - **`devMajor`** *int*
141
142 This OPTIONAL property contains the major device number of device files.
143 `char` and `block` files MUST set this property.
144
145 - **`devMinor`** *int*
146
147 This OPTIONAL property contains the minor device number of device files.
148 `char` and `block` files MUST set this property.
149
150 - **`xattrs`** *string-bytes map*
151
152 This OPTIONAL property contains the extended attribute for the tar entry.
153
154 - **`digest`** *string*
155
156 This OPTIONAL property contains the digest of the regular file contents.
157
158 - **`offset`** *int64*
159
160 This OPTIONAL property contains the offset of the gzip header of the regular file or chunk in the blob.
161 TOCEntries of non-empty `reg` and `chunk` MUST set this property.
162
163 - **`chunkOffset`** *int64*
164
165 This OPTIONAL property contains the offset of this chunk in the decompressed regular file payload.
166 TOCEntries of `chunk` type MUST set this property.
167
168 - **`chunkSize`** *int64*
169
170 This OPTIONAL property contains the decompressed size of this chunk.
171 The last `chunk` in a `reg` file or `reg` file that isn't chunked MUST set this property to zero.
172 Other `reg` and `chunk` MUST set this property.
173
174 - **`chunkDigest`** *string*
175
176 This OPTIONAL property contains a digest of this chunk.
177 TOCEntries of non-empty `reg` and `chunk` MUST set this property.
178 This MAY be used for verifying the data of the chunk.
179
180 - **`innerOffset`** *int64*
181
182 This OPTIONAL property indicates the uncompressed offset of the "reg" or "chunk" entry payload in a stream starts from `offset` field.
183
184 #### Details about `innerOffset`
185
186 `innerOffset` enables to put multiple "reg" or "chunk" payloads in one gzip stream starts from `offset`.
187 This field allows the following structure.
188
189 ![The structure of eStargz with innerOffset](/docs/images/estargz-inneroffset.png)
190
191 Use case of this field is `--estargz-min-chunk-size` flag of `ctr-remote`.
192 The value of this flag is the minimal number of bytes of data must be written in one gzip stream.
193 If it's > 0, multiple files and chunks can be written into one gzip stream.
194 Smaller number of gzip header and smaller size of the result blob can be expected.
195
196 ### Footer
197
198 At the end of the blob, a *footer* MUST be appended.
199 This MUST be an empty gzip member whose [Extra field](https://tools.ietf.org/html/rfc1952#section-2.3.1.1) contains the offset of TOC in the blob.
200 The footer MUST be the following 51 bytes (1 byte = 8 bits in gzip).
201
202 ```
203 - 10 bytes gzip header
204 - 2 bytes XLEN (length of Extra field) = 26 (4 bytes header + 16 hex digits + len("STARGZ"))
205 - 2 bytes Extra: SI1 = 'S', SI2 = 'G'
206 - 2 bytes Extra: LEN = 22 (16 hex digits + len("STARGZ"))
207 - 22 bytes Extra: subfield = fmt.Sprintf("%016xSTARGZ", offsetOfTOC)
208 - 5 bytes flate header: BFINAL = 1(last block), BTYPE = 0(non-compressed block), LEN = 0
209 - 8 bytes gzip footer
210 (End of eStargz)
211 ```
212
213 Runtimes MAY first read and parse the footer to get the offset of TOC.
214
215 Each file's metadata is recorded in the TOC so runtimes don't need to extract other parts of the archive as long as it only uses file metadata.
216 If runtime needs to get a regular file's content, it can get the size and offset of that content from the TOC and extract that range without scanning the entire blob.
217 By combining this with HTTP Range Request supported by [OCI Distribution Spec](https://github.com/opencontainers/distribution-spec/blob/ef28f81727c3b5e98ab941ae050098ea664c0960/detail.md#fetch-blob-part), runtimes can selectively download file entries from the registry.
218
219 ### Notes on compatibility with stargz
220
221 eStargz is designed aiming to compatibility with gzip layers.
222 For achieving this, eStargz's footer structure is incompatible with [stargz's one](https://github.com/google/crfs/blob/71d77da419c90be7b05d12e59945ac7a8c94a543/stargz/stargz.go#L36-L49).
223 eStargz adds SI1, SI2 and LEN fields to the footer to make it compliant to [Extra field definition in RFC1952](https://tools.ietf.org/html/rfc1952#section-2.3.1.1).
224 TOC, TOCEntry and the position of gzip headers are still compatible with stargz.
225
226 ## Prioritized Files and Landmark Files
227
228 ![Prioritized files and landmark files](/docs/images/estargz-landmark.png)
229
230 Lazy pulling can cause runtime performance overhead by on-demand fetching of each file.
231 eStargz mitigates this by supporting prefetching of important files called *prioritized files*.
232
233 eStargz encodes the information about prioritized files to the *order* of file entries with some *landmark* file entries.
234
235 File entries in eStargz are grouped into the following groups,
236
237 - A. *prioritized files*
238 - B. non *prioritized files*
239
240 If no files are belonging to A, a landmark file *no-prefetch landmark* MUST be contained in the archive.
241
242 If one or more files are belonging to A, eStargz MUST consist of two separated areas corresponding to these groups and a landmark file *prefetch landmark* MUST be contained at the boundary between these two areas.
243
244 The Landmark file MUST be a regular file entry with 4 bits contents 0xf in eStargz.
245 It MUST be recorded to TOC as a TOCEntry. Prefetch landmark MUST be named `.prefetch.landmark`. No-prefetch landmark MUST be named `.no.prefetch.landmark`.
246
247 ### Example use-case of prioritized files: workload-based image optimization in Stargz Snapshotter
248
249 Stargz Snapshotter makes use of eStargz's prioritized files for *workload-based* optimization to mitigate the overhead of reading files.
250 The *workload* of the image is the runtime configuration defined in the Dockerfile, including entrypoint command, environment variables and user.
251
252 Stargz snapshotter provides an image converter command `ctr-remote images optimize` to create optimized eStargz images.
253 When converting the image, this command runs the specified workload in a sandboxed environment and profiles all file accesses.
254 This command treats all accessed files as prioritized files.
255 Then it constructs eStargz by
256
257 - putting prioritized files from the top of the archive, sorting them by the accessed order,
258 - putting *prefetch landmark* file entry at the end of this range, and
259 - putting all other files (non-prioritized files) after the prefetch landmark.
260
261 Before running the container, stargz snapshotter prefetches and pre-caches the range where prioritized files are contained, by a single HTTP Range Request supported by the registry.
262 This can increase the cache hit rate for the specified workload and can mitigate runtime overheads.
263
264 ## Content Verification in eStargz
265
266 The goal of the content verification in eStargz is to ensure the downloaded metadata and contents of all files are the expected ones, based on the calculated digests.
267 The verification of other components in the image including image manifests is out-of-scope of eStargz.
268 On the verification step of an eStargz layer, we assume that the manifest that references this eStargz layer is already verified (using digest tag, etc).
269
270 ![the overview of the verification](/docs/images/estargz-verification.png)
271
272 A non-eStargz layer can be verified by recalculating the digest and comparing it with the one written in the layer descriptor referencing that layer in the verified manifest.
273 However, an eStargz layer is *lazily* pulled from the registry in file (or chunk if that file is large) granularity so each one needs to be independently verified every time fetched.
274
275 The following describes how the verification of eStargz is done using the verified manifest.
276
277 eStargz consists of the following components to be verified:
278
279 - TOC (a set of metadata of all files contained in the layer)
280 - chunks of contents of each regular file
281
282 TOC contains metadata (name, type, mode, etc.) of all files and chunks in the blob.
283 On mounting eStargz, filesystem fetches the TOC from the registry.
284 For making the TOC verifiable using the verified manifest, we define an annotation `containerd.io/snapshot/stargz/toc.digest`.
285 The value of this annotation is the digest of the TOC and this MUST be contained in the descriptor that references this eStargz layer.
286 Using this annotation, filesystem can verify the TOC by recalculating the digest and comparing it to the annotation value.
287
288 Each file's metadata is encoded to a TOCEntry in the TOC.
289 TOCEntry is created also for each chunk of regular files.
290 For making the contents of each file and chunk verifiable using the verified manifest, TOCEntry has a property *chunkDigest*.
291 *chunkDigest* contains the digest of the content of the `reg` or `chunk` entry.
292 As mentioned above, the TOC is verifiable using the special annotation.
293 Using *chunkDigest* fields written in the verified TOC, each file and chunk can be independently verified by recalculating the digest and comparing it to the property.
294
295 As the conclusion, eStargz MUST contain the following metadata:
296
297 - `containerd.io/snapshot/stargz/toc.digest` annotation in the descriptor that references eStargz layer: The value is the digest of the TOC.
298 - *chunkDigest* properties of non-empty `reg` or `chunk` TOCEntry: The value is the digest of the contents of the file or chunk.
299
300 ### Example usecase: Content verification in Stargz Snapshotter
301
302 Stargz Snapshotter verifies eStargz layers leveraging the above metadata.
303 As mentioned above, the verification of other image components including the manifests is out-of-scope of the snapshotter.
304 When this snapshotter mounts an eStargz layer, the manifest that references this layer must be verified in advance and the TOC digest annotation written in the verified manifest must be passed down to this snapshotter.
305
306 On mounting a layer, stargz snapshotter fetches the TOC from the registry.
307 Then it verifies the TOC by recalculating the digest and comparing it with the one written in the manifest.
308 After the TOC is verified, the snapshotter mounts this layer using the metadata recorded in the TOC.
309
310 During runtime of the container, this snapshotter fetches chunks of regular file contents lazily.
311 Before providing a chunk to the filesystem user, snapshotter recalculates the digest and checks it matches the one recorded in the corresponding TOCEntry.
312
313 ## eStargz image with an external TOC (OPTIONAL)
314
315 This OPTIONAL feature allows separating TOC into another image called *TOC image*.
316 This type of eStargz is the same as the normal eStargz but doesn't contain TOC JSON file (`stargz.index.json`) in the layer blob and has a special footer.
317 This feature enables creating a smaller eStargz blob by avoiding including TOC JSON file in that blob.
318
319 Footer has the following structure:
320
321 ```
322 // The footer is an empty gzip stream with no compression and an Extra header.
323 //
324 // 46 comes from:
325 //
326 // 10 bytes gzip header
327 // 2 bytes XLEN (length of Extra field) = 21 (4 bytes header + len("STARGZEXTERNALTOC"))
328 // 2 bytes Extra: SI1 = 'S', SI2 = 'G'
329 // 2 bytes Extra: LEN = 17 (len("STARGZEXTERNALTOC"))
330 // 17 bytes Extra: subfield = "STARGZEXTERNALTOC"
331 // 5 bytes flate header
332 // 8 bytes gzip footer
333 // (End of the eStargz blob)
334 ```
335
336 TOC image is an OCI image containing TOC.
337 Each layer contains a TOC JSON file (`stargz.index.json`) in the root directory.
338
339 Layer descriptors in the manifest must contain an annotation `containerd.io/snapshot/stargz/layer.digest`.
340 The value of this annotation is the digest of the eStargz layer blob corresponding to that TOC.
341
342 The following is an example layer descriptor in the TOC image.
343 This layer (`sha256:64dedefd539280a5578c8b94bae6f7b4ebdbd12cb7a7df0770c4887a53d9af70`) contains the TOC JSON file (`stargz.index.json`) in the root directory and can be used for eStargz layer blob that has the digest `sha256:5da5601c1f2024c07f580c11b2eccf490cd499473883a113c376d64b9b10558f`.
344
345 ```json
346 {
347 "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
348 "digest": "sha256:64dedefd539280a5578c8b94bae6f7b4ebdbd12cb7a7df0770c4887a53d9af70",
349 "size": 154425,
350 "annotations": {
351 "containerd.io/snapshot/stargz/layer.digest": "sha256:5da5601c1f2024c07f580c11b2eccf490cd499473883a113c376d64b9b10558f"
352 }
353 }
354 ```
355
356 ### Example usecase: lazy pulling with Stargz Snapshotter
357
358 Stargz Snapshotter supports eStargz with external TOC.
359 If an eStargz blob's footer indicates that it requires the TOC image, stargz snapshotter also pulls it from the registry.
360
361 Stargz snapshotter assumes the TOC image has the reference name same as the eStargz with `-esgztoc` suffix.
362 For example, if an eStargz image is named `ghcr.io/stargz-containers/ubuntu:22.04-esgz`, stargz snapshotter acquires the TOC image from `ghcr.io/stargz-containers/ubuntu:22.04-esgz-esgztoc`.
363 Note that future versions of stargz snapshotter will support more ways to search the TOC image (e.g. allowing custom suffix, using OCI Reference Type, etc.)
364
365 Once stargz snapshotter acquires TOC image, it tries to find the TOC corresponding to the mounting eStargz blob, by looking `containerd.io/snapshot/stargz/layer.digest` annotations.
366 As describe in the above, the acquired TOC JSON is validated using `containerd.io/snapshot/stargz/toc.digest` annotation.
367
368 ## Example of TOC
369
370 Here is an example TOC JSON:
371
372 ```json
373 {
374 "version": 1,
375 "entries": [
376 {
377 "name": "bin/",
378 "type": "dir",
379 "modtime": "2019-08-20T10:30:43Z",
380 "mode": 16877,
381 "NumLink": 0
382 },
383 {
384 "name": "bin/busybox",
385 "type": "reg",
386 "size": 833104,
387 "modtime": "2019-06-12T17:52:45Z",
388 "mode": 33261,
389 "offset": 126,
390 "NumLink": 0,
391 "digest": "sha256:8b7c559b8cccca0d30d01bc4b5dc944766208a53d18a03aa8afe97252207521f",
392 "chunkDigest": "sha256:8b7c559b8cccca0d30d01bc4b5dc944766208a53d18a03aa8afe97252207521f"
393 },
394 {
395 "name": "lib/",
396 "type": "dir",
397 "modtime": "2019-08-20T10:30:43Z",
398 "mode": 16877,
399 "NumLink": 0
400 },
401 {
402 "name": "lib/ld-musl-x86_64.so.1",
403 "type": "reg",
404 "size": 580144,
405 "modtime": "2019-08-07T07:15:30Z",
406 "mode": 33261,
407 "offset": 512427,
408 "NumLink": 0,
409 "digest": "sha256:45c6ee3bd1862697eab8058ec0e462f5a760927331c709d7d233da8ffee40e9e",
410 "chunkDigest": "sha256:45c6ee3bd1862697eab8058ec0e462f5a760927331c709d7d233da8ffee40e9e"
411 },
412 {
413 "name": ".prefetch.landmark",
414 "type": "reg",
415 "size": 1,
416 "offset": 886633,
417 "NumLink": 0,
418 "digest": "sha256:dc0e9c3658a1a3ed1ec94274d8b19925c93e1abb7ddba294923ad9bde30f8cb8",
419 "chunkDigest": "sha256:dc0e9c3658a1a3ed1ec94274d8b19925c93e1abb7ddba294923ad9bde30f8cb8"
420 },
421 ... (omit) ...
422 ```