最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - get available space of temporary image file with ext4 filesystem mounted on to a specific directory - Stack Overflow

programmeradmin5浏览0评论

In the below code I'm creating a temporary image file of given size and mounting it as a filesystem at a given directory. But the available space returned using fs4::available_space doesn't match with temporary image file size created using dd command.

use std::process::Command;

const TMP_DIR: &str = "/tmp/memory_check_test/";

fn init() {
    let _ = Command::new("dd")
    .args(["if=/dev/zero", "of=mem_check_file", "bs=1000", "count=100"])
    .output()
    .expect("failed to create empty file");

    let _ = Command::new("mkfs.ext3")
    .arg("mem_check_file")
    .output()
    .expect("failed to format mem_check_file with ext3 filesystem");

    let _ = Command::new("mkdir")
    .arg("-p")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to create directory {}", TMP_DIR));

    let _ = Command::new("mount")
    .arg("-o")
    .args(["loop", "mem_check_file", TMP_DIR])
    .output()
    .expect(&format!("failed to mount file to {} directory", TMP_DIR));

    let _ = Command::new("chmod")
    .arg("-R")
    .args(["777", TMP_DIR])
    .output()
    .expect(&format!("failed to change permissions for {} directory", TMP_DIR));
}

fn teardown() {
    let _ = Command::new("umount")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to unmount {} directory", TMP_DIR));

    let _ = Command::new("rmdir")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to remove {} directory", TMP_DIR));

    let _ = Command::new("rm")
    .arg("mem_check_file")
    .output()
    .expect("failed to remove mem_check_file");
}

fn main() {
    init();
    let available_space = fs4::available_space(TMP_DIR).expect("Failed to retrieve available memory space");
    println!("{}", available_space);
    teardown();
    assert_eq!(available_space, 100000);
}

Output

thread 'main' panicked at src/main.rs:57:5:
assertion `left == right` failed
  left: 100000
 right: 839506960384
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Cargo.toml

[package]
name = "mem_check"
version = "0.1.0"
edition = "2024"

[dependencies]
fs4 = "0.13.1"

In the below code I'm creating a temporary image file of given size and mounting it as a filesystem at a given directory. But the available space returned using fs4::available_space doesn't match with temporary image file size created using dd command.

use std::process::Command;

const TMP_DIR: &str = "/tmp/memory_check_test/";

fn init() {
    let _ = Command::new("dd")
    .args(["if=/dev/zero", "of=mem_check_file", "bs=1000", "count=100"])
    .output()
    .expect("failed to create empty file");

    let _ = Command::new("mkfs.ext3")
    .arg("mem_check_file")
    .output()
    .expect("failed to format mem_check_file with ext3 filesystem");

    let _ = Command::new("mkdir")
    .arg("-p")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to create directory {}", TMP_DIR));

    let _ = Command::new("mount")
    .arg("-o")
    .args(["loop", "mem_check_file", TMP_DIR])
    .output()
    .expect(&format!("failed to mount file to {} directory", TMP_DIR));

    let _ = Command::new("chmod")
    .arg("-R")
    .args(["777", TMP_DIR])
    .output()
    .expect(&format!("failed to change permissions for {} directory", TMP_DIR));
}

fn teardown() {
    let _ = Command::new("umount")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to unmount {} directory", TMP_DIR));

    let _ = Command::new("rmdir")
    .arg(TMP_DIR)
    .output()
    .expect(&format!("failed to remove {} directory", TMP_DIR));

    let _ = Command::new("rm")
    .arg("mem_check_file")
    .output()
    .expect("failed to remove mem_check_file");
}

fn main() {
    init();
    let available_space = fs4::available_space(TMP_DIR).expect("Failed to retrieve available memory space");
    println!("{}", available_space);
    teardown();
    assert_eq!(available_space, 100000);
}

Output

thread 'main' panicked at src/main.rs:57:5:
assertion `left == right` failed
  left: 100000
 right: 839506960384
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Cargo.toml

[package]
name = "mem_check"
version = "0.1.0"
edition = "2024"

[dependencies]
fs4 = "0.13.1"
Share edited Mar 11 at 9:42 JensV 4,5892 gold badges22 silver badges48 bronze badges asked Mar 11 at 8:04 HarryHarry 3,2581 gold badge24 silver badges46 bronze badges 4
  • I'm not sure what you expect here. There's always going to be a bit of overhead to store file system information. When I repeat your commands and run fdisk -l <disk> I get a size of 97.5KiB. If I use df to show actual available space it's even lower. See e.g. unix.stackexchange/questions/40819/… – JensV Commented Mar 11 at 8:23
  • @JensV the assertion is getting failed – Harry Commented Mar 11 at 8:24
  • You are running the program as root right? Otherwise the mount will fail. I'm getting a result of 70656, which seems expected to me – JensV Commented Mar 11 at 9:01
  • @JensV no... but neither the application is getting panicked in between but only at the time of assertion check. Even if I run as sudo -E cargo run, the assertion still fails, I mean huge difference between left and right values – Harry Commented Mar 11 at 9:06
Add a comment  | 

1 Answer 1

Reset to default 1

There's two issues here. You assume that if the command you run produces an error, your expect function will be called which is not the case. The expect message is only produced if it's unable to run your command (for example if the command does not exist). If the command produces a non-zero exit status you need to check that yourself.

Since you don't really care about processing command output, you should rather use the following form:

let status = Command::new("...")
   # any args
   .status()
   .expect("failed run run command")

println!("process finished with: {status}");
assert!(status.success());

This has the added benefit, that stdout and stderr are redirected automatically and become visible, aiding in troubleshooting.

Running the program as root - which is required for the mount command to succeed - yields a value of 70656 in the end for me. Which is expected accounting for file system overhead and reserved space in the filesystem.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论