From 37fe0e03ff61bdc69c79e1b5420ef48551dbdb40 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Fri, 22 Jul 2022 19:03:39 -0400 Subject: [PATCH] Fix disk import when VM template has multiple disks (#96) The disk import operation is not exposed via Proxmox APIs, so implemented as a sequence of commands run as an inline script via ssh. `qm importdisk` is used to import a disk into VM from an external file, however, it auto-generates the disk ID. The following command `qm set` is used to assign the imported disk to a VM, and it requires the disk ID as a parameter. Update the import logic to read the disk ID from the output of `qm importdisk` command rather than trying to assume it from a number of disks defined in the VM. --- Makefile | 4 ++-- proxmoxtf/resource_virtual_environment_vm.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 9c39a184..5573c0df 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ build: rm -f "$(TERRAFORM_PLUGIN_EXECUTABLE)" go build -o "$(TERRAFORM_PLUGIN_EXECUTABLE)" -example: example-build example-init example-apply example-apply example-destroy +example: example-build example-init example-apply example-destroy example-apply: export TF_CLI_CONFIG_FILE="$(shell pwd -P)/example.tfrc" \ @@ -36,8 +36,8 @@ example-apply: && terraform apply -auto-approve example-build: + rm -rf "$(TERRAFORM_PLUGIN_DIRECTORY_EXAMPLE)" mkdir -p "$(TERRAFORM_PLUGIN_DIRECTORY_EXAMPLE)" - rm -f "$(TERRAFORM_PLUGIN_EXECUTABLE_EXAMPLE)" go build -o "$(TERRAFORM_PLUGIN_EXECUTABLE_EXAMPLE)" example-destroy: diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 4a0f6688..5ce6e7bc 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -1832,7 +1832,6 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(ctx context.Context, d *schem `set -e`, fmt.Sprintf(`datastore_id_image="%s"`, fileIDParts[0]), fmt.Sprintf(`datastore_id_target="%s"`, datastoreID), - fmt.Sprintf(`disk_count="%d"`, diskCount+importedDiskCount), fmt.Sprintf(`disk_index="%d"`, i), fmt.Sprintf(`disk_options="%s"`, diskOptions), fmt.Sprintf(`disk_size="%d"`, size), @@ -1849,8 +1848,8 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(ctx context.Context, d *schem `dsp_target="$(echo "$dsi_target" | cut -d ";" -f 1)"`, `cp "${dsp_image}${file_path}" "$file_path_tmp"`, `qemu-img resize "$file_path_tmp" "${disk_size}G"`, - `qm importdisk "$vm_id" "$file_path_tmp" "$datastore_id_target" -format qcow2`, - `disk_id="${datastore_id_target}:$([[ -n "$dsp_target" ]] && echo "${vm_id}/" || echo "")vm-${vm_id}-disk-${disk_count}$([[ -n "$dsp_target" ]] && echo ".qcow2" || echo "")${disk_options}"`, + `imported_disk="$(qm importdisk "$vm_id" "$file_path_tmp" "$datastore_id_target" -format qcow2 | grep "unused0" | cut -d ":" -f 3 | cut -d "'" -f 1)"`, + `disk_id="${datastore_id_target}:$([[ -n "$dsp_target" ]] && echo "${vm_id}/" || echo "")$imported_disk$([[ -n "$dsp_target" ]] && echo ".qcow2" || echo "")${disk_options}"`, `qm set "$vm_id" "-${disk_interface}" "$disk_id"`, `rm -f "$file_path_tmp"`, )