cloud-init/backport-fix-growpart-race-4618.patch

114 lines
3.7 KiB
Diff

From 598e0560d64f949369962ebbce2c53207763f5d2 Mon Sep 17 00:00:00 2001
From: Brett Holman <brett.holman@canonical.com>
Date: Fri, 5 Jan 2024 13:10:01 -0700
Subject: [PATCH] fix: fix growpart race (#4618)
Fixes GH-4613
Reference:https://github.com/canonical/cloud-init/commit/598e0560d64f949369962ebbce2c53207763f5d2
Conflict:(1)change tests/unittests/test_handler/test_handler_growpart.py not tests/unittests/config/test_cc_growpart.py.
(2)Community patch:
-from typing import Tuple
+from typing import Optional, Tuple
Adaptation patch:
+from typing import Optional
(3)add "import pytest" in test_handler_growpart.py
(4)The context of the code is slightly different.
---
cloudinit/config/cc_growpart.py | 22 +++++++++++++++----
.../test_handler/test_handler_growpart.py | 17 ++++++++++++++
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/cloudinit/config/cc_growpart.py b/cloudinit/config/cc_growpart.py
index 1ddc9dc..1552072 100644
--- a/cloudinit/config/cc_growpart.py
+++ b/cloudinit/config/cc_growpart.py
@@ -74,6 +74,7 @@ from cloudinit.settings import PER_ALWAYS
from cloudinit import subp
from cloudinit import temp_utils
from cloudinit import util
+from typing import Optional
frequency = PER_ALWAYS
@@ -212,12 +213,16 @@ class ResizeGpart(object):
return (before, get_size(partdev))
-def get_size(filename):
- fd = os.open(filename, os.O_RDONLY)
+def get_size(filename) -> Optional[int]:
+ fd = None
try:
+ fd = os.open(filename, os.O_RDONLY)
return os.lseek(fd, 0, os.SEEK_END)
+ except FileNotFoundError:
+ return None
finally:
- os.close(fd)
+ if fd:
+ os.close(fd)
def device_part_info(devpath):
@@ -318,10 +323,19 @@ def resize_devices(resizer, devices):
continue
try:
- (old, new) = resizer.resize(disk, ptnum, blockdev)
+ old, new = resizer.resize(disk, ptnum, blockdev)
if old == new:
info.append((devent, RESIZE.NOCHANGE,
"no change necessary (%s, %s)" % (disk, ptnum),))
+ elif new is None or old is None:
+ info.append(
+ (
+ devent,
+ RESIZE.CHANGED,
+ "changed (%s, %s) size, new size is unknown"
+ % (disk, ptnum),
+ )
+ )
else:
info.append((devent, RESIZE.CHANGED,
"changed (%s, %s) from %s to %s" %
diff --git a/tests/unittests/test_handler/test_handler_growpart.py b/tests/unittests/test_handler/test_handler_growpart.py
index 7f039b7..2f40e86 100644
--- a/tests/unittests/test_handler/test_handler_growpart.py
+++ b/tests/unittests/test_handler/test_handler_growpart.py
@@ -6,6 +6,7 @@ from cloudinit import subp
from cloudinit.tests.helpers import TestCase
+import pytest
import errno
import logging
import os
@@ -227,6 +228,22 @@ class TestResize(unittest.TestCase):
os.stat = real_stat
+class TestGetSize:
+ @pytest.mark.parametrize(
+ "file_exists, expected",
+ (
+ (False, None),
+ (True, 1),
+ ),
+ )
+ def test_get_size_behaves(self, file_exists, expected, tmp_path):
+ """Ensure that get_size() doesn't raise exception"""
+ tmp_file = tmp_path / "tmp.txt"
+ if file_exists:
+ tmp_file.write_bytes(b"0")
+ assert expected == cc_growpart.get_size(tmp_file)
+
+
def simple_device_part_info(devpath):
# simple stupid return (/dev/vda, 1) for /dev/vda
ret = re.search("([^0-9]*)([0-9]*)$", devpath)
--
2.33.0