Skip to content

Commit 4a45522

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

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
@@ -509,7 +509,7 @@ pub unsafe extern "C" fn krun_add_disk(
509509
let cfg = ctx_cfg.get_mut();
510510
let block_device_config = BlockDeviceConfig {
511511
block_id: block_id.to_string(),
512-
cache_type: CacheType::Writeback,
512+
cache_type: CacheType::auto(disk_path),
513513
disk_image_path: disk_path.to_string(),
514514
disk_image_format: ImageType::Raw,
515515
is_disk_read_only: read_only,
@@ -556,7 +556,7 @@ pub unsafe extern "C" fn krun_add_disk2(
556556
let cfg = ctx_cfg.get_mut();
557557
let block_device_config = BlockDeviceConfig {
558558
block_id: block_id.to_string(),
559-
cache_type: CacheType::Writeback,
559+
cache_type: CacheType::auto(disk_path),
560560
disk_image_path: disk_path.to_string(),
561561
disk_image_format: format,
562562
is_disk_read_only: read_only,
@@ -583,7 +583,7 @@ pub unsafe extern "C" fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_c
583583
let cfg = ctx_cfg.get_mut();
584584
let block_device_config = BlockDeviceConfig {
585585
block_id: "root".to_string(),
586-
cache_type: CacheType::Writeback,
586+
cache_type: CacheType::auto(disk_path),
587587
disk_image_path: disk_path.to_string(),
588588
disk_image_format: ImageType::Raw,
589589
is_disk_read_only: false,
@@ -610,7 +610,7 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
610610
let cfg = ctx_cfg.get_mut();
611611
let block_device_config = BlockDeviceConfig {
612612
block_id: "data".to_string(),
613-
cache_type: CacheType::Writeback,
613+
cache_type: CacheType::auto(disk_path),
614614
disk_image_path: disk_path.to_string(),
615615
disk_image_format: ImageType::Raw,
616616
is_disk_read_only: false,

0 commit comments

Comments
 (0)