Skip to content

devices: set CacheType::Unsafe for macOS raw disks #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/devices/src/virtio/block/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ pub enum CacheType {
Writeback,
}

impl CacheType {
/// Picks the appropriate cache type based on disk image or device path.
/// Special files like `/dev/rdisk*` on macOS do not support flush/sync.
pub fn auto(_path: &str) -> CacheType {
#[cfg(target_os = "macos")]
if _path.starts_with("/dev/rdisk") {
return CacheType::Unsafe;
}
CacheType::Writeback
}
}

/// Helper object for setting up all `Block` fields derived from its backing file.
pub(crate) struct DiskProperties {
cache_type: CacheType,
Expand Down
8 changes: 4 additions & 4 deletions src/libkrun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub unsafe extern "C" fn krun_add_disk(
let cfg = ctx_cfg.get_mut();
let block_device_config = BlockDeviceConfig {
block_id: block_id.to_string(),
cache_type: CacheType::Writeback,
cache_type: CacheType::auto(disk_path),
disk_image_path: disk_path.to_string(),
disk_image_format: ImageType::Raw,
is_disk_read_only: read_only,
Expand Down Expand Up @@ -556,7 +556,7 @@ pub unsafe extern "C" fn krun_add_disk2(
let cfg = ctx_cfg.get_mut();
let block_device_config = BlockDeviceConfig {
block_id: block_id.to_string(),
cache_type: CacheType::Writeback,
cache_type: CacheType::auto(disk_path),
disk_image_path: disk_path.to_string(),
disk_image_format: format,
is_disk_read_only: read_only,
Expand All @@ -583,7 +583,7 @@ pub unsafe extern "C" fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_c
let cfg = ctx_cfg.get_mut();
let block_device_config = BlockDeviceConfig {
block_id: "root".to_string(),
cache_type: CacheType::Writeback,
cache_type: CacheType::auto(disk_path),
disk_image_path: disk_path.to_string(),
disk_image_format: ImageType::Raw,
is_disk_read_only: false,
Expand All @@ -610,7 +610,7 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
let cfg = ctx_cfg.get_mut();
let block_device_config = BlockDeviceConfig {
block_id: "data".to_string(),
cache_type: CacheType::Writeback,
cache_type: CacheType::auto(disk_path),
disk_image_path: disk_path.to_string(),
disk_image_format: ImageType::Raw,
is_disk_read_only: false,
Expand Down
Loading