mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-08-22 19:38:35 +00:00
Merge pull request #17 from danitso/bugfix-disk-image-import
Fix disk import issue when importing from directory-based datastores
This commit is contained in:
commit
eb4e6893f3
@ -9,6 +9,7 @@ BUG FIXES:
|
||||
* library/virtual_environment_nodes: Fix node IP address format
|
||||
* resource/virtual_environment_container: Fix VM ID collision when `vm_id` is not specified
|
||||
* resource/virtual_environment_vm: Fix VM ID collision when `vm_id` is not specified
|
||||
* resource/virtual_environment_vm: Fix disk import issue when importing from directory-based datastores
|
||||
|
||||
WORKAROUNDS:
|
||||
|
||||
|
@ -35,7 +35,7 @@ func (c *VirtualEnvironmentClient) ExecuteNodeCommands(nodeName string, commands
|
||||
output, err := sshSession.CombinedOutput(
|
||||
fmt.Sprintf(
|
||||
"/bin/bash -c '%s'",
|
||||
strings.ReplaceAll(strings.Join(commands, " && "), "'", "'\"'\"'"),
|
||||
strings.ReplaceAll(strings.Join(commands, " && \\\n"), "'", "'\"'\"'"),
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -1616,17 +1616,28 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(d *schema.ResourceData, m int
|
||||
commands = append(
|
||||
commands,
|
||||
`set -e`,
|
||||
`export nr='^[A-Za-z0-9_]+: ([A-Za-z0-9_]+)$'`,
|
||||
`export pr='^[[:space:]]+path[[:space:]]+([^[:space:]]+)$'`,
|
||||
`export dn=""`,
|
||||
`export dp=""`,
|
||||
fmt.Sprintf(`while IFS='' read -r l || [[ -n "$l" ]]; do if [[ "$l" =~ $nr ]]; then export dn="${BASH_REMATCH[1]}"; elif [[ "$l" =~ $pr ]] && [[ "$dn" == "%s" ]]; then export dp="${BASH_REMATCH[1]}"; break; fi; done < /etc/pve/storage.cfg`, fileIDParts[0]),
|
||||
`if [[ -z "$dp" ]]; then echo "Failed to determine the datastore path"; exit 1; fi`,
|
||||
fmt.Sprintf(`cp "${dp}%s" %s`, filePath, filePathTmp),
|
||||
fmt.Sprintf(`qemu-img resize %s %dG`, filePathTmp, size),
|
||||
fmt.Sprintf(`qm importdisk %d %s %s -format qcow2`, vmID, filePathTmp, datastoreID),
|
||||
fmt.Sprintf(`qm set %d -scsi%d %s:vm-%d-disk-%d%s`, vmID, i, datastoreID, vmID, diskCount+importedDiskCount, diskOptions),
|
||||
fmt.Sprintf(`rm -f %s`, filePathTmp),
|
||||
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),
|
||||
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")"`,
|
||||
`dst_target="$(echo "$dsi_target" | cut -d ";" -f 2)"`,
|
||||
`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}:$([[ "$dst_target" == "dir" ]] && echo "${vm_id}/" || echo "")vm-${vm_id}-disk-${disk_count}$([[ "$dst_target" == "dir" ]] && echo ".qcow2" || echo "")${disk_options}"`,
|
||||
`qm set "$vm_id" "-scsi${disk_index}" "$disk_id"`,
|
||||
`rm -f "$file_path_tmp"`,
|
||||
)
|
||||
|
||||
importedDiskCount++
|
||||
|
Loading…
Reference in New Issue
Block a user