Report the result of the disk compaction step
This commit is contained in:
parent
10d93dffa4
commit
08f9d619a9
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
@ -33,13 +35,42 @@ func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multis
|
|||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Compacting all attached disk images")
|
ui.Say("Compacting all attached virtual disks...")
|
||||||
for i, diskPath := range diskPaths {
|
for i, diskPath := range diskPaths {
|
||||||
ui.Message(fmt.Sprintf("Compacting disk image %d", i+1))
|
ui.Message(fmt.Sprintf("Compacting virtual disk %d", i+1))
|
||||||
|
// Get the file size of the virtual disk prior to compaction
|
||||||
|
fi, err := os.Stat(diskPath)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", fmt.Errorf("Error getting virtual disk file info pre compaction: %s", err))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
diskFileSizeStart := fi.Size()
|
||||||
|
// Defragment and compact the disk
|
||||||
if err := driver.CompactDisk(diskPath); err != nil {
|
if err := driver.CompactDisk(diskPath); err != nil {
|
||||||
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
|
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
// Get the file size of the virtual disk post compaction
|
||||||
|
fi, err = os.Stat(diskPath)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", fmt.Errorf("Error getting virtual disk file info post compaction: %s", err))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
diskFileSizeEnd := fi.Size()
|
||||||
|
// Report compaction results
|
||||||
|
log.Printf("Before compaction the disk file size was: %d", diskFileSizeStart)
|
||||||
|
log.Printf("After compaction the disk file size was: %d", diskFileSizeEnd)
|
||||||
|
if diskFileSizeStart > 0 {
|
||||||
|
percentChange := ((float64(diskFileSizeEnd) / float64(diskFileSizeStart)) * 100.0) - 100.0
|
||||||
|
switch {
|
||||||
|
case percentChange < 0:
|
||||||
|
ui.Message(fmt.Sprintf("Compacting reduced the disk file size by %.2f%%", math.Abs(percentChange)))
|
||||||
|
case percentChange == 0:
|
||||||
|
ui.Message(fmt.Sprintf("The compacting operation left the disk file size unchanged"))
|
||||||
|
case percentChange > 0:
|
||||||
|
ui.Message(fmt.Sprintf("WARNING: Compacting increased the disk file size by %.2f%%", percentChange))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user