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.
Unlike statvfs, du accounts for differences in compression, sparse files, and excluded paths,
ensuring a more accurate estimate of required space for copying.

Resolves: rhbz#2326532
  • Loading branch information
KKoukiou committed Nov 19, 2024
1 parent 1176b22 commit ee43190
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 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,38 @@ 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 = ["-s", "--bytes", "--total", 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
lines = result.splitlines()
required_space = lines[-1].split()[0] # First column of last line 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

0 comments on commit ee43190

Please sign in to comment.