Skip to content

Commit

Permalink
Fix disk import when VM template has multiple disks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bpg committed Jul 22, 2022
1 parent 36dca01 commit 367061e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions proxmoxtf/resource_virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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"`,
)
Expand Down

0 comments on commit 367061e

Please sign in to comment.