Skip to content

Commit bea9fef

Browse files
jakecorrentislp
authored andcommitted
virtio-block: enable relaxed sync by default on macOS
On macOS, std::fs::File::flush() uses F_FULLFSYNC, which syncs the buffers to the drive and asks the drive to wait for flushing all the data to the permanent storage. In contrast, Apple Virtualization.Framework only issues an fsync(), which doesn't ask the drive to flush its buffered data. When on macOS, tell imago to use relaxed sync to match the behavior of Virtualization.Framework by default. Related to containers/podman#27216 Signed-off-by: Jake Correnti <[email protected]>
1 parent eca55e3 commit bea9fef

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use std::result;
1818
use std::sync::Arc;
1919
use std::thread::JoinHandle;
2020

21-
use imago::file::File as ImagoFile;
22-
use imago::qcow2::Qcow2;
23-
use imago::SyncFormatAccess;
21+
use imago::{
22+
file::File as ImagoFile, qcow2::Qcow2, raw::Raw, DynStorage, Storage, StorageOpenOptions,
23+
SyncFormatAccess,
24+
};
2425
use log::{error, warn};
2526
use utils::eventfd::{EventFd, EFD_NONBLOCK};
2627
use virtio_bindings::{
@@ -64,14 +65,14 @@ impl CacheType {
6465
/// Helper object for setting up all `Block` fields derived from its backing file.
6566
pub(crate) struct DiskProperties {
6667
cache_type: CacheType,
67-
pub(crate) file: Arc<SyncFormatAccess<ImagoFile>>,
68+
pub(crate) file: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
6869
nsectors: u64,
6970
image_id: Vec<u8>,
7071
}
7172

7273
impl DiskProperties {
7374
pub fn new(
74-
disk_image: Arc<SyncFormatAccess<ImagoFile>>,
75+
disk_image: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
7576
disk_image_id: Vec<u8>,
7677
cache_type: CacheType,
7778
) -> io::Result<Self> {
@@ -94,7 +95,7 @@ impl DiskProperties {
9495
})
9596
}
9697

97-
pub fn file(&self) -> &SyncFormatAccess<ImagoFile> {
98+
pub fn file(&self) -> &SyncFormatAccess<Box<dyn DynStorage>> {
9899
self.file.as_ref()
99100
}
100101

@@ -176,7 +177,7 @@ pub struct Block {
176177
// Host file and properties.
177178
disk: Option<DiskProperties>,
178179
cache_type: CacheType,
179-
disk_image: Arc<SyncFormatAccess<ImagoFile>>,
180+
disk_image: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
180181
disk_image_id: Vec<u8>,
181182
worker_thread: Option<JoinHandle<()>>,
182183
worker_stopfd: EventFd,
@@ -215,18 +216,32 @@ impl Block {
215216

216217
let disk_image_id = DiskProperties::build_disk_image_id(&disk_image);
217218

219+
let file_opts = StorageOpenOptions::new()
220+
.write(!is_disk_read_only)
221+
.filename(disk_image_path);
222+
#[cfg(target_os = "macos")]
223+
let file_opts = file_opts.relaxed_sync(true);
224+
let file = ImagoFile::open_sync(file_opts)?;
225+
218226
let disk_image = match disk_image_format {
219227
ImageType::Qcow2 => {
220-
let mut qcow_disk_image =
221-
Qcow2::<ImagoFile>::open_path_sync(disk_image_path, !is_disk_read_only)?;
222-
qcow_disk_image.open_implicit_dependencies_sync()?;
223-
SyncFormatAccess::new(qcow_disk_image)?
228+
let mut qcow2 =
229+
Qcow2::<Box<dyn DynStorage>, Arc<imago::FormatAccess<_>>>::open_image_sync(
230+
Box::new(file),
231+
!is_disk_read_only,
232+
)?;
233+
qcow2.open_implicit_dependencies_sync()?;
234+
SyncFormatAccess::new(qcow2)?
224235
}
225236
ImageType::Raw => {
226-
let raw = imago::raw::Raw::open_path_sync(disk_image_path, !is_disk_read_only)?;
237+
let raw = Raw::<Box<dyn DynStorage>>::open_image_sync(
238+
Box::new(file),
239+
!is_disk_read_only,
240+
)?;
227241
SyncFormatAccess::new(raw)?
228242
}
229243
};
244+
230245
let disk_image = Arc::new(disk_image);
231246

232247
let disk_properties =

0 commit comments

Comments
 (0)