forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_forkstart.go
59 lines (48 loc) · 1.41 KB
/
main_forkstart.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
package main
import (
"fmt"
"os"
"syscall"
"gopkg.in/lxc/go-lxc.v2"
"github.com/lxc/lxd/shared"
)
/*
* This is called by lxd when called as "lxd forkstart <container>"
* 'forkstart' is used instead of just 'start' in the hopes that people
* do not accidentally type 'lxd start' instead of 'lxc start'
*/
func cmdForkStart(args *Args) error {
if len(args.Params) != 3 {
return fmt.Errorf("Bad arguments: %q", args.Params)
}
name := args.Params[0]
lxcpath := args.Params[1]
configPath := args.Params[2]
c, err := lxc.NewContainer(name, lxcpath)
if err != nil {
return fmt.Errorf("Error initializing container for start: %q", err)
}
err = c.LoadConfigFile(configPath)
if err != nil {
return fmt.Errorf("Error opening startup config file: %q", err)
}
/* due to https://github.com/golang/go/issues/13155 and the
* CollectOutput call we make for the forkstart process, we need to
* close our stdin/stdout/stderr here. Collecting some of the logs is
* better than collecting no logs, though.
*/
os.Stdin.Close()
os.Stderr.Close()
os.Stdout.Close()
// Redirect stdout and stderr to a log file
logPath := shared.LogPath(name, "forkstart.log")
if shared.PathExists(logPath) {
os.Remove(logPath)
}
logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
if err == nil {
syscall.Dup3(int(logFile.Fd()), 1, 0)
syscall.Dup3(int(logFile.Fd()), 2, 0)
}
return c.Start()
}