mirror of
https://github.com/criyle/go-sandbox.git
synced 2025-09-26 23:19:11 +08:00
container: add support for temp root dir
This commit is contained in:
parent
701fed8d1c
commit
5eeaadffdf
@ -291,12 +291,6 @@ func start() (*runner.Result, error) {
|
||||
}
|
||||
|
||||
if runt == "container" {
|
||||
root, err := os.MkdirTemp("", "dm")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot make temp root for container namespace: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(root)
|
||||
|
||||
var credG container.CredGenerator
|
||||
if cred {
|
||||
credG = newCredGen()
|
||||
@ -312,7 +306,7 @@ func start() (*runner.Result, error) {
|
||||
}
|
||||
|
||||
b := container.Builder{
|
||||
Root: root,
|
||||
TmpRoot: "dm",
|
||||
Mounts: mb.Mounts,
|
||||
Stderr: stderr,
|
||||
CredGenerator: credG,
|
||||
|
@ -24,6 +24,9 @@ type Builder struct {
|
||||
// Root is container root mount path, empty uses current work path
|
||||
Root string
|
||||
|
||||
// TmpRoot defines the tmp dir pattern if not nil. Temp directory will be created as container root dir
|
||||
TmpRoot string
|
||||
|
||||
// Mounts defines container mount points, empty uses default mounts
|
||||
Mounts []mount.Mount
|
||||
|
||||
@ -142,6 +145,12 @@ func (b *Builder) Build() (Environment, error) {
|
||||
|
||||
// container root directory on the host
|
||||
root := b.Root
|
||||
if b.TmpRoot != "" {
|
||||
if root, err = os.MkdirTemp(b.Root, b.TmpRoot); err != nil {
|
||||
return nil, fmt.Errorf("container: failed to make tmp container root at %s %v", b.Root, err)
|
||||
}
|
||||
defer os.Remove(root)
|
||||
}
|
||||
if root == "" {
|
||||
if root, err = os.Getwd(); err != nil {
|
||||
return nil, fmt.Errorf("container: failed to get work directory %v", err)
|
||||
|
4
go.mod
4
go.mod
@ -4,8 +4,8 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/elastic/go-seccomp-bpf v1.2.0
|
||||
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
|
||||
golang.org/x/net v0.0.0-20220708220712-1185a9018129
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
|
||||
)
|
||||
|
||||
require github.com/pkg/errors v0.9.1 // indirect
|
||||
|
8
go.sum
8
go.sum
@ -13,12 +13,12 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861 h1:yssD99+7tqHWO5Gwh81phT+67hg+KttniBr6UnEXOY8=
|
||||
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
|
||||
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
Loading…
Reference in New Issue
Block a user