Skip to content

Commit

Permalink
Fix free space check by measuring bytes, not kilobytes
Browse files Browse the repository at this point in the history
I didn't realize that df defaults to kilobytes, so our calculations were
off by a factor of a whole SI prefix. We can pass `-B1` to df to
calculate plain bytes instead.
  • Loading branch information
legoktm committed Dec 2, 2024
1 parent b40b0da commit b41046a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions noble-migration/src/bin/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ fn estimate_backup_size() -> Result<u64> {
/// conservatively), and not take up more than 90% of the disk.
fn check_free_space() -> Result<bool> {
// Also no simple bindings to get disk size, so shell out to df
// Explicitly specify -B1 for bytes (not kilobytes)
let output = process::Command::new("df")
.arg("/")
.args(["-B1", "/"])
.output()
.context("spawning df failed")?;
if !output.status.success() {
Expand All @@ -168,7 +169,7 @@ fn check_free_space() -> Result<bool> {

if parsed.free < total_needs {
println!(
"free space: not enough free space, have {}, need {total_needs}",
"free space: not enough free space, have {} free bytes, need {total_needs} bytes",
parsed.free
);
Ok(false)
Expand All @@ -178,6 +179,7 @@ fn check_free_space() -> Result<bool> {
}
}

/// Sizes are in bytes
struct DfOutput {
total: u64,
free: u64,
Expand Down Expand Up @@ -345,15 +347,14 @@ mod tests {

#[test]
fn test_parse_df_output() {
// Taken from my Qubes VM, but the output of df is the same on Ubuntu
let output = parse_df_output(
"Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/dmroot 20260052 10727468 8478072 56% /
"Filesystem 1B-blocks Used Available Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv 105089261568 8573784064 91129991168 9% /
",
)
.unwrap();

assert_eq!(output.total, 20260052);
assert_eq!(output.free, 8478072);
assert_eq!(output.total, 105089261568);
assert_eq!(output.free, 91129991168);
}
}

0 comments on commit b41046a

Please sign in to comment.