Skip to content

Commit

Permalink
add mounting of root partition
Browse files Browse the repository at this point in the history
  • Loading branch information
tuunit committed Nov 1, 2024
1 parent b458ea9 commit 9e2a8ff
Showing 1 changed file with 95 additions and 10 deletions.
105 changes: 95 additions & 10 deletions internal/providers/ionoscloud/ionoscloud.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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() {
Expand All @@ -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("cidata drive 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-configdrive")
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 config drive"); 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
}

0 comments on commit 9e2a8ff

Please sign in to comment.