Skip to content

Commit

Permalink
Merge pull request #6009 from KKoukiou/live-check-required-size
Browse files Browse the repository at this point in the history
payload: utilize disk usage for assuming the minimum required disk size in live OS
  • Loading branch information
KKoukiou authored Nov 25, 2024
2 parents e5be57a + 26bf4cf commit 7365822
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
36 changes: 31 additions & 5 deletions pyanaconda/modules/payloads/source/live_os/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7365822

Please sign in to comment.