Skip to content

Commit 86b9296

Browse files
committed
lxcs can now be cloned
1 parent 75a1fe9 commit 86b9296

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- `POST /custom-api/v1/lxc/{id}/exec-async` implementation as `PVE.LXC.ExecAsync`.
2222
- `GET /custom-api/v1/cmd/{id}` implementation as `PVE.LXC.GetCMDResult`.
2323
- `GET /nodes/{node}/lxc/{id}/config` implementation as `PVE.LXC.Update`.
24+
- `POST /nodes/{node}/lxc/{vmid}/clone` implementation as `PVE.LXC.Clone`.
2425
- `POST /nodes/{node}/lxc/{vmid}/template` implementation as `PVE.LXC.CreateTemplate`.
2526

2627
## [v0.6.1]

pkg/pve/pve_lxc.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,78 @@ func (s *PVELxcService) Update(req UpdateLxcRequest) (err error) {
846846
return nil
847847
}
848848

849+
type pveLXCCloneRequest struct {
850+
Node string `in:"nonzero;path=node"`
851+
VMID int `in:"nonzero;path=vmid"`
852+
NewVMID int `in:"nonzero;form=newid"`
853+
BWLimit int `in:"omitempty;form=bwlimit"`
854+
Desc string `in:"omitempty;form=description"`
855+
Full int `in:"omitempty;form=full"` // bool
856+
Hostname string `in:"omitempty;form=hostname"`
857+
Pool string `in:"omitempty;form=pool"`
858+
Snapname string `in:"omitempty;form=snapname"`
859+
Storage string `in:"omitempty;form=storage"`
860+
Target string `in:"omitempty;form=target"`
861+
}
862+
863+
type CloneLxcRequest struct {
864+
Node string // The cluster node name.
865+
VMID int // The (unique) ID of the source VM.
866+
NewVMID int // The (unique) ID of the target VM (if not set, the next available VMID will be used).
867+
BWLimit int // Override I/O bandwidth limit (in KiB/s).
868+
Desc string // Description for the Container.
869+
Full bool // Create a full copy of all disks. This is always done when you clone a normal CT. For CT templates, we try to create a linked clone by default.
870+
Hostname string // Set a host name for the container.
871+
Pool string // Add the VM to the specified pool.
872+
Snapname string // The name of the snapshot.
873+
Storage string // Target storage for full clone.
874+
Target string // Target node. Only allowed if the original VM is on shared storage.
875+
}
876+
877+
// Clone creates a clone or copy of an existing LXC container.
878+
//
879+
// POST /nodes/{node}/lxc/{vmid}/clone needs 'VM.Clone' permissions
880+
// on /vms/{vmid}, and 'VM.Allocate' permissions on /vms/{newid}
881+
// (or on the VM pool /pool/{pool}). You also need
882+
// 'Datastore.AllocateSpace' on any used storage, and 'SDN.Use'
883+
// on any bridge.
884+
func (s *PVELxcService) Clone(req CloneLxcRequest) (newVMID int, err error) {
885+
method := http.MethodPost
886+
path := "/nodes/{node}/lxc/{vmid}/clone"
887+
888+
if req.VMID == 0 {
889+
return 0, fmt.Errorf("rep.VMID is required")
890+
}
891+
892+
if req.NewVMID == 0 {
893+
if req.NewVMID, err = s.api.Cluster.GetNextVMID(); err != nil {
894+
return 0, err
895+
}
896+
}
897+
898+
// convert bool to int
899+
full := helpers.BoolToInt(req.Full)
900+
901+
payload := pveLXCCloneRequest{
902+
Node: req.Node,
903+
VMID: req.VMID,
904+
NewVMID: req.NewVMID,
905+
BWLimit: req.BWLimit,
906+
Desc: req.Desc,
907+
Full: full,
908+
Hostname: req.Hostname,
909+
Pool: req.Pool,
910+
Snapname: req.Snapname,
911+
Storage: req.Storage,
912+
Target: req.Target,
913+
}
914+
if err = s.api.client.sendReq3(method, path, &payload, nil, nil); err != nil {
915+
return 0, err
916+
}
917+
918+
return req.NewVMID, nil
919+
}
920+
849921
func (s *PVELxcService) CreateTemplate(node string, vmid int) (err error) {
850922
method := http.MethodPost
851923
path := "/nodes/{node}/lxc/{vmid}/template"

0 commit comments

Comments
 (0)