-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathchangevm.rs
More file actions
155 lines (127 loc) · 4.37 KB
/
Copy pathchangevm.rs
File metadata and controls
155 lines (127 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2021 Red Hat, Inc.
// SPDX-License-Identifier: Apache-2.0
use clap::Args;
use std::collections::HashMap;
use crate::utils::{path_pairs_to_hash_map, port_pairs_to_hash_map, PathPair, PortPair};
use crate::{store_config, KrunvmConfig, NetworkMode};
use super::list::printvm;
/// Change the configuration of a microVM
#[derive(Args, Debug)]
pub struct ChangeVmCmd {
/// Name of the VM to be modified
name: String,
/// Assign a new name to the VM
#[arg(long)]
new_name: Option<String>,
/// Number of vCPUs
#[arg(long)]
cpus: Option<u32>,
/// Amount of RAM in MiB
#[arg(long)]
mem: Option<u32>,
/// Network mode to use for the microVM
#[arg(long, value_enum)]
network: Option<NetworkMode>,
/// Working directory inside the microVM
#[arg(short, long)]
workdir: Option<String>,
/// Remove all volume mappings
#[arg(long)]
remove_volumes: bool,
/// Volume(s) in form "host_path:guest_path" to be exposed to the guest
#[arg(short, long = "volume")]
volumes: Vec<PathPair>,
/// Remove all port mappings
#[arg(long)]
remove_ports: bool,
/// Port(s) in format "host_port:guest_port" to be exposed to the host
#[arg(long = "port")]
ports: Vec<PortPair>,
}
impl ChangeVmCmd {
pub fn run(self, cfg: &mut KrunvmConfig) {
let mut cfg_changed = false;
let vmcfg = if let Some(new_name) = &self.new_name {
if cfg.vmconfig_map.contains_key(new_name) {
println!("A VM with name {} already exists", new_name);
std::process::exit(-1);
}
let mut vmcfg = match cfg.vmconfig_map.remove(&self.name) {
None => {
println!("No VM found with name {}", &self.name);
std::process::exit(-1);
}
Some(vmcfg) => vmcfg,
};
cfg_changed = true;
let name = new_name.to_string();
vmcfg.name = name.clone();
cfg.vmconfig_map.insert(name.clone(), vmcfg);
cfg.vmconfig_map.get_mut(&name).unwrap()
} else {
match cfg.vmconfig_map.get_mut(&self.name) {
None => {
println!("No VM found with name {}", self.name);
std::process::exit(-1);
}
Some(vmcfg) => vmcfg,
}
};
if let Some(cpus) = self.cpus {
if cpus > 8 {
println!("Error: the maximum number of CPUs supported is 8");
} else {
vmcfg.cpus = cpus;
cfg_changed = true;
}
}
if let Some(mem) = self.mem {
if mem > 16384 {
println!("Error: the maximum amount of RAM supported is 16384 MiB");
} else {
vmcfg.mem = mem;
cfg_changed = true;
}
}
if self.network == Some(NetworkMode::None) && !self.ports.is_empty() {
println!("Error: port mappings cannot be used with --network=none");
std::process::exit(-1);
}
if let Some(network) = self.network {
vmcfg.network = network;
cfg_changed = true;
}
if self.remove_volumes {
vmcfg.mapped_volumes = HashMap::new();
cfg_changed = true;
} else {
let mapped_volumes = path_pairs_to_hash_map(self.volumes);
if !mapped_volumes.is_empty() {
vmcfg.mapped_volumes = mapped_volumes;
cfg_changed = true;
}
}
// TODO: don't just silently ignore --volume args when --remove_volumes is specified
if self.remove_ports {
vmcfg.mapped_ports = HashMap::new();
cfg_changed = true;
} else {
let mapped_ports = port_pairs_to_hash_map(self.ports);
if !mapped_ports.is_empty() {
vmcfg.mapped_ports = mapped_ports;
cfg_changed = true;
}
}
// TODO: don't just silently ignore --port args when --remove_ports is specified
if let Some(workdir) = self.workdir {
vmcfg.workdir = workdir.to_string();
cfg_changed = true;
}
println!();
printvm(vmcfg);
println!();
if cfg_changed {
store_config(cfg);
}
}
}