From e2aa15e642df5991fe12d8984e36c0e7c394a74f Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Wed, 4 Sep 2024 20:42:34 -0600 Subject: [PATCH] Update grub2disk: Use alpine for final container in the Dockerfile as there are quite a few shell commands used. Signed-off-by: Jacob Weinstock --- grub2disk/Dockerfile | 20 +++++++++----------- grub2disk/grub/grub.go | 7 +++---- grub2disk/main.go | 4 ---- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/grub2disk/Dockerfile b/grub2disk/Dockerfile index 5eb8b8d..82663bb 100644 --- a/grub2disk/Dockerfile +++ b/grub2disk/Dockerfile @@ -1,6 +1,4 @@ -# syntax=docker/dockerfile:1 - -FROM golang:1.21-alpine AS base +FROM golang:1.23-alpine AS base FROM base AS build-amd64 RUN apk add --no-cache grub grub-bios git ca-certificates gcc musl-dev @@ -9,12 +7,12 @@ FROM base AS build-arm64 RUN apk add --no-cache grub git ca-certificates gcc musl-dev FROM build-${TARGETARCH} AS grub2disk -COPY . /src -WORKDIR /src/grub2disk -RUN --mount=type=cache,sharing=locked,id=gomod,target=/go/pkg/mod/cache \ - --mount=type=cache,sharing=locked,id=goroot,target=/root/.cache/go-build \ - CGO_ENABLED=1 GOOS=linux go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o grub2disk +WORKDIR /src +COPY go.mod go.sum /src/ +RUN go mod download +COPY . /src/ +RUN CGO_ENABLED=1 GOOS=linux go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /src/grub2disk/grub2disk /src/grub2disk/ -FROM scratch -COPY --from=grub2disk /src/grub2disk/grub2disk /usr/bin/grub2disk -ENTRYPOINT ["/usr/bin/grub2disk"] +FROM alpine:3.20.2 +COPY --from=grub2disk /src/grub2disk/grub2disk /usr/sbin/grub2disk +ENTRYPOINT ["/usr/sbin/grub2disk"] diff --git a/grub2disk/grub/grub.go b/grub2disk/grub/grub.go index 7bf6615..99206a1 100644 --- a/grub2disk/grub/grub.go +++ b/grub2disk/grub/grub.go @@ -2,6 +2,7 @@ package grub import ( "fmt" + "os" "os/exec" "syscall" @@ -9,10 +10,8 @@ import ( ) func makeDirAndMount(devicePath string, mountPoint string, filesystemType string) (err error) { - // if err = os.Mkdir(mountPoint, os.ModeDir); err != nil { - if _, err = exec.Command("/bin/sh", "-c", "/bin/mkdir -p "+mountPoint).Output(); err != nil { - log.Error(fmt.Errorf("failed to create the directory %s", mountPoint)) - return err + if err := os.MkdirAll(mountPoint, os.ModeDir); err != nil { + return fmt.Errorf("failed to create the directory %s: %w", mountPoint, err) } // if err = syscall.Mount(devicePath, mountPoint, filesystemType, flag, ""); err != nil { diff --git a/grub2disk/main.go b/grub2disk/main.go index 31b74e3..421f61d 100644 --- a/grub2disk/main.go +++ b/grub2disk/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "os" - "os/exec" log "github.com/sirupsen/logrus" "github.com/tinkerbell/actions/grub2disk/grub" @@ -15,9 +14,6 @@ func main() { grubBlockDevice := os.Getenv("GRUB_DISK") filesystemType := os.Getenv("FS_TYPE") - if _, err := exec.Command("/bin/sh", "-c", "apk add grub grub-bios").Output(); err != nil { - log.Fatal(fmt.Errorf("failed to install grub-install with error %w", err)) - } if err := grub.MountGrub(grubInstallPath, grubBlockDevice, filesystemType); err != nil { log.Fatal(err) }