diff options
author | Ryan Harper <ryan.harper@canonical.com> | 2020-07-10 08:08:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-10 09:08:44 -0400 |
commit | 3d06abc2e0017436dadbf9c26fefa9f95368db9a (patch) | |
tree | c4aded5d47ee8c1162bcc5b4487edaaa3ac533db | |
parent | e753141fc7844e68b1f380d26fb3d250eb464ff7 (diff) | |
download | vyos-cloud-init-3d06abc2e0017436dadbf9c26fefa9f95368db9a.tar.gz vyos-cloud-init-3d06abc2e0017436dadbf9c26fefa9f95368db9a.zip |
cc_mounts: handle missing fstab (#484)
Do not fail if /etc/fstab is not present. Some images, like container
rootfs may not include this file by default.
LP: #1886531
-rw-r--r-- | cloudinit/config/cc_mounts.py | 21 | ||||
-rw-r--r-- | tests/unittests/test_handler/test_handler_mounts.py | 12 |
2 files changed, 23 insertions, 10 deletions
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py index e57d1b1f..773b8285 100644 --- a/cloudinit/config/cc_mounts.py +++ b/cloudinit/config/cc_mounts.py @@ -379,17 +379,18 @@ def handle(_name, cfg, cloud, log, _args): fstab_devs = {} fstab_removed = [] - for line in util.load_file(FSTAB_PATH).splitlines(): - if MNT_COMMENT in line: - fstab_removed.append(line) - continue + if os.path.exists(FSTAB_PATH): + for line in util.load_file(FSTAB_PATH).splitlines(): + if MNT_COMMENT in line: + fstab_removed.append(line) + continue - try: - toks = WS.split(line) - except Exception: - pass - fstab_devs[toks[0]] = line - fstab_lines.append(line) + try: + toks = WS.split(line) + except Exception: + pass + fstab_devs[toks[0]] = line + fstab_lines.append(line) for i in range(len(cfgmnt)): # skip something that wasn't a list diff --git a/tests/unittests/test_handler/test_handler_mounts.py b/tests/unittests/test_handler/test_handler_mounts.py index b643e3ae..e193f4d4 100644 --- a/tests/unittests/test_handler/test_handler_mounts.py +++ b/tests/unittests/test_handler/test_handler_mounts.py @@ -183,6 +183,18 @@ class TestFstabHandling(test_helpers.FilesystemMockingTestCase): return dev + def test_no_fstab(self): + """ Handle images which do not include an fstab. """ + self.assertFalse(os.path.exists(cc_mounts.FSTAB_PATH)) + fstab_expected_content = ( + '%s\tnone\tswap\tsw,comment=cloudconfig\t' + '0\t0\n' % (self.swap_path,) + ) + cc_mounts.handle(None, {}, self.mock_cloud, self.mock_log, []) + with open(cc_mounts.FSTAB_PATH, 'r') as fd: + fstab_new_content = fd.read() + self.assertEqual(fstab_expected_content, fstab_new_content) + def test_swap_integrity(self): '''Ensure that the swap file is correctly created and can swapon successfully. Fixing the corner case of: |