File tree 4 files changed +53
-2
lines changed
4 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,16 @@ pub struct VmConfig {
60
60
#[ serde( default ) ]
61
61
#[ arg( long, requires( "tpm" ) ) ]
62
62
pub ovmf_code_path : Option < String > ,
63
+
64
+ /// Number of vCPUs
65
+ #[ serde( default ) ]
66
+ #[ arg( long) ]
67
+ pub vcpus : Option < usize > ,
68
+
69
+ /// Amount of memory, in MBs
70
+ #[ serde( default ) ]
71
+ #[ arg( long) ]
72
+ pub memory : Option < usize > ,
63
73
}
64
74
65
75
impl VmConfig {
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ struct Args {
31
31
}
32
32
33
33
#[ derive( clap:: Subcommand , Debug ) ]
34
+ #[ allow( clippy:: large_enum_variant) ]
34
35
enum Commands {
35
36
/// Manage configuration for tests and VMs
36
37
#[ clap( subcommand) ]
@@ -148,6 +149,7 @@ enum Commands {
148
149
}
149
150
150
151
#[ derive( clap:: Subcommand , Debug ) ]
152
+ #[ allow( clippy:: large_enum_variant) ]
151
153
enum ConfigArg {
152
154
/// Print the current config
153
155
Get ,
@@ -159,6 +161,7 @@ enum ConfigArg {
159
161
}
160
162
161
163
#[ derive( clap:: Subcommand , Debug ) ]
164
+ #[ allow( clippy:: large_enum_variant) ]
162
165
enum VmConfig {
163
166
/// Create or edit a VM config
164
167
Set {
Original file line number Diff line number Diff line change @@ -25,6 +25,12 @@ const STDERR_LOG_LEVEL: log::Level = log::Level::Error;
25
25
const STDOUT_LOG_LEVEL : log:: Level = log:: Level :: Debug ;
26
26
const OBTAIN_IP_TIMEOUT : Duration = Duration :: from_secs ( 180 ) ;
27
27
28
+ /// Default number of VCPU cores (passed to -smp)
29
+ const DEFAULT_NUM_VCPUS : usize = 2 ;
30
+
31
+ /// Default amount of memory, in MBs (passed to -m)
32
+ const DEFAULT_AMOUNT_MEMORY : usize = 2048 ;
33
+
28
34
#[ derive( thiserror:: Error , Debug ) ]
29
35
pub enum Error {
30
36
#[ error( "Failed to set up network" ) ]
@@ -83,15 +89,21 @@ pub async fn run(config: &Config, vm_config: &VmConfig) -> Result<QemuInstance>
83
89
. map_err ( Error :: Network ) ?;
84
90
85
91
let mut qemu_cmd = Command :: new ( "qemu-system-x86_64" ) ;
92
+ let vcpus = vm_config. vcpus . unwrap_or ( DEFAULT_NUM_VCPUS ) ;
93
+ let memory = vm_config. memory . unwrap_or ( DEFAULT_AMOUNT_MEMORY ) ;
94
+
95
+ log:: debug!( "CPU count: {vcpus}" ) ;
96
+ log:: debug!( "Memory: {memory}M" ) ;
97
+
86
98
qemu_cmd. args ( [
87
99
"-cpu" ,
88
100
"host" ,
89
101
"-accel" ,
90
102
"kvm" ,
91
103
"-m" ,
92
- "4096" ,
104
+ & memory . to_string ( ) ,
93
105
"-smp" ,
94
- "2" ,
106
+ & vcpus . to_string ( ) ,
95
107
"-drive" ,
96
108
& format ! ( "file={}" , vm_config. image_path) ,
97
109
"-device" ,
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ pub async fn run(config: &Config, vm_config: &VmConfig) -> Result<TartInstance>
49
49
MachineCopy :: clone_vm ( & vm_config. image_path ) . await ?
50
50
} ;
51
51
52
+ if let Err ( err) = machine_copy. configure ( vm_config) . await {
53
+ log:: error!( "Failed to configure tart vm: {err}" ) ;
54
+ }
55
+
52
56
// Start VM
53
57
let mut tart_cmd = Command :: new ( "tart" ) ;
54
58
tart_cmd. args ( [ "run" , & machine_copy. name , "--serial" ] ) ;
@@ -167,6 +171,28 @@ impl MachineCopy {
167
171
} )
168
172
}
169
173
174
+ pub async fn configure ( & self , vm_config : & VmConfig ) -> Result < ( ) > {
175
+ let mut args = vec ! [ ] ;
176
+ if let Some ( cpu) = vm_config. vcpus {
177
+ args. extend ( [ "--cpu" . to_owned ( ) , cpu. to_string ( ) ] ) ;
178
+ log:: info!( "vCPUs: {cpu}" ) ;
179
+ }
180
+ if let Some ( mem) = vm_config. memory {
181
+ args. extend ( [ "--memory" . to_owned ( ) , mem. to_string ( ) ] ) ;
182
+ log:: info!( "Memory: {mem} MB" ) ;
183
+ }
184
+ if !args. is_empty ( ) {
185
+ let mut tart_cmd = Command :: new ( "tart" ) ;
186
+ tart_cmd. args ( [ "set" , & self . name ] ) ;
187
+ tart_cmd. args ( args) ;
188
+ tart_cmd
189
+ . status ( )
190
+ . await
191
+ . context ( "failed to update tart config" ) ?;
192
+ }
193
+ Ok ( ( ) )
194
+ }
195
+
170
196
pub async fn cleanup ( mut self ) {
171
197
let _ = tokio:: task:: spawn_blocking ( move || self . try_destroy ( ) ) . await ;
172
198
}
You can’t perform that action at this time.
0 commit comments