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 <dave.scott@docker.com>
This commit is contained in:
David Scott 2018-06-29 14:47:46 +01:00
parent 6dfd40a639
commit 1795c15276
4 changed files with 31 additions and 21 deletions

View File

@ -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

View File

@ -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/

View File

@ -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)

View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/memlogd -daemonize