Skip to content

Commit

Permalink
Merge pull request #255 from Tinyblargon/Overhaul-Qemu-Disks-Merge
Browse files Browse the repository at this point in the history
Overhaul qemu disks merge
  • Loading branch information
mleone87 authored May 14, 2023
2 parents 5c7733d + 9fa3565 commit 8da8251
Show file tree
Hide file tree
Showing 13 changed files with 9,989 additions and 617 deletions.
12 changes: 11 additions & 1 deletion proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (vmr *VmRef) HaGroup() string {
return vmr.haGroup
}

func (vmr *VmRef) nilCheck() error {
if vmr == nil {
return errors.New("vm reference may not be nil")
}
return nil
}

func NewVmRef(vmId int) (vmr *VmRef) {
vmr = &VmRef{vmId: vmId, node: "", vmType: ""}
return
Expand Down Expand Up @@ -703,7 +710,7 @@ func (c *Client) RollbackQemuVm(vmr *VmRef, snapshot string) (exitStatus string,
return RollbackSnapshot(c, vmr, snapshot)
}

// SetVmConfig - send config options
// DEPRECATED SetVmConfig - send config options
func (c *Client) SetVmConfig(vmr *VmRef, params map[string]interface{}) (exitStatus interface{}, err error) {
return c.PostWithTask(params, "/nodes/"+vmr.node+"/"+vmr.vmType+"/"+strconv.Itoa(vmr.vmId)+"/config")
}
Expand Down Expand Up @@ -743,6 +750,7 @@ func (c *Client) MigrateNode(vmr *VmRef, newTargetNode string, online bool) (exi
}

// ResizeQemuDisk allows the caller to increase the size of a disk by the indicated number of gigabytes
// TODO Deprecate once LXC is able to resize disk by itself (qemu can already do this)
func (c *Client) ResizeQemuDisk(vmr *VmRef, disk string, moreSizeGB int) (exitStatus interface{}, err error) {
size := fmt.Sprintf("+%dG", moreSizeGB)
return c.ResizeQemuDiskRaw(vmr, disk, size)
Expand All @@ -753,6 +761,7 @@ func (c *Client) ResizeQemuDisk(vmr *VmRef, disk string, moreSizeGB int) (exitSt
// your desired size with a '+' character it will ADD size to the disk. If you just specify the size by
// itself it will do an absolute resizing to the specified size. Permitted suffixes are K, M, G, T
// to indicate order of magnitude (kilobyte, megabyte, etc). Decrease of disk size is not permitted.
// TODO Deprecate once LXC is able to resize disk by itself (qemu can already do this)
func (c *Client) ResizeQemuDiskRaw(vmr *VmRef, disk string, size string) (exitStatus interface{}, err error) {
// PUT
//disk:virtio0
Expand Down Expand Up @@ -793,6 +802,7 @@ func (c *Client) MoveLxcDisk(vmr *VmRef, disk string, storage string) (exitStatu
return
}

// DEPRECATED use MoveQemuDisk() instead.
// MoveQemuDisk - Move a disk from one storage to another
func (c *Client) MoveQemuDisk(vmr *VmRef, disk string, storage string) (exitStatus interface{}, err error) {
if disk == "" {
Expand Down
37 changes: 37 additions & 0 deletions proxmox/config_guest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package proxmox

import (
"strconv"
)

// All code LXC and Qemu have in common should be placed here.

// Check if there are any pending changes that require a reboot to be applied.
func GuestHasPendingChanges(vmr *VmRef, client *Client) (bool, error) {
params, err := pendingGuestConfigFromApi(vmr, client)
if err != nil {
return false, err
}
return keyExists(params, "pending"), nil
}

// Reboot the specified guest
func GuestReboot(vmr *VmRef, client *Client) (err error) {
_, err = client.ShutdownVm(vmr)
if err != nil {
return
}
_, err = client.StartVm(vmr)
return
}

func pendingGuestConfigFromApi(vmr *VmRef, client *Client) ([]interface{}, error) {
err := vmr.nilCheck()
if err != nil {
return nil, err
}
if err = client.CheckVmRef(vmr); err != nil {
return nil, err
}
return client.GetItemConfigInterfaceArray("/nodes/"+vmr.node+"/"+vmr.vmType+"/"+strconv.Itoa(vmr.vmId)+"/pending", "Guest", "PENDING CONFIG")
}
Loading

0 comments on commit 8da8251

Please sign in to comment.