Skip to content

Commit ad95c6f

Browse files
authored
Merge pull request linuxkit#4085 from deitch/volume-image
additional volume support in building
2 parents e4d4106 + 76f4802 commit ad95c6f

File tree

3 files changed

+125
-11
lines changed

3 files changed

+125
-11
lines changed

docs/yaml.md

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ For private registries or private repositories on a registry credentials provide
1818

1919
## Sections
2020

21-
The configuration file is processed in the order `kernel`, `init`, `onboot`, `onshutdown`,
22-
`services`, `files`, `volumes`. Each section adds files to the root file system. Sections may be omitted.
21+
The configuration file is processed in the order:
22+
23+
1. `kernel`
24+
1. `init`
25+
1. `volumes`
26+
1. `onboot`
27+
1. `onshutdown`
28+
1. `services`
29+
1. `files`
30+
31+
Each section adds files to the root file system. Sections may be omitted.
2332

2433
Each container that is specified is allocated a unique `uid` and `gid` that it may use if it
2534
wishes to run as an isolated user (or user namespace). Anywhere you specify a `uid` or `gid`
@@ -100,8 +109,13 @@ including those in `services`, `onboot` and `onshutdown`. The volumes are create
100109
chosen by linuxkit at build-time. The volumes then can be referenced by other containers and
101110
mounted into them.
102111

103-
Volumes normally are blank directories. If an image is provided, the contents of that image
104-
will be used to populate the volume.
112+
Volumes can be in one of several formats:
113+
114+
* Blank directory: This is the default, and is an empty directory that is created at build-time. It is an overlayfs mount, and can be shared among multiple containers.
115+
* Image laid out as filesystem: The contents of the image are used to populate the volume. Default format when an image is provided.
116+
* Image as OCI v1-layout: The image is used as an [OCI v1-layout](https://github.com/opencontainers/image-spec/blob/main/image-layout.md). Indicated by `format: oci`.
117+
118+
Examples of each are given later in this section.
105119

106120
The `volumes` section can declare a volume to be read-write or read-only. If the volume is read-write,
107121
a volume that is mounted into a container can be mounted read-only or read-write. If the volume is read-only,
@@ -111,7 +125,36 @@ By default, volumes are created read-write, and are mounted read-write.
111125
Volume names **must** be unique, and must contain only lower-case alphanumeric characters, hyphens, and
112126
underscores.
113127

114-
Sample `volumes` section:
128+
#### Samples of `volumes`
129+
130+
##### Empty directory
131+
132+
Yaml showing both read-only and read-write:
133+
134+
```yml
135+
volumes:
136+
- name: dira
137+
readonly: true
138+
- name: dirb
139+
readonly: true
140+
```
141+
142+
Contents:
143+
144+
```sh
145+
$ cd dir && ls -la
146+
drwxr-xr-x 19 root wheel 608 Sep 30 15:03 .
147+
drwxrwxrwt 130 root wheel 4160 Sep 30 15:03 ..
148+
```
149+
150+
In the above example:
151+
152+
* `dira` is empty and is read-only.
153+
* `volb` is empty and is read-write.
154+
155+
##### Image directory
156+
157+
Yaml showing both read-only and read-write:
115158

116159
```yml
117160
volumes:
@@ -120,16 +163,64 @@ volumes:
120163
readonly: true
121164
- name: volb
122165
image: alpine:latest
123-
readonly: false
124-
- name: volc
166+
format: filesystem # optional, as this is the default format
125167
readonly: false
126168
```
127169
128170
In the above example:
129171
130172
* `vola` is populated by the contents of `alpine:latest` and is read-only.
131173
* `volb` is populated by the contents of `alpine:latest` and is read-write.
132-
* `volc` is an empty volume and is read-write.
174+
175+
Contents:
176+
177+
```sh
178+
$ cd dir && ls -la
179+
drwxr-xr-x 19 root wheel 608 Sep 30 15:03 .
180+
drwxrwxrwt 130 root wheel 4160 Sep 30 15:03 ..
181+
drwxr-xr-x 84 root wheel 2688 Sep 6 14:34 bin
182+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 dev
183+
drwxr-xr-x 37 root wheel 1184 Sep 6 14:34 etc
184+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 home
185+
drwxr-xr-x 13 root wheel 416 Sep 6 14:34 lib
186+
drwxr-xr-x 5 root wheel 160 Sep 6 14:34 media
187+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 mnt
188+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 opt
189+
dr-xr-xr-x 2 root wheel 64 Sep 6 14:34 proc
190+
drwx------ 2 root wheel 64 Sep 6 14:34 root
191+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 run
192+
drwxr-xr-x 63 root wheel 2016 Sep 6 14:34 sbin
193+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 srv
194+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 sys
195+
drwxr-xr-x 2 root wheel 64 Sep 6 14:34 tmp
196+
drwxr-xr-x 7 root wheel 224 Sep 6 14:34 usr
197+
drwxr-xr-x 13 root wheel 416 Sep 6 14:34 var
198+
```
199+
200+
##### Image OCI Layout
201+
202+
Yaml showing both read-only and read-write, and both all architectures and a limited subset:
203+
204+
```yml
205+
volumes:
206+
- name: volo
207+
image: alpine:latest
208+
format: oci
209+
readonly: true
210+
- name: volp
211+
image: alpine:latest
212+
readonly: false
213+
format: oci
214+
platforms:
215+
- linux/amd64
216+
```
217+
218+
In the above example:
219+
220+
* `volo` is populated by the contents of `alpine:latest` as an OCI v1-layout for all architectures and is read-only.
221+
* `volb` is populated by the contents of `alpine:latest` as an OCI v1-layout just for linux/amd64 and is read-write.
222+
223+
##### Volumes in `services`
133224

134225
Sample usage of volumes in `services` section:
135226

src/cmd/linuxkit/moby/build/build.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strings"
1717

1818
"github.com/containerd/containerd/reference"
19+
v1 "github.com/google/go-containerregistry/pkg/v1"
1920
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2021

2122
// drop-in 100% compatible replacement and 17% faster than compress/gzip.
@@ -284,8 +285,28 @@ func Build(m moby.Moby, w io.Writer, opts BuildOpts) error {
284285
lowerPath := strings.TrimPrefix(lower, "/") + "/"
285286

286287
// get volume tarball from container
287-
if err := ImageTar(location, vol.ImageRef(), lowerPath, apkTar, resolvconfSymlink, opts); err != nil {
288-
return fmt.Errorf("failed to build volume filesystem tarball from %s: %v", vol.Name, err)
288+
switch {
289+
case vol.ImageRef() == nil || vol.Format == "" || vol.Format == "filesystem":
290+
if err := ImageTar(location, vol.ImageRef(), lowerPath, apkTar, resolvconfSymlink, opts); err != nil {
291+
return fmt.Errorf("failed to build volume filesystem tarball from %s: %v", vol.Name, err)
292+
}
293+
case vol.Format == "oci":
294+
// convert platforms into imagespec platforms
295+
platforms := make([]imagespec.Platform, len(vol.Platforms))
296+
for i, p := range vol.Platforms {
297+
platform, err := v1.ParsePlatform(p)
298+
if err != nil {
299+
return fmt.Errorf("failed to parse platform %s: %v", p, err)
300+
}
301+
platforms[i] = imagespec.Platform{
302+
Architecture: platform.Architecture,
303+
OS: platform.OS,
304+
Variant: platform.Variant,
305+
}
306+
}
307+
if err := ImageOCITar(location, vol.ImageRef(), lowerPath, apkTar, opts, platforms); err != nil {
308+
return fmt.Errorf("failed to build volume OCI v1 layout tarball from %s: %v", vol.Name, err)
309+
}
289310
}
290311

291312
// make upper and merged dirs which will be used for mounting

src/cmd/linuxkit/moby/schema.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ var schema = `
4343
"properties": {
4444
"name": {"type": "string"},
4545
"image": {"type": "string"},
46-
"readonly": {"type": "boolean"}
46+
"readonly": {"type": "boolean"},
47+
"format": {"enum": ["oci","filesystem"]},
48+
"platforms": {"$ref": "#/definitions/strings"}
4749
}
4850
},
4951
"volumes": {

0 commit comments

Comments
 (0)