Skip to content

Commit 67406b3

Browse files
committed
overlay: add an optional label of upperdir location of each snapshot
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent ba70277 commit 67406b3

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

snapshots/overlay/overlay.go

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ import (
3737
"github.com/sirupsen/logrus"
3838
)
3939

40+
// upperdirKey is a key of an optional lablel to each snapshot.
41+
// This optional label of a snapshot contains the location of "upperdir" where
42+
// the change set between this snapshot and its parent is stored.
43+
const upperdirKey = "containerd.io/snapshot/overlay.upperdir"
44+
4045
// SnapshotterConfig is used to configure the overlay snapshotter instance
4146
type SnapshotterConfig struct {
42-
asyncRemove bool
47+
asyncRemove bool
48+
upperdirLabel bool
4349
}
4450

4551
// Opt is an option to configure the overlay snapshotter
@@ -54,12 +60,22 @@ func AsynchronousRemove(config *SnapshotterConfig) error {
5460
return nil
5561
}
5662

63+
// WithUpperdirLabel adds as an optional label
64+
// "containerd.io/snapshot/overlay.upperdir". This stores the location
65+
// of the upperdir that contains the changeset between the labelled
66+
// snapshot and its parent.
67+
func WithUpperdirLabel(config *SnapshotterConfig) error {
68+
config.upperdirLabel = true
69+
return nil
70+
}
71+
5772
type snapshotter struct {
58-
root string
59-
ms *storage.MetaStore
60-
asyncRemove bool
61-
indexOff bool
62-
userxattr bool // whether to enable "userxattr" mount option
73+
root string
74+
ms *storage.MetaStore
75+
asyncRemove bool
76+
upperdirLabel bool
77+
indexOff bool
78+
userxattr bool // whether to enable "userxattr" mount option
6379
}
6480

6581
// NewSnapshotter returns a Snapshotter which uses overlayfs. The overlayfs
@@ -105,11 +121,12 @@ func NewSnapshotter(root string, opts ...Opt) (snapshots.Snapshotter, error) {
105121
}
106122

107123
return &snapshotter{
108-
root: root,
109-
ms: ms,
110-
asyncRemove: config.asyncRemove,
111-
indexOff: indexOff,
112-
userxattr: userxattr,
124+
root: root,
125+
ms: ms,
126+
asyncRemove: config.asyncRemove,
127+
upperdirLabel: config.upperdirLabel,
128+
indexOff: indexOff,
129+
userxattr: userxattr,
113130
}, nil
114131
}
115132

@@ -124,11 +141,18 @@ func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, err
124141
return snapshots.Info{}, err
125142
}
126143
defer t.Rollback()
127-
_, info, _, err := storage.GetInfo(ctx, key)
144+
id, info, _, err := storage.GetInfo(ctx, key)
128145
if err != nil {
129146
return snapshots.Info{}, err
130147
}
131148

149+
if o.upperdirLabel {
150+
if info.Labels == nil {
151+
info.Labels = make(map[string]string)
152+
}
153+
info.Labels[upperdirKey] = o.upperPath(id)
154+
}
155+
132156
return info, nil
133157
}
134158

@@ -148,6 +172,17 @@ func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
148172
return snapshots.Info{}, err
149173
}
150174

175+
if o.upperdirLabel {
176+
id, _, _, err := storage.GetInfo(ctx, info.Name)
177+
if err != nil {
178+
return snapshots.Info{}, err
179+
}
180+
if info.Labels == nil {
181+
info.Labels = make(map[string]string)
182+
}
183+
info.Labels[upperdirKey] = o.upperPath(id)
184+
}
185+
151186
return info, nil
152187
}
153188

@@ -292,6 +327,19 @@ func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...str
292327
return err
293328
}
294329
defer t.Rollback()
330+
if o.upperdirLabel {
331+
return storage.WalkInfo(ctx, func(ctx context.Context, info snapshots.Info) error {
332+
id, _, _, err := storage.GetInfo(ctx, info.Name)
333+
if err != nil {
334+
return err
335+
}
336+
if info.Labels == nil {
337+
info.Labels = make(map[string]string)
338+
}
339+
info.Labels[upperdirKey] = o.upperPath(id)
340+
return fn(ctx, info)
341+
}, fs...)
342+
}
295343
return storage.WalkInfo(ctx, fn, fs...)
296344
}
297345

snapshots/overlay/plugin/plugin.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
// Config represents configuration for the overlay plugin.
3030
type Config struct {
3131
// Root directory for the plugin
32-
RootPath string `toml:"root_path"`
32+
RootPath string `toml:"root_path"`
33+
UpperdirLabel bool `toml:"upperdir_label"`
3334
}
3435

3536
func init() {
@@ -50,8 +51,13 @@ func init() {
5051
root = config.RootPath
5152
}
5253

54+
var oOpts []overlay.Opt
55+
if config.UpperdirLabel {
56+
oOpts = append(oOpts, overlay.WithUpperdirLabel)
57+
}
58+
5359
ic.Meta.Exports["root"] = root
54-
return overlay.NewSnapshotter(root, overlay.AsynchronousRemove)
60+
return overlay.NewSnapshotter(root, append(oOpts, overlay.AsynchronousRemove)...)
5561
},
5662
})
5763
}

0 commit comments

Comments
 (0)