Skip to content

Commit

Permalink
payload: utilize du command for finding the required disk size in l…
Browse files Browse the repository at this point in the history
…ive OS

This approach replaces statvfs-based space calculation with du --bytes --total,
which considers the apparent size of files instead of filesystem block usage.

Resolves: rhbz#2326532
  • Loading branch information
KKoukiou committed Nov 22, 2024
1 parent 1176b22 commit 26bf4cf
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 26bf4cf

Please sign in to comment.