-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (84 loc) · 2.09 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
package main
import (
"io/ioutil"
"log"
"os"
"runtime"
"github.com/mholt/archiver/v3"
"github.com/openSUSE/umoci"
"github.com/openSUSE/umoci/oci/cas/dir"
"github.com/openSUSE/umoci/oci/casext"
"github.com/opencontainers/runc/libcontainer"
_ "github.com/opencontainers/runc/libcontainer/nsenter"
"github.com/opencontainers/runc/libcontainer/specconv"
)
func createBundleFromImage(srcTar, dest, tag string) error {
ociImageDir, err := ioutil.TempDir("", "ociImage")
if err != nil {
log.Fatal(err)
}
//defer os.RemoveAll(ociImageDir)
err = archiver.Unarchive(srcTar, ociImageDir)
if err != nil {
log.Fatal(err)
}
var meta umoci.Meta
meta.Version = umoci.MetaVersion
meta.MapOptions.KeepDirlinks = true
engine, err := dir.Open(ociImageDir)
if err != nil {
return err
}
engineExt := casext.NewEngine(engine)
defer engine.Close()
return umoci.Unpack(engineExt, tag, dest, meta.MapOptions)
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "init" {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
factory, _ := libcontainer.New("")
if err := factory.StartInitialization(); err != nil {
log.Fatal(err)
}
panic("--this line should have never been executed, congratulations--")
}
factory, err := libcontainer.New("/tmp", libcontainer.RootlessCgroupfs)
if err != nil {
log.Fatal(err)
}
spec := specconv.Example()
specconv.ToRootless(spec)
conf, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{
CgroupName: "foo",
Spec: spec,
RootlessEUID: os.Geteuid() != 0,
RootlessCgroups: true,
})
if err != nil {
log.Fatal(err)
}
err = createBundleFromImage("/image.tar", "/bundle", "latest")
if err != nil {
log.Fatal(err)
}
conf.Rootfs = "/bundle/rootfs"
conf.Readonlyfs = true
// Mounts: []*configs.Mount{{
// Source: "/data/test",
// Destination: "/test",
// }},
c, err := factory.Create("foo", conf)
if err != nil {
log.Fatal(err)
}
err = c.Run(&libcontainer.Process{
Args: []string{"/usr/bin/safe", "--version"},
Stdout: os.Stdout,
Stderr: os.Stderr,
Init: true,
})
if err != nil {
log.Fatal(err)
}
}