From ce432829d509f3e17bd5a7662113285b57376fb7 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 24 May 2019 00:40:21 +0200 Subject: [PATCH] pkg: add support for systemd >= 240 and native btrfs parititions Systemd prior to version 240 was creating /var/lib/machines.raw as btrfs volume for storing machined disks. However, this functionality is now remove and now it stores it as regular files. More details here: https://github.com/systemd/systemd/blob/401faa3533280b05fee972e0c64885caf4b31e4c/units/var-lib-machines.mount#L10 Additionally, it someone already have /var/lib/machines baked by btrfs, machined won't create machines.raw, which will also cause setting up cluster to fail. This patch adds check, if machineImage exists and it not, it fallbacks to just checking disk space available for /var/lib/machines. Closes #339 #293 Signed-off-by: Mateusz Gozdek --- pkg/bootstrap/node.go | 32 +++++++++++++++++++++++++++----- pkg/cluster/cluster.go | 20 +++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/pkg/bootstrap/node.go b/pkg/bootstrap/node.go index 890e9740..0f90b3ab 100644 --- a/pkg/bootstrap/node.go +++ b/pkg/bootstrap/node.go @@ -62,19 +62,30 @@ func GetPoolSize(baseImageName string, nodes int) (int64, error) { var extraSizeRatio float64 = 0.5 var err error - baseImageAbspath := path.Join(machinesDir, baseImageName+".raw") - - if poolSize, err = getAllocatedFileSize(machinesImage); err != nil { + // Select correct path to check disk space. On older systemd versions + // machinesImage is used. + p := machinesDir + poolExists, err := CheckPoolExists() + if err != nil { return 0, err + } else if poolExists { + p = machinesImage + + if poolSize, err = getAllocatedFileSize(machinesImage); err != nil { + return 0, err + } + + extraSize = int64(float64(poolSize) * extraSizeRatio) } - extraSize = int64(float64(poolSize) * extraSizeRatio) + + baseImageAbspath := path.Join(machinesDir, baseImageName+".raw") if biSize, err = getAllocatedFileSize(baseImageAbspath); err != nil { return 0, err } extraSize += int64(float64(biSize)*extraSizeRatio) * int64(nodes) - varDir, _ := path.Split(machinesImage) + varDir, _ := path.Split(p) freeVolSpace, err := getVolFreeSpace(varDir) if err != nil { return 0, err @@ -95,6 +106,17 @@ func GetPoolSize(baseImageName string, nodes int) (int64, error) { return poolSize, nil } +func CheckPoolExists() (bool, error) { + if _, err := os.Stat(machinesImage); err != nil { + if os.IsNotExist(err) { + return false, nil + } else { + return false, err + } + } + return true, nil +} + func setPoolLimit(poolSize int64) error { var cmdPath string var err error diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index aa6dd607..d91dea54 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -368,15 +368,29 @@ func (c *Cluster) Start(numberNodes int, cniPluginDir, cniPlugin, flatcarChannel return err } - poolSize, err := bootstrap.GetPoolSize(bootstrap.BaseImageName, numberNodes) + poolExists, err := bootstrap.CheckPoolExists() if err != nil { return err } - log.Printf("new poolSize to be : %d\n", poolSize) - if err := bootstrap.EnlargeStoragePool(poolSize); err != nil { + + // TODO This shouldn't be hardcoded, but I don't know where to put it yet + imageName := "flatcar" + if poolExists { + imageName = bootstrap.BaseImageName + } + + poolSize, err := bootstrap.GetPoolSize(imageName, numberNodes) + if err != nil { return err } + if poolExists { + log.Printf("new poolSize to be : %d\n", poolSize) + if err := bootstrap.EnlargeStoragePool(poolSize); err != nil { + return err + } + } + ctx, cancel := context.WithCancel(context.Background()) defer cancel()