Skip to content

Commit

Permalink
add vm clone/config
Browse files Browse the repository at this point in the history
  • Loading branch information
sp-yduck committed Dec 12, 2023
1 parent e27bc03 commit 1d590a6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
41 changes: 38 additions & 3 deletions api/qemu_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
type Arch string
type OSType string
type ScsiHw string
type StorageFormat string

const (
X86_64 Arch = "x86_64"
Expand Down Expand Up @@ -66,6 +67,12 @@ const (
Pvscsi = "pvscsi"
)

const (
Raw StorageFormat = "raw"
Qcow2 StorageFormat = "qcow2"
Vmdk StorageFormat = "vmdk"
)

type Ide struct {
Ide0 string `json:"ide0,omitempty"`
Ide1 string `json:"ide1,omitempty"`
Expand Down Expand Up @@ -479,11 +486,11 @@ type VirtualMachineConfig struct {
// Use STORAGE_ID:0 and the 'import-from' parameter to import from an existing volume.
Ide `json:",inline"`
IPConfig `json:",inline"`
IvshMem string `json:"ivshmem,omitempty"`
IVshMem string `json:"ivshmem,omitempty"`
KeepHugePages int8 `json:"keephugepages,omitempty"`
Keyboard string `json:"keyboard,omitempty"`
// enable/disable KVM hardware virtualization
Kvm int8 `json:"kvm,omitempty"`
KVM int8 `json:"kvm,omitempty"`
LocalTime int8 `json:"localtime,omitempty"`
Lock string `json:"lock,omitempty"`
// specifies the QEMU machine type
Expand All @@ -498,7 +505,8 @@ type VirtualMachineConfig struct {
NameServer string `json:"nameserver,omitempty"`
// network device
Net `json:",inline"`
Numa int8 `json:"numa,omitempty"`
Node string `json:"-"`
Numa int8 `json:"numa,omitempty"`
NumaS `json:",inline"`
// specifies whether a VM will be started during system bootup
OnBoot int8 `json:"onboot,omitempty"`
Expand Down Expand Up @@ -581,3 +589,30 @@ type VirtualMachineStopOption struct {
SkipLock int8 `json:"skiplock,omitempty"`
TimeOut int `json:"timeout,omitempty"`
}

type VirtualMachineCloneOption struct {
// VMID for the clone.
NewID int `json:"newid"`
// The cluster node name.
Node string `json:"node"`
// The (unique) ID of the VM.
VMID int `json:"vmid"`
// Override I/O bandwidth limit (in KiB/s).
BWLimit int `json:"bwlimit,omitempty"`
// Description for the new VM.
Description string `json:"description,omitempty"`
// Target format for file storage. Only valid for full clone.
Format StorageFormat `json:"format,omitempty"`
// Create a full copy of all disks. This is always done when you clone a normal VM. For VM templates, we try to create a linked clone by default.
Full int8 `json:"full,omitempty"`
// Set a name for the new VM.
Name string `json:"name,omitempty"`
// Add the new VM to the specified pool.
Pool string `json:"pool,omitempty"`
// The name of the snapshot.
SnapName string `json:"snapname,omitempty"`
// Target storage for full clone.
Storage string `json:"storage,omitempty"`
// Target node. Only allowed if the original VM is on shared storage.
Target string `json:"target,omitempty"`
}
20 changes: 20 additions & 0 deletions proxmox/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func (s *Service) CreateVirtualMachine(ctx context.Context, node string, vmid in
return s.VirtualMachine(ctx, vmid)
}

func (s *Service) CloneVirtualMachine(ctx context.Context, node string, vmid int, newid int, option api.VirtualMachineCloneOption) (*VirtualMachine, error) {
taskid, err := s.restclient.CreateVirtualMachineClone(ctx, node, vmid, newid, option)
if err != nil {
return nil, err
}
if err := s.EnsureTaskDone(ctx, node, *taskid); err != nil {
return nil, err
}
return s.VirtualMachine(ctx, newid)
}

// VirtualMachineFromUUID attempts to find virtual machine based on SMBIOS UUID. It will ignore any error that prevents
// it from inspecting additional virtual machines (e.g. offline node, vm config not accessible, malformed uuids)
func (s *Service) VirtualMachineFromUUID(ctx context.Context, uuid string) (*VirtualMachine, error) {
Expand Down Expand Up @@ -149,6 +160,15 @@ func (c *VirtualMachine) GetConfig(ctx context.Context) (*api.VirtualMachineConf
return c.config, err
}

// Set virtual machine options (asynchrounous API).
func (c *VirtualMachine) SetConfigAsync(ctx context.Context, config api.VirtualMachineConfig) error {
taskid, err := c.restclient.SetVirtualMachineConfigAsync(ctx, c.Node, c.VM.VMID, config)
if err != nil {
return err
}
return c.service.EnsureTaskDone(ctx, c.Node, *taskid)
}

func (c *VirtualMachine) GetOSInfo(ctx context.Context) (*api.OSInfo, error) {
var osInfo *api.OSInfo
path := fmt.Sprintf("/nodes/%s/qemu/%d/agent/get-osinfo", c.Node, c.VM.VMID)
Expand Down
22 changes: 22 additions & 0 deletions rest/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func (c *RESTClient) GetVirtualMachineConfig(ctx context.Context, node string, v
return config, nil
}

// Set virtual machine options (asynchrounous API).
func (c *RESTClient) SetVirtualMachineConfigAsync(ctx context.Context, node string, vmid int, options api.VirtualMachineConfig) (*string, error) {
path := fmt.Sprintf("/nodes/%s/qemu/%d/config", node, vmid)
var upid *string
if err := c.Post(ctx, path, options, &upid); err != nil {
return nil, err
}
return upid, nil
}

func (c *RESTClient) GetVirtualMachineStatus(ctx context.Context, node string, vmid int) (*api.ProcessStatus, error) {
path := fmt.Sprintf("/nodes/%s/qemu/%d/status", node, vmid)
var status *api.ProcessStatus
Expand All @@ -65,3 +75,15 @@ func (c *RESTClient) GetVirtualMachineStatus(ctx context.Context, node string, v
}
return status, nil
}

func (c *RESTClient) CreateVirtualMachineClone(ctx context.Context, node string, templateid, vmid int, option api.VirtualMachineCloneOption) (*string, error) {
option.Node = node
option.VMID = templateid
option.NewID = vmid
path := fmt.Sprintf("/nodes/%s/qemu/%d/clone", node, templateid)
var upid *string
if err := c.Post(ctx, path, option, &upid); err != nil {
return nil, err
}
return upid, nil
}
26 changes: 26 additions & 0 deletions rest/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,29 @@ func (s *TestSuite) TestGetVirtualMachineConfig() {
}
s.T().Logf("get vm config: %v", *config)
}

func (s *TestSuite) TestSetVirtualMachineConfigAsync() {
nodeName := "assam"
vmid := 999
config := api.VirtualMachineConfig{
CiPassword: "pve",
CiUser: "pve",
}
upid, err := s.restclient.SetVirtualMachineConfigAsync(context.TODO(), nodeName, vmid, config)
if err != nil {
s.T().Fatalf("failed to set vm: %v", err)
}
s.T().Logf("set vm config: %s", *upid)

}

func (s *TestSuite) TestCreateVirtualMachineClone() {
nodeName := s.GetTestNode().Node
vmid := s.GetTestVM().VMID
option := api.VirtualMachineCloneOption{}
upid, err := s.restclient.CreateVirtualMachineClone(context.TODO(), nodeName, vmid, 999, option)
if err != nil {
s.T().Fatalf("failed to clone vm: %v", err)
}
s.T().Logf("clone vm: %s", *upid)
}

0 comments on commit 1d590a6

Please sign in to comment.