1
0
mirror of https://github.com/rancher/os.git synced 2025-09-01 14:48:55 +00:00

Update vendor

This commit is contained in:
Josh Curl
2016-10-19 16:05:56 -07:00
parent 0ae29130cd
commit 1f4d23bf50
24 changed files with 0 additions and 2155 deletions

View File

@@ -1,3 +0,0 @@
base-image
assets
.vendor

View File

@@ -1,6 +0,0 @@
build:
image: rancher/dapper:1.10.3
volumes:
- /var/run/docker.sock:/var/run/docker.sock
commands:
- dapper ./scripts/ci

View File

@@ -1,8 +0,0 @@
/dist
/build
/assets
/base-image/assets/
/base-image/build/
/base-image/cache/
/base-image/dist/
/.vendor

View File

@@ -1 +0,0 @@
--privileged

View File

@@ -1,8 +0,0 @@
FROM scratch
ADD build/base-files.tar.gz /
COPY build/ca-certificates.crt /usr/etc/ssl/certs/
COPY build/dockerlaunch /usr/bin/
COPY build/docker* /usr/bin/
VOLUME /var/lib/docker
ENTRYPOINT ["/usr/bin/dockerlaunch", "/usr/bin/docker"]
CMD ["daemon", "-s", "overlay"]

View File

@@ -1,41 +0,0 @@
FROM ubuntu:16.04
RUN apt-get update && \
apt-get -y install locales sudo vim less curl wget git rsync build-essential isolinux xorriso gccgo \
libblkid-dev libmount-dev libselinux1-dev cpio genisoimage qemu-kvm python-pip ca-certificates pkg-config tox
COPY ./scripts/install-libs.sh /tmp/
RUN /tmp/install-libs.sh
RUN wget -O - https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz | tar -xz -C /usr/local
RUN wget -O /usr/local/bin/docker -L https://get.docker.com/builds/Linux/x86_64/docker-1.10.3 && \
chmod +x /usr/local/bin/docker
ENV PATH /usr/local/go/bin:$PATH
RUN mkdir -p /go/src /go/bin && chmod -R 777 /go
ENV GOPATH /go
ENV PATH /go/bin:$PATH
ENV DAPPER_SOURCE /go/src/github.com/rancher/docker-from-scratch
ENV DAPPER_OUTPUT ""
ENV DAPPER_DOCKER_SOCKET true
ENV DAPPER_ENV NO_TEST ARCH
RUN mkdir -p ${DAPPER_SOURCE}/assets && ln -s ${DAPPER_SOURCE} /source
WORKDIR ${DAPPER_SOURCE}/assets
RUN wget https://github.com/rancher/docker-from-scratch/releases/download/bin-v0.4.0/base-files_amd64.tar.gz && \
wget https://github.com/rancher/docker-from-scratch/releases/download/bin-v0.4.0/base-files_arm.tar.gz && \
wget https://github.com/rancher/docker-from-scratch/releases/download/bin-v0.4.0/base-files_arm64.tar.gz
ENV DOCKER_VERSION=1.11.2 DOCKER_PATCH_VERSION=v1.11.2-ros1
ENV VERSION=v${DOCKER_VERSION}-2
RUN wget -O docker-${DOCKER_VERSION}_amd64.tgz -L https://get.docker.com/builds/Linux/x86_64/docker-${DOCKER_VERSION}.tgz && \
wget -L https://github.com/rancher/docker/releases/download/${DOCKER_PATCH_VERSION}/docker-${DOCKER_VERSION}_arm.tgz && \
wget -L https://github.com/rancher/docker/releases/download/${DOCKER_PATCH_VERSION}/docker-${DOCKER_VERSION}_arm64.tgz
WORKDIR ${DAPPER_SOURCE}
CMD ./scripts/ci

View File

@@ -1,178 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@@ -1,105 +0,0 @@
# Docker `FROM scratch`
Docker-in-Docker image based off of the empty image `scratch`. Only the bare minimum required files are included to make Docker run. This image weighs in around 25MB expanded.
## Running
### Overlay
```bash
# Daemon
docker run --name daemon --privileged -d rancher/docker
# Client
docker exec -it daemon docker ps
```
### Aufs
```bash
# Daemon
docker run --name daemon --privileged -d rancher/docker daemon -s aufs
# Client
docker exec -it daemon docker ps
```
## Embed in Custom Image
Since docker-from-scratch doesn't assume a base Linux distro it can be easily copied into an other image to add Docker-in-Docker capabilities
```bash
docker export $(docker create rancher/docker) > files.tar
cat > Dockerfile << EOF
FROM ubuntu
ADD files.tar /
ENTRYPOINT ["/usr/bin/dockerlaunch", "/usr/bin/docker"]
VOLUME /var/lib/docker
CMD ["daemon", "-s", "overlay"]
EOF
docker build -t custom-dind .
```
## Graph Driver Compatibility
This image is really designed to run with overlay. Aufs is known to work but other graph drivers may not work properly or be missing userspace programs needed.
## Seriously, Why?
This code and the supporting files were extracted out of RancherOS into a separate library and are still used by RancherOS. RancherOS runs Docker as the first process but before we can exec Docker we need to setup a minimal environment for Docker in which to run. Since RancherOS is executed by the kernel there is absolutely nothing setup in the system. At Rancher we wrote a small amount of code to setup all the required mounts and directories to launch Docker.
We moved this code out into a separate project for two reasons. First was simply that we wanted to clean up and modularize the RancherOS code base. Second is that we wanted to demonstrate clearly what exactly Docker requires from the Linux user space. For the most part Docker requires the standard mounts (`/proc`, `/sys`, `/run`, `/var/run`, etc) and the cgroup mounts in `/sys/fs/cgroup` plus the following programs/files:
```
/etc/ssl/certs/ca-certificates.crt
/usr/bin/modprobe
/usr/bin/iptables
/usr/bin/ssh
/usr/bin/xz
/usr/bin/git
/usr/bin/ps
/usr/libexec/git-core/git-clone
/usr/libexec/git-core/git-submodule
/usr/libexec/git-core/git-checkout
```
This list can be reduced to a bare minimum if you ignore certain features of Docker. A full description of why each program is needed is below.
File | Description | Can it be ignored
-----|-------------|------------------
`/etc/ssl/certs/ca-certificates.crt` | Used as the CA roots to validate SSL connections | No
`/usr/bin/modprobe` | Used to ensure that bridge, nf_nat, br_netfilter, aufs, or overlay modules are loaded. Additionally iptables loads kernel modules based on the configuration of the rules | Yes, just load the modules from the host that you will need.
`/usr/bin/iptables` | Docker uses IPtables to setup networking | Yes, add `--iptables=false` to the `docker -d` command. Networking will have to be manually configured in this situation
`/usr/bin/ssh`| Used by git to clone repos over SSH | Yes, don't use git based Docker builds
`/usr/bin/xz` | Used to extract *legacy* Docker images that were compressed with xz | Yes, only use newer images. Most popular images are not based on xz
`/usr/bin/git` | Used to do Docker builds from a git URL | Yes, don't use git based Docker builds
`/usr/bin/ps` | `docker ps` uses the host `ps` to get information about the running process in a container | No
`/usr/libexec/git-core/git-clone`| Used by git | Yes, don't use git based Docker builds
`/usr/libexec/git-core/git-submodule`| Used by git | Yes, don't use git based Docker builds
`/usr/libexec/git-core/git-checkout`| Used by git | Yes, don't use git based Docker builds
## Custom Bridge Name
If you want to run with a custom bridge name you must pass both `--bip` and `-b` as arguments. Normally this would be an error for Docker but in this situation the docker-from-scratch container will create the bridge device with the IP address specified and then old pass `-b` to Docker.
# Troubleshooting
## Zombies
If you are running containers with `--pid=host` you can get zombies. When you launch docker-from-scratch just add `-e DOCKER_LAUNCH_REAP=true` as a parameter to the `docker run` command to fix this.
## Weird module loading errors
For various reasons Docker or iptables may try to load a kernel module. You can either manually load all the needed modules from the host or you can bind mount in the kernel modules by adding `-v /lib/modules/$(uname -r)/lib/modules/$(uname -r)` to your `docker run` command
## Debug Logging
To enable debug logging on the startup of docker-from-scrach just add `-e DOCKER_LAUNCH_DEBUG=true` to the `docker run` command. For example:
docker run --name daemon --privileged -d -e DOCKER_LAUNCH_DEBUG=true rancher/docker

View File

@@ -1,7 +0,0 @@
#!/bin/bash
cd $(dirname $0)
rm -rf ./build
export NO_TEST=true
dapper ./scripts/ci

View File

@@ -1,23 +0,0 @@
// +build linux
package dockerlaunch
import (
"os"
"os/signal"
"syscall"
)
func PidOne() error {
c := make(chan os.Signal, 2048)
signal.Notify(c, syscall.SIGCHLD)
for range c {
for {
if pid, err := syscall.Wait4(-1, nil, syscall.WNOHANG, nil); err != nil || pid <= 0 {
break
}
}
}
return nil
}

View File

@@ -1,712 +0,0 @@
package dockerlaunch
import (
"bufio"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/resolvconf"
"github.com/rancher/docker-from-scratch/selinux"
"github.com/rancher/docker-from-scratch/util"
"github.com/rancher/netconf"
)
const (
defaultPrefix = "/usr"
iptables = "/sbin/iptables"
modprobe = "/sbin/modprobe"
distSuffix = ".dist"
)
var (
mounts = [][]string{
{"devtmpfs", "/dev", "devtmpfs", ""},
{"none", "/dev/pts", "devpts", ""},
{"shm", "/dev/shm", "tmpfs", "rw,nosuid,nodev,noexec,relatime,size=65536k"},
{"mqueue", "/dev/mqueue", "mqueue", "rw,nosuid,nodev,noexec,relatime"},
{"none", "/proc", "proc", ""},
{"none", "/run", "tmpfs", ""},
{"none", "/sys", "sysfs", ""},
{"none", "/sys/fs/cgroup", "tmpfs", ""},
}
optionalMounts = [][]string{
{"none", "/sys/fs/selinux", "selinuxfs", ""},
}
)
type Config struct {
Fork bool
PidOne bool
CommandName string
DnsConfig netconf.DnsConfig
BridgeName string
BridgeAddress string
BridgeMtu int
CgroupHierarchy map[string]string
LogFile string
NoLog bool
NoFiles uint64
Environment []string
GraphDirectory string
DaemonConfig string
}
func createMounts(mounts ...[]string) error {
for _, mount := range mounts {
log.Debugf("Mounting %s %s %s %s", mount[0], mount[1], mount[2], mount[3])
err := util.Mount(mount[0], mount[1], mount[2], mount[3])
if err != nil {
return err
}
}
return nil
}
func createOptionalMounts(mounts ...[]string) {
for _, mount := range mounts {
log.Debugf("Mounting %s %s %s %s", mount[0], mount[1], mount[2], mount[3])
err := util.Mount(mount[0], mount[1], mount[2], mount[3])
if err != nil {
log.Debugf("Unable to mount %s %s %s %s: %s", mount[0], mount[1], mount[2], mount[3], err)
}
}
}
func createDirs(dirs ...string) error {
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Debugf("Creating %s", dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
}
return nil
}
func mountCgroups(hierarchyConfig map[string]string) error {
f, err := os.Open("/proc/cgroups")
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
hierarchies := make(map[string][]string)
for scanner.Scan() {
text := scanner.Text()
log.Debugf("/proc/cgroups: %s", text)
fields := strings.Split(text, "\t")
cgroup := fields[0]
if cgroup == "" || cgroup[0] == '#' || (len(fields) > 3 && fields[3] == "0") {
continue
}
hierarchy := hierarchyConfig[cgroup]
if hierarchy == "" {
hierarchy = fields[1]
}
if hierarchy == "0" {
hierarchy = cgroup
}
hierarchies[hierarchy] = append(hierarchies[hierarchy], cgroup)
}
for _, hierarchy := range hierarchies {
if err := mountCgroup(strings.Join(hierarchy, ",")); err != nil {
return err
}
}
if err = scanner.Err(); err != nil {
return err
}
log.Debug("Done mouting cgroupfs")
return nil
}
func CreateSymlinks(pathSets [][]string) error {
for _, paths := range pathSets {
if err := CreateSymlink(paths[0], paths[1]); err != nil {
return err
}
}
return nil
}
func CreateSymlink(src, dest string) error {
if _, err := os.Lstat(dest); os.IsNotExist(err) {
log.Debugf("Symlinking %s => %s", dest, src)
if err = os.Symlink(src, dest); err != nil {
return err
}
}
return nil
}
func mountCgroup(cgroup string) error {
if err := createDirs("/sys/fs/cgroup/" + cgroup); err != nil {
return err
}
if err := createMounts([][]string{{"none", "/sys/fs/cgroup/" + cgroup, "cgroup", cgroup}}...); err != nil {
return err
}
parts := strings.Split(cgroup, ",")
if len(parts) > 1 {
for _, part := range parts {
if err := CreateSymlink("/sys/fs/cgroup/"+cgroup, "/sys/fs/cgroup/"+part); err != nil {
return err
}
}
}
return nil
}
func execDocker(config *Config, docker, cmd string, args []string) (*exec.Cmd, error) {
if len(args) > 0 && args[0] == "docker" {
args = args[1:]
}
log.Debugf("Launching Docker %s %s %v", docker, cmd, args)
env := os.Environ()
if len(config.Environment) != 0 {
env = append(env, config.Environment...)
}
if config.Fork {
cmd := exec.Command(docker, args...)
if !config.NoLog {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
cmd.Env = env
err := cmd.Start()
if err != nil {
return cmd, err
}
if config.PidOne {
PidOne()
}
return cmd, err
} else {
err := syscall.Exec(expand(docker), append([]string{cmd}, args...), env)
return nil, err
}
}
func copyDefault(folder, name string) error {
defaultFile := path.Join(defaultPrefix, folder, name)
if err := CopyFile(defaultFile, folder, name); err != nil {
return err
}
return nil
}
func copyDefaultFolder(folder string) error {
log.Debugf("Copying folder %s", folder)
defaultFolder := path.Join(defaultPrefix, folder)
files, _ := ioutil.ReadDir(defaultFolder)
for _, file := range files {
var err error
if file.IsDir() {
err = copyDefaultFolder(path.Join(folder, file.Name()))
} else {
err = copyDefault(folder, file.Name())
}
if err != nil {
return err
}
}
return nil
}
func defaultFiles(files ...string) error {
for _, file := range files {
dir := path.Dir(file)
name := path.Base(file)
if err := copyDefault(dir, name); err != nil {
return err
}
}
return nil
}
func defaultFolders(folders ...string) error {
for _, folder := range folders {
if err := copyDefaultFolder(folder); err != nil {
return err
}
}
return nil
}
func CopyFile(src, folder, name string) error {
if _, err := os.Lstat(src); os.IsNotExist(err) {
log.Debugf("Not copying %s, does not exists", src)
return nil
}
dst := path.Join(folder, name)
if _, err := os.Lstat(dst); err == nil {
log.Debugf("Not copying %s => %s already exists", src, dst)
return nil
}
if err := createDirs(folder); err != nil {
return err
}
stat, err := os.Lstat(src)
if err != nil {
return err
}
if stat.Mode()&os.ModeSymlink != 0 {
symDst, err := os.Readlink(src)
if err != nil {
log.Errorf("Failed to readlink: %v", err)
return err
}
// file is a symlink
log.Debugf("Symlinking %s => %s", dst, symDst)
return os.Symlink(symDst, dst)
}
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
log.Debugf("Copying %s => %s", src, dst)
_, err = io.Copy(dstFile, srcFile)
return err
}
func tryCreateFile(name, content string) error {
if _, err := os.Stat(name); err == nil {
return nil
}
if err := createDirs(path.Dir(name)); err != nil {
return err
}
return ioutil.WriteFile(name, []byte(content), 0644)
}
func createPasswd() error {
return tryCreateFile("/etc/passwd", "root:x:0:0:root:/root:/bin/sh\n")
}
func createGroup() error {
return tryCreateFile("/etc/group", "root:x:0:\n")
}
func setupNetworking(config *Config) error {
if config == nil {
return nil
}
hostname, err := os.Hostname()
if err != nil {
return err
}
tryCreateFile("/etc/hosts", `127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 `+hostname)
if len(config.DnsConfig.Nameservers) != 0 {
if _, err := resolvconf.Build("/etc/resolv.conf", config.DnsConfig.Nameservers, config.DnsConfig.Search, nil); err != nil {
return err
}
}
if config.BridgeName != "" && config.BridgeName != "none" {
log.Debugf("Creating bridge %s (%s)", config.BridgeName, config.BridgeAddress)
if err := netconf.ApplyNetworkConfigs(&netconf.NetworkConfig{
Interfaces: map[string]netconf.InterfaceConfig{
config.BridgeName: {
Address: config.BridgeAddress,
MTU: config.BridgeMtu,
Bridge: "true",
},
},
}); err != nil {
return err
}
}
return nil
}
func ParseConfig(config *Config, args ...string) []string {
for i, arg := range args {
if strings.HasPrefix(arg, "--bip") {
config.BridgeAddress = util.GetValue(i, args)
} else if strings.HasPrefix(arg, "--fixed-cidr") {
config.BridgeAddress = util.GetValue(i, args)
} else if strings.HasPrefix(arg, "-b") || strings.HasPrefix(arg, "--bridge") {
config.BridgeName = util.GetValue(i, args)
} else if strings.HasPrefix(arg, "--config-file") {
config.DaemonConfig = util.GetValue(i, args)
} else if strings.HasPrefix(arg, "--mtu") {
mtu, err := strconv.Atoi(util.GetValue(i, args))
if err != nil {
config.BridgeMtu = mtu
}
} else if strings.HasPrefix(arg, "-g") || strings.HasPrefix(arg, "--graph") {
config.GraphDirectory = util.GetValue(i, args)
}
}
if config.BridgeName != "" && config.BridgeAddress != "" {
newArgs := []string{}
skip := false
for _, arg := range args {
if skip {
skip = false
continue
}
if arg == "--bip" {
skip = true
continue
} else if strings.HasPrefix(arg, "--bip=") {
continue
}
newArgs = append(newArgs, arg)
}
args = newArgs
}
return args
}
func PrepareFs(config *Config) error {
if err := createMounts(mounts...); err != nil {
return err
}
createOptionalMounts(optionalMounts...)
if err := mountCgroups(config.CgroupHierarchy); err != nil {
return err
}
if err := createLayout(config); err != nil {
return err
}
if err := firstPrepare(); err != nil {
return err
}
return nil
}
func touchSocket(path string) error {
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
return err
}
return ioutil.WriteFile(path, []byte{}, 0700)
}
func touchSockets(args ...string) error {
touched := false
for i, arg := range args {
if strings.HasPrefix(arg, "-H") {
val := util.GetValue(i, args)
if strings.HasPrefix(val, "unix://") {
val = val[len("unix://"):]
log.Debugf("Creating temp file at %s", val)
if err := touchSocket(val); err != nil {
return err
}
touched = true
}
}
}
if !touched {
return touchSocket("/var/run/docker.sock")
}
return nil
}
func createDaemonConfig(config *Config) error {
if config.DaemonConfig == "" {
return nil
}
if _, err := os.Stat(config.DaemonConfig); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(config.DaemonConfig), 0755); err != nil {
return err
}
return ioutil.WriteFile(config.DaemonConfig, []byte("{}"), 0600)
}
return nil
}
func cleanupFiles(graphDirectory string) {
zeroFiles := []string{
"/etc/docker/key.json",
"/etc/docker/daemon.json",
"/etc/docker/system-daemon.json",
path.Join(graphDirectory, "image/overlay/repositories.json"),
}
for _, file := range zeroFiles {
if stat, err := os.Stat(file); err == nil {
if stat.Size() < 2 {
log.Warnf("Deleting invalid json file: %s", file)
os.Remove(file)
}
}
}
}
func createLayout(config *Config) error {
if err := createDirs("/tmp", "/root/.ssh", "/var", "/usr/lib"); err != nil {
return err
}
graphDirectory := config.GraphDirectory
if config.GraphDirectory == "" {
graphDirectory = "/var/lib/docker"
}
if err := createDirs(graphDirectory); err != nil {
return err
}
if err := createDaemonConfig(config); err != nil {
return err
}
cleanupFiles(graphDirectory)
selinux.SetFileContext(graphDirectory, "system_u:object_r:var_lib_t:s0")
return CreateSymlinks([][]string{
{"usr/lib", "/lib"},
{"usr/sbin", "/sbin"},
{"../run", "/var/run"},
})
}
func firstPrepare() error {
os.Setenv("PATH", "/sbin:/usr/sbin:/usr/bin")
if err := defaultFiles(
"/etc/ssl/certs/ca-certificates.crt",
"/etc/passwd",
"/etc/group",
); err != nil {
return err
}
if err := defaultFolders(
"/etc/docker",
"/etc/selinux",
"/etc/selinux/ros",
"/etc/selinux/ros/policy",
"/etc/selinux/ros/contexts",
"/var/lib/cni",
); err != nil {
return err
}
if err := createPasswd(); err != nil {
return err
}
if err := createGroup(); err != nil {
return err
}
return nil
}
func secondPrepare(config *Config, docker string, args ...string) error {
if err := setupNetworking(config); err != nil {
return err
}
if err := touchSockets(args...); err != nil {
return err
}
if err := setupLogging(config); err != nil {
return err
}
for _, i := range []string{docker, iptables, modprobe} {
if err := setupBin(config, i); err != nil {
return err
}
}
if err := setUlimit(config); err != nil {
return err
}
ioutil.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0655)
return nil
}
func expand(bin string) string {
expanded, err := exec.LookPath(bin)
if err == nil {
return expanded
}
return bin
}
func setupBin(config *Config, bin string) error {
expanded, err := exec.LookPath(bin)
if err == nil {
return nil
}
expanded, err = exec.LookPath(bin + distSuffix)
if err != nil {
// Purposely not returning error
return nil
}
return CreateSymlink(expanded, expanded[:len(expanded)-len(distSuffix)])
}
func setupLogging(config *Config) error {
if config.LogFile == "" {
return nil
}
if err := createDirs(path.Dir(config.LogFile)); err != nil {
return err
}
output, err := os.OpenFile(config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
syscall.Dup3(int(output.Fd()), int(os.Stdout.Fd()), 0)
syscall.Dup3(int(output.Fd()), int(os.Stderr.Fd()), 0)
return nil
}
func setUlimit(cfg *Config) error {
var rLimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return err
}
if cfg.NoFiles == 0 {
rLimit.Max = 1000000
} else {
rLimit.Max = cfg.NoFiles
}
rLimit.Cur = rLimit.Max
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
}
func runOrExec(config *Config, docker string, args ...string) (*exec.Cmd, error) {
if err := secondPrepare(config, docker, args...); err != nil {
return nil, err
}
cmd := path.Base(docker)
if config != nil && config.CommandName != "" {
cmd = config.CommandName
}
if cmd == "dockerd" && len(args) > 1 && args[0] == "daemon" {
args = args[1:]
}
return execDocker(config, docker, cmd, args)
}
func LaunchDocker(config *Config, docker string, args ...string) (*exec.Cmd, error) {
if err := PrepareFs(config); err != nil {
return nil, err
}
return runOrExec(config, docker, args...)
}
func Main() {
if os.Getenv("DOCKER_LAUNCH_DEBUG") == "true" {
log.SetLevel(log.DebugLevel)
}
if len(os.Args) < 2 {
log.Fatalf("Usage Example: %s /usr/bin/docker -d -D", os.Args[0])
}
args := []string{}
if len(os.Args) > 1 {
args = os.Args[2:]
}
var config Config
args = ParseConfig(&config, args...)
if os.Getenv("DOCKER_LAUNCH_REAP") == "true" {
config.Fork = true
config.PidOne = true
}
log.Debugf("Launch config %#v", config)
_, err := LaunchDocker(&config, os.Args[1], args...)
if err != nil {
log.Fatal(err)
}
}

View File

@@ -1,10 +0,0 @@
package selinux
// #cgo pkg-config: libselinux
// #include <selinux/selinux.h>
import "C"
func SetFileContext(path string, context string) (int, error) {
ret, err := C.setfilecon(C.CString(path), C.CString(context))
return int(ret), err
}

View File

@@ -1,37 +0,0 @@
package: github.com/rancher/docker-from-scratch
import:
- package: github.com/Sirupsen/logrus
version: 3cb248e9df77413d58a6330dde84236d04c197d5
- package: github.com/docker/docker
version: v1.9.1
subpackages:
- pkg/ioutils
- pkg/mount
- package: github.com/docker/libnetwork
version: 077b0764e4926509c9e6213ae7b110de48494810
subpackages:
- resolvconf
- package: github.com/j-keck/arping
version: 4f4d2c8983a18e2c9c63a3f339bc9a998c4557bc
- package: github.com/rancher/netconf
version: bf95fd720be9de4f7aa3a4a529b70f2865dd0fc7
- package: github.com/ryanuber/go-glob
version: 0067a9abd927e50aed5190662702f81231413ae0
- package: github.com/stretchr/testify
version: a1f97990ddc16022ec7610326dd9bce31332c116
subpackages:
- assert
- require
- package: github.com/vishvananda/netlink
version: edcd99c0881a4de0fdb3818af6b24f4ee6948464
- package: github.com/flynn/go-shlex
version: 3f9db97f856818214da2e1057f8ad84803971cff

View File

@@ -1,19 +0,0 @@
package util
import (
"strings"
)
func GetValue(index int, args []string) string {
val := args[index]
parts := strings.SplitN(val, "=", 2)
if len(parts) == 1 {
if len(args) > index+1 {
return args[index+1]
} else {
return ""
}
} else {
return parts[1]
}
}

View File

@@ -1,41 +0,0 @@
// +build linux
package util
import (
"os"
"syscall"
"github.com/docker/docker/pkg/mount"
)
func mountProc() error {
if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) {
if _, err := os.Stat("/proc"); os.IsNotExist(err) {
if err = os.Mkdir("/proc", 0755); err != nil {
return err
}
}
if err := syscall.Mount("none", "/proc", "proc", 0, ""); err != nil {
return err
}
}
return nil
}
func Mount(device, directory, fsType, options string) error {
if err := mountProc(); err != nil {
return nil
}
if _, err := os.Stat(directory); os.IsNotExist(err) {
err = os.MkdirAll(directory, 0755)
if err != nil {
return err
}
}
return mount.Mount(device, directory, fsType, options)
}

View File

@@ -1 +0,0 @@
/netconf

View File

@@ -1,178 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@@ -1,3 +0,0 @@
# RancherOS Netconf
Simple wrapper around various netlink calls to configure networking

View File

@@ -1,143 +0,0 @@
package netconf
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
)
const (
base = "/sys/class/net/"
bondingMasters = "/sys/class/net/bonding_masters"
)
type Bonding struct {
name string
}
func (b *Bonding) init() error {
_, err := os.Stat(bondingMasters)
if os.IsNotExist(err) {
logrus.Info("Loading bonding kernel module")
cmd := exec.Command("modprobe", "bonding")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdin
err = cmd.Run()
if err != nil {
for i := 0; i < 30; i++ {
if _, err = os.Stat(bondingMasters); err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
}
}
_, err = os.Stat(bondingMasters)
return err
}
func contains(file, word string) (bool, error) {
words, err := ioutil.ReadFile(file)
if err != nil {
return false, err
}
for _, s := range strings.Split(strings.TrimSpace(string(words)), " ") {
if s == strings.TrimSpace(word) {
return true, nil
}
}
return false, nil
}
func (b *Bonding) linkDown() error {
link, err := netlink.LinkByName(b.name)
if err != nil {
return err
}
return netlink.LinkSetDown(link)
}
func (b *Bonding) ListSlaves() ([]string, error) {
file := base + b.name + "/bonding/slaves"
words, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
result := []string{}
for _, s := range strings.Split(strings.TrimSpace(string(words)), " ") {
if s != "" {
result = append(result, s)
}
}
return result, nil
}
func (b *Bonding) RemoveSlave(slave string) error {
if ok, err := contains(base+b.name+"/bonding/slaves", slave); err != nil {
return err
} else if !ok {
return nil
}
p := base + b.name + "/bonding/slaves"
logrus.Infof("Removing slave %s from master %s", slave, b.name)
return ioutil.WriteFile(p, []byte("-"+slave), 0644)
}
func (b *Bonding) AddSlave(slave string) error {
if ok, err := contains(base+b.name+"/bonding/slaves", slave); err != nil {
return err
} else if ok {
return nil
}
p := base + b.name + "/bonding/slaves"
logrus.Infof("Adding slave %s to master %s", slave, b.name)
return ioutil.WriteFile(p, []byte("+"+slave), 0644)
}
func (b *Bonding) Opt(key, value string) error {
if key == "mode" {
// Don't care about errors here
b.linkDown()
slaves, _ := b.ListSlaves()
for _, slave := range slaves {
b.RemoveSlave(slave)
}
}
p := base + b.name + "/bonding/" + key
if err := ioutil.WriteFile(p, []byte(value), 0644); err != nil {
logrus.Errorf("Failed to set %s=%s on %s: %v", key, value, b.name, err)
return err
} else {
logrus.Infof("Set %s=%s on %s", key, value, b.name)
}
return nil
}
func Bond(name string) (*Bonding, error) {
b := &Bonding{name: name}
if err := b.init(); err != nil {
return nil, err
}
if ok, err := contains(bondingMasters, name); err != nil {
return nil, err
} else if ok {
return b, nil
}
logrus.Infof("Creating bond %s", name)
return b, ioutil.WriteFile(bondingMasters, []byte("+"+name), 0644)
}

View File

@@ -1,48 +0,0 @@
package netconf
import (
"fmt"
"github.com/vishvananda/netlink"
)
type Bridge struct {
name string
}
func NewBridge(name string) (*Bridge, error) {
b := &Bridge{name: name}
return b, b.init()
}
func (b *Bridge) init() error {
link, err := netlink.LinkByName(b.name)
if err == nil {
if _, ok := link.(*netlink.Bridge); !ok {
return fmt.Errorf("%s is not a bridge device", b.name)
}
return nil
}
bridge := netlink.Bridge{}
bridge.LinkAttrs.Name = b.name
return netlink.LinkAdd(&bridge)
}
func (b *Bridge) AddLink(link netlink.Link) error {
existing, err := netlink.LinkByName(b.name)
if err != nil {
return err
}
if bridge, ok := existing.(*netlink.Bridge); ok {
if link.Attrs().MasterIndex != bridge.Index {
return netlink.LinkSetMaster(link, bridge)
}
} else {
return fmt.Errorf("%s is not a bridge", b.name)
}
return nil
}

View File

@@ -1,75 +0,0 @@
package netconf
import (
"encoding/binary"
"fmt"
"math/rand"
"net"
log "github.com/Sirupsen/logrus"
"github.com/j-keck/arping"
"github.com/vishvananda/netlink"
)
func AssignLinkLocalIP(link netlink.Link) error {
ifaceName := link.Attrs().Name
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
log.Error("could not get information about interface")
return err
}
addrs, err := iface.Addrs()
if err != nil {
log.Error("Error fetching existing ip on interface")
}
for _, addr := range addrs {
if addr.String()[:7] == "169.254" {
log.Info("Link Local IP already set on interface")
return nil
}
}
randSource, err := getPseudoRandomGenerator(link.Attrs().HardwareAddr)
if err != nil {
return err
}
// try a random address upto 10 times
for i := 0; i < 10; i++ {
randGenerator := rand.New(*randSource)
randomNum := randGenerator.Uint32()
dstIP := getNewIPV4LLAddr(randomNum)
if dstIP[2] == 0 || dstIP[2] == 255 {
i--
continue
}
_, _, err := arping.PingOverIfaceByName(dstIP, ifaceName)
if err != nil {
// this ip is not being used
addr, err := netlink.ParseAddr(dstIP.String() + "/16")
if err != nil {
log.Errorf("error while parsing ipv4ll addr, err = %v", err)
return err
}
if err := netlink.AddrAdd(link, addr); err != nil {
log.Error("ipv4ll addr add failed")
return err
}
log.Infof("Set %s on %s", dstIP.String(), link.Attrs().Name)
return nil
}
}
log.Error("Could not find a suitable ipv4ll")
return fmt.Errorf("Could not find a suitable ipv4ll")
}
func getNewIPV4LLAddr(randomNum uint32) net.IP {
byte1 := randomNum & 255 // use least significant 8 bits
byte2 := randomNum >> 24 // use most significant 8 bits
return []byte{169, 254, byte(byte1), byte(byte2)}
}
func getPseudoRandomGenerator(haAddr []byte) (*rand.Source, error) {
seed, _ := binary.Varint(haAddr)
src := rand.NewSource(seed)
return &src, nil
}

View File

@@ -1,395 +0,0 @@
package netconf
import (
"bytes"
"errors"
"net"
"os"
"os/exec"
"strings"
"sync"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/flynn/go-shlex"
"github.com/ryanuber/go-glob"
"github.com/vishvananda/netlink"
)
const (
CONF = "/var/lib/rancher/conf"
MODE = "mode"
)
var (
defaultDhcpArgs = []string{"dhcpcd", "-MA4"}
)
func createInterfaces(netCfg *NetworkConfig) {
configured := map[string]bool{}
for name, iface := range netCfg.Interfaces {
if iface.Bridge == "true" {
if _, err := NewBridge(name); err != nil {
log.Errorf("Failed to create bridge %s: %v", name, err)
}
} else if iface.Bridge != "" {
if _, err := NewBridge(iface.Bridge); err != nil {
log.Errorf("Failed to create bridge %s: %v", iface.Bridge, err)
}
} else if iface.Bond != "" {
bond, err := Bond(iface.Bond)
if err != nil {
log.Errorf("Failed to create bond %s: %v", iface.Bond, err)
continue
}
if !configured[iface.Bond] {
if bondIface, ok := netCfg.Interfaces[iface.Bond]; ok {
// Other settings depends on mode, so set it first
if v, ok := bondIface.BondOpts[MODE]; ok {
bond.Opt(MODE, v)
}
for k, v := range bondIface.BondOpts {
if k != MODE {
bond.Opt(k, v)
}
}
configured[iface.Bond] = true
}
}
}
}
}
func createSlaveInterfaces(netCfg *NetworkConfig) {
links, err := netlink.LinkList()
if err != nil {
log.Errorf("Failed to list links: %v", err)
return
}
for _, link := range links {
match, ok := findMatch(link, netCfg)
if !ok {
continue
}
vlanDefs, err := ParseVlanDefinitions(match.Vlans)
if err != nil {
log.Errorf("Failed to create vlans on device %s: %v", link.Attrs().Name, err)
continue
}
for _, vlanDef := range vlanDefs {
if _, err = NewVlan(link, vlanDef.Name, vlanDef.Id); err != nil {
log.Errorf("Failed to create vlans on device %s, id %d: %v", link.Attrs().Name, vlanDef.Id, err)
}
}
}
}
func findMatch(link netlink.Link, netCfg *NetworkConfig) (InterfaceConfig, bool) {
linkName := link.Attrs().Name
var match InterfaceConfig
exactMatch := false
found := false
for key, netConf := range netCfg.Interfaces {
if netConf.Match == "" {
netConf.Match = key
}
if netConf.Match == "" {
continue
}
if strings.HasPrefix(netConf.Match, "mac") {
haAddr, err := net.ParseMAC(netConf.Match[4:])
if err != nil {
log.Errorf("Failed to parse mac %s: %v", netConf.Match[4:], err)
continue
}
// Don't match mac address of the bond because it is the same as the slave
if bytes.Compare(haAddr, link.Attrs().HardwareAddr) == 0 && link.Attrs().Name != netConf.Bond {
// MAC address match is used over all other matches
return netConf, true
}
}
if !exactMatch && glob.Glob(netConf.Match, linkName) {
match = netConf
found = true
}
if netConf.Match == linkName {
// Found exact match, use it over wildcard match
match = netConf
exactMatch = true
}
}
return match, exactMatch || found
}
func populateDefault(netCfg *NetworkConfig) {
if netCfg.Interfaces == nil {
netCfg.Interfaces = map[string]InterfaceConfig{}
}
if len(netCfg.Interfaces) == 0 {
netCfg.Interfaces["eth*"] = InterfaceConfig{
DHCP: true,
}
}
if _, ok := netCfg.Interfaces["lo"]; !ok {
netCfg.Interfaces["lo"] = InterfaceConfig{
Address: "127.0.0.1/8",
}
}
}
func ApplyNetworkConfigs(netCfg *NetworkConfig) error {
populateDefault(netCfg)
log.Debugf("Config: %#v", netCfg)
runCmds(netCfg.PreCmds, "")
createInterfaces(netCfg)
createSlaveInterfaces(netCfg)
links, err := netlink.LinkList()
if err != nil {
return err
}
//apply network config
for _, link := range links {
linkName := link.Attrs().Name
if match, ok := findMatch(link, netCfg); ok && !match.DHCP {
if err := applyInterfaceConfig(link, match); err != nil {
log.Errorf("Failed to apply settings to %s : %v", linkName, err)
}
}
}
runCmds(netCfg.PostCmds, "")
return err
}
func RunDhcp(netCfg *NetworkConfig, setHostname, setDns bool) error {
populateDefault(netCfg)
links, err := netlink.LinkList()
if err != nil {
return err
}
dhcpLinks := map[string]string{}
for _, link := range links {
if match, ok := findMatch(link, netCfg); ok && match.DHCP {
dhcpLinks[link.Attrs().Name] = match.DHCPArgs
}
}
//run dhcp
wg := sync.WaitGroup{}
for iface, args := range dhcpLinks {
wg.Add(1)
go func(iface, args string) {
runDhcp(netCfg, iface, args, setHostname, setDns)
wg.Done()
}(iface, args)
}
wg.Wait()
return err
}
func runDhcp(netCfg *NetworkConfig, iface string, argstr string, setHostname, setDns bool) {
log.Infof("Running DHCP on %s", iface)
args := []string{}
if argstr != "" {
var err error
args, err = shlex.Split(argstr)
if err != nil {
log.Errorf("Failed to parse [%s]: %v", argstr, err)
}
}
if len(args) == 0 {
args = defaultDhcpArgs
}
if setHostname {
args = append(args, "-e", "force_hostname=true")
}
if !setDns {
args = append(args, "--nohook", "resolv.conf")
}
args = append(args, iface)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Error(err)
}
}
func linkUp(link netlink.Link, netConf InterfaceConfig) error {
if err := netlink.LinkSetUp(link); err != nil {
log.Errorf("failed to setup link: %v", err)
return err
}
return nil
}
func applyAddress(address string, link netlink.Link, netConf InterfaceConfig) error {
addr, err := netlink.ParseAddr(address)
if err != nil {
return err
}
if err := netlink.AddrAdd(link, addr); err == syscall.EEXIST {
//Ignore this error
} else if err != nil {
log.Errorf("addr add failed: %v", err)
} else {
log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
}
return nil
}
func setGateway(gateway string) error {
if gateway == "" {
return nil
}
gatewayIp := net.ParseIP(gateway)
if gatewayIp == nil {
return errors.New("Invalid gateway address " + gateway)
}
route := netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
Gw: gatewayIp,
}
if err := netlink.RouteAdd(&route); err == syscall.EEXIST {
//Ignore this error
} else if err != nil {
log.Errorf("gateway set failed: %v", err)
return err
}
log.Infof("Set default gateway %s", gateway)
return nil
}
func applyInterfaceConfig(link netlink.Link, netConf InterfaceConfig) error {
if netConf.Bond != "" {
if err := netlink.LinkSetDown(link); err != nil {
return err
}
b, err := Bond(netConf.Bond)
if err != nil {
return err
}
if err := b.AddSlave(link.Attrs().Name); err != nil {
return err
}
return nil
}
if netConf.Bridge != "" && netConf.Bridge != "true" {
b, err := NewBridge(netConf.Bridge)
if err != nil {
return err
}
if err := b.AddLink(link); err != nil {
return err
}
return linkUp(link, netConf)
}
if netConf.IPV4LL {
if err := AssignLinkLocalIP(link); err != nil {
log.Errorf("IPV4LL set failed: %v", err)
return err
}
} else {
addresses := []string{}
if netConf.Address != "" {
addresses = append(addresses, netConf.Address)
}
if len(netConf.Addresses) > 0 {
addresses = append(addresses, netConf.Addresses...)
}
for _, address := range addresses {
err := applyAddress(address, link, netConf)
if err != nil {
log.Errorf("Failed to apply address %s to %s: %v", address, link.Attrs().Name, err)
}
}
}
if netConf.MTU > 0 {
if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
log.Errorf("set MTU Failed: %v", err)
return err
}
}
runCmds(netConf.PreUp, link.Attrs().Name)
if err := linkUp(link, netConf); err != nil {
return err
}
if err := setGateway(netConf.Gateway); err != nil {
log.Errorf("Fail to set gateway %s", netConf.Gateway)
}
if err := setGateway(netConf.GatewayIpv6); err != nil {
log.Errorf("Fail to set gateway %s", netConf.GatewayIpv6)
}
runCmds(netConf.PostUp, link.Attrs().Name)
return nil
}
func runCmds(cmds []string, iface string) {
for _, cmd := range cmds {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
continue
}
args, err := shlex.Split(strings.Replace(cmd, "$iface", iface, -1))
if err != nil {
log.Errorf("Failed to parse command [%s]: %v", cmd, err)
continue
}
log.Infof("Running command %s %v", args[0], args[1:])
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Errorf("Failed to run command [%s]: %v", cmd, err)
continue
}
}
}

View File

@@ -1,34 +0,0 @@
package netconf
type NetworkConfig struct {
PreCmds []string `yaml:"pre_cmds,omitempty"`
Dns DnsConfig `yaml:"dns,omitempty"`
Interfaces map[string]InterfaceConfig `yaml:"interfaces,omitempty"`
PostCmds []string `yaml:"post_cmds,omitempty"`
HttpProxy string `yaml:"http_proxy,omitempty"`
HttpsProxy string `yaml:"https_proxy,omitempty"`
NoProxy string `yaml:"no_proxy,omitempty"`
}
type InterfaceConfig struct {
Match string `yaml:"match,omitempty"`
DHCP bool `yaml:"dhcp,omitempty"`
DHCPArgs string `yaml:"dhcp_args,omitempty"`
Address string `yaml:"address,omitempty"`
Addresses []string `yaml:"addresses,omitempty"`
IPV4LL bool `yaml:"ipv4ll,omitempty"`
Gateway string `yaml:"gateway,omitempty"`
GatewayIpv6 string `yaml:"gateway_ipv6,omitempty"`
MTU int `yaml:"mtu,omitempty"`
Bridge string `yaml:"bridge,omitempty"`
Bond string `yaml:"bond,omitempty"`
BondOpts map[string]string `yaml:"bond_opts,omitempty"`
PostUp []string `yaml:"post_up,omitempty"`
PreUp []string `yaml:"pre_up,omitempty"`
Vlans string `yaml:"vlans,omitempty"`
}
type DnsConfig struct {
Nameservers []string `yaml:"nameservers,flow,omitempty"`
Search []string `yaml:"search,flow,omitempty"`
}

View File

@@ -1,79 +0,0 @@
package netconf
import (
"fmt"
"strconv"
"strings"
"github.com/vishvananda/netlink"
)
type VlanDefinition struct {
Id int
Name string
}
type Vlan struct {
name string
link netlink.Link
id int
}
func NewVlan(link netlink.Link, name string, id int) (*Vlan, error) {
if name == "" {
name = fmt.Sprintf("%s.%d", link.Attrs().Name, id)
}
v := &Vlan{
name: name,
link: link,
id: id,
}
return v, v.init()
}
func (v *Vlan) init() error {
link, err := netlink.LinkByName(v.name)
if err == nil {
if _, ok := link.(*netlink.Vlan); !ok {
return fmt.Errorf("%s is not a VLAN device", v.name)
}
return nil
}
vlan := netlink.Vlan{}
vlan.ParentIndex = v.link.Attrs().Index
vlan.Name = v.name
vlan.VlanId = v.id
return netlink.LinkAdd(&vlan)
}
func ParseVlanDefinitions(vlans string) ([]VlanDefinition, error) {
vlans = strings.TrimSpace(vlans)
if vlans == "" {
return nil, nil
}
result := []VlanDefinition{}
for _, vlan := range strings.Split(vlans, ",") {
idName := strings.SplitN(strings.TrimSpace(vlan), ":", 2)
id, err := strconv.Atoi(idName[0])
if err != nil {
return nil, fmt.Errorf("Invalid format in %s: %v", vlans, err)
}
def := VlanDefinition{
Id: id,
}
if len(idName) > 1 {
def.Name = idName[1]
}
result = append(result, def)
}
return result, nil
}