Skip to content

Commit a456e6a

Browse files
committed
devices: set CacheType::Unsafe for macOS raw disks
Signed-off-by: Jan Noha <[email protected]>
1 parent 6976402 commit a456e6a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/devices/src/virtio/block/device.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ pub enum CacheType {
5151
Writeback,
5252
}
5353

54+
impl CacheType {
55+
/// Picks the appropriate cache type based on disk image or device path.
56+
/// Special files like `/dev/rdisk*` on macOS do not support flush/sync.
57+
pub fn auto(_path: &str) -> CacheType {
58+
#[cfg(target_os = "macos")]
59+
if _path.starts_with("/dev/rdisk") {
60+
return CacheType::Unsafe;
61+
}
62+
CacheType::Writeback
63+
}
64+
}
65+
5466
/// Helper object for setting up all `Block` fields derived from its backing file.
5567
pub(crate) struct DiskProperties {
5668
cache_type: CacheType,

src/libkrun/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ pub unsafe extern "C" fn krun_add_disk(
512512
let cfg = ctx_cfg.get_mut();
513513
let block_device_config = BlockDeviceConfig {
514514
block_id: block_id.to_string(),
515-
cache_type: CacheType::Writeback,
515+
cache_type: CacheType::auto(disk_path),
516516
disk_image_path: disk_path.to_string(),
517517
disk_image_format: ImageType::Raw,
518518
is_disk_read_only: read_only,
@@ -559,7 +559,7 @@ pub unsafe extern "C" fn krun_add_disk2(
559559
let cfg = ctx_cfg.get_mut();
560560
let block_device_config = BlockDeviceConfig {
561561
block_id: block_id.to_string(),
562-
cache_type: CacheType::Writeback,
562+
cache_type: CacheType::auto(disk_path),
563563
disk_image_path: disk_path.to_string(),
564564
disk_image_format: format,
565565
is_disk_read_only: read_only,
@@ -586,7 +586,7 @@ pub unsafe extern "C" fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_c
586586
let cfg = ctx_cfg.get_mut();
587587
let block_device_config = BlockDeviceConfig {
588588
block_id: "root".to_string(),
589-
cache_type: CacheType::Writeback,
589+
cache_type: CacheType::auto(disk_path),
590590
disk_image_path: disk_path.to_string(),
591591
disk_image_format: ImageType::Raw,
592592
is_disk_read_only: false,
@@ -613,7 +613,7 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
613613
let cfg = ctx_cfg.get_mut();
614614
let block_device_config = BlockDeviceConfig {
615615
block_id: "data".to_string(),
616-
cache_type: CacheType::Writeback,
616+
cache_type: CacheType::auto(disk_path),
617617
disk_image_path: disk_path.to_string(),
618618
disk_image_format: ImageType::Raw,
619619
is_disk_read_only: false,

0 commit comments

Comments
 (0)