Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect kernel version before swap file creation #428

Merged
merged 20 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a56ef54
Detect kernel version before swap file creation
otubo Feb 19, 2020
f2eae6a
Changing into a tuple and fixing pycodestyle
otubo Feb 20, 2020
73f97e9
fixing pycodestyle: Missing whitespace
otubo Feb 20, 2020
c746b03
Replace regex by os function calls
otubo Jun 11, 2020
e5fc9d4
adding new line to avoid flake8 complain
otubo Jun 11, 2020
183c5ae
adding @lru_cache() as the kernel version is unlikely to change durin…
otubo Jun 15, 2020
bf109a0
Update cloudinit/config/cc_mounts.py
otubo Jun 23, 2020
ec1f08b
Adding tests for util.kernel_version() new function
otubo Jun 23, 2020
d2cd4b4
Adding test to check swap file creation method
otubo Jun 23, 2020
d01111b
Wrong mock wasn't actually testing kernel_version
otubo Jun 24, 2020
4f6b9fc
Mocking os.uname() should set release and not return_value
otubo Jun 24, 2020
d33cea3
Wrapping test_kernel_version() into class
otubo Jul 6, 2020
52f1ca5
Splitting test_swap_creation_method() test into 3
otubo Jul 6, 2020
a2527be
Moving swap tests from TestFstabHandling to TestSwapFileCreation
otubo Jul 9, 2020
8082a78
removing blank line again
otubo Jul 9, 2020
474c47b
Merge branch 'master' into detect-kernel-version
OddBloke Jul 15, 2020
a521f6c
Removing fstab writing from tests
otubo Jul 30, 2020
2a8239c
Adding an additional test for the case of fallocate on xfs
otubo Aug 17, 2020
97801c7
Merge branch 'master' into detect-kernel-version
otubo Aug 18, 2020
9b0ac2c
fixing merge, removing pytest import (done by my patch) and removing …
otubo Aug 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cloudinit/config/cc_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from string import whitespace

import logging
import os.path
import os
import re

from cloudinit import type_utils
Expand Down Expand Up @@ -263,7 +263,8 @@ def create_swap(fname, size, method):

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:
Expand All @@ -273,7 +274,8 @@ def create_swap(fname, size, method):
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:
Expand Down
4 changes: 4 additions & 0 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
['lxc-is-container'])


def kernel_version():
otubo marked this conversation as resolved.
Show resolved Hide resolved
return tuple(map(int, os.uname().release.split('.')[:2]))
OddBloke marked this conversation as resolved.
Show resolved Hide resolved


@lru_cache()
def get_dpkg_architecture(target=None):
"""Return the sanitized string output by `dpkg --print-architecture`.
Expand Down
94 changes: 94 additions & 0 deletions tests/unittests/test_handler/test_handler_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,100 @@ def test_network_device_returns_network_device(self):
cc_mounts.sanitize_devname(disk_path, None, mock.Mock()))


class TestSwapFileCreation(test_helpers.FilesystemMockingTestCase):

def setUp(self):
super(TestSwapFileCreation, self).setUp()
self.new_root = self.tmp_dir()
self.patchOS(self.new_root)

self.fstab_path = os.path.join(self.new_root, 'etc/fstab')
self.swap_path = os.path.join(self.new_root, 'swap.img')
self._makedirs('/etc')

self.add_patch('cloudinit.config.cc_mounts.FSTAB_PATH',
'mock_fstab_path',
self.fstab_path,
autospec=False)

self.add_patch('cloudinit.config.cc_mounts.subp.subp',
'm_subp_subp')

self.add_patch('cloudinit.config.cc_mounts.util.mounts',
'mock_util_mounts',
return_value={
'/dev/sda1': {'fstype': 'ext4',
'mountpoint': '/',
'opts': 'rw,relatime,discard'
}})

self.mock_cloud = mock.Mock()
self.mock_log = mock.Mock()
self.mock_cloud.device_name_to_device = self.device_name_to_device

self.cc = {
'swap': {
'filename': self.swap_path,
'size': '512',
'maxsize': '512'}}

def _makedirs(self, directory):
directory = os.path.join(self.new_root, directory.lstrip('/'))
if not os.path.exists(directory):
os.makedirs(directory)

def device_name_to_device(self, path):
if path == 'swap':
return self.swap_path
else:
dev = None

return dev

@mock.patch('cloudinit.util.get_mount_info')
@mock.patch('cloudinit.util.kernel_version')
def test_swap_creation_method_xfs(self, m_kernel_version,
m_get_mount_info):
m_kernel_version.return_value = (3, 18)
m_get_mount_info.return_value = ["", "xfs"]
otubo marked this conversation as resolved.
Show resolved Hide resolved

cc_mounts.handle(None, self.cc, self.mock_cloud, self.mock_log, [])
self.m_subp_subp.assert_has_calls([
mock.call(['dd', 'if=/dev/zero',
'of=' + self.swap_path,
'bs=1M', 'count=0'], capture=True),
mock.call(['mkswap', self.swap_path]),
mock.call(['swapon', '-a'])])

@mock.patch('cloudinit.util.get_mount_info')
@mock.patch('cloudinit.util.kernel_version')
def test_swap_creation_method_btrfs(self, m_kernel_version,
m_get_mount_info):
m_kernel_version.return_value = (4, 20)
m_get_mount_info.return_value = ["", "btrfs"]

cc_mounts.handle(None, self.cc, self.mock_cloud, self.mock_log, [])
self.m_subp_subp.assert_has_calls([
mock.call(['dd', 'if=/dev/zero',
'of=' + self.swap_path,
'bs=1M', 'count=0'], capture=True),
mock.call(['mkswap', self.swap_path]),
mock.call(['swapon', '-a'])])

@mock.patch('cloudinit.util.get_mount_info')
@mock.patch('cloudinit.util.kernel_version')
def test_swap_creation_method_ext4(self, m_kernel_version,
m_get_mount_info):
m_kernel_version.return_value = (5, 14)
m_get_mount_info.return_value = ["", "ext4"]

cc_mounts.handle(None, self.cc, self.mock_cloud, self.mock_log, [])
self.m_subp_subp.assert_has_calls([
mock.call(['fallocate', '-l', '0M', self.swap_path], capture=True),
mock.call(['mkswap', self.swap_path]),
mock.call(['swapon', '-a'])])


class TestFstabHandling(test_helpers.FilesystemMockingTestCase):

swap_path = '/dev/sdb1'
Expand Down
17 changes: 17 additions & 0 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import stat
import tempfile
import yaml
import pytest
from unittest import mock

from cloudinit import subp
Expand Down Expand Up @@ -967,6 +968,22 @@ def test_get_proc_ppid(self):
self.assertEqual(my_ppid, util.get_proc_ppid(my_pid))


class TestKernelVersion():
"""test kernel version function"""

params = [
('5.6.19-300.fc32.x86_64', (5, 6)),
('4.15.0-101-generic', (4, 15)),
('3.10.0-1062.12.1.vz7.131.10', (3, 10)),
('4.18.0-144.el8.x86_64', (4, 18))]

@mock.patch('os.uname')
@pytest.mark.parametrize("uname_release,expected", params)
def test_kernel_version(self, m_uname, uname_release, expected):
m_uname.return_value.release = uname_release
assert expected == util.kernel_version()


@mock.patch('cloudinit.subp.subp')
def test_find_devs_with_openbsd(m_subp):
m_subp.return_value = (
Expand Down