Skip to content

Commit

Permalink
[OPTDEV-2284] Fixes for step_partition_image.go (#286)
Browse files Browse the repository at this point in the history
The original code has bugs in MBR/DOS partitioniong, so narrow use cases fail miserably.

Additionally output handling is improved.
  • Loading branch information
michalfita authored Jul 15, 2023
1 parent 07a5004 commit e8b278a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions builder/step_partition_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func partitionDOS(ui packer.Ui, config *Config) multistep.StepAction {
for i, partition := range config.ImageConfig.ImagePartitions {
line := fmt.Sprintf(
"%s%d: type=%s",
config.ImageConfig.ImagePath,
partition.Name, //config.ImageConfig.ImagePath,
i+1,
partition.Type,
)
Expand All @@ -67,9 +67,15 @@ func partitionDOS(ui packer.Ui, config *Config) multistep.StepAction {
line += fmt.Sprintf(", size=%s", partition.Size)
}

if partition.Name != "" {
line += fmt.Sprintf(", name=%s", partition.Name)
}

lines = append(lines, line)
}

ui.Error(fmt.Sprintf("** DEBUG: %s", strings.Join(lines, "\n")))

cmd := exec.Command("sfdisk", config.ImageConfig.ImagePath)

stdout, err := cmd.StdoutPipe()
Expand All @@ -79,6 +85,13 @@ func partitionDOS(ui packer.Ui, config *Config) multistep.StepAction {
}
defer stdout.Close()

stderr, err := cmd.StderrPipe()
if err != nil {
ui.Error(fmt.Sprintf("error while getting stderr pipe %v", err))
return multistep.ActionHalt
}
defer stderr.Close()

stdin, err := cmd.StdinPipe()
if err != nil {
ui.Error(fmt.Sprintf("error while getting stdin pipe %v", err))
Expand All @@ -100,11 +113,15 @@ func partitionDOS(ui packer.Ui, config *Config) multistep.StepAction {
stdin.Close()

if err := cmd.Wait(); err != nil {
out, _ := io.ReadAll(stdout)
joined := io.MultiReader(stderr, stdout)
out, _ := io.ReadAll(joined)
ui.Error(fmt.Sprintf("error sfdisk %v: %s", err, string(out)))
return multistep.ActionHalt
}

out, _ := io.ReadAll(stdout)
ui.Message(string(out))

return multistep.ActionContinue
}

Expand Down

0 comments on commit e8b278a

Please sign in to comment.