@@ -63,14 +63,21 @@ pub enum ResourcesError {
6363 EntropyDevice ( #[ from] EntropyDeviceError ) ,
6464}
6565
66+ #[ derive( Serialize , Deserialize , PartialEq , Eq , Debug ) ]
67+ #[ serde( untagged) ]
68+ enum CustomCpuTemplateOrPath {
69+ Path ( PathBuf ) ,
70+ Template ( CustomCpuTemplate ) ,
71+ }
72+
6673/// Used for configuring a vmm from one single json passed to the Firecracker process.
6774#[ derive( Debug , Default , PartialEq , Eq , Deserialize , Serialize ) ]
6875#[ serde( rename_all = "kebab-case" ) ]
6976pub struct VmmConfig {
7077 balloon : Option < BalloonDeviceConfig > ,
7178 drives : Vec < BlockDeviceConfig > ,
7279 boot_source : BootSourceConfig ,
73- cpu_config : Option < PathBuf > ,
80+ cpu_config : Option < CustomCpuTemplateOrPath > ,
7481 logger : Option < crate :: logger:: LoggerConfig > ,
7582 machine_config : Option < MachineConfig > ,
7683 metrics : Option < MetricsConfig > ,
@@ -136,11 +143,18 @@ impl VmResources {
136143 resources. update_machine_config ( & machine_config) ?;
137144 }
138145
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+ }
144158 }
145159
146160 resources. build_boot_source ( vmm_config. boot_source ) ?;
@@ -505,6 +519,7 @@ mod tests {
505519
506520 use super :: * ;
507521 use crate :: HTTP_MAX_PAYLOAD_SIZE ;
522+ use crate :: cpu_config:: templates:: test_utils:: TEST_TEMPLATE_JSON ;
508523 use crate :: cpu_config:: templates:: { CpuTemplateType , StaticCpuTemplate } ;
509524 use crate :: devices:: virtio:: balloon:: Balloon ;
510525 use crate :: devices:: virtio:: block:: virtio:: VirtioBlockError ;
@@ -1051,6 +1066,43 @@ mod tests {
10511066 assert ! ( matches!( error, ResourcesError :: File ( _) ) , "{:?}" , error) ;
10521067 }
10531068
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+
10541106 #[ test]
10551107 fn test_cpu_config_from_valid_json ( ) {
10561108 // Valid cpu config file path.
0 commit comments