-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
139 lines (122 loc) · 3.03 KB
/
main.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
package main
import (
"flag"
"os"
"runtime"
"github.com/leoh0/binctr/container"
"github.com/opencontainers/runc/libcontainer"
_ "github.com/opencontainers/runc/libcontainer/nsenter"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
const (
defaultRoot = "/tmp/kakaotalk-binctr"
defaultRootfsDir = "rootfs"
)
var (
containerID string
root string
file string
dir string
shortpath string
)
func init() {
// Parse flags
flag.StringVar(&containerID, "id", "kakaotalk", "container ID")
flag.StringVar(&root, "root", defaultRoot, "root directory of container state, should be tmpfs")
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
}
//go:generate go run generate.go
func main() {
if len(os.Args) > 1 && os.Args[1] == "init" {
runInit()
return
}
dir := os.Getenv("HOME") + "/.kwine"
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
// Create a new container spec with the following options.
opts := container.SpecOpts{
Rootless: false,
Terminal: false,
Args: []string{
"/usr/local/bin/entrypoint.sh",
},
Env: []string{
"LANG=ko_KR.UTF-8",
"LANGUAGE=ko_KR.UTF-8",
"GTK_IM_MODULE=uim",
"XMODIFIERS=@im=uim",
"QT_IM_MODULE=uim",
"DISPLAY=unix:0",
"PULSE_SERVER=tcp:127.0.0.1:4713",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
},
Mounts: []specs.Mount{
{
Destination: "/etc/localtime",
Type: "bind",
Source: "/etc/localtime",
Options: []string{"ro", "rbind", "rprivate"},
},
{
Destination: "/tmp/.X11-unix",
Type: "bind",
Source: "/tmp/.X11-unix",
Options: []string{"rw", "rbind", "rprivate"},
},
{
Destination: "/root/.wine",
Type: "bind",
Source: dir,
Options: []string{"rw", "rbind", "rprivate"},
},
},
}
spec := container.Spec(opts)
// Initialize the container object.
c := &container.Container{
ID: containerID,
Spec: spec,
Root: root,
Rootless: false,
Detach: false,
NoPivotRoot: true,
UseSystemdCgroup: false,
HostNetwork: true,
ShareIPC: true,
}
// Unpack the rootfs.
if err := c.UnpackRootfs(defaultRootfsDir, Asset); err != nil {
logrus.Fatal(err)
}
// Run the container.
status, err := c.Run()
if err != nil {
logrus.Fatal(err)
}
// Remove the rootfs after the container has exited.
if err := os.RemoveAll(defaultRootfsDir); err != nil {
logrus.Warnf("removing rootfs failed: %v", err)
}
// Exit with the container's exit status.
os.Exit(status)
}
func runInit() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
factory, _ := libcontainer.New("")
if err := factory.StartInitialization(); err != nil {
// as the error is sent back to the parent there is no need to log
// or write it to stderr because the parent process will handle this
os.Exit(1)
}
panic("libcontainer: container init failed to exec")
}