@@ -63,14 +63,21 @@ pub enum ResourcesError {
63
63
EntropyDevice ( #[ from] EntropyDeviceError ) ,
64
64
}
65
65
66
+ #[ derive( Serialize , Deserialize , PartialEq , Eq , Debug ) ]
67
+ #[ serde( untagged) ]
68
+ enum CustomCpuTemplateOrPath {
69
+ Path ( PathBuf ) ,
70
+ Template ( CustomCpuTemplate ) ,
71
+ }
72
+
66
73
/// Used for configuring a vmm from one single json passed to the Firecracker process.
67
74
#[ derive( Debug , Default , PartialEq , Eq , Deserialize , Serialize ) ]
68
75
#[ serde( rename_all = "kebab-case" ) ]
69
76
pub struct VmmConfig {
70
77
balloon : Option < BalloonDeviceConfig > ,
71
78
drives : Vec < BlockDeviceConfig > ,
72
79
boot_source : BootSourceConfig ,
73
- cpu_config : Option < PathBuf > ,
80
+ cpu_config : Option < CustomCpuTemplateOrPath > ,
74
81
logger : Option < crate :: logger:: LoggerConfig > ,
75
82
machine_config : Option < MachineConfig > ,
76
83
metrics : Option < MetricsConfig > ,
@@ -136,11 +143,18 @@ impl VmResources {
136
143
resources. update_machine_config ( & machine_config) ?;
137
144
}
138
145
139
- if let Some ( cpu_config) = vmm_config. cpu_config {
140
- let cpu_config_json =
141
- std:: fs:: read_to_string ( cpu_config) . map_err ( ResourcesError :: File ) ?;
142
- let cpu_template = CustomCpuTemplate :: try_from ( cpu_config_json. as_str ( ) ) ?;
143
- resources. set_custom_cpu_template ( cpu_template) ;
146
+ if let Some ( either) = vmm_config. cpu_config {
147
+ match either {
148
+ CustomCpuTemplateOrPath :: Path ( path) => {
149
+ let cpu_config_json =
150
+ std:: fs:: read_to_string ( path) . map_err ( ResourcesError :: File ) ?;
151
+ let cpu_template = CustomCpuTemplate :: try_from ( cpu_config_json. as_str ( ) ) ?;
152
+ resources. set_custom_cpu_template ( cpu_template) ;
153
+ }
154
+ CustomCpuTemplateOrPath :: Template ( template) => {
155
+ resources. set_custom_cpu_template ( template)
156
+ }
157
+ }
144
158
}
145
159
146
160
resources. build_boot_source ( vmm_config. boot_source ) ?;
@@ -505,6 +519,7 @@ mod tests {
505
519
506
520
use super :: * ;
507
521
use crate :: HTTP_MAX_PAYLOAD_SIZE ;
522
+ use crate :: cpu_config:: templates:: test_utils:: TEST_TEMPLATE_JSON ;
508
523
use crate :: cpu_config:: templates:: { CpuTemplateType , StaticCpuTemplate } ;
509
524
use crate :: devices:: virtio:: balloon:: Balloon ;
510
525
use crate :: devices:: virtio:: block:: virtio:: VirtioBlockError ;
@@ -1051,6 +1066,43 @@ mod tests {
1051
1066
assert ! ( matches!( error, ResourcesError :: File ( _) ) , "{:?}" , error) ;
1052
1067
}
1053
1068
1069
+ #[ test]
1070
+ fn test_cpu_config_inline ( ) {
1071
+ // Include custom cpu template directly inline in config json
1072
+ let kernel_file = TempFile :: new ( ) . unwrap ( ) ;
1073
+ let rootfs_file = TempFile :: new ( ) . unwrap ( ) ;
1074
+ let default_instance_info = InstanceInfo :: default ( ) ;
1075
+
1076
+ let json = format ! (
1077
+ r#"{{
1078
+ "boot-source": {{
1079
+ "kernel_image_path": "{}",
1080
+ "boot_args": "console=ttyS0 reboot=k panic=1 pci=off"
1081
+ }},
1082
+ "cpu-config": {},
1083
+ "drives": [
1084
+ {{
1085
+ "drive_id": "rootfs",
1086
+ "path_on_host": "{}",
1087
+ "is_root_device": true,
1088
+ "is_read_only": false
1089
+ }}
1090
+ ]
1091
+ }}"# ,
1092
+ kernel_file. as_path( ) . to_str( ) . unwrap( ) ,
1093
+ TEST_TEMPLATE_JSON ,
1094
+ rootfs_file. as_path( ) . to_str( ) . unwrap( ) ,
1095
+ ) ;
1096
+
1097
+ VmResources :: from_json (
1098
+ json. as_str ( ) ,
1099
+ & default_instance_info,
1100
+ HTTP_MAX_PAYLOAD_SIZE ,
1101
+ None ,
1102
+ )
1103
+ . unwrap ( ) ;
1104
+ }
1105
+
1054
1106
#[ test]
1055
1107
fn test_cpu_config_from_valid_json ( ) {
1056
1108
// Valid cpu config file path.
0 commit comments