-
Notifications
You must be signed in to change notification settings - Fork 94
bootc: support grub2 serial console customization #2403
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package osbuild | ||
|
|
||
| // Grub2DStageOptions represents options for the | ||
| // org.osbuild.grub2.d stage. | ||
| // | ||
| // This stage writes a GRUB2 drop-in configuration file at a | ||
| // configurable path relative to the filesystem root. | ||
| type Grub2DStageOptions struct { | ||
| // Path relative to the filesystem root | ||
| Path string `json:"path"` | ||
|
|
||
| // GRUB2 configuration to write | ||
| Config *Grub2DConfig `json:"config"` | ||
|
Member
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. Nitpick: this doesn't need to be a pointer, since |
||
| } | ||
|
|
||
| // Grub2DConfig contains GRUB2 settings for a drop-in config file. | ||
| type Grub2DConfig struct { | ||
| TerminalInput []string `json:"terminal_input,omitempty"` | ||
| TerminalOutput []string `json:"terminal_output,omitempty"` | ||
| Serial string `json:"serial,omitempty"` | ||
| } | ||
|
|
||
| func (Grub2DStageOptions) isStageOptions() {} | ||
|
|
||
| func NewGrub2DStage(options *Grub2DStageOptions) *Stage { | ||
| return &Stage{ | ||
| Type: "org.osbuild.grub2.d", | ||
| Options: options, | ||
| } | ||
| } | ||
|
Comment on lines
+25
to
+30
Member
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. The |
||
|
|
||
| // NewGrub2DConfigFromGrub2Config creates a Grub2DConfig from a | ||
| // GRUB2Config, extracting only the console-related fields. | ||
| // Returns nil if no console settings are present. | ||
| func NewGrub2DConfigFromGrub2Config(cfg *GRUB2Config) *Grub2DConfig { | ||
| if cfg == nil { | ||
| return nil | ||
| } | ||
| c := &Grub2DConfig{ | ||
| TerminalInput: cfg.TerminalInput, | ||
| TerminalOutput: cfg.TerminalOutput, | ||
| Serial: cfg.Serial, | ||
| } | ||
| if len(c.TerminalInput) == 0 && len(c.TerminalOutput) == 0 && c.Serial == "" { | ||
| return nil | ||
| } | ||
| return c | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package osbuild | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewGrub2DStage(t *testing.T) { | ||
| opts := &Grub2DStageOptions{ | ||
| Path: "tree:///boot/grub2/console.cfg", | ||
| Config: &Grub2DConfig{ | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| }, | ||
| } | ||
| stage := NewGrub2DStage(opts) | ||
| assert.Equal(t, "org.osbuild.grub2.d", stage.Type) | ||
| assert.Equal(t, opts, stage.Options) | ||
| } | ||
|
|
||
| func TestNewGrub2DConfigFromGrub2Config(t *testing.T) { | ||
| tests := map[string]struct { | ||
| input *GRUB2Config | ||
| expected *Grub2DConfig | ||
| }{ | ||
| "nil": { | ||
| input: nil, | ||
| expected: nil, | ||
| }, | ||
| "empty": { | ||
| input: &GRUB2Config{}, | ||
| expected: nil, | ||
| }, | ||
| "only-unrelated-fields": { | ||
| // GRUB2Config fields that are not console-related | ||
| // should not produce a Grub2DConfig | ||
| input: &GRUB2Config{ | ||
| Default: "saved", | ||
| Distributor: "$(sed 's, release .*$,,g' /etc/system-release)", | ||
| Timeout: 5, | ||
| }, | ||
| expected: nil, | ||
| }, | ||
| "serial-only": { | ||
| input: &GRUB2Config{ | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| }, | ||
| expected: &Grub2DConfig{ | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| }, | ||
| }, | ||
| "terminal-input-only": { | ||
| input: &GRUB2Config{ | ||
| TerminalInput: []string{"serial", "console"}, | ||
| }, | ||
| expected: &Grub2DConfig{ | ||
| TerminalInput: []string{"serial", "console"}, | ||
| }, | ||
| }, | ||
| "terminal-output-only": { | ||
| input: &GRUB2Config{ | ||
| TerminalOutput: []string{"serial"}, | ||
| }, | ||
| expected: &Grub2DConfig{ | ||
| TerminalOutput: []string{"serial"}, | ||
| }, | ||
| }, | ||
| "all-console-fields": { | ||
| input: &GRUB2Config{ | ||
| TerminalInput: []string{"serial", "console"}, | ||
| TerminalOutput: []string{"serial", "console"}, | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| // unrelated fields should be ignored | ||
| Default: "saved", | ||
| Timeout: 10, | ||
| }, | ||
| expected: &Grub2DConfig{ | ||
| TerminalInput: []string{"serial", "console"}, | ||
| TerminalOutput: []string{"serial", "console"}, | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| result := NewGrub2DConfigFromGrub2Config(tc.input) | ||
| assert.Equal(t, tc.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGrub2DStageOptionsJSON(t *testing.T) { | ||
| opts := &Grub2DStageOptions{ | ||
| Path: "tree:///boot/grub2/console.cfg", | ||
| Config: &Grub2DConfig{ | ||
| TerminalInput: []string{"serial", "console"}, | ||
| TerminalOutput: []string{"serial", "console"}, | ||
| Serial: "serial --unit=0 --speed=115200", | ||
| }, | ||
| } | ||
|
|
||
| data, err := json.MarshalIndent(opts, "", " ") | ||
| require.NoError(t, err) | ||
|
|
||
| expected := `{ | ||
| "path": "tree:///boot/grub2/console.cfg", | ||
| "config": { | ||
| "terminal_input": [ | ||
| "serial", | ||
| "console" | ||
| ], | ||
| "terminal_output": [ | ||
| "serial", | ||
| "console" | ||
| ], | ||
| "serial": "serial --unit=0 --speed=115200" | ||
| } | ||
| }` | ||
| assert.Equal(t, expected, string(data)) | ||
| } | ||
|
|
||
| func TestGrub2DStageOptionsJSONOmitEmpty(t *testing.T) { | ||
| // only serial set, terminal fields should be omitted | ||
| opts := &Grub2DStageOptions{ | ||
| Path: "tree:///boot/grub2/console.cfg", | ||
| Config: &Grub2DConfig{ | ||
| Serial: "serial --unit=0 --speed=9600", | ||
| }, | ||
| } | ||
|
|
||
| data, err := json.MarshalIndent(opts, "", " ") | ||
| require.NoError(t, err) | ||
|
|
||
| expected := `{ | ||
| "path": "tree:///boot/grub2/console.cfg", | ||
| "config": { | ||
| "serial": "serial --unit=0 --speed=9600" | ||
| } | ||
| }` | ||
| assert.Equal(t, expected, string(data)) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.