sailesh1993 / rpms / cloud-init

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