@@ -18,16 +18,16 @@ const EXIT_CODE_ERROR: i32 = 1;
18
18
enum Error {
19
19
#[ error( "Failed to operate file: {0}" ) ]
20
20
FileIo ( #[ from] std:: io:: Error ) ,
21
- #[ error( "{0}" ) ]
22
- DumpCpuConfig ( #[ from] dump:: Error ) ,
23
21
#[ error( "CPU template is not specified: {0}" ) ]
24
22
NoCpuTemplate ( #[ from] GetCpuTemplateError ) ,
25
23
#[ error( "Failed to serialize/deserialize JSON file: {0}" ) ]
26
24
Serde ( #[ from] serde_json:: Error ) ,
27
25
#[ error( "{0}" ) ]
28
26
Utils ( #[ from] utils:: Error ) ,
29
27
#[ error( "{0}" ) ]
30
- VerifyCpuTemplate ( #[ from] verify:: Error ) ,
28
+ TemplateDump ( #[ from] dump:: Error ) ,
29
+ #[ error( "{0}" ) ]
30
+ TemplateVerify ( #[ from] verify:: Error ) ,
31
31
}
32
32
33
33
type Result < T > = std:: result:: Result < T , Error > ;
@@ -41,7 +41,17 @@ struct Cli {
41
41
42
42
#[ derive( Subcommand ) ]
43
43
enum Command {
44
- /// Dump CPU configuration in custom CPU template format.
44
+ /// Template-related operations
45
+ #[ command( subcommand) ]
46
+ Template ( TemplateOperation ) ,
47
+ /// Fingerprint-related operations
48
+ #[ command( subcommand) ]
49
+ Fingerprint ( FingerprintOperation ) ,
50
+ }
51
+
52
+ #[ derive( Subcommand ) ]
53
+ enum TemplateOperation {
54
+ /// Dump guest CPU configuration in the custom CPU template format.
45
55
Dump {
46
56
/// Path of firecracker config file.
47
57
#[ arg( short, long, value_name = "PATH" ) ]
@@ -50,7 +60,7 @@ enum Command {
50
60
#[ arg( short, long, value_name = "PATH" , default_value = "cpu_config.json" ) ]
51
61
output : PathBuf ,
52
62
} ,
53
- /// Strip items shared between multiple CPU configurations .
63
+ /// Strip entries shared between multiple CPU template files .
54
64
Strip {
55
65
/// List of paths of input CPU configuration files.
56
66
#[ arg( short, long, num_args = 2 ..) ]
@@ -59,55 +69,61 @@ enum Command {
59
69
#[ arg( short, long, default_value = "_stripped" ) ]
60
70
suffix : String ,
61
71
} ,
62
- /// Verify that the given CPU template is applied as intended.
72
+ /// Verify that the given CPU template file is applied as intended.
63
73
Verify {
64
- /// Path of firecracker config file specifying CPU template.
74
+ /// Path of firecracker config file specifying the target CPU template.
65
75
#[ arg( short, long, value_name = "PATH" ) ]
66
76
config : PathBuf ,
67
77
} ,
68
78
}
69
79
80
+ #[ derive( Subcommand ) ]
81
+ enum FingerprintOperation { }
82
+
70
83
fn run ( cli : Cli ) -> Result < ( ) > {
71
84
match cli. command {
72
- Command :: Dump { config, output } => {
73
- let config = read_to_string ( config) ?;
74
- let ( vmm, _) = utils:: build_microvm_from_config ( & config) ?;
75
-
76
- let cpu_config = dump:: dump ( vmm) ?;
77
-
78
- let cpu_config_json = serde_json:: to_string_pretty ( & cpu_config) ?;
79
- write ( output, cpu_config_json) ?;
80
- }
81
- Command :: Strip { paths, suffix } => {
82
- let mut templates = Vec :: with_capacity ( paths. len ( ) ) ;
83
- for path in & paths {
84
- let template_json = read_to_string ( path) ?;
85
- let template: CustomCpuTemplate = serde_json:: from_str ( & template_json) ?;
86
- templates. push ( template) ;
87
- }
85
+ Command :: Template ( op) => match op {
86
+ TemplateOperation :: Dump { config, output } => {
87
+ let config = read_to_string ( config) ?;
88
+ let ( vmm, _) = utils:: build_microvm_from_config ( & config) ?;
88
89
89
- let stripped_templates = strip :: strip ( templates ) ;
90
+ let cpu_config = dump :: dump ( vmm ) ? ;
90
91
91
- for ( path, template) in paths. into_iter ( ) . zip ( stripped_templates. into_iter ( ) ) {
92
- let path = utils:: add_suffix ( & path, & suffix) ;
93
- let template_json = serde_json:: to_string_pretty ( & template) ?;
94
- write ( path, template_json) ?;
92
+ let cpu_config_json = serde_json:: to_string_pretty ( & cpu_config) ?;
93
+ write ( output, cpu_config_json) ?;
95
94
}
96
- }
97
- Command :: Verify { config } => {
98
- let config = read_to_string ( config) ?;
99
- let ( vmm, vm_resources) = utils:: build_microvm_from_config ( & config) ?;
100
-
101
- let cpu_template = vm_resources
102
- . vm_config
103
- . cpu_template
104
- . get_cpu_template ( ) ?
105
- . into_owned ( ) ;
106
- let cpu_config = dump:: dump ( vmm) ?;
107
-
108
- verify:: verify ( cpu_template, cpu_config) ?;
109
- }
110
- } ;
95
+ TemplateOperation :: Strip { paths, suffix } => {
96
+ let mut templates = Vec :: with_capacity ( paths. len ( ) ) ;
97
+ for path in & paths {
98
+ let template_json = read_to_string ( path) ?;
99
+ let template: CustomCpuTemplate = serde_json:: from_str ( & template_json) ?;
100
+ templates. push ( template) ;
101
+ }
102
+
103
+ let stripped_templates = strip:: strip ( templates) ;
104
+
105
+ for ( path, template) in paths. into_iter ( ) . zip ( stripped_templates. into_iter ( ) ) {
106
+ let path = utils:: add_suffix ( & path, & suffix) ;
107
+ let template_json = serde_json:: to_string_pretty ( & template) ?;
108
+ write ( path, template_json) ?;
109
+ }
110
+ }
111
+ TemplateOperation :: Verify { config } => {
112
+ let config = read_to_string ( config) ?;
113
+ let ( vmm, vm_resources) = utils:: build_microvm_from_config ( & config) ?;
114
+
115
+ let cpu_template = vm_resources
116
+ . vm_config
117
+ . cpu_template
118
+ . get_cpu_template ( ) ?
119
+ . into_owned ( ) ;
120
+ let cpu_config = dump:: dump ( vmm) ?;
121
+
122
+ verify:: verify ( cpu_template, cpu_config) ?;
123
+ }
124
+ } ,
125
+ Command :: Fingerprint ( _) => { }
126
+ }
111
127
112
128
Ok ( ( ) )
113
129
}
@@ -263,6 +279,7 @@ mod tests {
263
279
264
280
let args = vec ! [
265
281
"cpu-template-helper" ,
282
+ "template" ,
266
283
"dump" ,
267
284
"--config" ,
268
285
config_file. as_path( ) . to_str( ) . unwrap( ) ,
@@ -278,7 +295,7 @@ mod tests {
278
295
fn test_strip_command ( ) {
279
296
let files = vec ! [ generate_sample_modifiers( ) , generate_sample_modifiers( ) ] ;
280
297
281
- let mut args = vec ! [ "cpu-template-helper" , "strip" , "-p" ] ;
298
+ let mut args = vec ! [ "cpu-template-helper" , "template" , " strip", "-p" ] ;
282
299
let paths = files
283
300
. iter ( )
284
301
. map ( |file| file. as_path ( ) . to_str ( ) . unwrap ( ) )
@@ -302,6 +319,7 @@ mod tests {
302
319
303
320
let args = vec ! [
304
321
"cpu-template-helper" ,
322
+ "template" ,
305
323
"verify" ,
306
324
"--config" ,
307
325
config_file. as_path( ) . to_str( ) . unwrap( ) ,
0 commit comments