/
common.go
276 lines (229 loc) · 7.53 KB
/
common.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2014 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 common defines values shared by different parts
// of rkt (e.g. stage0 and stage1)
package common
import (
"bufio"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema/types"
)
const (
sharedVolumesDir = "/sharedVolumes"
stage1Dir = "/stage1"
stage2Dir = "/opt/stage2"
AppsInfoDir = "/appsinfo"
EnvLockFd = "RKT_LOCK_FD"
EnvSELinuxContext = "RKT_SELINUX_CONTEXT"
Stage1TreeStoreIDFilename = "stage1TreeStoreID"
AppTreeStoreIDFilename = "treeStoreID"
OverlayPreparedFilename = "overlay-prepared"
PrivateUsersPreparedFilename = "private-users-prepared"
PrepareLock = "prepareLock"
MetadataServicePort = 18112
MetadataServiceRegSock = "/run/rkt/metadata-svc.sock"
APIServiceListenAddr = "localhost:15441"
DefaultLocalConfigDir = "/etc/rkt"
DefaultSystemConfigDir = "/usr/lib/rkt"
)
// Stage1ImagePath returns the path where the stage1 app image (unpacked ACI) is rooted,
// (i.e. where its contents are extracted during stage0).
func Stage1ImagePath(root string) string {
return filepath.Join(root, stage1Dir)
}
// Stage1RootfsPath returns the path to the stage1 rootfs
func Stage1RootfsPath(root string) string {
return filepath.Join(Stage1ImagePath(root), aci.RootfsDir)
}
// Stage1ManifestPath returns the path to the stage1's manifest file inside the expanded ACI.
func Stage1ManifestPath(root string) string {
return filepath.Join(Stage1ImagePath(root), aci.ManifestFile)
}
// PodManifestPath returns the path in root to the Pod Manifest
func PodManifestPath(root string) string {
return filepath.Join(root, "pod")
}
// AppsPath returns the path where the apps within a pod live.
func AppsPath(root string) string {
return filepath.Join(Stage1RootfsPath(root), stage2Dir)
}
// AppPath returns the path to an app's rootfs.
func AppPath(root string, appName types.ACName) string {
return filepath.Join(AppsPath(root), appName.String())
}
// AppRootfsPath returns the path to an app's rootfs.
func AppRootfsPath(root string, appName types.ACName) string {
return filepath.Join(AppPath(root, appName), aci.RootfsDir)
}
// RelAppPath returns the path of an app relative to the stage1 chroot.
func RelAppPath(appName types.ACName) string {
return filepath.Join(stage2Dir, appName.String())
}
// RelAppRootfsPath returns the path of an app's rootfs relative to the stage1 chroot.
func RelAppRootfsPath(appName types.ACName) string {
return filepath.Join(RelAppPath(appName), aci.RootfsDir)
}
// ImageManifestPath returns the path to the app's manifest file of a pod.
func ImageManifestPath(root string, appName types.ACName) string {
return filepath.Join(AppPath(root, appName), aci.ManifestFile)
}
// AppsInfoPath returns the path to the appsinfo directory of a pod.
func AppsInfoPath(root string) string {
return filepath.Join(root, AppsInfoDir)
}
// AppInfoPath returns the path to the app's appsinfo directory of a pod.
func AppInfoPath(root string, appName types.ACName) string {
return filepath.Join(AppsInfoPath(root), appName.String())
}
// AppTreeStoreIDPath returns the path to the app's treeStoreID file of a pod.
func AppTreeStoreIDPath(root string, appName types.ACName) string {
return filepath.Join(AppInfoPath(root, appName), AppTreeStoreIDFilename)
}
// AppImageManifestPath returns the path to the app's ImageManifest file
func AppImageManifestPath(root string, appName types.ACName) string {
return filepath.Join(AppInfoPath(root, appName), aci.ManifestFile)
}
// SharedVolumesPath returns the path to the shared (empty) volumes of a pod.
func SharedVolumesPath(root string) string {
return filepath.Join(root, sharedVolumesDir)
}
// MetadataServicePublicURL returns the public URL used to host the metadata service
func MetadataServicePublicURL(ip net.IP, token string) string {
return fmt.Sprintf("http://%v:%v/%v", ip, MetadataServicePort, token)
}
func GetRktLockFD() (int, error) {
if v := os.Getenv(EnvLockFd); v != "" {
fd, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return -1, err
}
return int(fd), nil
}
return -1, fmt.Errorf("%v env var is not set", EnvLockFd)
}
// SupportsOverlay returns whether the system supports overlay filesystem
func SupportsOverlay() bool {
exec.Command("modprobe", "overlay").Run()
f, err := os.Open("/proc/filesystems")
if err != nil {
fmt.Println("error opening /proc/filesystems")
return false
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if s.Text() == "nodev\toverlay" {
return true
}
}
return false
}
// SupportsUserNS returns whether the kernel has CONFIG_USER_NS set
func SupportsUserNS() bool {
if _, err := os.Stat("/proc/self/uid_map"); err == nil {
return true
}
return false
}
// NetList implements the flag.Value interface to allow specification of --net with and without values
// Example: --net="all,net1:k1=v1;k2=v2,net2:l1=w1"
type NetList struct {
mapping map[string]string
}
func (l *NetList) String() string {
return strings.Join(l.Strings(), ",")
}
func (l *NetList) Set(value string) error {
if l.mapping == nil {
l.mapping = make(map[string]string)
}
for _, s := range strings.Split(value, ",") {
netArgsPair := strings.Split(s, ":")
netName := netArgsPair[0]
if netName == "" {
return fmt.Errorf("netname must not be empty")
}
if _, duplicate := l.mapping[netName]; duplicate {
return fmt.Errorf("found duplicate netname %q", netName)
}
switch {
case len(netArgsPair) == 1:
l.mapping[netName] = ""
case len(netArgsPair) == 2:
if netName == "all" ||
netName == "host" {
return fmt.Errorf("arguments are not supported by special netname %q", netName)
}
l.mapping[netName] = netArgsPair[1]
case len(netArgsPair) > 2:
return fmt.Errorf("network %q provided with invalid arguments: %v", netName, netArgsPair[1:])
default:
return fmt.Errorf("unexpected case when processing network %q", s)
}
}
return nil
}
func (l *NetList) Type() string {
return "netList"
}
func (l *NetList) Strings() []string {
if len(l.mapping) == 0 {
return []string{"default"}
}
var list []string
for k, v := range l.mapping {
if v == "" {
list = append(list, k)
} else {
list = append(list, fmt.Sprintf("%s:%s", k, v))
}
}
return list
}
func (l *NetList) StringsOnlyNames() []string {
var list []string
for k, _ := range l.mapping {
list = append(list, k)
}
return list
}
// Check if host networking has been requested
func (l *NetList) Host() bool {
return l.Specific("host")
}
// Check if 'none' (loopback only) networking has been requested
func (l *NetList) None() bool {
return l.Specific("none")
}
// Check if the container needs to be put in a separate network namespace
func (l *NetList) Contained() bool {
return !l.Host() && len(l.mapping) > 0
}
func (l *NetList) Specific(net string) bool {
_, exists := l.mapping[net]
return exists
}
func (l *NetList) SpecificArgs(net string) string {
return l.mapping[net]
}
func (l *NetList) All() bool {
return l.Specific("all")
}