Fix disk path parsing for directory storage- Parse actual disk path from qm importdisk output- Support both LVM (local-lvm:vm-X-disk-0) and directory (local:VMID/vm-X-disk-0.raw) storage- Fix 'unable to parse directory volume name' error
This commit is contained in:
57
proxmox.go
57
proxmox.go
@@ -123,17 +123,64 @@ func createProxmoxVM(config *Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Creating VM and converting to template...")
|
fmt.Println("Creating VM and converting to template...")
|
||||||
commands := [][]string{
|
|
||||||
{"qm", "create", fmt.Sprintf("%d", config.VMID),
|
// Create VM
|
||||||
|
createCmd := []string{"qm", "create", fmt.Sprintf("%d", config.VMID),
|
||||||
"--name", config.VMName,
|
"--name", config.VMName,
|
||||||
"--memory", fmt.Sprintf("%d", config.Memory),
|
"--memory", fmt.Sprintf("%d", config.Memory),
|
||||||
"--cores", fmt.Sprintf("%d", config.Cores),
|
"--cores", fmt.Sprintf("%d", config.Cores),
|
||||||
"--net0", buildNetworkConfig(config),
|
"--net0", buildNetworkConfig(config),
|
||||||
},
|
}
|
||||||
{"qm", "importdisk", fmt.Sprintf("%d", config.VMID), remotePath, config.Storage},
|
|
||||||
|
fmt.Printf("Running: %s\n", strings.Join(createCmd, " "))
|
||||||
|
cmd := sshCmd(createCmd...)
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("failed to create VM: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import disk and capture output to get actual disk path
|
||||||
|
importCmd := []string{"qm", "importdisk", fmt.Sprintf("%d", config.VMID), remotePath, config.Storage}
|
||||||
|
fmt.Printf("Running: %s\n", strings.Join(importCmd, " "))
|
||||||
|
|
||||||
|
cmd = sshCmd(importCmd...)
|
||||||
|
var importOut bytes.Buffer
|
||||||
|
cmd.Stdout = &importOut
|
||||||
|
cmd.Stderr = &importOut
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
fmt.Println(importOut.String())
|
||||||
|
return fmt.Errorf("failed to import disk: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := importOut.String()
|
||||||
|
fmt.Println(output)
|
||||||
|
|
||||||
|
// Parse disk path from output
|
||||||
|
// Example: "unused0: successfully imported disk 'local:10001/vm-10001-disk-0.raw'"
|
||||||
|
var diskPath string
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.Contains(line, "successfully imported disk") {
|
||||||
|
start := strings.Index(line, "'")
|
||||||
|
end := strings.LastIndex(line, "'")
|
||||||
|
if start != -1 && end != -1 && end > start {
|
||||||
|
diskPath = line[start+1 : end]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if diskPath == "" {
|
||||||
|
return fmt.Errorf("failed to parse disk path from importdisk output")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Imported disk path: %s\n", diskPath)
|
||||||
|
|
||||||
|
// Configure VM with the actual disk path
|
||||||
|
commands := [][]string{
|
||||||
{"qm", "set", fmt.Sprintf("%d", config.VMID),
|
{"qm", "set", fmt.Sprintf("%d", config.VMID),
|
||||||
"--scsihw", "virtio-scsi-pci",
|
"--scsihw", "virtio-scsi-pci",
|
||||||
"--scsi0", fmt.Sprintf("%s:vm-%d-disk-0", config.Storage, config.VMID),
|
"--scsi0", diskPath,
|
||||||
},
|
},
|
||||||
{"qm", "set", fmt.Sprintf("%d", config.VMID),
|
{"qm", "set", fmt.Sprintf("%d", config.VMID),
|
||||||
"--ide2", fmt.Sprintf("%s:cloudinit", config.Storage),
|
"--ide2", fmt.Sprintf("%s:cloudinit", config.Storage),
|
||||||
|
|||||||
Reference in New Issue
Block a user