Switch runc and containerd startup to be entirely Go

At present they use a small shared function called "prepare"
that does the read-write remounts, that I will switch to doing overlay
mounts soon.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack
2017-07-26 16:16:25 +01:00
parent 5194bf13d1
commit cb2ca4ef66
14 changed files with 128 additions and 56 deletions

View File

@@ -1,4 +1,4 @@
FROM linuxkit/alpine:8bb8664eec04e02a8a131c53aa7d5d94119270ef as alpine
FROM linuxkit/alpine:a39a433162a873519910a07beeb3e8db22529956 as alpine
RUN \
apk add \
bash \
@@ -10,7 +10,7 @@ RUN \
linux-headers \
make \
&& true
ENV GOPATH=/root/go
ENV GOPATH=/go PATH=$PATH:/go/bin
ENV RUNC_COMMIT=429a5387123625040bacfbb60d96b1cbd02293ab
RUN mkdir -p $GOPATH/src/github.com/opencontainers && \
cd $GOPATH/src/github.com/opencontainers && \
@@ -20,8 +20,14 @@ RUN git checkout $RUNC_COMMIT
RUN make static BUILDTAGS="seccomp" EXTRA_FLAGS="-buildmode pie" EXTRA_LDFLAGS="-extldflags \\\"-fno-PIC -static\\\""
RUN cp runc /usr/bin/
ADD cmd /go/src/cmd
RUN go-compile.sh /go/src/cmd/onboot
RUN mkdir -p /etc/init.d && ln -s /usr/bin/onboot /etc/init.d/010-onboot
RUN mkdir -p /etc/shutdown.d && ln -s /usr/bin/onboot /etc/shutdown.d/010-onshutdown
FROM scratch
WORKDIR /
ENTRYPOINT []
COPY --from=alpine /usr/bin/runc /usr/bin/
COPY etc etc/
COPY --from=alpine /usr/bin/runc /go/bin/onboot /usr/bin/
COPY --from=alpine /etc/init.d/ /etc/init.d/
COPY --from=alpine /etc/shutdown.d/ /etc/shutdown.d/

View File

@@ -1,5 +1,5 @@
IMAGE=runc
NETWORK=1
DEPS=$(wildcard etc/init.d/*)
DEPS=$(wildcard cmd/onboot/*.go)
include ../package.mk

View File

@@ -0,0 +1,63 @@
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
runcBinary = "/usr/bin/runc"
onbootPath = "/containers/onboot"
shutdownPath = "/containers/onshutdown"
)
func main() {
// try to work out how we are being called
command := os.Args[0]
if len(os.Args) > 1 {
command = os.Args[1]
}
var path = onbootPath
switch {
case strings.Contains(command, "boot"):
path = onbootPath
case strings.Contains(command, "shutdown"):
path = shutdownPath
}
// do nothing if the path does not exist
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
os.Exit(0)
}
// get files; note ReadDir already sorts them
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatalf("Cannot read files in %s: %v", path, err)
}
status := 0
for _, file := range files {
name := file.Name()
fullPath := filepath.Join(path, name)
if err := prepare(fullPath); err != nil {
log.Printf("Error preparing %s: %v", name, err)
status = 1
continue
}
cmd := exec.Command(runcBinary, "run", "--bundle", fullPath, name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("Error running %s: %v", name, err)
status = 1
}
}
os.Exit(status)
}

View File

@@ -0,0 +1,21 @@
package main
// Please note this file is shared between pkg/runc and pkg/containerd
// Update it in both places if you make changes
import (
"path/filepath"
"syscall"
)
func prepare(path string) error {
rootfs := filepath.Join(path, "rootfs")
if err := syscall.Mount(rootfs, rootfs, "", syscall.MS_BIND, ""); err != nil {
return err
}
// remount rw
if err := syscall.Mount("", rootfs, "", syscall.MS_REMOUNT, ""); err != nil {
return err
}
return nil
}

View File

@@ -1,15 +0,0 @@
#!/bin/sh
# start onboot containers, run to completion
if [ -d /containers/onboot ]
then
for f in $(find /containers/onboot -mindepth 1 -maxdepth 1 | sort)
do
base="$(basename $f)"
/bin/mount --bind "$f/rootfs" "$f/rootfs"
mount -o remount,rw "$f/rootfs"
/usr/bin/runc run --bundle "$f" "$(basename $f)"
printf " - $base\n"
done
fi

View File

@@ -1,15 +0,0 @@
#!/bin/sh
# start onshutdown containers, run to completion
if [ -d /containers/onshutdown ]
then
for f in $(find /containers/onshutdown -mindepth 1 -maxdepth 1 | sort)
do
base="$(basename $f)"
/bin/mount --bind "$f/rootfs" "$f/rootfs"
mount -o remount,rw "$f/rootfs"
/usr/bin/runc run --bundle "$f" "$(basename $f)"
printf " - $base\n"
done
fi