@@ -53,20 +53,15 @@ pub enum CacheType {
53
53
}
54
54
55
55
/// The engine file type, either Sync or Async (through io_uring).
56
- #[ derive( Debug , PartialEq , Eq , Deserialize , Serialize ) ]
56
+ #[ derive( Debug , Default , Clone , Copy , PartialEq , Eq , Deserialize , Serialize ) ]
57
57
pub enum FileEngineType {
58
58
/// Use an Async engine, based on io_uring.
59
59
Async ,
60
60
/// Use a Sync engine, based on blocking system calls.
61
+ #[ default]
61
62
Sync ,
62
63
}
63
64
64
- impl Default for FileEngineType {
65
- fn default ( ) -> Self {
66
- Self :: Sync
67
- }
68
- }
69
-
70
65
impl FileEngineType {
71
66
/// Whether the Async engine is supported on the current host kernel.
72
67
pub fn is_supported ( & self ) -> Result < bool , utils:: kernel_version:: Error > {
@@ -203,22 +198,23 @@ impl DiskProperties {
203
198
pub struct VirtioBlockConfig {
204
199
/// Unique identifier of the drive.
205
200
pub drive_id : String ,
206
- /// Path of the backing file on the host
207
- pub path_on_host : String ,
201
+ /// Part-UUID. Represents the unique id of the boot partition of this device. It is
202
+ /// optional and it will be used only if the `is_root_device` field is true.
203
+ pub partuuid : Option < String > ,
208
204
/// If set to true, it makes the current device the root block device.
209
205
/// Setting this flag to true will mount the block device in the
210
206
/// guest under /dev/vda unless the partuuid is present.
211
207
pub is_root_device : bool ,
212
- /// Part-UUID. Represents the unique id of the boot partition of this device. It is
213
- /// optional and it will be used only if the `is_root_device` field is true.
214
- pub partuuid : Option < String > ,
215
- /// If set to true, the drive is opened in read-only mode. Otherwise, the
216
- /// drive is opened as read-write.
217
- pub is_read_only : bool ,
218
208
/// If set to true, the drive will ignore flush requests coming from
219
209
/// the guest driver.
220
210
#[ serde( default ) ]
221
211
pub cache_type : CacheType ,
212
+
213
+ /// If set to true, the drive is opened in read-only mode. Otherwise, the
214
+ /// drive is opened as read-write.
215
+ pub is_read_only : bool ,
216
+ /// Path of the backing file on the host
217
+ pub path_on_host : String ,
222
218
/// Rate Limiter for I/O operations.
223
219
pub rate_limiter : Option < RateLimiterConfig > ,
224
220
/// The type of IO engine used by the device.
@@ -227,17 +223,24 @@ pub struct VirtioBlockConfig {
227
223
pub file_engine_type : FileEngineType ,
228
224
}
229
225
230
- impl From < BlockDeviceConfig > for VirtioBlockConfig {
231
- fn from ( value : BlockDeviceConfig ) -> Self {
232
- Self {
233
- drive_id : value. drive_id ,
234
- path_on_host : value. path_on_host ,
235
- is_root_device : value. is_root_device ,
236
- partuuid : value. partuuid ,
237
- is_read_only : value. is_read_only ,
238
- cache_type : value. cache_type ,
239
- rate_limiter : value. rate_limiter ,
240
- file_engine_type : value. file_engine_type ,
226
+ impl TryFrom < & BlockDeviceConfig > for VirtioBlockConfig {
227
+ type Error = VirtioBlockError ;
228
+
229
+ fn try_from ( value : & BlockDeviceConfig ) -> Result < Self , Self :: Error > {
230
+ if value. path_on_host . is_some ( ) && value. socket . is_none ( ) {
231
+ Ok ( Self {
232
+ drive_id : value. drive_id . clone ( ) ,
233
+ partuuid : value. partuuid . clone ( ) ,
234
+ is_root_device : value. is_root_device ,
235
+ cache_type : value. cache_type ,
236
+
237
+ is_read_only : value. is_read_only . unwrap_or ( false ) ,
238
+ path_on_host : value. path_on_host . as_ref ( ) . unwrap ( ) . clone ( ) ,
239
+ rate_limiter : value. rate_limiter ,
240
+ file_engine_type : value. file_engine_type ,
241
+ } )
242
+ } else {
243
+ Err ( VirtioBlockError :: Config )
241
244
}
242
245
}
243
246
}
@@ -246,13 +249,16 @@ impl From<VirtioBlockConfig> for BlockDeviceConfig {
246
249
fn from ( value : VirtioBlockConfig ) -> Self {
247
250
Self {
248
251
drive_id : value. drive_id ,
249
- path_on_host : value. path_on_host ,
250
- is_root_device : value. is_root_device ,
251
252
partuuid : value. partuuid ,
252
- is_read_only : value. is_read_only ,
253
+ is_root_device : value. is_root_device ,
253
254
cache_type : value. cache_type ,
255
+
256
+ is_read_only : Some ( value. is_read_only ) ,
257
+ path_on_host : Some ( value. path_on_host ) ,
254
258
rate_limiter : value. rate_limiter ,
255
259
file_engine_type : value. file_engine_type ,
260
+
261
+ socket : None ,
256
262
}
257
263
}
258
264
}
@@ -363,7 +369,7 @@ impl VirtioBlock {
363
369
}
364
370
}
365
371
366
- /// Process a single event in the VirtIO queue.
372
+ /// Process a single event in the Virtio queue.
367
373
///
368
374
/// This function is called by the event manager when the guest notifies us
369
375
/// about new buffers in the queue.
@@ -747,6 +753,54 @@ mod tests {
747
753
use crate :: rate_limiter:: TokenType ;
748
754
use crate :: vstate:: memory:: { Address , Bytes , GuestAddress } ;
749
755
756
+ #[ test]
757
+ fn test_from_config ( ) {
758
+ let block_config = BlockDeviceConfig {
759
+ drive_id : "" . to_string ( ) ,
760
+ partuuid : None ,
761
+ is_root_device : false ,
762
+ cache_type : CacheType :: Unsafe ,
763
+
764
+ is_read_only : Some ( true ) ,
765
+ path_on_host : Some ( "path" . to_string ( ) ) ,
766
+ rate_limiter : None ,
767
+ file_engine_type : Default :: default ( ) ,
768
+
769
+ socket : None ,
770
+ } ;
771
+ assert ! ( VirtioBlockConfig :: try_from( & block_config) . is_ok( ) ) ;
772
+
773
+ let block_config = BlockDeviceConfig {
774
+ drive_id : "" . to_string ( ) ,
775
+ partuuid : None ,
776
+ is_root_device : false ,
777
+ cache_type : CacheType :: Unsafe ,
778
+
779
+ is_read_only : None ,
780
+ path_on_host : None ,
781
+ rate_limiter : None ,
782
+ file_engine_type : Default :: default ( ) ,
783
+
784
+ socket : Some ( "sock" . to_string ( ) ) ,
785
+ } ;
786
+ assert ! ( VirtioBlockConfig :: try_from( & block_config) . is_err( ) ) ;
787
+
788
+ let block_config = BlockDeviceConfig {
789
+ drive_id : "" . to_string ( ) ,
790
+ partuuid : None ,
791
+ is_root_device : false ,
792
+ cache_type : CacheType :: Unsafe ,
793
+
794
+ is_read_only : Some ( true ) ,
795
+ path_on_host : Some ( "path" . to_string ( ) ) ,
796
+ rate_limiter : None ,
797
+ file_engine_type : Default :: default ( ) ,
798
+
799
+ socket : Some ( "sock" . to_string ( ) ) ,
800
+ } ;
801
+ assert ! ( VirtioBlockConfig :: try_from( & block_config) . is_err( ) ) ;
802
+ }
803
+
750
804
#[ test]
751
805
fn test_disk_backing_file_helper ( ) {
752
806
let num_sectors = 2 ;
0 commit comments