diff --git a/pyanaconda/modules/payloads/source/live_os/initialization.py b/pyanaconda/modules/payloads/source/live_os/initialization.py index ecb225c7288..d3a1dde1699 100644 --- a/pyanaconda/modules/payloads/source/live_os/initialization.py +++ b/pyanaconda/modules/payloads/source/live_os/initialization.py @@ -108,11 +108,37 @@ def run(self): return SetupLiveOSResult(required_space=required_space) def _calculate_required_space(self): - """Calculate size of the live image image.""" - source = os.statvfs(self._target_mount) - required_space = source.f_frsize * (source.f_blocks - source.f_bfree) - log.debug("Required space: %s", Size(required_space)) - return required_space + """ + Calculate the disk space required for the live OS by summing up + the size of relevant directories using 'du -s'. + """ + exclude_patterns = [ + "/dev/", + "/proc/", + "/tmp/*", + "/sys/", + "/run/", + "/boot/*rescue*", + "/boot/loader/", + "/boot/efi/loader/", + "/etc/machine-id", + "/etc/machine-info" + ] + + # Build the `du` command + du_cmd_args = ["--bytes", "--summarize", self._target_mount] + for pattern in exclude_patterns: + du_cmd_args.extend(["--exclude", f"{self._target_mount}{pattern}"]) + + try: + # Execute the `du` command + result = execWithCapture("du", du_cmd_args) + # Parse the output for the total size + required_space = result.split()[0] # First column is the total + log.debug("Required space: %s", Size(required_space)) + return int(required_space) + except (OSError, FileNotFoundError) as e: + raise SourceSetupError(str(e)) from e @property def name(self): diff --git a/tests/unit_tests/pyanaconda_tests/modules/payloads/source/test_source_live_os.py b/tests/unit_tests/pyanaconda_tests/modules/payloads/source/test_source_live_os.py index 63a7ffbe9fe..77ad4c74ea1 100644 --- a/tests/unit_tests/pyanaconda_tests/modules/payloads/source/test_source_live_os.py +++ b/tests/unit_tests/pyanaconda_tests/modules/payloads/source/test_source_live_os.py @@ -128,10 +128,10 @@ def test_repr(self): class LiveOSSourceTasksTestCase(unittest.TestCase): """Test the tasks of the Live OS source.""" - @patch("pyanaconda.modules.payloads.source.live_os.initialization.os.statvfs") - def test_live_os_image_size(self, statvfs): + @patch("pyanaconda.modules.payloads.source.live_os.initialization.execWithCapture") + def test_live_os_image_size(self, exec_mock): """Test Live OS image size calculation.""" - statvfs.return_value = Mock(f_frsize=512, f_blocks=100, f_bfree=42) + exec_mock.return_value = "29696 /path/to/base/image/" task = SetUpLiveOSSourceTask( "/path/to/base/image",