@@ -10,7 +10,6 @@ import (
10
10
"strconv"
11
11
12
12
securejoin "github.com/cyphar/filepath-securejoin"
13
- "github.com/moby/sys/mountinfo"
14
13
"golang.org/x/sys/unix"
15
14
16
15
"github.com/opencontainers/runc/libcontainer/cgroups/manager"
@@ -28,60 +27,30 @@ const (
28
27
29
28
var idRegex = regexp .MustCompile (`^[\w+-\.]+$` )
30
29
31
- // TmpfsRoot is an option func to mount LinuxFactory.Root to tmpfs.
32
- func TmpfsRoot (l * LinuxFactory ) error {
33
- mounted , err := mountinfo .Mounted (l .Root )
34
- if err != nil {
35
- return err
36
- }
37
- if ! mounted {
38
- if err := mount ("tmpfs" , l .Root , "" , "tmpfs" , 0 , "" ); err != nil {
39
- return err
40
- }
41
- }
42
- return nil
43
- }
44
-
45
- // New returns a linux based container factory based in the root directory and
46
- // configures the factory with the provided option funcs.
47
- func New (root string , options ... func (* LinuxFactory ) error ) (Factory , error ) {
48
- if root != "" {
49
- if err := os .MkdirAll (root , 0o700 ); err != nil {
50
- return nil , err
51
- }
52
- }
53
- l := & LinuxFactory {
54
- Root : root ,
55
- }
56
-
57
- for _ , opt := range options {
58
- if opt == nil {
59
- continue
60
- }
61
- if err := opt (l ); err != nil {
62
- return nil , err
63
- }
64
- }
65
- return l , nil
66
- }
67
-
68
- // LinuxFactory implements the default factory interface for linux based systems.
69
- type LinuxFactory struct {
70
- // Root directory for the factory to store state.
71
- Root string
72
- }
73
-
74
- func (l * LinuxFactory ) Create (id string , config * configs.Config ) (Container , error ) {
75
- if l .Root == "" {
30
+ // Create creates a new container with the given id inside a given state
31
+ // directory (root), and returns a Container object.
32
+ //
33
+ // The root is a state directory which many containers can share. It can be
34
+ // used later to get the list of containers, or to get information about a
35
+ // particular container (see Load).
36
+ //
37
+ // The id must not be empty and consist of only the following characters:
38
+ // ASCII letters, digits, underscore, plus, minus, period. The id must be
39
+ // unique and non-existent for the given root path.
40
+ func Create (root , id string , config * configs.Config ) (Container , error ) {
41
+ if root == "" {
76
42
return nil , errors .New ("root not set" )
77
43
}
78
- if err := l . validateID (id ); err != nil {
44
+ if err := validateID (id ); err != nil {
79
45
return nil , err
80
46
}
81
47
if err := validate .Validate (config ); err != nil {
82
48
return nil , err
83
49
}
84
- containerRoot , err := securejoin .SecureJoin (l .Root , id )
50
+ if err := os .MkdirAll (root , 0o700 ); err != nil {
51
+ return nil , err
52
+ }
53
+ containerRoot , err := securejoin .SecureJoin (root , id )
85
54
if err != nil {
86
55
return nil , err
87
56
}
@@ -125,7 +94,8 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
125
94
return nil , errors .New ("container's cgroup unexpectedly frozen" )
126
95
}
127
96
128
- if err := os .MkdirAll (containerRoot , 0o711 ); err != nil {
97
+ // Parent directory is already created above, so Mkdir is enough.
98
+ if err := os .Mkdir (containerRoot , 0o711 ); err != nil {
129
99
return nil , err
130
100
}
131
101
c := & linuxContainer {
@@ -139,19 +109,22 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
139
109
return c , nil
140
110
}
141
111
142
- func (l * LinuxFactory ) Load (id string ) (Container , error ) {
143
- if l .Root == "" {
112
+ // Load takes a path to the state directory (root) and an id of an existing
113
+ // container, and returns a Container object reconstructed from the saved
114
+ // state. This presents a read only view of the container.
115
+ func Load (root , id string ) (Container , error ) {
116
+ if root == "" {
144
117
return nil , errors .New ("root not set" )
145
118
}
146
119
// when load, we need to check id is valid or not.
147
- if err := l . validateID (id ); err != nil {
120
+ if err := validateID (id ); err != nil {
148
121
return nil , err
149
122
}
150
- containerRoot , err := securejoin .SecureJoin (l . Root , id )
123
+ containerRoot , err := securejoin .SecureJoin (root , id )
151
124
if err != nil {
152
125
return nil , err
153
126
}
154
- state , err := l . loadState (containerRoot )
127
+ state , err := loadState (containerRoot )
155
128
if err != nil {
156
129
return nil , err
157
130
}
@@ -181,13 +154,10 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
181
154
return c , nil
182
155
}
183
156
184
- func (l * LinuxFactory ) Type () string {
185
- return "libcontainer"
186
- }
187
-
188
- // StartInitialization loads a container by opening the pipe fd from the parent to read the configuration and state
189
- // This is a low level implementation detail of the reexec and should not be consumed externally
190
- func (l * LinuxFactory ) StartInitialization () (err error ) {
157
+ // StartInitialization loads a container by opening the pipe fd from the parent
158
+ // to read the configuration and state. This is a low level implementation
159
+ // detail of the reexec and should not be consumed externally.
160
+ func StartInitialization () (err error ) {
191
161
// Get the INITPIPE.
192
162
envInitPipe := os .Getenv ("_LIBCONTAINER_INITPIPE" )
193
163
pipefd , err := strconv .Atoi (envInitPipe )
@@ -269,7 +239,7 @@ func (l *LinuxFactory) StartInitialization() (err error) {
269
239
return i .Init ()
270
240
}
271
241
272
- func ( l * LinuxFactory ) loadState (root string ) (* State , error ) {
242
+ func loadState (root string ) (* State , error ) {
273
243
stateFilePath , err := securejoin .SecureJoin (root , stateFilename )
274
244
if err != nil {
275
245
return nil , err
@@ -289,7 +259,7 @@ func (l *LinuxFactory) loadState(root string) (*State, error) {
289
259
return state , nil
290
260
}
291
261
292
- func ( l * LinuxFactory ) validateID (id string ) error {
262
+ func validateID (id string ) error {
293
263
if ! idRegex .MatchString (id ) || string (os .PathSeparator )+ id != utils .CleanPath (string (os .PathSeparator )+ id ) {
294
264
return ErrInvalidID
295
265
}
0 commit comments