From b749548a9eb43b34cce64f8688107645411abc8c Mon Sep 17 00:00:00 2001 From: Eduardo Otubo Date: Tue, 18 Aug 2020 23:12:02 +0200 Subject: Detect kernel version before swap file creation (#428) According to man page `man 8 swapon', "Preallocated swap files are supported on XFS since Linux 4.18". This patch checks for kernel version before attepting to create swapfile, using dd for XFS only on kernel versions <= 4.18 or btrfs. Add new func util.kernel_version which returns a tuple of ints (major, minor) Signed-off-by: Eduardo Otubo otubo@redhat.com --- cloudinit/config/cc_mounts.py | 8 +++++--- cloudinit/util.py | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py index 773b8285..54f2f878 100644 --- a/cloudinit/config/cc_mounts.py +++ b/cloudinit/config/cc_mounts.py @@ -65,7 +65,7 @@ swap file is created. from string import whitespace import logging -import os.path +import os import re from cloudinit import type_utils @@ -263,7 +263,8 @@ def create_swapfile(fname: str, size: str) -> None: fstype = util.get_mount_info(swap_dir)[1] - if fstype in ("xfs", "btrfs"): + if (fstype == "xfs" and + util.kernel_version() < (4, 18)) or fstype == "btrfs": create_swap(fname, size, "dd") else: try: @@ -273,7 +274,8 @@ def create_swapfile(fname: str, size: str) -> None: LOG.warning("Will attempt with dd.") create_swap(fname, size, "dd") - util.chmod(fname, 0o600) + if os.path.exists(fname): + util.chmod(fname, 0o600) try: subp.subp(['mkswap', fname]) except subp.ProcessExecutionError: diff --git a/cloudinit/util.py b/cloudinit/util.py index 624c560d..edd37039 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -68,6 +68,10 @@ CONTAINER_TESTS = (['systemd-detect-virt', '--quiet', '--container'], ['lxc-is-container']) +def kernel_version(): + return tuple(map(int, os.uname().release.split('.')[:2])) + + @lru_cache() def get_dpkg_architecture(target=None): """Return the sanitized string output by `dpkg --print-architecture`. -- cgit v1.2.3