Create a new loopback block device targeting the provided file path.
(path: &Path)
| 519 | impl LoopbackDevice { |
| 520 | // Create a new loopback block device targeting the provided file path. |
| 521 | pub fn new(path: &Path) -> Result<Self> { |
| 522 | let direct_io = match env::var("BOOTC_DIRECT_IO") { |
| 523 | Ok(val) => { |
| 524 | if val == "on" { |
| 525 | "on" |
| 526 | } else { |
| 527 | "off" |
| 528 | } |
| 529 | } |
| 530 | Err(_e) => "off", |
| 531 | }; |
| 532 | |
| 533 | let dev = Command::new("losetup") |
| 534 | .args([ |
| 535 | "--show", |
| 536 | format!("--direct-io={direct_io}").as_str(), |
| 537 | "-P", |
| 538 | "--find", |
| 539 | ]) |
| 540 | .arg(path) |
| 541 | .run_get_string()?; |
| 542 | let dev = Utf8PathBuf::from(dev.trim()); |
| 543 | tracing::debug!("Allocated loopback {dev}"); |
| 544 | |
| 545 | // Try to spawn cleanup helper, but don't fail if it doesn't work |
| 546 | let cleanup_handle = match Self::spawn_cleanup_helper(dev.as_str()) { |
| 547 | Ok(handle) => Some(handle), |
| 548 | Err(e) => { |
| 549 | tracing::warn!( |
| 550 | "Failed to spawn loopback cleanup helper for {}: {}. \ |
| 551 | Loopback device may not be cleaned up if process is interrupted.", |
| 552 | dev, |
| 553 | e |
| 554 | ); |
| 555 | None |
| 556 | } |
| 557 | }; |
| 558 | |
| 559 | Ok(Self { |
| 560 | dev: Some(dev), |
| 561 | cleanup_handle, |
| 562 | }) |
| 563 | } |
| 564 | |
| 565 | // Access the path to the loopback block device. |
| 566 | pub fn path(&self) -> &Utf8Path { |
nothing calls this directly
no test coverage detected