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
package nsexec
import (
"errors"
"fmt"
"strings"
)
const Unshare = "unshare"
type Bind struct {
Source string
Target string
}
type Config struct {
Hostname string
Binds []Bind
Argv []string
}
var ErrNoTarget = errors.New("nsexec: no command after --")
func ParseBinds(raw string) ([]Bind, error) {
var out []Bind
for b := range strings.SplitSeq(raw, ",") {
b = strings.TrimSpace(b)
if b == "" {
continue
}
src, dst, ok := strings.Cut(b, ":")
if !ok || !strings.HasPrefix(src, "/") || !strings.HasPrefix(dst, "/") {
return nil, fmt.Errorf("nsexec: bind %q is not <absolute source>:<absolute target>", b)
}
out = append(out, Bind{Source: src, Target: dst})
}
return out, nil
}
func FormatBinds(b []Bind) string {
parts := make([]string, 0, len(b))
for _, x := range b {
parts = append(parts, x.Source+":"+x.Target)
}
return strings.Join(parts, ",")
}
func (c Config) Validate() error {
if len(c.Argv) == 0 || c.Argv[0] == "" {
return ErrNoTarget
}
if c.Hostname == "" || len(c.Hostname) > 64 {
return fmt.Errorf("nsexec: the private hostname %q must be 1 to 64 bytes", c.Hostname)
}
return nil
}
func Wrap(self []string, hostname string, binds []Bind, argv []string) []string {
out := append(append([]string(nil), self...), "--private-hostname", hostname)
if len(binds) > 0 {
out = append(out, "--bind", FormatBinds(binds))
}
return append(append(out, "--"), argv...)
}