config: Detect if VM memory smaller than image

Add a heuristic to ensure the amount of memory allocated to the
hypervisor is bigger than the size of the image.

This catches simple configuration issues where `default_memory=` is set
to a smaller value than the size of either the `image=` or `initrd=`
files.

If the configured image type is `initrd`, fail but only warn in the
logs for `image` as although it seems a highly unlikely scenario, it is
permitted.

Update tests to ensure that created resources have `>0` bytes.

Fixes #636.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt
2018-08-28 16:17:55 +01:00
parent 1ba4841865
commit b5ea753ff4
5 changed files with 226 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
)
const (
@@ -230,3 +231,15 @@ func writeFile(filePath string, data string, fileMode os.FileMode) error {
func isEmptyString(b []byte) bool {
return len(bytes.Trim(b, "\n")) == 0
}
// fileSize returns the number of bytes in the specified file
func fileSize(file string) (int64, error) {
st := syscall.Stat_t{}
err := syscall.Stat(file, &st)
if err != nil {
return 0, err
}
return st.Size, nil
}