@@ -3,13 +3,13 @@ package chroot
33import (
44 "context"
55 "fmt"
6- "os"
7- "path"
8- "path/filepath"
9-
106 "github.com/hashicorp/packer-plugin-sdk/multistep"
117 packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
128 "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
9+ "os"
10+ "path"
11+ "path/filepath"
12+ "strings"
1313)
1414
1515// StepPrepareSourceImage process the source image.
@@ -70,6 +70,25 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er
7070 if _ , err := RunCommand (state , fmt .Sprintf ("qemu-img resize %s %dG" , s .rawImage , config .ImageSize )); err != nil {
7171 return fmt .Errorf ("cannot resize raw image : %s" , err )
7272 }
73+ device , err := RunCommand (state , fmt .Sprintf ("losetup -f --show %s" , s .rawImage ))
74+ if err != nil {
75+ return fmt .Errorf ("get device name error: %s" , err )
76+ }
77+ //get parted
78+ content , err := RunCommand (state , fmt .Sprintf ("parted -m %s p" , device ))
79+ if err != nil {
80+ return fmt .Errorf ("parted error: %s" , err )
81+ }
82+
83+ arr := strings .Split (content , "\n " )
84+ lastPartNumber := strings .Split (arr [len (arr )- 1 ], ":" )[0 ]
85+
86+ if _ , err := RunCommand (state , fmt .Sprintf ("parted -m %s resizepart %s 100%" , device , lastPartNumber )); err != nil {
87+ return fmt .Errorf ("resizepart error : %s" , err )
88+ }
89+ if _ , err := RunCommand (state , fmt .Sprintf ("losetup -d %s" , device )); err != nil {
90+ return fmt .Errorf ("uninsall device error: %s" , err )
91+ }
7392 }
7493 return nil
7594}
0 commit comments