This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
overlay.go
70 lines (60 loc) · 2.15 KB
/
overlay.go
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
// Copyright 2016 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package overlay
import (
"fmt"
"strings"
"syscall"
"github.com/hashicorp/errwrap"
"github.com/opencontainers/selinux/go-selinux/label"
)
// sanitizer defines a string translator used to escape colon and comma
// characters in the directories names.
var sanitizer = strings.NewReplacer(`:`, `\:`, `,`, `\,`)
// MountCfg contains the needed data to construct the overlay mount syscall.
// The Lower and Upper fields are paths to the filesystems to be merged. The
// Work field should be an empty directory. Dest is where the mount will be
// located. Lbl is an SELinux label.
type MountCfg struct {
Lower,
Upper,
Work,
Dest,
Lbl string
}
// sanitize escapes the colon and comma symbols in order to support the dir
// names with these characters, otherwise they will be treated as separators
// between the directory names.
func sanitize(dir string) string {
return sanitizer.Replace(dir)
}
// Opts returns options for mount system call.
func (cfg *MountCfg) Opts() string {
opts := fmt.Sprintf(
"lowerdir=%s,upperdir=%s,workdir=%s",
sanitize(cfg.Lower), sanitize(cfg.Upper), sanitize(cfg.Work),
)
return label.FormatMountLabel(opts, cfg.Lbl)
}
// Mount mounts the upper and lower directories to the destination directory.
// The MountCfg struct supplies information required to build the mount system
// call.
func Mount(cfg *MountCfg) error {
err := syscall.Mount("overlay", cfg.Dest, "overlay", 0, cfg.Opts())
if err != nil {
const text = "error mounting overlay with options '%s' and dest '%s'"
return errwrap.Wrap(fmt.Errorf(text, cfg.Opts(), cfg.Dest), err)
}
return nil
}