CreateLXC creates an LXC container and returns the queued task UPID.
(nodeName string, options LXCCreateOptions)
| 135 | |
| 136 | // CreateLXC creates an LXC container and returns the queued task UPID. |
| 137 | func (c *Client) CreateLXC(nodeName string, options LXCCreateOptions) (string, error) { |
| 138 | if strings.TrimSpace(nodeName) == "" { |
| 139 | return "", fmt.Errorf("node name is required") |
| 140 | } |
| 141 | if options.VMID <= 0 { |
| 142 | return "", fmt.Errorf("vmid must be positive") |
| 143 | } |
| 144 | if strings.TrimSpace(options.Hostname) == "" { |
| 145 | return "", fmt.Errorf("hostname is required") |
| 146 | } |
| 147 | if strings.TrimSpace(options.OSTemplate) == "" { |
| 148 | return "", fmt.Errorf("os template is required") |
| 149 | } |
| 150 | if strings.TrimSpace(options.RootFSStorage) == "" { |
| 151 | return "", fmt.Errorf("rootfs storage is required") |
| 152 | } |
| 153 | if options.RootFSSizeGB <= 0 { |
| 154 | return "", fmt.Errorf("rootfs size must be positive") |
| 155 | } |
| 156 | if options.MemoryMB <= 0 { |
| 157 | options.MemoryMB = 512 |
| 158 | } |
| 159 | if options.Cores <= 0 { |
| 160 | options.Cores = 1 |
| 161 | } |
| 162 | if options.SwapMB < 0 { |
| 163 | options.SwapMB = 0 |
| 164 | } |
| 165 | if strings.TrimSpace(options.Bridge) == "" { |
| 166 | options.Bridge = "vmbr0" |
| 167 | } |
| 168 | |
| 169 | unprivileged := 0 |
| 170 | if options.Unprivileged { |
| 171 | unprivileged = 1 |
| 172 | } |
| 173 | |
| 174 | data := map[string]interface{}{ |
| 175 | "vmid": options.VMID, |
| 176 | "hostname": strings.TrimSpace(options.Hostname), |
| 177 | "memory": options.MemoryMB, |
| 178 | "swap": options.SwapMB, |
| 179 | "cores": options.Cores, |
| 180 | "ostemplate": strings.TrimSpace(options.OSTemplate), |
| 181 | "rootfs": fmt.Sprintf("%s:%d", strings.TrimSpace(options.RootFSStorage), options.RootFSSizeGB), |
| 182 | "net0": fmt.Sprintf("name=eth0,bridge=%s,ip=dhcp", strings.TrimSpace(options.Bridge)), |
| 183 | "unprivileged": unprivileged, |
| 184 | } |
| 185 | if options.Nesting { |
| 186 | data["features"] = "nesting=1" |
| 187 | } |
| 188 | if options.Start { |
| 189 | data["start"] = 1 |
| 190 | } |
| 191 | |
| 192 | var res map[string]interface{} |
| 193 | if err := c.PostWithResponse(fmt.Sprintf("/nodes/%s/lxc", nodeName), data, &res); err != nil { |
| 194 | return "", fmt.Errorf("failed to create LXC on node %s: %w", nodeName, err) |