From fcf98102522821c9dfb4534731747233bd627d38 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:53:06 -0500 Subject: [PATCH] feat(vm): update VM disc import logic (#241) * feat(vm): updated VM disc import logic fixes: #187, #203 * ignore linter error --- example/resource_virtual_environment_vm.tf | 2 +- proxmox/virtual_environment_nodes.go | 3 +- proxmoxtf/resource_virtual_environment_vm.go | 33 ++++++-------------- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/example/resource_virtual_environment_vm.tf b/example/resource_virtual_environment_vm.tf index 670170c0..339fdd70 100644 --- a/example/resource_virtual_environment_vm.tf +++ b/example/resource_virtual_environment_vm.tf @@ -23,7 +23,7 @@ resource "proxmox_virtual_environment_vm" "example_template" { discard = "on" ssd = true } -# + # disk { # datastore_id = "nfs" # interface = "scsi1" diff --git a/proxmox/virtual_environment_nodes.go b/proxmox/virtual_environment_nodes.go index 6faac9e3..f74e6c45 100644 --- a/proxmox/virtual_environment_nodes.go +++ b/proxmox/virtual_environment_nodes.go @@ -41,10 +41,11 @@ func (c *VirtualEnvironmentClient) ExecuteNodeCommands( } defer closeOrLogError(sshSession) + script := strings.Join(commands, " && \\\n") output, err := sshSession.CombinedOutput( fmt.Sprintf( "/bin/bash -c '%s'", - strings.ReplaceAll(strings.Join(commands, " && \\\n"), "'", "'\"'\"'"), + strings.ReplaceAll(script, "'", "'\"'\"'"), ), ) if err != nil { diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index cf726527..b2fda77d 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -2088,7 +2088,7 @@ func resourceVirtualEnvironmentVMCreateCustomDisks( // Generate the commands required to import the specified disks. importedDiskCount := 0 - for i, d := range disk { + for _, d := range disk { block := d.(map[string]interface{}) fileID, _ := block[mkResourceVirtualEnvironmentVMDiskFileID].(string) @@ -2150,15 +2150,6 @@ func resourceVirtualEnvironmentVMCreateCustomDisks( diskOptions += fmt.Sprintf(",mbps_wr_max=%d", speedLimitWriteBurstable) } - fileIDParts := strings.Split(fileID, ":") - filePath := "" - - if strings.HasPrefix(fileIDParts[1], "iso/") { - filePath = fmt.Sprintf("/template/%s", fileIDParts[1]) - } else { - filePath = fmt.Sprintf("/%s", fileIDParts[1]) - } - filePathTmp := fmt.Sprintf( "/tmp/vm-%d-disk-%d.%s", vmID, @@ -2166,29 +2157,23 @@ func resourceVirtualEnvironmentVMCreateCustomDisks( fileFormat, ) + //nolint:lll commands = append( commands, `set -e`, - fmt.Sprintf(`datastore_id_image="%s"`, fileIDParts[0]), + fmt.Sprintf(`file_id="%s"`, fileID), + fmt.Sprintf(`file_format="%s"`, fileFormat), fmt.Sprintf(`datastore_id_target="%s"`, datastoreID), - fmt.Sprintf(`disk_index="%d"`, i), fmt.Sprintf(`disk_options="%s"`, diskOptions), fmt.Sprintf(`disk_size="%d"`, size), fmt.Sprintf(`disk_interface="%s"`, diskInterface), - fmt.Sprintf(`file_path="%s"`, filePath), fmt.Sprintf(`file_path_tmp="%s"`, filePathTmp), fmt.Sprintf(`vm_id="%d"`, vmID), - `getdsi() { local nr='^([A-Za-z0-9_-]+): ([A-Za-z0-9_-]+)$'; local pr='^[[:space:]]+path[[:space:]]+([^[:space:]]+)$'; local dn=""; local dt=""; while IFS='' read -r l || [[ -n "$l" ]]; do if [[ "$l" =~ $nr ]]; then dt="${BASH_REMATCH[1]}"; dn="${BASH_REMATCH[2]}"; elif [[ "$l" =~ $pr ]] && [[ "$dn" == "$1" ]]; then echo "${BASH_REMATCH[1]};${dt}"; break; fi; done < /etc/pve/storage.cfg; }`, - `dsi_image="$(getdsi "$datastore_id_image")"`, - `dsp_image="$(echo "$dsi_image" | cut -d ";" -f 1)"`, - `dst_image="$(echo "$dsi_image" | cut -d ";" -f 2)"`, - `if [[ -z "$dsp_image" ]]; then echo "Failed to determine the path for datastore '${datastore_id_image}' (${dsi_image})"; exit 1; fi`, - `dsi_target="$(getdsi "$datastore_id_target")"`, - `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"`, - `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}"`, + `source_image=$(pvesm path "$file_id")`, + `cp "$source_image" "$file_path_tmp"`, + `qemu-img resize -f "$file_format" "$file_path_tmp" "${disk_size}G"`, + `imported_disk="$(qm importdisk "$vm_id" "$file_path_tmp" "$datastore_id_target" -format $file_format | grep "unused0" | cut -d ":" -f 3 | cut -d "'" -f 1)"`, + `disk_id="${datastore_id_target}:$imported_disk${disk_options}"`, `qm set "$vm_id" "-${disk_interface}" "$disk_id"`, `rm -f "$file_path_tmp"`, )