Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit d36f2ac

Browse files
committed
Remove userns sidecar process
Move the network setup back into the standard init even for user namespaces now that mounts are fully supported and working. Signed-off-by: Michael Crosby <[email protected]>
1 parent 339edce commit d36f2ac

10 files changed

+25
-195
lines changed

error.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,3 @@ type Error interface {
5757
// Returns the error code for this error.
5858
Code() ErrorCode
5959
}
60-
61-
type initError struct {
62-
Message string `json:"message,omitempty"`
63-
}
64-
65-
func (i initError) Error() string {
66-
return i.Message
67-
}

generic_error.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func newGenericError(err error, c ErrorCode) Error {
2828
return &genericError{
2929
Timestamp: time.Now(),
3030
Err: err,
31+
Message: err.Error(),
3132
ECode: c,
3233
Stack: stacktrace.Capture(1),
3334
}
@@ -41,19 +42,21 @@ func newSystemError(err error) Error {
4142
Timestamp: time.Now(),
4243
Err: err,
4344
ECode: SystemError,
45+
Message: err.Error(),
4446
Stack: stacktrace.Capture(1),
4547
}
4648
}
4749

4850
type genericError struct {
4951
Timestamp time.Time
5052
ECode ErrorCode
51-
Err error
53+
Err error `json:"-"`
54+
Message string
5255
Stack stacktrace.Stacktrace
5356
}
5457

5558
func (e *genericError) Error() string {
56-
return fmt.Sprintf("[%d] %s: %s", e.ECode, e.ECode, e.Err)
59+
return fmt.Sprintf("[%d] %s: %s", e.ECode, e.ECode, e.Message)
5760
}
5861

5962
func (e *genericError) Code() ErrorCode {

linux_container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func (c *linuxContainer) newInitProcess(p *Process, cmd *exec.Cmd, parentPipe, c
147147
if cmd.SysProcAttr.Credential == nil {
148148
cmd.SysProcAttr.Credential = &syscall.Credential{}
149149
}
150-
t = "_LIBCONTAINER_INITTYPE=userns"
151150
}
152151
cmd.Env = append(cmd.Env, t)
153152
cmd.SysProcAttr.Cloneflags = cloneFlags

linux_factory.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ func (l *LinuxFactory) StartInitialization(pipefd uintptr) (err error) {
166166
// ensure that any data sent from the parent is consumed so it doesn't
167167
// receive ECONNRESET when the child writes to the pipe.
168168
ioutil.ReadAll(pipe)
169-
if err := json.NewEncoder(pipe).Encode(initError{
170-
Message: err.Error(),
171-
}); err != nil {
169+
if err := json.NewEncoder(pipe).Encode(newSystemError(err)); err != nil {
172170
panic(err)
173171
}
174172
}

linux_init.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import (
2121
type initType string
2222

2323
const (
24-
initSetns initType = "setns"
25-
initStandard initType = "standard"
26-
initUserns initType = "userns"
27-
initUsernsSetup initType = "userns_setup"
24+
initSetns initType = "setns"
25+
initStandard initType = "standard"
2826
)
2927

3028
type pid struct {
@@ -67,14 +65,6 @@ func newContainerInit(t initType, pipe *os.File) (initer, error) {
6765
return &linuxSetnsInit{
6866
config: config,
6967
}, nil
70-
case initUserns:
71-
return &linuxUsernsInit{
72-
config: config,
73-
}, nil
74-
case initUsernsSetup:
75-
return &linuxUsernsSideCar{
76-
config: config,
77-
}, nil
7868
case initStandard:
7969
return &linuxStandardInit{
8070
config: config,

linux_process.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ package libcontainer
44

55
import (
66
"encoding/json"
7-
"fmt"
87
"io"
98
"os"
109
"os/exec"
1110
"syscall"
1211

13-
log "github.com/Sirupsen/logrus"
1412
"github.com/docker/libcontainer/cgroups"
1513
"github.com/docker/libcontainer/system"
1614
)
@@ -145,28 +143,12 @@ func (p *initProcess) start() error {
145143
if err := p.createNetworkInterfaces(); err != nil {
146144
return newSystemError(err)
147145
}
148-
// Start the setup process to setup the init process
149-
if p.cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWUSER != 0 {
150-
parent, err := p.newUsernsSetupProcess()
151-
if err != nil {
152-
return newSystemError(err)
153-
}
154-
if err := parent.start(); err != nil {
155-
if err := parent.terminate(); err != nil {
156-
log.Warn(err)
157-
}
158-
return err
159-
}
160-
if _, err := parent.wait(); err != nil {
161-
return newSystemError(err)
162-
}
163-
}
164146
if err := p.sendConfig(); err != nil {
165147
return newSystemError(err)
166148
}
167149
// wait for the child process to fully complete and receive an error message
168150
// if one was encoutered
169-
var ierr *initError
151+
var ierr *genericError
170152
if err := json.NewDecoder(p.parentPipe).Decode(&ierr); err != nil && err != io.EOF {
171153
return newSystemError(err)
172154
}
@@ -229,26 +211,6 @@ func (p *initProcess) createNetworkInterfaces() error {
229211
return nil
230212
}
231213

232-
func (p *initProcess) newUsernsSetupProcess() (parentProcess, error) {
233-
parentPipe, childPipe, err := newPipe()
234-
if err != nil {
235-
return nil, newSystemError(err)
236-
}
237-
cmd := exec.Command(p.cmd.Args[0], p.cmd.Args[1:]...)
238-
cmd.ExtraFiles = []*os.File{childPipe}
239-
cmd.Dir = p.cmd.Dir
240-
cmd.Env = append(cmd.Env,
241-
fmt.Sprintf("_LIBCONTAINER_INITPID=%d", p.pid()),
242-
fmt.Sprintf("_LIBCONTAINER_INITTYPE=userns_setup"),
243-
)
244-
return &setnsProcess{
245-
cmd: cmd,
246-
childPipe: childPipe,
247-
parentPipe: parentPipe,
248-
config: p.config,
249-
}, nil
250-
}
251-
252214
func (p *initProcess) signal(s os.Signal) error {
253215
return p.cmd.Process.Signal(s)
254216
}

linux_rootfs.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,27 @@ func createDevices(config *configs.Config) error {
199199

200200
// Creates the device node in the rootfs of the container.
201201
func createDeviceNode(rootfs string, node *configs.Device) error {
202-
var (
203-
dest = filepath.Join(rootfs, node.Path)
204-
parent = filepath.Dir(dest)
205-
)
206-
if err := os.MkdirAll(parent, 0755); err != nil {
202+
dest := filepath.Join(rootfs, node.Path)
203+
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
207204
return err
208205
}
209206
if err := mknodDevice(dest, node); err != nil {
210207
if os.IsExist(err) {
211208
return nil
212209
}
210+
if err != syscall.EPERM {
211+
return err
212+
}
213213
// containers running in a user namespace are not allowed to mknod
214214
// devices so we can just bind mount it from the host.
215-
if err == syscall.EPERM {
216-
f, err := os.Create(dest)
217-
if err != nil {
218-
if os.IsExist(err) {
219-
return nil
220-
}
221-
return err
222-
}
215+
f, err := os.Create(dest)
216+
if err != nil && !os.IsExist(err) {
217+
return err
218+
}
219+
if f != nil {
223220
f.Close()
224-
return syscall.Mount(node.Path, dest, "bind", syscall.MS_BIND, "")
225221
}
222+
return syscall.Mount(node.Path, dest, "bind", syscall.MS_BIND, "")
226223
}
227224
return nil
228225
}

linux_userns_init.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

linux_userns_sidecar_init.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

nsinit/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var createFlags = []cli.Flag{
3333
cli.StringFlag{Name: "mount-label", Usage: "set the mount label"},
3434
cli.StringFlag{Name: "rootfs", Usage: "set the rootfs"},
3535
cli.IntFlag{Name: "userns-root-uid", Usage: "set the user namespace root uid"},
36+
cli.StringFlag{Name: "hostname", Value: "nsinit", Usage: "hostname value for the container"},
3637
cli.StringFlag{Name: "net", Value: "", Usage: "network namespace"},
3738
cli.StringFlag{Name: "ipc", Value: "", Usage: "ipc namespace"},
3839
cli.StringFlag{Name: "pid", Value: "", Usage: "pid namespace"},
@@ -158,6 +159,9 @@ func modify(config *configs.Config, context *cli.Context) {
158159
},
159160
}
160161
}
162+
if v == "uts" {
163+
config.Hostname = context.String("hostname")
164+
}
161165
default:
162166
config.Namespaces.Remove(value)
163167
config.Namespaces.Add(value, v)
@@ -218,8 +222,7 @@ func getTemplate() *configs.Config {
218222
AllowAllDevices: false,
219223
AllowedDevices: configs.DefaultAllowedDevices,
220224
},
221-
Devices: configs.DefaultAutoCreatedDevices,
222-
Hostname: "nsinit",
225+
Devices: configs.DefaultAutoCreatedDevices,
223226
MaskPaths: []string{
224227
"/proc/kcore",
225228
},

0 commit comments

Comments
 (0)