From 488d302a0863ede5b723aea4ddd558f96e318569 Mon Sep 17 00:00:00 2001 From: Jan Larwig Date: Fri, 1 Nov 2024 09:40:18 +0100 Subject: [PATCH] add mounting of root partition --- internal/providers/ionoscloud/ionoscloud.go | 105 ++++++++++++++++++-- 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/internal/providers/ionoscloud/ionoscloud.go b/internal/providers/ionoscloud/ionoscloud.go index 061ecbf30..3fd5787f2 100644 --- a/internal/providers/ionoscloud/ionoscloud.go +++ b/internal/providers/ionoscloud/ionoscloud.go @@ -1,4 +1,4 @@ -// Copyright 2019 Red Hat, Inc. +// Copyright 2024 Red Hat, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,18 +19,28 @@ package ionoscloud import ( + "context" + "fmt" "os" + "os/exec" + "path/filepath" + "time" "github.com/coreos/ignition/v2/config/v3_6_experimental/types" + "github.com/coreos/ignition/v2/internal/distro" + "github.com/coreos/ignition/v2/internal/log" "github.com/coreos/ignition/v2/internal/platform" "github.com/coreos/ignition/v2/internal/providers/util" "github.com/coreos/ignition/v2/internal/resource" + ut "github.com/coreos/ignition/v2/internal/util" "github.com/coreos/vcontext/report" ) const ( - defaultFilename = "/var/lib/cloud/seed/nocloud/user-data" + rootLabelEnvVar = "IGNITION_CONFIG_ROOT_LABEL" + defaultRootLabel = "ROOT" + userDataPath = "/var/lib/cloud/seed/nocloud/user-data" ) func init() { @@ -41,18 +51,93 @@ func init() { } func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) { - f.Logger.Info("using config file at %q", defaultFilename) + var data []byte + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - rawConfig, err := os.ReadFile(defaultFilename) + dispatch := func(name string, fn func() ([]byte, error)) { + raw, err := fn() + if err != nil { + switch err { + case context.Canceled: + case context.DeadlineExceeded: + f.Logger.Err("timed out while fetching config from %s", name) + default: + f.Logger.Err("failed to fetch config from %s: %v", name, err) + } + return + } + + data = raw + cancel() + } + + deviceLabel := os.Getenv(rootLabelEnvVar) + if deviceLabel == "" { + deviceLabel = defaultRootLabel + } + + go dispatch( + "load config from root partition", func() ([]byte, error) { + return fetchConfigFromDevice(f.Logger, ctx, filepath.Join(distro.DiskByLabelDir(), deviceLabel)) + }, + ) + + <-ctx.Done() + if ctx.Err() == context.DeadlineExceeded { + f.Logger.Info("root partition was not available in time. Continuing without a config...") + } + + return util.ParseConfig(f.Logger, data) +} + +func fileExists(path string) bool { + _, err := os.Stat(path) + return (err == nil) +} + +func fetchConfigFromDevice(logger *log.Logger, ctx context.Context, device string) ([]byte, error) { + for !fileExists(device) { + logger.Debug("root partition (%q) not found. Waiting...", device) + select { + case <-time.After(time.Second): + case <-ctx.Done(): + return nil, ctx.Err() + } + } + + logger.Debug("creating temporary mount point") + mnt, err := os.MkdirTemp("", "ignition-config") + if err != nil { + return nil, fmt.Errorf("failed to create temp directory: %v", err) + } + defer os.Remove(mnt) + + cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", device, mnt) + if _, err := logger.LogCmd(cmd, "mounting root partition"); err != nil { + return nil, err + } + defer func() { + _ = logger.LogOp( + func() error { + return ut.UmountPath(mnt) + }, + "unmounting %q at %q", device, mnt, + ) + }() + + if !fileExists(filepath.Join(mnt, userDataPath)) { + return nil, nil + } + + contents, err := os.ReadFile(filepath.Join(mnt, userDataPath)) if err != nil { - f.Logger.Err("couldn't read config %q: %v", defaultFilename, err) - return types.Config{}, report.Report{}, err + return nil, err } - if util.IsCloudConfig(rawConfig) { - f.Logger.Debug("config drive (%q) contains a cloud-config configuration, ignoring", defaultFilename) - return types.Config{}, report.Report{}, err + if util.IsCloudConfig(contents) { + logger.Debug("root partition (%q) contains a cloud-config configuration, ignoring", device) + return nil, nil } - return util.ParseConfig(f.Logger, rawConfig) + return contents, nil }