From 1795c1527616097a8ab38f2acb1d9f57c8fd6f7e Mon Sep 17 00:00:00 2001 From: David Scott Date: Fri, 29 Jun 2018 14:47:46 +0100 Subject: [PATCH] projects/logging: start memlogd from init.d Previously memlogd would always run in the foreground. This patch adds a `-daemonize` option which binds the /var/run sockets, forks and execs itself and immediately returns. Therefore the program won't block (important for an init.d script) but guarantees the sockets will be available for any program started afterwards. This also removes the alpine base from the memlogd image as `init` "containers" are treated as simple file overlays. Signed-off-by: David Scott --- projects/logging/examples/logging.yml | 2 +- projects/logging/pkg/memlogd/Dockerfile | 21 ++------------- .../logging/pkg/memlogd/cmd/memlogd/main.go | 26 ++++++++++++++++++- .../pkg/memlogd/etc/init.d/002-logging | 3 +++ 4 files changed, 31 insertions(+), 21 deletions(-) create mode 100755 projects/logging/pkg/memlogd/etc/init.d/002-logging diff --git a/projects/logging/examples/logging.yml b/projects/logging/examples/logging.yml index 4e5c87128..92a2ba1b8 100644 --- a/projects/logging/examples/logging.yml +++ b/projects/logging/examples/logging.yml @@ -6,7 +6,7 @@ init: - linuxkit/runc:v0.4 - linuxkit/containerd:27a4c84cc8fab2d5ea04342546ecd20453ec99b3 - linuxkit/ca-certificates:v0.4 - - linuxkit/memlogd:9b5834189f598f43c507f6938077113906f51012 + - linuxkit/memlogd:4c16d6720075bcb2aeb298a8ee2d08d74aa20512 onboot: - name: sysctl image: linuxkit/sysctl:v0.4 diff --git a/projects/logging/pkg/memlogd/Dockerfile b/projects/logging/pkg/memlogd/Dockerfile index 1fab5f75b..bf6b45d82 100644 --- a/projects/logging/pkg/memlogd/Dockerfile +++ b/projects/logging/pkg/memlogd/Dockerfile @@ -1,20 +1,3 @@ -FROM linuxkit/alpine:1b05307ae8152e3d38f79e297b0632697a30c65c AS mirror - -RUN mkdir -p /out/etc/apk && cp -r /etc/apk/* /out/etc/apk/ -RUN apk add --no-cache --initdb -p /out \ - alpine-baselayout \ - busybox \ - e2fsprogs \ - e2fsprogs-extra \ - btrfs-progs \ - xfsprogs \ - xfsprogs-extra \ - musl \ - sfdisk \ - util-linux \ - && true -RUN rm -rf /out/etc/apk /out/lib/apk /out/var/cache - FROM linuxkit/alpine:1b05307ae8152e3d38f79e297b0632697a30c65c AS build RUN apk add --no-cache go musl-dev @@ -29,8 +12,8 @@ FROM scratch ENTRYPOINT [] CMD [] WORKDIR / -COPY --from=mirror /out/ / COPY --from=build /go/bin/memlogd usr/bin/memlogd COPY --from=build /go/bin/logread usr/bin/logread COPY --from=build /go/bin/logwrite usr/bin/logwrite -CMD ["/usr/bin/memlogd"] +# We'll start from init.d +COPY etc/ /etc/ diff --git a/projects/logging/pkg/memlogd/cmd/memlogd/main.go b/projects/logging/pkg/memlogd/cmd/memlogd/main.go index 0c4955815..4a3dae60e 100644 --- a/projects/logging/pkg/memlogd/cmd/memlogd/main.go +++ b/projects/logging/pkg/memlogd/cmd/memlogd/main.go @@ -11,6 +11,7 @@ import ( "log" "net" "os" + "os/exec" "sync" "syscall" "time" @@ -254,6 +255,7 @@ func main() { var passedLogFD int var linesInBuffer int var lineMaxLength int + var daemonize bool flag.StringVar(&socketQueryPath, "socket-query", "/var/run/memlogdq.sock", "unix domain socket for responding to log queries. Overridden by -fd-query") flag.StringVar(&socketLogPath, "socket-log", "/var/run/linuxkit-external-logging.sock", "unix domain socket to listen for new fds to add to log. Overridden by -fd-log") @@ -261,7 +263,7 @@ func main() { flag.IntVar(&passedQueryFD, "fd-query", -1, "an existing SOCK_STREAM for receiving log read requets. Overrides -socket-query.") flag.IntVar(&linesInBuffer, "max-lines", 5000, "Number of log lines to keep in memory") flag.IntVar(&lineMaxLength, "max-line-len", 1024, "Maximum line length recorded. Additional bytes are dropped.") - + flag.BoolVar(&daemonize, "daemonize", false, "Bind sockets and then daemonize.") flag.Parse() var connLogFd *net.UnixConn @@ -302,6 +304,28 @@ func main() { } defer connQuery.Close() + if daemonize { + child := exec.Command(os.Args[0], + "-fd-log", "3", // connLogFd in ExtraFiles below + "-fd-query", "4", // connQuery in ExtraFiles below + "-max-lines", fmt.Sprintf("%d", linesInBuffer), + "-max-line-len", fmt.Sprintf("%d", lineMaxLength), + ) + connLogFile, err := connLogFd.File() + if err != nil { + log.Fatalf("The -fd-log cannot be represented as a *File: %s", err) + } + connQueryFile, err := connQuery.File() + if err != nil { + log.Fatalf("The -fd-query cannot be represented as a *File: %s", err) + } + child.ExtraFiles = append(child.ExtraFiles, connLogFile, connQueryFile) + if err := child.Start(); err != nil { + log.Fatalf("Failed to re-exec: %s", err) + } + os.Exit(0) + } + logCh := make(chan logEntry) fdMsgChan := make(chan fdMessage) queryMsgChan := make(chan queryMessage) diff --git a/projects/logging/pkg/memlogd/etc/init.d/002-logging b/projects/logging/pkg/memlogd/etc/init.d/002-logging new file mode 100755 index 000000000..50a53fd3d --- /dev/null +++ b/projects/logging/pkg/memlogd/etc/init.d/002-logging @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/memlogd -daemonize