Skip to content

Commit 1d20255

Browse files
authored
Add resize method to enlarge the raw image (#3)
1 parent 28bc9cc commit 1d20255

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

builder/qemu/chroot/step_mount_device.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,13 @@ func (s *StepMountDevice) Run(_ context.Context, state multistep.StateBag) multi
6868
if config.ImageSize > 0 {
6969
// sync the file system
7070
var fileOsResult string
71-
if fileOsResult, err = RunCommand(state, fmt.Sprintf("df -T | grep %s", device)); err != nil {
71+
if fileOsResult, err = RunCommand(state, fmt.Sprintf("df --output=source,fstype | grep %s", device)); err != nil {
7272
return Halt(state, fmt.Errorf("cannot peek the file system of deivce:\"%s\" ,%s", device, err))
7373
}
74-
//remove extra space
75-
for {
76-
if !strings.Contains(fileOsResult, " ") {
77-
break
78-
}
79-
fileOsResult = strings.Replace(fileOsResult, " ", " ", -1)
80-
}
81-
fileOs := strings.SplitN(fileOsResult, " ", -1)[1]
82-
74+
arr := strings.Split(fileOsResult, " ")
75+
fileOs := arr[len(arr)-1]
8376
if fileOs == "xfs" {
84-
if _, err := RunCommand(state, fmt.Sprintf("xfs_growfs %s", device)); err != nil {
77+
if _, err := RunCommand(state, fmt.Sprintf("xfs_growfs %s", mountPath)); err != nil {
8578
return Halt(state, fmt.Errorf("sync xfs file system error, device: \"%s\"\t err: %s", device, err))
8679
}
8780
} else {

builder/qemu/chroot/step_prepare_source_image.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package chroot
33
import (
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

Comments
 (0)