| // Copyright 2025 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package main |
| |
| import ( |
| "context" |
| "encoding/json" |
| "flag" |
| "log" |
| |
| "cos.googlesource.com/cos/tools.git/src/pkg/config" |
| "cos.googlesource.com/cos/tools.git/src/pkg/fs" |
| "cos.googlesource.com/cos/tools.git/src/pkg/provisioner" |
| "github.com/google/subcommands" |
| ) |
| |
| // SedGrubConfig implements subcommands.Command for the "sed-grub-config" |
| // command. This command runs a given sed script on the grub configuration. |
| // Useful for editing the kernel command line. |
| type SedGrubConfig struct { |
| sedScript string |
| } |
| |
| // Name implements subcommands.Command.Name. |
| func (s *SedGrubConfig) Name() string { |
| return "sed-grub-config" |
| } |
| |
| // Synopsis implements subcommands.Command.Synopsis. |
| func (s *SedGrubConfig) Synopsis() string { |
| return "Edit the grub configuration during the image build. Changes take effect on the next reboot. This step does not do any reboots." |
| } |
| |
| // Usage implements subcommands.Command.Usage. |
| func (s *SedGrubConfig) Usage() string { |
| return `sed-grub-config [flags] |
| ` |
| } |
| |
| // SetFlags implements subcommands.Command.SetFlags. |
| func (s *SedGrubConfig) SetFlags(f *flag.FlagSet) { |
| f.StringVar(&s.sedScript, "sed-script", "", `Sed script to run, e.g. "s|module.sig_enforce=1|module.sig_enforce=0|g".`) |
| } |
| |
| func (s *SedGrubConfig) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { |
| if f.NArg() != 0 { |
| f.Usage() |
| return subcommands.ExitUsageError |
| } |
| files := args[0].(*fs.Files) |
| var provConfig provisioner.Config |
| if err := config.LoadFromFile(files.ProvConfig, &provConfig); err != nil { |
| log.Println(err) |
| return subcommands.ExitFailure |
| } |
| buf, err := json.Marshal(&provisioner.SedGrubConfigStep{ |
| SedScript: s.sedScript, |
| }) |
| if err != nil { |
| log.Println(err) |
| return subcommands.ExitFailure |
| } |
| provConfig.Steps = append(provConfig.Steps, provisioner.StepConfig{ |
| Type: "SedGrubConfigStep", |
| Args: json.RawMessage(buf), |
| }) |
| if err := config.SaveConfigToPath(files.ProvConfig, &provConfig); err != nil { |
| log.Println(err) |
| return subcommands.ExitFailure |
| } |
| return subcommands.ExitSuccess |
| } |