| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | package vz |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| 11 | "runtime" |
| 12 | |
| 13 | "github.com/Code-Hex/vz/v3" |
| 14 | "github.com/docker/go-units" |
| 15 | ) |
| 16 | |
| 17 | func (l *LimaVzDriver) SetMemoryTarget(ctx context.Context, target uint64) error { |
| 18 | if err := ctx.Err(); err != nil { |
| 19 | return err |
| 20 | } |
| 21 | maximum, err := units.RAMInBytes(*l.Instance.Config.Memory) |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | if target < 512<<20 || target > uint64(maximum) || target%(1<<20) != 0 { |
| 26 | return errors.New("memory target is outside the VM capacity") |
| 27 | } |
| 28 | if l.machine == nil || l.machine.State() != vz.VirtualMachineStateRunning { |
| 29 | return errors.New("VM is not running") |
| 30 | } |
| 31 | for _, device := range l.machine.MemoryBalloonDevices() { |
| 32 | if balloon := vz.AsVirtioTraditionalMemoryBalloonDevice(device); balloon != nil { |
| 33 | |
| 34 | |
| 35 | |
| 36 | runtime.SetFinalizer(balloon, nil) |
| 37 | balloon.SetTargetVirtualMachineMemorySize(target) |
| 38 | runtime.KeepAlive(l.machine) |
| 39 | return nil |
| 40 | } |
| 41 | } |
| 42 | return errors.New("memory balloon is unavailable") |
| 43 | } |