| // 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 provisioner |
| |
| import ( |
| "context" |
| "fmt" |
| "log" |
| "os" |
| "os/exec" |
| "path/filepath" |
| |
| "cos.googlesource.com/cos/tools.git/src/pkg/tools/partutil" |
| ) |
| |
| type SedGrubConfigStep struct { |
| SedScript string |
| } |
| |
| func (s *SedGrubConfigStep) run(ctx context.Context, runState *state, deps *stepDeps) error { |
| if s.SedScript == "" { |
| log.Println("No sed script provided; not making any grub config changes") |
| return nil |
| } |
| log.Printf("Running sed script %q on grub config", s.SedScript) |
| efiPath, err := partutil.MountEFIPartition() |
| if err != nil { |
| return err |
| } |
| grubCfgPath := filepath.Join(efiPath, partutil.GRUBCfgEFIPath) |
| cmd := exec.Command(deps.SedCmd, "-i", "-e", s.SedScript, grubCfgPath) |
| cmd.Stdout = os.Stdout |
| cmd.Stderr = os.Stderr |
| if err := cmd.Run(); err != nil { |
| umountErr := partutil.UnmountEFIPartition() |
| if umountErr != nil { |
| log.Printf("ERROR: failed to umount EFI partition: %v", umountErr) |
| } |
| return fmt.Errorf("could not sed grub config, see logs for details: %v", err) |
| } |
| return partutil.UnmountEFIPartition() |
| } |