-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
78 lines (65 loc) · 1.56 KB
/
utils.go
File metadata and controls
78 lines (65 loc) · 1.56 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"github.com/docker/docker/pkg/archive"
"github.com/docker/libcontainer/netlink"
)
func setupNetBridge() error {
// Enable IPv4 forwarding
if err := ioutil.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte{'1', '\n'}, 0644); err != nil {
return err
}
if err := netlink.CreateBridge(vethBridge, false); err != nil {
// the bridge may already exist, therefore we can ignore an "exists" error
if !os.IsExist(err) {
return err
}
}
iface, err := net.InterfaceByName(vethBridge)
if err != nil {
return err
}
ipAddr, ipNet, err := net.ParseCIDR(vethNetwork)
if err != nil {
return err
}
if netlink.NetworkLinkAddIp(iface, ipAddr, ipNet); err != nil {
return fmt.Errorf("failed to add private network: %s", err)
}
if err := netlink.NetworkLinkUp(iface); err != nil {
return fmt.Errorf("failed to start network bridge: %s", err)
}
return nil
}
func validateIPAddr(ip string) error {
formatErr := fmt.Errorf("invalid ip address %s. Expecting 172.18.XXX.XXX", ip)
comps := strings.Split(ip, ".")
if len(comps) != 4 {
return formatErr
}
if comps[0] != "172" || comps[1] != "18" {
return formatErr
}
ipID, err := strconv.Atoi(comps[3])
if err != nil {
return err
}
if ipID < 2 || ipID > 254 {
return fmt.Errorf("%s out of ip range (2..254)", comps[3])
}
return nil
}
func exportRootfs(basePath string) error {
tar, err := Asset("redis_rootfs.tar")
if err != nil {
return err
}
buf := bytes.NewBuffer(tar)
return archive.Untar(buf, basePath, nil)
}