forked from apptainer/singularity
/
underlay.go
187 lines (167 loc) · 5.15 KB
/
underlay.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
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
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package underlay
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/internal/pkg/util/fs/layout"
"github.com/sylabs/singularity/internal/pkg/util/fs/mount"
)
const underlayDir = "/underlay"
type pathLen struct {
path string
len uint16
}
// Underlay layer manager
type Underlay struct {
session *layout.Session
}
// New creates and returns an overlay layer manager
func New() *Underlay {
return &Underlay{}
}
// Add adds required directory in session layout
func (u *Underlay) Add(session *layout.Session, system *mount.System) error {
u.session = session
if err := u.session.AddDir(underlayDir); err != nil {
return err
}
return system.RunBeforeTag(mount.PreLayerTag, u.createUnderlay)
}
func (u *Underlay) createUnderlay(system *mount.System) error {
points := system.Points.GetByTag(mount.RootfsTag)
if len(points) <= 0 {
return fmt.Errorf("no root fs image found")
}
return u.createLayer(points[0].Destination, system)
}
// createLayer creates underlay layer based on content of root filesystem
func (u *Underlay) createLayer(rootFsPath string, system *mount.System) error {
st := new(syscall.Stat_t)
points := system.Points
createdPath := make([]pathLen, 0)
sessionDir := u.session.Path()
for _, tag := range mount.GetTagList() {
for _, point := range points.GetByTag(tag) {
flags, _ := mount.ConvertOptions(point.Options)
if flags&syscall.MS_REMOUNT != 0 {
continue
}
if strings.HasPrefix(point.Destination, sessionDir) {
continue
}
if err := syscall.Stat(rootFsPath+point.Destination, st); err == nil {
continue
}
if err := syscall.Stat(point.Source, st); err != nil {
sylog.Warningf("skipping mount of %s: %s", point.Source, err)
continue
}
dst := underlayDir + point.Destination
if _, err := u.session.GetPath(dst); err == nil {
continue
}
switch st.Mode & syscall.S_IFMT {
case syscall.S_IFDIR:
if err := u.session.AddDir(dst); err != nil {
return err
}
default:
if err := u.session.AddFile(dst, nil); err != nil {
return err
}
}
createdPath = append(createdPath, pathLen{path: point.Destination, len: uint16(strings.Count(point.Destination, "/"))})
}
}
sort.SliceStable(createdPath, func(i, j int) bool { return createdPath[i].len < createdPath[j].len })
for _, pl := range createdPath {
splitted := strings.Split(filepath.Dir(pl.path), string(os.PathSeparator))
l := len(splitted)
p := ""
for i := 1; i < l; i++ {
s := splitted[i : i+1][0]
p += "/" + s
if s != "" {
if _, err := u.session.GetPath(p); err != nil {
if err := u.session.AddDir(p); err != nil {
return err
}
}
if err := u.duplicateDir(p, system, pl.path); err != nil {
return err
}
}
}
}
if err := u.duplicateDir("/", system, ""); err != nil {
return err
}
flags := uintptr(syscall.MS_BIND | syscall.MS_REC | syscall.MS_RDONLY)
path, _ := u.session.GetPath(underlayDir)
err := system.Points.AddBind(mount.LayerTag, path, u.session.FinalPath(), flags)
if err != nil {
return err
}
err = system.Points.AddRemount(mount.LayerTag, u.session.FinalPath(), flags)
if err != nil {
return err
}
return u.session.Update()
}
func (u *Underlay) duplicateDir(dir string, system *mount.System, existingPath string) error {
binds := 0
path := filepath.Clean(u.session.RootFsPath() + dir)
files, err := ioutil.ReadDir(path)
if err != nil {
// directory doesn't exists, nothing to duplicate
return nil
}
for _, file := range files {
dst := filepath.Join(underlayDir+dir, file.Name())
src := filepath.Join(path, file.Name())
// no error means entry is already created
if _, err := u.session.GetPath(dst); err == nil {
continue
}
if file.IsDir() {
if err := u.session.AddDir(dst); err != nil {
return fmt.Errorf("can't add directory %s to underlay: %s", dst, err)
}
dst, _ = u.session.GetPath(dst)
if err := system.Points.AddBind(mount.PreLayerTag, src, dst, syscall.MS_BIND); err != nil {
return fmt.Errorf("can't add bind mount point: %s", err)
}
binds++
} else if file.Mode()&os.ModeSymlink != 0 {
tgt, err := os.Readlink(src)
if err != nil {
return fmt.Errorf("can't read symlink information for %s: %s", src, err)
}
if err := u.session.AddSymlink(dst, tgt); err != nil {
return fmt.Errorf("can't add symlink: %s", err)
}
} else {
if err := u.session.AddFile(dst, nil); err != nil {
return fmt.Errorf("can't add directory %s to underlay: %s", dst, err)
}
dst, _ = u.session.GetPath(dst)
if err := system.Points.AddBind(mount.PreLayerTag, src, dst, syscall.MS_BIND); err != nil {
return fmt.Errorf("can't add bind mount point: %s", err)
}
binds++
}
}
if binds > 50 && existingPath != "" {
sylog.Warningf("underlay of %s required more than 50 (%d) bind mounts", existingPath, binds)
}
return nil
}