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 11 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
5 changes: 3 additions & 2 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 Down
5 changes: 5 additions & 0 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
['lxc-is-container'])


@lru_cache()
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
52 changes: 52 additions & 0 deletions tests/unittests/test_handler/test_handler_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from cloudinit.tests import helpers as test_helpers

from cloudinit import util


class TestSanitizeDevname(test_helpers.FilesystemMockingTestCase):

Expand Down Expand Up @@ -183,6 +185,56 @@ def device_name_to_device(self, path):

return dev

@mock.patch('cloudinit.util.kernel_version')
@mock.patch('cloudinit.util.get_mount_info')
def test_swap_creation_method(self, m_kernel_version, m_get_mount_info):
otubo marked this conversation as resolved.
Show resolved Hide resolved
log = mock.MagicMock()
m_kernel_version.return_value = (3, 18)
m_get_mount_info.return_value = ["", "xfs"]
otubo marked this conversation as resolved.
Show resolved Hide resolved
fstab = '/swap.img swap swap defaults 0 0\n'

with open(cc_mounts.FSTAB_PATH, 'w') as fd:
fd.write(fstab)
otubo marked this conversation as resolved.
Show resolved Hide resolved
cc = {'swap': ['filename: /swap.img', 'size: 512', 'maxsize: 512']}
cc_mounts.handle(None, cc, self.mock_cloud, self.mock_log, [])

swap_log = 'Creating swapfile in /swap.img on fstype xfs using dd'
util.multi_log('{0}\n'.format(swap_log), log=log)
self.assertEqual((mock.ANY, swap_log), log.log.call_args[0])
otubo marked this conversation as resolved.
Show resolved Hide resolved

m_kernel_version.reset_mock()
m_get_mount_info.reset_mock()

m_kernel_version.return_value = (4, 20)
m_get_mount_info.return_value = ["", "btrfs"]
fstab = '/swap2.img swap swap defaults 0 0\n'

with open(cc_mounts.FSTAB_PATH, 'w') as fd:
fd.write(fstab)
cc = {'swap': ['filename: /swap2.img', 'size: 512', 'maxsize: 512']}
cc_mounts.handle(None, cc, self.mock_cloud, self.mock_log, [])

swap_log = 'Creating swapfile in /swap.img on fstype btrfs using dd'
util.multi_log('{0}\n'.format(swap_log), log=log)
self.assertEqual((mock.ANY, swap_log), log.log.call_args[0])

m_kernel_version.reset_mock()
m_get_mount_info.reset_mock()

m_kernel_version.return_value = (4, 20)
m_get_mount_info.return_value = ["", "ext4"]
fstab = '/swap3.img swap swap defaults 0 0\n'

with open(cc_mounts.FSTAB_PATH, 'w') as fd:
fd.write(fstab)
cc = {'swap': ['filename: /swap3.img', 'size: 512', 'maxsize: 512']}
cc_mounts.handle(None, cc, self.mock_cloud, self.mock_log, [])

swap_log = 'Creating swapfile in /swap.img on fstype btrfs using' \
' fallocate'
util.multi_log('{0}\n'.format(swap_log), log=log)
self.assertEqual((mock.ANY, swap_log), log.log.call_args[0])

def test_swap_integrity(self):
'''Ensure that the swap file is correctly created and can
swapon successfully. Fixing the corner case of:
Expand Down
6 changes: 6 additions & 0 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,12 @@ def test_get_proc_ppid(self):
self.assertEqual(my_ppid, util.get_proc_ppid(my_pid))


@mock.patch('os.uname')
otubo marked this conversation as resolved.
Show resolved Hide resolved
def test_kernel_version(m_uname):
m_uname().release = '5.6.19-300.fc32.x86_64'
assert util.kernel_version() == (5, 6)
otubo marked this conversation as resolved.
Show resolved Hide resolved


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