2015-10-29 06:54:25 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-10-29 06:54:25 -04:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2018-06-28 17:21:28 -04:00
|
|
|
"strings"
|
2019-04-08 11:57:27 -04:00
|
|
|
"time"
|
2015-10-29 06:54:25 -04:00
|
|
|
|
2018-06-28 17:21:28 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-10-29 06:54:25 -04:00
|
|
|
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step converts the virtual disk that was used as the
|
|
|
|
// hard drive for the virtual machine.
|
|
|
|
type stepConvertDisk struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConvertDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-29 06:54:25 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
2019-06-24 19:35:06 -04:00
|
|
|
diskName := config.VMName
|
2015-10-29 06:54:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if config.SkipCompaction && !config.DiskCompression {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := diskName + ".convert"
|
|
|
|
|
|
|
|
sourcePath := filepath.Join(config.OutputDir, diskName)
|
|
|
|
targetPath := filepath.Join(config.OutputDir, name)
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"convert",
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.DiskCompression {
|
|
|
|
command = append(command, "-c")
|
|
|
|
}
|
|
|
|
|
|
|
|
command = append(command, []string{
|
|
|
|
"-O", config.Format,
|
|
|
|
sourcePath,
|
|
|
|
targetPath,
|
|
|
|
}...,
|
|
|
|
)
|
|
|
|
|
|
|
|
ui.Say("Converting hard drive...")
|
2018-06-28 17:21:28 -04:00
|
|
|
// Retry the conversion a few times in case it takes the qemu process a
|
|
|
|
// moment to release the lock
|
2019-04-08 11:57:27 -04:00
|
|
|
err := retry.Config{
|
|
|
|
Tries: 10,
|
|
|
|
ShouldRetry: func(err error) bool {
|
2018-06-28 17:21:28 -04:00
|
|
|
if strings.Contains(err.Error(), `Failed to get shared "write" lock`) {
|
|
|
|
ui.Say("Error getting file lock for conversion; retrying...")
|
2019-04-08 11:57:27 -04:00
|
|
|
return true
|
2018-06-28 17:21:28 -04:00
|
|
|
}
|
2019-04-08 11:57:27 -04:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 1 * time.Second, MaxBackoff: 10 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
return driver.QemuImg(command...)
|
2018-06-28 17:21:28 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == common.RetryExhaustedError {
|
|
|
|
err = fmt.Errorf("Exhausted retries for getting file lock: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Error converting hard drive: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-29 06:54:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(targetPath, sourcePath); err != nil {
|
|
|
|
err := fmt.Errorf("Error moving converted hard drive: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConvertDisk) Cleanup(state multistep.StateBag) {}
|