From f40c3df7d7b0e776640e7bd952352526ab7eab98 Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Thu, 15 Jul 2021 19:16:11 +0800 Subject: [PATCH 1/6] Modify xfs_growfs --- builder/qemu/chroot/step_mount_device.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/qemu/chroot/step_mount_device.go b/builder/qemu/chroot/step_mount_device.go index bb1eb31..8ad0973 100644 --- a/builder/qemu/chroot/step_mount_device.go +++ b/builder/qemu/chroot/step_mount_device.go @@ -81,7 +81,7 @@ func (s *StepMountDevice) Run(_ context.Context, state multistep.StateBag) multi fileOs := strings.SplitN(fileOsResult, " ", -1)[1] if fileOs == "xfs" { - if _, err := RunCommand(state, fmt.Sprintf("xfs_growfs %s", device)); err != nil { + if _, err := RunCommand(state, fmt.Sprintf("xfs_growfs %s", mountPath)); err != nil { return Halt(state, fmt.Errorf("sync xfs file system error, device: \"%s\"\t err: %s", device, err)) } } else { From 1690d551c1342241c0f7a71a06705eee137c71e5 Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Fri, 16 Jul 2021 10:06:21 +0800 Subject: [PATCH 2/6] Change the partition of raw img --- .../qemu/chroot/step_prepare_source_image.go | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/builder/qemu/chroot/step_prepare_source_image.go b/builder/qemu/chroot/step_prepare_source_image.go index 146b171..f9bfe1b 100644 --- a/builder/qemu/chroot/step_prepare_source_image.go +++ b/builder/qemu/chroot/step_prepare_source_image.go @@ -3,13 +3,12 @@ package chroot import ( "context" "fmt" - "os" - "path" - "path/filepath" - "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata" + "os" + "path" + "path/filepath" ) // StepPrepareSourceImage process the source image. @@ -70,6 +69,41 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er if _, err := RunCommand(state, fmt.Sprintf("qemu-img resize %s %dG", s.rawImage, config.ImageSize)); err != nil { return fmt.Errorf("cannot resize raw image : %s", err) } + device, err := RunCommand(state, fmt.Sprintf("losetup -f --show %s", s.rawImage)) + if err != nil { + return fmt.Errorf("get device name error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("fdisk %s", device)); err != nil { + return fmt.Errorf("fdisk error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("d")); err != nil { + return fmt.Errorf("delete the partation of device error : %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("n")); err != nil { + return fmt.Errorf("new partation error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { + return fmt.Errorf("first enter error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { + return fmt.Errorf("second enter error : %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { + return fmt.Errorf("third enter error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { + return fmt.Errorf("fourth enter error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("w")); err != nil { + return fmt.Errorf("save parrarion error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("partprobe")); err != nil { + return fmt.Errorf("partprobe error: %s", err) + } + if _, err := RunCommand(state, fmt.Sprintf("losetup -d %s", device)); err != nil { + return fmt.Errorf("uninsall device error: %s", err) + } + } return nil } From 12a528671042393ddc01fe85f914247e77b541b1 Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Mon, 19 Jul 2021 10:09:24 +0800 Subject: [PATCH 3/6] Change fdisk to parted --- .../qemu/chroot/step_prepare_source_image.go | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/builder/qemu/chroot/step_prepare_source_image.go b/builder/qemu/chroot/step_prepare_source_image.go index f9bfe1b..8c59d42 100644 --- a/builder/qemu/chroot/step_prepare_source_image.go +++ b/builder/qemu/chroot/step_prepare_source_image.go @@ -73,37 +73,21 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er if err != nil { return fmt.Errorf("get device name error: %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("fdisk %s", device)); err != nil { - return fmt.Errorf("fdisk error: %s", err) + if _, err := RunCommand(state, fmt.Sprintf("parted %s", device)); err != nil { + return fmt.Errorf("parted error: %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("d")); err != nil { - return fmt.Errorf("delete the partation of device error : %s", err) + if _, err := RunCommand(state, fmt.Sprintf("resizepart 1")); err != nil { + return fmt.Errorf("resizepart error : %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("n")); err != nil { - return fmt.Errorf("new partation error: %s", err) + if _, err := RunCommand(state, fmt.Sprintf("%d", config.ImageSize*1024)); err != nil { + return fmt.Errorf("change part size error : %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { - return fmt.Errorf("first enter error: %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { - return fmt.Errorf("second enter error : %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { - return fmt.Errorf("third enter error: %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("")); err != nil { - return fmt.Errorf("fourth enter error: %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("w")); err != nil { - return fmt.Errorf("save parrarion error: %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("partprobe")); err != nil { - return fmt.Errorf("partprobe error: %s", err) + if _, err := RunCommand(state, fmt.Sprintf("q")); err != nil { + return fmt.Errorf("quit parted error : %s", err) } if _, err := RunCommand(state, fmt.Sprintf("losetup -d %s", device)); err != nil { return fmt.Errorf("uninsall device error: %s", err) } - } return nil } From afd2c32fd0b12fd9c7d514d112c8232fcff02e3a Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Tue, 20 Jul 2021 11:58:11 +0800 Subject: [PATCH 4/6] change parted unInteractive model --- builder/qemu/chroot/step_mount_device.go | 13 +++---------- builder/qemu/chroot/step_prepare_source_image.go | 14 ++++++-------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/builder/qemu/chroot/step_mount_device.go b/builder/qemu/chroot/step_mount_device.go index 8ad0973..bb63dab 100644 --- a/builder/qemu/chroot/step_mount_device.go +++ b/builder/qemu/chroot/step_mount_device.go @@ -68,18 +68,11 @@ func (s *StepMountDevice) Run(_ context.Context, state multistep.StateBag) multi if config.ImageSize > 0 { // sync the file system var fileOsResult string - if fileOsResult, err = RunCommand(state, fmt.Sprintf("df -T | grep %s", device)); err != nil { + if fileOsResult, err = RunCommand(state, fmt.Sprintf("df --output=source,fstype | grep %s", device)); err != nil { return Halt(state, fmt.Errorf("cannot peek the file system of deivce:\"%s\" ,%s", device, err)) } - //remove extra space - for { - if !strings.Contains(fileOsResult, " ") { - break - } - fileOsResult = strings.Replace(fileOsResult, " ", " ", -1) - } - fileOs := strings.SplitN(fileOsResult, " ", -1)[1] - + arr := strings.Split(fileOsResult, " ") + fileOs := arr[len(arr)-1] if fileOs == "xfs" { if _, err := RunCommand(state, fmt.Sprintf("xfs_growfs %s", mountPath)); err != nil { return Halt(state, fmt.Errorf("sync xfs file system error, device: \"%s\"\t err: %s", device, err)) diff --git a/builder/qemu/chroot/step_prepare_source_image.go b/builder/qemu/chroot/step_prepare_source_image.go index 8c59d42..9c5bebb 100644 --- a/builder/qemu/chroot/step_prepare_source_image.go +++ b/builder/qemu/chroot/step_prepare_source_image.go @@ -9,6 +9,7 @@ import ( "os" "path" "path/filepath" + "strings" ) // StepPrepareSourceImage process the source image. @@ -73,18 +74,15 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er if err != nil { return fmt.Errorf("get device name error: %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("parted %s", device)); err != nil { + //get parted + content, err := RunCommand(state, fmt.Sprintf("parted -m %s p", device)) + if err != nil { return fmt.Errorf("parted error: %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("resizepart 1")); err != nil { + lastPartNumber := len(strings.Split(content, ";")) - 3 //last string is empty + if _, err := RunCommand(state, fmt.Sprintf("parted -m %s resizepart %d 100%", device, lastPartNumber)); err != nil { return fmt.Errorf("resizepart error : %s", err) } - if _, err := RunCommand(state, fmt.Sprintf("%d", config.ImageSize*1024)); err != nil { - return fmt.Errorf("change part size error : %s", err) - } - if _, err := RunCommand(state, fmt.Sprintf("q")); err != nil { - return fmt.Errorf("quit parted error : %s", err) - } if _, err := RunCommand(state, fmt.Sprintf("losetup -d %s", device)); err != nil { return fmt.Errorf("uninsall device error: %s", err) } From 4ecd9dcdbb296ef0f0a18a0d87b5d4356f91bab5 Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Tue, 20 Jul 2021 15:29:52 +0800 Subject: [PATCH 5/6] Modify the way to get partation number --- builder/qemu/chroot/step_prepare_source_image.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/builder/qemu/chroot/step_prepare_source_image.go b/builder/qemu/chroot/step_prepare_source_image.go index 9c5bebb..be1242f 100644 --- a/builder/qemu/chroot/step_prepare_source_image.go +++ b/builder/qemu/chroot/step_prepare_source_image.go @@ -79,8 +79,11 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er if err != nil { return fmt.Errorf("parted error: %s", err) } - lastPartNumber := len(strings.Split(content, ";")) - 3 //last string is empty - if _, err := RunCommand(state, fmt.Sprintf("parted -m %s resizepart %d 100%", device, lastPartNumber)); err != nil { + + arr := strings.Split(content, "\n") + lastPartNumber := strings.Split(arr[len(arr)-1], ":")[0] + + if _, err := RunCommand(state, fmt.Sprintf("parted -m %s resizepart %s 100%", device, lastPartNumber)); err != nil { return fmt.Errorf("resizepart error : %s", err) } if _, err := RunCommand(state, fmt.Sprintf("losetup -d %s", device)); err != nil { From 065bee59a42e82a9506be45a8884bdc543265334 Mon Sep 17 00:00:00 2001 From: wangjiaxi <308886997@qq.com> Date: Tue, 20 Jul 2021 15:38:45 +0800 Subject: [PATCH 6/6] Modify % to %% --- builder/qemu/chroot/step_prepare_source_image.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/qemu/chroot/step_prepare_source_image.go b/builder/qemu/chroot/step_prepare_source_image.go index be1242f..fa8fec0 100644 --- a/builder/qemu/chroot/step_prepare_source_image.go +++ b/builder/qemu/chroot/step_prepare_source_image.go @@ -83,7 +83,7 @@ func (s *StepPrepareSourceImage) prepareSourceImage(state multistep.StateBag) er arr := strings.Split(content, "\n") lastPartNumber := strings.Split(arr[len(arr)-1], ":")[0] - if _, err := RunCommand(state, fmt.Sprintf("parted -m %s resizepart %s 100%", device, lastPartNumber)); err != nil { + if _, err := RunCommand(state, fmt.Sprintf("parted -m %s resizepart %s 100%%", device, lastPartNumber)); err != nil { return fmt.Errorf("resizepart error : %s", err) } if _, err := RunCommand(state, fmt.Sprintf("losetup -d %s", device)); err != nil {