-
Notifications
You must be signed in to change notification settings - Fork 277
cmdline: add support for loading config from a local device #2230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atd9876
wants to merge
4
commits into
coreos:main
Choose a base branch
from
atd9876:add-metal-config-drive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f34f4a
add support for loading config from device
tuunit 79fc035
add tests and documentation for cmdline device config support
atd9876 a13e77b
cmdline: improve robustness of parseCmdline and tests
atd9876 0dc3790
cmdline: address review feedback and add blackbox test
atd9876 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,29 +13,48 @@ | |
| // limitations under the License. | ||
|
|
||
| // The cmdline provider fetches a remote configuration from the URL specified | ||
| // in the kernel boot option "ignition.config.url". | ||
| // in the kernel boot option "ignition.config.url", or from a local device | ||
| // specified by "ignition.config.device" and "ignition.config.path". | ||
|
|
||
| package cmdline | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/url" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| configErrors "github.com/coreos/ignition/v2/config/shared/errors" | ||
| "github.com/coreos/ignition/v2/config/v3_7_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" | ||
| ) | ||
|
|
||
| type cmdlineFlag string | ||
|
|
||
| const ( | ||
| cmdlineUrlFlag = "ignition.config.url" | ||
| flagUrl cmdlineFlag = "ignition.config.url" | ||
| flagDeviceLabel cmdlineFlag = "ignition.config.device" | ||
| flagUserDataPath cmdlineFlag = "ignition.config.path" | ||
| ) | ||
|
|
||
| type cmdlineOpts struct { | ||
| Url *url.URL | ||
| UserDataPath string | ||
| DeviceLabel string | ||
| } | ||
|
|
||
| var ( | ||
| // we are a special-cased system provider; don't register ourselves | ||
| // for lookup by name | ||
|
|
@@ -46,59 +65,152 @@ var ( | |
| ) | ||
|
|
||
| func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) { | ||
| url, err := readCmdline(f.Logger) | ||
| opts, err := parseCmdline(f.Logger, distro.KernelCmdlinePath()) | ||
| if err != nil { | ||
| return types.Config{}, report.Report{}, err | ||
| } | ||
|
|
||
| if url == nil { | ||
| return types.Config{}, report.Report{}, platform.ErrNoProvider | ||
| var data []byte | ||
|
|
||
| if opts.Url != nil { | ||
| data, err = f.FetchToBuffer(*opts.Url, resource.FetchOptions{}) | ||
| if err != nil { | ||
| return types.Config{}, report.Report{}, err | ||
| } | ||
|
|
||
| return util.ParseConfig(f.Logger, data) | ||
| } | ||
|
|
||
| if opts.UserDataPath != "" && opts.DeviceLabel != "" { | ||
| return fetchConfigFromDevice(f.Logger, opts) | ||
| } | ||
|
|
||
| data, err := f.FetchToBuffer(*url, resource.FetchOptions{}) | ||
| if err != nil { | ||
| return types.Config{}, report.Report{}, err | ||
| if opts.UserDataPath != "" || opts.DeviceLabel != "" { | ||
| f.Logger.Warning("both %q and %q must be provided together; ignoring", | ||
| string(flagDeviceLabel), string(flagUserDataPath)) | ||
| } | ||
|
|
||
| return util.ParseConfig(f.Logger, data) | ||
| return types.Config{}, report.Report{}, platform.ErrNoProvider | ||
| } | ||
|
|
||
| func readCmdline(logger *log.Logger) (*url.URL, error) { | ||
| args, err := os.ReadFile(distro.KernelCmdlinePath()) | ||
| func parseCmdline(logger *log.Logger, path string) (*cmdlineOpts, error) { | ||
| cmdline, err := os.ReadFile(path) | ||
| if err != nil { | ||
| logger.Err("couldn't read cmdline: %v", err) | ||
| return nil, err | ||
| } | ||
|
|
||
| rawUrl := parseCmdline(args) | ||
| logger.Debug("parsed url from cmdline: %q", rawUrl) | ||
| if rawUrl == "" { | ||
| logger.Info("no config URL provided") | ||
| return nil, nil | ||
| opts := &cmdlineOpts{} | ||
|
|
||
| for _, arg := range strings.Fields(string(cmdline)) { | ||
| parts := strings.SplitN(strings.TrimSpace(arg), "=", 2) | ||
| if len(parts) != 2 { | ||
| continue | ||
| } | ||
|
|
||
| key := cmdlineFlag(parts[0]) | ||
| value := parts[1] | ||
|
|
||
| switch key { | ||
| case flagUrl: | ||
| if value == "" { | ||
| logger.Info("url flag found but no value provided") | ||
| continue | ||
| } | ||
|
|
||
| parsedURL, err := url.Parse(value) | ||
| if err != nil { | ||
| logger.Err("failed to parse url: %v", err) | ||
| continue | ||
| } | ||
| opts.Url = parsedURL | ||
| case flagDeviceLabel: | ||
| if value == "" { | ||
| logger.Info("device label flag found but no value provided") | ||
| continue | ||
| } | ||
| opts.DeviceLabel = value | ||
| case flagUserDataPath: | ||
| if value == "" { | ||
| logger.Info("user data path flag found but no value provided") | ||
| continue | ||
| } | ||
| opts.UserDataPath = value | ||
| } | ||
| } | ||
|
|
||
| url, err := url.Parse(rawUrl) | ||
| return opts, nil | ||
| } | ||
|
|
||
| func fetchConfigFromDevice(logger *log.Logger, opts *cmdlineOpts) (types.Config, report.Report, error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
|
prestist marked this conversation as resolved.
|
||
| defer cancel() | ||
|
|
||
| data, err := tryMounting(logger, ctx, opts) | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return types.Config{}, report.Report{}, fmt.Errorf("device %q did not appear within timeout", opts.DeviceLabel) | ||
| } | ||
|
atd9876 marked this conversation as resolved.
|
||
| if err != nil { | ||
| logger.Err("failed to parse url: %v", err) | ||
| return nil, err | ||
| return types.Config{}, report.Report{}, err | ||
| } | ||
| if data == nil { | ||
| logger.Info("config file %q not found on device. Continuing without config...", opts.UserDataPath) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not sure we would want to continue if we cannot find the user specified config. |
||
| return types.Config{}, report.Report{}, configErrors.ErrEmpty | ||
| } | ||
|
|
||
| return url, err | ||
| return util.ParseConfig(logger, data) | ||
| } | ||
|
|
||
| func parseCmdline(cmdline []byte) (url string) { | ||
| for _, arg := range strings.Split(string(cmdline), " ") { | ||
| parts := strings.SplitN(strings.TrimSpace(arg), "=", 2) | ||
| key := parts[0] | ||
|
|
||
| if key != cmdlineUrlFlag { | ||
| continue | ||
| func tryMounting(logger *log.Logger, ctx context.Context, opts *cmdlineOpts) ([]byte, error) { | ||
| device := filepath.Join(distro.DiskByLabelDir(), opts.DeviceLabel) | ||
| for !fileExists(device) { | ||
| logger.Debug("disk (%q) not found. Waiting...", device) | ||
| select { | ||
| case <-time.After(time.Second): | ||
| case <-ctx.Done(): | ||
| return nil, ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| if len(parts) == 2 { | ||
| url = parts[1] | ||
| 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 func() { | ||
| if err := os.Remove(mnt); err != nil { | ||
| logger.Err("failed to remove temporary mount point %q: %v", mnt, err) | ||
| } | ||
| }() | ||
|
|
||
| cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", device, mnt) | ||
| if _, err := logger.LogCmd(cmd, "mounting disk"); err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| _ = logger.LogOp( | ||
| func() error { | ||
| return ut.UmountPath(mnt) | ||
| }, | ||
| "unmounting %q at %q", device, mnt, | ||
| ) | ||
| }() | ||
|
|
||
| configPath := filepath.Join(mnt, filepath.Clean(filepath.Join("/", opts.UserDataPath))) | ||
| if !fileExists(configPath) { | ||
| logger.Debug("config file %q not found on device %q", opts.UserDataPath, opts.DeviceLabel) | ||
| return nil, nil | ||
| } | ||
|
|
||
| contents, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return | ||
| return contents, nil | ||
| } | ||
|
|
||
| func fileExists(path string) bool { | ||
| _, err := os.Stat(path) | ||
| return (err == nil) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this feels like it could lead to misconfiguration, if a user sets the wrong file location, we would get an error, but then that would essentially lead to a log, and the continuation of the provisioning no?