diff options
author | Scott Moser <smoser@ubuntu.com> | 2014-09-30 16:01:00 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2014-09-30 16:01:00 -0400 |
commit | e76fea0e88ecc361eb7e301c0916a89cb4505c24 (patch) | |
tree | ebb80791984ae8ef844f3b4cb1522ce32fcc2888 /cloudinit/config/cc_mounts.py | |
parent | a429725ede3395a7e9c21b864a82e2c092657ae2 (diff) | |
download | vyos-cloud-init-e76fea0e88ecc361eb7e301c0916a89cb4505c24.tar.gz vyos-cloud-init-e76fea0e88ecc361eb7e301c0916a89cb4505c24.zip |
add code for setting up swap file
Diffstat (limited to 'cloudinit/config/cc_mounts.py')
-rw-r--r-- | cloudinit/config/cc_mounts.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py index ba1303d1..7af73c71 100644 --- a/cloudinit/config/cc_mounts.py +++ b/cloudinit/config/cc_mounts.py @@ -75,6 +75,92 @@ 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 + + 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 + + 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) + + 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: + return 0 + return size + + return maxsize + + +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, size + + 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 +248,20 @@ 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") + if len(actlist) == 0: log.debug("No modifications to fstab needed.") return |