summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2014-10-01 16:10:01 -0400
committerScott Moser <smoser@ubuntu.com>2014-10-01 16:10:01 -0400
commitdc626ee8750e79518c16769a778e143461d33dc4 (patch)
treefdb809873eb6b4cfc467ab3436cc345fc7865a87
parenta429725ede3395a7e9c21b864a82e2c092657ae2 (diff)
parent5bb6a49f66c76d9accb912a5612c970a52f5b964 (diff)
downloadvyos-cloud-init-dc626ee8750e79518c16769a778e143461d33dc4.tar.gz
vyos-cloud-init-dc626ee8750e79518c16769a778e143461d33dc4.zip
cc_mounts: add ability to create swap file
User can now configure setting of a swap file. Only supports un-encrypted swap for now. swap: filename: /swap.img size: "auto" or size in bytes maxsize: size in bytes Also adds 2 util: read_meminfo: return how much memory on system. human2bytes: convert human numbers (8G) to bytes.
-rw-r--r--ChangeLog1
-rw-r--r--cloudinit/config/cc_mounts.py157
-rw-r--r--cloudinit/util.py50
-rw-r--r--doc/examples/cloud-config-mount-points.txt7
4 files changed, 215 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 9091a694..b655946b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,7 @@
- cc_grub_dpkg: fix EC2 hvm instances to avoid prompt on grub update.
(LP: #1336855)
- FreeBsd: support config drive datasource [Joseph bajin]
+ - cc_mounts: support creating a swap file
0.7.5:
- open 0.7.5
- Add a debug log message around import failures
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
index ba1303d1..1cb1e839 100644
--- a/cloudinit/config/cc_mounts.py
+++ b/cloudinit/config/cc_mounts.py
@@ -75,6 +75,159 @@ def sanitize_devname(startname, transformer, log):
return devnode_for_dev_part(blockdev, part)
+def suggested_swapsize(memsize=None, maxsize=None, fsys=None):
+ # make a suggestion on the size of swap for this system.
+ if memsize is None:
+ memsize = util.read_meminfo()['total']
+
+ GB = 2 ** 30
+ sugg_max = 8 * GB
+
+ info = {'avail': 'na', 'max_in': maxsize, 'mem': memsize}
+
+ if fsys is None and maxsize is None:
+ # set max to 8GB default if no filesystem given
+ maxsize = sugg_max
+ elif fsys:
+ statvfs = os.statvfs(fsys)
+ avail = statvfs.f_frsize * statvfs.f_bfree
+ info['avail'] = avail
+
+ if maxsize is None:
+ # set to 25% of filesystem space
+ maxsize = min(int(avail / 4), sugg_max)
+ elif maxsize > ((avail * .9)):
+ # set to 90% of available disk space
+ maxsize = int(avail * .9)
+ elif maxsize is None:
+ maxsize = sugg_max
+
+ info['max'] = maxsize
+
+ formulas = [
+ # < 1G: swap = double memory
+ (1 * GB, lambda x: x * 2),
+ # < 2G: swap = 2G
+ (2 * GB, lambda x: 2 * GB),
+ # < 4G: swap = memory
+ (4 * GB, lambda x: x),
+ # < 16G: 4G
+ (16 * GB, lambda x: 4 * GB),
+ # < 64G: 1/2 M up to max
+ (64 * GB, lambda x: x / 2),
+ ]
+
+ size = None
+ for top, func in formulas:
+ if memsize <= top:
+ size = min(func(memsize), maxsize)
+ # if less than 1/2 memory and not much, return 0
+ if size < (memsize / 2) and size < 4 * GB:
+ size = 0
+ break
+ break
+
+ if size is not None:
+ size = maxsize
+
+ info['size'] = size
+
+ MB = 2 ** 20
+ pinfo = {}
+ for k, v in info.items():
+ if isinstance(v, int):
+ pinfo[k] = "%s MB" % (v / MB)
+ else:
+ pinfo[k] = v
+
+ LOG.debug("suggest %(size)s swap for %(mem)s memory with '%(avail)s'"
+ " disk given max=%(max_in)s [max=%(max)s]'" % pinfo)
+ return size
+
+
+def setup_swapfile(fname, size=None, maxsize=None):
+ """
+ fname: full path string of filename to setup
+ size: the size to create. set to "auto" for recommended
+ maxsize: the maximum size
+ """
+ tdir = os.path.dirname(fname)
+ if str(size).lower() == "auto":
+ try:
+ memsize = util.read_meminfo()['total']
+ except IOError as e:
+ LOG.debug("Not creating swap. failed to read meminfo")
+ return
+
+ util.ensure_dir(tdir)
+ size = suggested_swapsize(fsys=tdir, maxsize=maxsize,
+ memsize=memsize)
+
+ if not size:
+ LOG.debug("Not creating swap: suggested size was 0")
+ return
+
+ mbsize = str(int(size / (2 ** 20)))
+ msg = "creating swap file '%s' of %sMB" % (fname, mbsize)
+ try:
+ util.ensure_dir(tdir)
+ util.log_time(LOG.debug, msg, func=util.subp,
+ args=[['sh', '-c',
+ ('rm -f "$1" && umask 0066 && '
+ 'dd if=/dev/zero "of=$1" bs=1M "count=$2" && '
+ 'mkswap "$1" || { r=$?; rm -f "$1"; exit $r; }'),
+ 'setup_swap', fname, mbsize]])
+
+ except Exception as e:
+ raise IOError("Failed %s: %s" % (msg, e))
+
+ return fname
+
+
+def handle_swapcfg(swapcfg):
+ """handle the swap config, calling setup_swap if necessary.
+ return None or (filename, size)
+ """
+ if not isinstance(swapcfg, dict):
+ LOG.warn("input for swap config was not a dict.")
+ return None
+
+ fname = swapcfg.get('filename', '/swap.img')
+ size = swapcfg.get('size', 0)
+ maxsize = swapcfg.get('maxsize', None)
+
+ if not (size and fname):
+ LOG.debug("no need to setup swap")
+ return
+
+ if os.path.exists(fname):
+ if not os.path.exists("/proc/swaps"):
+ LOG.debug("swap file %s existed. no /proc/swaps. Being safe.",
+ fname)
+ return fname
+ try:
+ for line in util.load_file("/proc/swaps").splitlines():
+ if line.startswith(fname + " "):
+ LOG.debug("swap file %s already in use.", fname)
+ return fname
+ LOG.debug("swap file %s existed, but not in /proc/swaps", fname)
+ except:
+ LOG.warn("swap file %s existed. Error reading /proc/swaps", fname)
+ return fname
+
+ 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"]
@@ -162,6 +315,10 @@ def handle(_name, cfg, cloud, log, _args):
else:
actlist.append(x)
+ swapret = handle_swapcfg(cfg.get('swap', {}))
+ if swapret:
+ actlist.append([swapret, "none", "swap", "sw", "0", "0"])
+
if len(actlist) == 0:
log.debug("No modifications to fstab needed.")
return
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 76e91951..f236d0bf 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1957,3 +1957,53 @@ def pathprefix2dict(base, required=None, optional=None, delim=os.path.sep):
raise ValueError("Missing required files: %s", ','.join(missing))
return ret
+
+
+def read_meminfo(meminfo="/proc/meminfo", raw=False):
+ # read a /proc/meminfo style file and return
+ # a dict with 'total', 'free', and 'available'
+ mpliers = {'kB': 2**10, 'mB': 2 ** 20, 'B': 1, 'gB': 2 ** 30}
+ kmap = {'MemTotal:': 'total', 'MemFree:': 'free',
+ 'MemAvailable:': 'available'}
+ ret = {}
+ for line in load_file(meminfo).splitlines():
+ try:
+ key, value, unit = line.split()
+ except ValueError:
+ key, value = line.split()
+ unit = 'B'
+ if raw:
+ ret[key] = int(value) * mpliers[unit]
+ elif key in kmap:
+ 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])
diff --git a/doc/examples/cloud-config-mount-points.txt b/doc/examples/cloud-config-mount-points.txt
index 416006db..3b45b47f 100644
--- a/doc/examples/cloud-config-mount-points.txt
+++ b/doc/examples/cloud-config-mount-points.txt
@@ -37,3 +37,10 @@ mounts:
# complete. This must be an array, and must have 7 fields.
mount_default_fields: [ None, None, "auto", "defaults,nobootwait", "0", "2" ]
+
+# swap can also be set up by the 'mounts' module
+# default is to not create any swap files, because 'size' is set to 0
+swap:
+ filename: /swap.img
+ size: "auto" or size in bytes
+ maxsize: size in bytes