summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/config/cc_mounts.py43
-rw-r--r--cloudinit/util.py29
2 files changed, 59 insertions, 13 deletions
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
index 7af73c71..b9aa9a12 100644
--- a/cloudinit/config/cc_mounts.py
+++ b/cloudinit/config/cc_mounts.py
@@ -128,6 +128,7 @@ def setup_swapfile(fname, size=None, maxsize=None):
size: the size to create. set to "auto" for recommended
maxsize: the maximum size
"""
+ print("fname: %s, size: %s maxsize: %s" % (fname, size, maxsize))
tdir = os.path.dirname(fname)
if str(size).lower() == "auto":
try:
@@ -161,6 +162,32 @@ def setup_swapfile(fname, size=None, maxsize=None):
return fname, size
+def handle_swapcfg(swapcfg):
+ """handle the swap config, calling setup_swap if necessary.
+ return None or (filename, size)
+ """
+ fname = swapcfg.get('filename', '/swap.img')
+ size = swapcfg.get('size', 0)
+ maxsize = swapcfg.get('maxsize', 0)
+
+ if not (size and fname):
+ LOG.debug("no need to setup swap")
+ return
+
+ try:
+ if isinstance(size, str) and size != "auto":
+ size = util.human2bytes(size)
+ if isinstance(maxsize, str):
+ maxsize = util.human2bytes(maxsize)
+ return setup_swapfile(fname=fname, size=size, maxsize=maxsize)
+
+ except Exception as e:
+ LOG.warn("failed to setup swap: %s", e)
+
+ return None
+
+
+
def handle(_name, cfg, cloud, log, _args):
# fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno
defvals = [None, None, "auto", "defaults,nobootwait", "0", "2"]
@@ -248,19 +275,9 @@ def handle(_name, cfg, cloud, log, _args):
else:
actlist.append(x)
- swapcfg = cfg.get('swap', {})
- swapfile = swapcfg.get('filename', '/swap.img')
- if swapcfg.get('size') and swapfile:
- try:
- sret = setup_swapfile(fpath=swapfile,
- size=swapcfg.get('size'),
- maxsize=swapcfg.get('maxsize'))
- if sret is not None:
- actlist.append([sret[0], "none", "swap", "sw", "0", "0"])
- except Exception as e:
- log.warn("failed to setup swap: %s", e)
- else:
- log.debug("no swap to setup")
+ swapret = handle_swapcfg(cfg.get('swap'))
+ if swapret:
+ actlist.append([swapret[0], "none", "swap", "sw", "0", "0"])
if len(actlist) == 0:
log.debug("No modifications to fstab needed.")
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 349d0155..f236d0bf 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1978,3 +1978,32 @@ def read_meminfo(meminfo="/proc/meminfo", raw=False):
ret[kmap[key]] = int(value) * mpliers[unit]
return ret
+
+
+def human2bytes(size):
+ """Convert human string or integer to size in bytes
+ 10M => 10485760
+ .5G => 536870912
+ """
+ size_in = size
+ if size.endswith("B"):
+ size = size[:-1]
+
+ mpliers = {'B': 1, 'K': 2 ** 10, 'M': 2 ** 20, 'G': 2 ** 30, 'T': 2 ** 40}
+
+ num = size
+ mplier = 'B'
+ for m in mpliers:
+ if size.endswith(m):
+ mplier = m
+ num = size[0:-len(m)]
+
+ try:
+ num = float(num)
+ except ValueError:
+ raise ValueError("'%s' is not valid input." % size_in)
+
+ if num < 0:
+ raise ValueError("'%s': cannot be negative" % size_in)
+
+ return int(num * mpliers[mplier])