CreateVM creates a QEMU VM and returns the queued task UPID.
(nodeName string, options VMCreateOptions)
| 59 | |
| 60 | // CreateVM creates a QEMU VM and returns the queued task UPID. |
| 61 | func (c *Client) CreateVM(nodeName string, options VMCreateOptions) (string, error) { |
| 62 | if strings.TrimSpace(nodeName) == "" { |
| 63 | return "", fmt.Errorf("node name is required") |
| 64 | } |
| 65 | if options.VMID <= 0 { |
| 66 | return "", fmt.Errorf("vmid must be positive") |
| 67 | } |
| 68 | if strings.TrimSpace(options.Name) == "" { |
| 69 | return "", fmt.Errorf("vm name is required") |
| 70 | } |
| 71 | if strings.TrimSpace(options.DiskStorage) == "" { |
| 72 | return "", fmt.Errorf("disk storage is required") |
| 73 | } |
| 74 | if strings.TrimSpace(options.ImportFrom) == "" && options.DiskSizeGB <= 0 { |
| 75 | return "", fmt.Errorf("disk size must be positive") |
| 76 | } |
| 77 | |
| 78 | if options.MemoryMB <= 0 { |
| 79 | options.MemoryMB = 2048 |
| 80 | } |
| 81 | if options.Cores <= 0 { |
| 82 | options.Cores = 2 |
| 83 | } |
| 84 | if options.Sockets <= 0 { |
| 85 | options.Sockets = 1 |
| 86 | } |
| 87 | if strings.TrimSpace(options.Bridge) == "" { |
| 88 | options.Bridge = "vmbr0" |
| 89 | } |
| 90 | |
| 91 | data := map[string]interface{}{ |
| 92 | "vmid": options.VMID, |
| 93 | "name": strings.TrimSpace(options.Name), |
| 94 | "memory": strconv.Itoa(options.MemoryMB), |
| 95 | "cores": options.Cores, |
| 96 | "sockets": options.Sockets, |
| 97 | "ostype": "l26", |
| 98 | "net0": fmt.Sprintf("virtio,bridge=%s", strings.TrimSpace(options.Bridge)), |
| 99 | } |
| 100 | if strings.TrimSpace(options.ImportFrom) != "" { |
| 101 | data["scsi0"] = fmt.Sprintf("%s:0,import-from=%s", |
| 102 | strings.TrimSpace(options.DiskStorage), |
| 103 | strings.TrimSpace(options.ImportFrom), |
| 104 | ) |
| 105 | } else { |
| 106 | data["scsi0"] = fmt.Sprintf("%s:%d", strings.TrimSpace(options.DiskStorage), options.DiskSizeGB) |
| 107 | } |
| 108 | if strings.TrimSpace(options.ISOVolume) != "" { |
| 109 | data["cdrom"] = strings.TrimSpace(options.ISOVolume) |
| 110 | data["boot"] = "order=ide2;scsi0;net0" |
| 111 | } else if strings.TrimSpace(options.ImportFrom) != "" { |
| 112 | data["boot"] = "order=scsi0;net0" |
| 113 | } |
| 114 | if options.Start { |
| 115 | data["start"] = true |
| 116 | } |
| 117 | |
| 118 | var res map[string]interface{} |