Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 1bf1556

Browse files
authored
Merge pull request #487 from WeiZhang555/fix-shim-not-started
Create symlink before starting shim
2 parents c409b21 + 80dd707 commit 1bf1556

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

create.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,14 @@ func runContainer(context *cli.Context, createOnly bool) {
240240
}
241241
}
242242

243-
address := filepath.Join(namespace, "namespaced.sock")
244-
err = createContainer(context, container, address, spec)
243+
err = createContainer(context, container, namespace, spec)
245244
if err != nil {
246245
fmt.Fprintf(os.Stderr, "error %v\n", err)
247246
cmd.Process.Signal(syscall.SIGINT)
248247
os.Exit(-1)
249248
}
250-
err = os.Symlink(namespace, filepath.Join(root, container, "namespace"))
251-
if err != nil {
252-
cmd.Process.Signal(syscall.SIGINT)
253-
os.Exit(-1)
254-
}
255249
if !createOnly {
250+
address := filepath.Join(namespace, "namespaced.sock")
256251
startContainer(context, bundle, container, address, spec, context.Bool("detach"))
257252
}
258253
os.Exit(0)
@@ -290,7 +285,8 @@ func checkConsole(context *cli.Context, p *specs.Process, createOnly bool) {
290285
// * In runv, shared namespaces multiple containers are located in the same VM which is managed by a runv-daemon.
291286
// * Any running container can exit in any arbitrary order, the runv-daemon and the VM are existed until the last container of the VM is existed
292287

293-
func createContainer(context *cli.Context, container, address string, config *specs.Spec) error {
288+
func createContainer(context *cli.Context, container, namespace string, config *specs.Spec) error {
289+
address := filepath.Join(namespace, "namespaced.sock")
294290
c := getClient(address)
295291

296292
return ociCreate(context, container, func(stdin, stdout, stderr string) error {
@@ -306,6 +302,12 @@ func createContainer(context *cli.Context, container, address string, config *sp
306302
if _, err := c.CreateContainer(netcontext.Background(), r); err != nil {
307303
return err
308304
}
305+
306+
// create symbol link to namespace file
307+
namespaceDir := filepath.Join(context.GlobalString("root"), container, "namespace")
308+
if err := os.Symlink(namespace, namespaceDir); err != nil {
309+
return fmt.Errorf("failed to create symbol link %q: %v", filepath.Join(namespaceDir, "namespaced.sock"), err)
310+
}
309311
return nil
310312
})
311313

@@ -355,8 +357,14 @@ func ociCreate(context *cli.Context, container string, createFunc func(stdin, st
355357
if context.String("pid-file") != "" || tty != nil {
356358
args := []string{
357359
"runv", "--root", context.GlobalString("root"),
358-
"shim", "--container", container, "--process", "init",
359360
}
361+
if context.GlobalString("log_dir") != "" {
362+
args = append(args, "--log_dir", filepath.Join(context.GlobalString("log_dir"), "shim-"+container))
363+
}
364+
if context.GlobalBool("debug") {
365+
args = append(args, "--debug")
366+
}
367+
args = append(args, "shim", "--container", container, "--process", "init")
360368
if context.String("pid-file") != "" {
361369
args = append(args, "--proxy-exit-code", "--proxy-signal")
362370
}

shim.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"syscall"
99

1010
"github.com/codegangsta/cli"
11+
"github.com/golang/glog"
1112
"github.com/hyperhq/runv/containerd/api/grpc/types"
1213
"github.com/hyperhq/runv/lib/term"
1314
"golang.org/x/net/context"
@@ -37,13 +38,15 @@ var shimCommand = cli.Command{
3738
root := context.GlobalString("root")
3839
container := context.String("container")
3940
process := context.String("process")
40-
c := getClient(filepath.Join(root, container, "namespace/namespaced.sock"))
41+
c := getClient(filepath.Join(root, container, "namespace", "namespaced.sock"))
4142
exitcode := -1
4243
if context.Bool("proxy-exit-code") {
44+
glog.V(3).Infof("using shim to proxy exit code")
4345
defer func() { os.Exit(exitcode) }()
4446
}
4547

4648
if context.Bool("proxy-winsize") {
49+
glog.V(3).Infof("using shim to proxy winsize")
4750
s, err := term.SetRawTerminal(os.Stdin.Fd())
4851
if err != nil {
4952
fmt.Printf("error %v\n", err)
@@ -55,6 +58,7 @@ var shimCommand = cli.Command{
5558

5659
if context.Bool("proxy-signal") {
5760
// TODO
61+
glog.V(3).Infof("using shim to proxy signal")
5862
sigc := forwardAllSignals(c, container, process)
5963
defer signal.Stop(sigc)
6064
}
@@ -85,15 +89,19 @@ func forwardAllSignals(c types.APIClient, cid, process string) chan os.Signal {
8589
// forward this signal to containerd
8690
sysSig, ok := s.(syscall.Signal)
8791
if !ok {
88-
fmt.Fprintf(os.Stderr, "can't forward unknown signal %q", s.String())
92+
err := fmt.Errorf("can't forward unknown signal %q", s.String())
93+
fmt.Fprintf(os.Stderr, "%v", err)
94+
glog.Errorf("%v", err)
8995
continue
9096
}
9197
if _, err := c.Signal(context.Background(), &types.SignalRequest{
9298
Id: cid,
9399
Pid: process,
94100
Signal: uint32(sysSig),
95101
}); err != nil {
96-
fmt.Fprintf(os.Stderr, "forward signal %q failed: %v", s.String(), err)
102+
err = fmt.Errorf("forward signal %q failed: %v", s.String(), err)
103+
fmt.Fprintf(os.Stderr, "%v", err)
104+
glog.Errorf("%v", err)
97105
}
98106
}
99107
}()

0 commit comments

Comments
 (0)