← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~adobrawy/cloud-init:swap-linux into cloud-init:master

 

Adam Dobrawy has proposed merging ~adobrawy/cloud-init:swap-linux into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~adobrawy/cloud-init/+git/cloud-init/+merge/354680

xfs, which is default on CentOS7 doesn't handle fallocate correctly when used with swapfiles.
Therefore we need to check if the FS is fallocate-safe. Not sure if other FS' are affected.
https://unix.stackexchange.com/questions/294600/i-cant-enable-swap-space-on-centos-7#294605

This is a friendly transfer of the pull-request originally reported on GitHub: https://github.com/cloud-init/cloud-init/pull/11
I work with Krzysztof Biernat and I have obtained permission from him for such an operation.
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~adobrawy/cloud-init:swap-linux into cloud-init:master.
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
index 339baba..e54e753 100644
--- a/cloudinit/config/cc_mounts.py
+++ b/cloudinit/config/cc_mounts.py
@@ -249,14 +249,22 @@ def setup_swapfile(fname, size=None, maxsize=None):
     msg = "creating swap file '%s' of %sMB" % (fname, mbsize)
     try:
         util.ensure_dir(tdir)
+        # Check if filesystem is safe for fallocate
+        fname_fs_type = util.get_mount_info(fname)[1]
+        if fname_fs_type in ['xfs']:
+            create_swapfile_command = 'dd if=/dev/zero "of=$1" bs=1M "count=$2"'
+        else:
+            create_swapfile_command = 'fallocate -l "${2}M" "$1"'
+
         util.log_time(LOG.debug, msg, func=util.subp,
                       args=[['sh', '-c',
                             ('rm -f "$1" && umask 0066 && '
-                             '{ fallocate -l "${2}M" "$1" || '
-                             ' dd if=/dev/zero "of=$1" bs=1M "count=$2"; } && '
-                             'mkswap "$1" || { r=$?; rm -f "$1"; exit $r; }'),
+                             '%s && '
+                             'mkswap "$1" || { r=$?; rm -f "$1"; exit $r; }' %
+                             create_swapfile_command),
                              'setup_swap', fname, mbsize]])
 
+
     except Exception as e:
         raise IOError("Failed %s: %s" % (msg, e))
 

Follow ups