-
Notifications
You must be signed in to change notification settings - Fork 6
/
instance.go
93 lines (74 loc) · 2.22 KB
/
instance.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
/*
file created by Junlin Chen in 2022
*/
package fs
import (
"context"
"github.com/containerd/containerd/log"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
iofs "io/fs"
"os"
"syscall"
"time"
)
type ImageManager interface {
GetPathByStack(stack int64) string
GetPathBySerial(stack int64) string
LookUpFile(stack int64, filename string) ReceivedFile
LogTrace(stack int64, filename string, access, complete time.Time)
}
// Instance should be created using
type Instance struct {
ctx context.Context
Root ReceivedFile
rootInode *StarlightFsNode
stack int64
mountPoint string
manager ImageManager
server *fuse.Server
}
func (fi *Instance) GetMountPoint() string { return fi.mountPoint }
func (fi *Instance) GetServer() *fuse.Server { return fi.server }
// Teardown unmounts the file system and close the logging file if there is one writing
// should you need this function, please consider using Manager.Teardown instead.
func (fi *Instance) Teardown() error {
return fi.GetServer().Unmount()
}
func (fi *Instance) Serve() {
fi.server.Serve()
}
func NewInstance(ctx context.Context, m ImageManager, root ReceivedFile, stack int64, dir string, options *fs.Options, debug bool) (fi *Instance, err error) {
fi = &Instance{
ctx: ctx,
manager: m,
stack: stack,
}
fi.rootInode = &StarlightFsNode{fs.Inode{}, root, fi}
fi.mountPoint = dir
one := time.Second
options.EntryTimeout = &one
options.AttrTimeout = &one
options.AllowOther = true
options.Name = "starlightfs"
options.Options = []string{"suid", "ro"}
options.DirectMount = true
options.NullPermissions = true
options.RememberInodes = false
if debug {
options.Debug = true
}
if _, err = os.Stat(dir); err != nil && err.(*iofs.PathError).Err == syscall.ENOTCONN {
// if the directory exists, it means that the snapshot is already created
// or there is a problem unmounting the snapshot
// Transport endpoint is not connected
err = syscall.Unmount(dir, UnmountFlag)
log.G(ctx).
WithError(err).
WithField("dir", dir).
Warn("fs: cleanup disconnected mountpoint")
}
rawFs := fs.NewNodeFS(fi.rootInode, options)
fi.server, err = fuse.NewServer(rawFs, dir, &options.MountOptions)
return fi, err
}