Skip to content

Commit

Permalink
feat: implement ability to clone Qemu VM to different storage
Browse files Browse the repository at this point in the history
Allows cloning a Qemu VM to a different storage location without changing
the original configuration.
This is already documented in the README.md but was not implemented yet.
  • Loading branch information
OidaTiftla committed Aug 11, 2024
1 parent bdb805f commit d5be3ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ConfigQemu struct {
Description *string `json:"description,omitempty"`
Disks *QemuStorages `json:"disks,omitempty"`
EFIDisk QemuDevice `json:"efidisk,omitempty"` // TODO should be a struct
Storage string `json:"storage,omitempty"` // for clone
FullClone *int `json:"fullclone,omitempty"` // TODO should probably be a bool
HaGroup string `json:"hagroup,omitempty"`
HaState string `json:"hastate,omitempty"` // TODO should be custom type with enum
Expand Down Expand Up @@ -878,13 +879,16 @@ storage:xxx
*/
func (config ConfigQemu) CloneVm(sourceVmr *VmRef, vmr *VmRef, client *Client) (err error) {
vmr.SetVmType("qemu")
var storage string
storage := config.Storage
fullClone := "1"
if config.FullClone != nil {
fullClone = strconv.Itoa(*config.FullClone)
}
if disk0Storage, ok := config.QemuDisks[0]["storage"].(string); ok && len(disk0Storage) > 0 {
storage = disk0Storage
// if storage is not set, use the storage of the first disk
if storage == "" && len(config.QemuDisks) > 0 {
if disk0Storage, ok := config.QemuDisks[0]["storage"].(string); ok && len(disk0Storage) > 0 {
storage = disk0Storage
}
}
params := map[string]interface{}{
"newid": vmr.vmId,
Expand Down
21 changes: 21 additions & 0 deletions test/api/Qemu/qemu_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func Test_Clone_Qemu_VM(t *testing.T) {

}

func Test_Clone_Qemu_VM_To_Different_Storage(t *testing.T) {
Test := api_test.Test{}
_ = Test.CreateTest()
config := _create_vm_spec(false)

config.Create(_create_vmref(), Test.GetClient())

cloneConfig := _create_vm_spec(false)

fullClone := 1

cloneConfig.Name = "test-qemu02"
cloneConfig.FullClone = &fullClone
cloneConfig.Storage = "other-storage"

err := cloneConfig.CloneVm(_create_vmref(), _create_clone_vmref(), Test.GetClient())

require.NoError(t, err)

}

func Test_Qemu_VM_Is_Cloned(t *testing.T) {
Test := api_test.Test{}
_ = Test.CreateTest()
Expand Down

0 comments on commit d5be3ac

Please sign in to comment.