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