• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::path::PathBuf;
6 
7 use serde::Deserialize;
8 use serde::Serialize;
9 
10 pub(crate) mod sys;
11 
12 pub mod commands;
13 pub mod constants;
14 mod device;
15 
16 pub use device::Controller;
17 pub use device::DiskConfig;
18 
scsi_option_block_size_default() -> u3219 fn scsi_option_block_size_default() -> u32 {
20     512
21 }
22 
23 /// Parameters for setting up a SCSI device.
24 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, serde_keyvalue::FromKeyValues)]
25 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
26 pub struct ScsiOption {
27     // Path to the SCSI image.
28     pub path: PathBuf,
29     // Indicates whether the device is ready only.
30     #[serde(default, rename = "ro")]
31     pub read_only: bool,
32     // The block size of the device.
33     #[serde(default = "scsi_option_block_size_default")]
34     pub block_size: u32,
35     /// Whether this scsi device should be the root device. Can only be set once. Only useful for
36     /// adding specific command-line options.
37     #[serde(default)]
38     pub root: bool,
39 }
40 
41 #[cfg(test)]
42 mod tests {
43     use std::path::Path;
44 
45     use serde_keyvalue::from_key_values;
46 
47     use super::*;
48 
49     #[test]
parse_scsi_options()50     fn parse_scsi_options() {
51         let scsi_option = from_key_values::<ScsiOption>("/path/to/image").unwrap();
52         assert_eq!(
53             scsi_option,
54             ScsiOption {
55                 path: Path::new("/path/to/image").to_path_buf(),
56                 read_only: false,
57                 block_size: 512,
58                 root: false,
59             }
60         );
61 
62         let scsi_option = from_key_values::<ScsiOption>("/path/to/image,ro").unwrap();
63         assert_eq!(
64             scsi_option,
65             ScsiOption {
66                 path: Path::new("/path/to/image").to_path_buf(),
67                 read_only: true,
68                 block_size: 512,
69                 root: false,
70             }
71         );
72 
73         let scsi_option = from_key_values::<ScsiOption>("/path/to/image,block-size=1024").unwrap();
74         assert_eq!(
75             scsi_option,
76             ScsiOption {
77                 path: Path::new("/path/to/image").to_path_buf(),
78                 read_only: false,
79                 block_size: 1024,
80                 root: false,
81             }
82         );
83 
84         let scsi_option =
85             from_key_values::<ScsiOption>("/path/to/image,block-size=1024,root").unwrap();
86         assert_eq!(
87             scsi_option,
88             ScsiOption {
89                 path: Path::new("/path/to/image").to_path_buf(),
90                 read_only: false,
91                 block_size: 1024,
92                 root: true,
93             }
94         );
95     }
96 }
97