f6265e
From e3f04e297ce950ce0d183ca87a434ec932ae6a86 Mon Sep 17 00:00:00 2001
f6265e
From: Eduardo Otubo <otubo@redhat.com>
f6265e
Date: Wed, 15 May 2019 12:15:29 +0200
f6265e
Subject: [PATCH 5/5] cc_mounts: check if mount -a on no-change fstab path
f6265e
f6265e
RH-Author: Eduardo Otubo <otubo@redhat.com>
f6265e
Message-id: <20190515121529.11191-6-otubo@redhat.com>
f6265e
Patchwork-id: 87886
f6265e
O-Subject: [rhel-7 cloud-init PATCHv2 5/5] cc_mounts: check if mount -a on no-change fstab path
f6265e
Bugzilla: 1687565
f6265e
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
f6265e
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
f6265e
f6265e
From: "Jason Zions (MSFT)" <jasonzio@microsoft.com>
f6265e
f6265e
BZ: 1687565
f6265e
BRANCH: rhel7/master-18.5
f6265e
UPSTREAM: acc25d8d
f6265e
BREW: 21696239
f6265e
f6265e
commit acc25d8d7d603313059ac35b4253b504efc560a9
f6265e
Author: Jason Zions (MSFT) <jasonzio@microsoft.com>
f6265e
Date:   Wed May 8 22:47:07 2019 +0000
f6265e
f6265e
    cc_mounts: check if mount -a on no-change fstab path
f6265e
f6265e
    Under some circumstances, cc_disk_setup may reformat volumes which
f6265e
    already appear in /etc/fstab (e.g. Azure ephemeral drive is reformatted
f6265e
    from NTFS to ext4 after service-heal). Normally, cc_mounts only calls
f6265e
    mount -a if it altered /etc/fstab. With this change cc_mounts will read
f6265e
    /proc/mounts and verify if configured mounts are already mounted and if
f6265e
    not raise flag to request a mount -a.  This handles the case where no
f6265e
    changes to fstab occur but a mount -a is required due to change in
f6265e
    underlying device which prevented the .mount unit from running until
f6265e
    after disk was reformatted.
f6265e
f6265e
    LP: #1825596
f6265e
f6265e
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
f6265e
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
f6265e
---
f6265e
 cloudinit/config/cc_mounts.py                      | 11 ++++++++
f6265e
 .../unittests/test_handler/test_handler_mounts.py  | 30 +++++++++++++++++++++-
f6265e
 2 files changed, 40 insertions(+), 1 deletion(-)
f6265e
f6265e
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
f6265e
index 339baba..123ffb8 100644
f6265e
--- a/cloudinit/config/cc_mounts.py
f6265e
+++ b/cloudinit/config/cc_mounts.py
f6265e
@@ -439,6 +439,7 @@ def handle(_name, cfg, cloud, log, _args):
f6265e
 
f6265e
     cc_lines = []
f6265e
     needswap = False
f6265e
+    need_mount_all = False
f6265e
     dirs = []
f6265e
     for line in actlist:
f6265e
         # write 'comment' in the fs_mntops, entry,  claiming this
f6265e
@@ -449,11 +450,18 @@ def handle(_name, cfg, cloud, log, _args):
f6265e
             dirs.append(line[1])
f6265e
         cc_lines.append('\t'.join(line))
f6265e
 
f6265e
+    mount_points = [v['mountpoint'] for k, v in util.mounts().items()
f6265e
+                    if 'mountpoint' in v]
f6265e
     for d in dirs:
f6265e
         try:
f6265e
             util.ensure_dir(d)
f6265e
         except Exception:
f6265e
             util.logexc(log, "Failed to make '%s' config-mount", d)
f6265e
+        # dirs is list of directories on which a volume should be mounted.
f6265e
+        # If any of them does not already show up in the list of current
f6265e
+        # mount points, we will definitely need to do mount -a.
f6265e
+        if not need_mount_all and d not in mount_points:
f6265e
+            need_mount_all = True
f6265e
 
f6265e
     sadds = [WS.sub(" ", n) for n in cc_lines]
f6265e
     sdrops = [WS.sub(" ", n) for n in fstab_removed]
f6265e
@@ -473,6 +481,9 @@ def handle(_name, cfg, cloud, log, _args):
f6265e
         log.debug("No changes to /etc/fstab made.")
f6265e
     else:
f6265e
         log.debug("Changes to fstab: %s", sops)
f6265e
+        need_mount_all = True
f6265e
+
f6265e
+    if need_mount_all:
f6265e
         activate_cmds.append(["mount", "-a"])
f6265e
         if uses_systemd:
f6265e
             activate_cmds.append(["systemctl", "daemon-reload"])
f6265e
diff --git a/tests/unittests/test_handler/test_handler_mounts.py b/tests/unittests/test_handler/test_handler_mounts.py
f6265e
index 8fea6c2..0fb160b 100644
f6265e
--- a/tests/unittests/test_handler/test_handler_mounts.py
f6265e
+++ b/tests/unittests/test_handler/test_handler_mounts.py
f6265e
@@ -154,7 +154,15 @@ class TestFstabHandling(test_helpers.FilesystemMockingTestCase):
f6265e
                        return_value=True)
f6265e
 
f6265e
         self.add_patch('cloudinit.config.cc_mounts.util.subp',
f6265e
-                       'mock_util_subp')
f6265e
+                       'm_util_subp')
f6265e
+
f6265e
+        self.add_patch('cloudinit.config.cc_mounts.util.mounts',
f6265e
+                       'mock_util_mounts',
f6265e
+                       return_value={
f6265e
+                           '/dev/sda1': {'fstype': 'ext4',
f6265e
+                                         'mountpoint': '/',
f6265e
+                                         'opts': 'rw,relatime,discard'
f6265e
+                                         }})
f6265e
 
f6265e
         self.mock_cloud = mock.Mock()
f6265e
         self.mock_log = mock.Mock()
f6265e
@@ -230,4 +238,24 @@ class TestFstabHandling(test_helpers.FilesystemMockingTestCase):
f6265e
             fstab_new_content = fd.read()
f6265e
             self.assertEqual(fstab_expected_content, fstab_new_content)
f6265e
 
f6265e
+    def test_no_change_fstab_sets_needs_mount_all(self):
f6265e
+        '''verify unchanged fstab entries are mounted if not call mount -a'''
f6265e
+        fstab_original_content = (
f6265e
+            'LABEL=cloudimg-rootfs / ext4 defaults 0 0\n'
f6265e
+            'LABEL=UEFI /boot/efi vfat defaults 0 0\n'
f6265e
+            '/dev/vdb /mnt auto defaults,noexec,comment=cloudconfig 0 2\n'
f6265e
+        )
f6265e
+        fstab_expected_content = fstab_original_content
f6265e
+        cc = {'mounts': [
f6265e
+                 ['/dev/vdb', '/mnt', 'auto', 'defaults,noexec']]}
f6265e
+        with open(cc_mounts.FSTAB_PATH, 'w') as fd:
f6265e
+            fd.write(fstab_original_content)
f6265e
+        with open(cc_mounts.FSTAB_PATH, 'r') as fd:
f6265e
+            fstab_new_content = fd.read()
f6265e
+            self.assertEqual(fstab_expected_content, fstab_new_content)
f6265e
+        cc_mounts.handle(None, cc, self.mock_cloud, self.mock_log, [])
f6265e
+        self.m_util_subp.assert_has_calls([
f6265e
+            mock.call(['mount', '-a']),
f6265e
+            mock.call(['systemctl', 'daemon-reload'])])
f6265e
+
f6265e
 # vi: ts=4 expandtab
f6265e
-- 
f6265e
1.8.3.1
f6265e