diff --git a/src/qga/qga.go b/src/qga/qga.go index 3c0ca98bf505c30d44520374a009741b925cb3c3..8c90baa157f79024f3f9e00c4454fd6adf02b624 100644 --- a/src/qga/qga.go +++ b/src/qga/qga.go @@ -3,6 +3,7 @@ package qga import ( "fmt" "net" + "errors" "encoding/base64" "nexus-server/logger" ) @@ -38,7 +39,7 @@ func (q *QGACon)SendShutdown() error { } func (q *QGACon)WriteFile(filePath string) error { - filePath := "/home/nexus/pipo.txt" + filePath = "/home/nexus/pipo.txt" _, err := q.sock.Write([]byte(`{"execute":"guest-file-open", "arguments":{"path":"`+filePath+`","mode":"w+"}}`)) buf := make([]byte, 128) _, err = q.sock.Read(buf) @@ -51,7 +52,10 @@ func (q *QGACon)WriteFile(filePath string) error { // content must be encoded in base64 content := "Hello ma poule!" contentB64 := base64.StdEncoding.EncodeToString([]byte(content)) - status, err = q.sock.Write([]byte(`{"execute":"guest-file-write", "arguments":{"handle":1000,"buf-b64":"`+contentB64+`"}}`)) + status, err := q.sock.Write([]byte(`{"execute":"guest-file-write", "arguments":{"handle":1000,"buf-b64":"`+contentB64+`"}}`)) status, err = q.sock.Write([]byte(`{"execute":"guest-file-close", "arguments":{"handle":1000}}`)) + if status < 0 { + return errors.New("write error") + } return err } diff --git a/src/vms/vms.go b/src/vms/vms.go index 3112bc5d27434d8bbd5181fcb6f1df555ed13974..662f583535b2e012b295dc8a751b06486ab3ac46 100644 --- a/src/vms/vms.go +++ b/src/vms/vms.go @@ -2,7 +2,6 @@ package vms import ( "os" - "math" "sort" "sync" "errors" @@ -24,7 +23,6 @@ type ( dir string // Base directory where VMs are stored rwlock *sync.RWMutex // RWlock to ensure the coherency of the map (m) of VMs usedPorts [65536]bool // Indicates which ports are used by the VMs - usedRAM int // Used RAM by running VMs (in MB) } ) @@ -41,7 +39,7 @@ func GetVMsInstance() *VMs { // NOTE: path is the root directory where VMs reside. func InitVMs() error { vmsDir := paths.GetInstance().VMsDir - vms = &VMs { m: make(map[string]VM), dir: vmsDir, rwlock: new(sync.RWMutex), usedRAM: 0 } + vms = &VMs { m: make(map[string]VM), dir: vmsDir, rwlock: new(sync.RWMutex) } errMsg := "Failed reading VMs directory: " dirs1, err := utils.GetSubDirs(vmsDir) @@ -172,18 +170,18 @@ func (vms *VMs)StartVM(vmID uuid.UUID) (int, string, error) { return 0, "", err } - // Check there will be at least 2GB of RAM left after the VM has started, - // otherwise, we prevent the execution of the VM to avoid RAM saturation. - // Furthermore, given Linux's KSVM technology allows page sharing, we - // assume it's able to save 30% of RAM (should be a conservative estimate). _, availRAM, err := utils.GetRAM() if err != nil { return -1, "", errors.New("Failed obtaining memory info: "+err.Error()) } - vms.usedRAM += vm.Ram - if availRAM - int(math.Round(float64(vms.usedRAM)*0.7)) < 2048 { - vms.usedRAM -= vm.Ram + // Check there will be at least 16GB of RAM left after the VM has started, + // otherwise, we prevent the execution of the VM to avoid RAM saturation. + // Thanks to Linux's KSVM technology, pages sharing the same content are actually shared + // which dramatically reduces the amount of RAM being used (roughly ~50% in average). + log.Info("vm.RAM: ", vm.Ram) + log.Info("Available RAM: ", availRAM) + if availRAM - vm.Ram < 16*1024 { return -1, "", errors.New("Insufficient free RAM to start VM") } @@ -207,7 +205,6 @@ func (vms *VMs)StartVM(vmID uuid.UUID) (int, string, error) { vm.mutex.Unlock() vms.rwlock.Lock() vms.usedPorts[vm.Run.Port] = false - vms.usedRAM -= vm.Ram vms.updateVMMap(vm) vms.rwlock.Unlock() }