-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathapp.rs
More file actions
1199 lines (1098 loc) · 40.9 KB
/
Copy pathapp.rs
File metadata and controls
1199 lines (1098 loc) · 40.9 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use crate::config::{Config, Networking, ProcessAnnotation, Protocol};
use dstack_port_forward::{ForwardRule, ForwardService, Protocol as FwdProtocol};
use anyhow::{bail, Context, Result};
use bon::Builder;
use dstack_kms_rpc::kms_client::KmsClient;
use dstack_types::shared_filenames::{
APP_COMPOSE, ENCRYPTED_ENV, INSTANCE_INFO, SYS_CONFIG, USER_CONFIG,
};
use dstack_vmm_rpc::{
self as pb, GpuInfo, ReloadVmsResponse, StatusRequest, StatusResponse, VmConfiguration,
};
use fs_err as fs;
use guest_api::client::DefaultClient as GuestClient;
use id_pool::IdPool;
use or_panic::ResultOrPanic;
use ra_rpc::client::RaClient;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::SystemTime;
use supervisor_client::SupervisorClient;
use tracing::{debug, error, info, warn};
pub use image::{Image, ImageInfo};
pub use qemu::{VmConfig, VmWorkDir};
mod id_pool;
mod image;
mod qemu;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PortMapping {
pub address: IpAddr,
pub protocol: Protocol,
pub from: u16,
pub to: u16,
}
#[derive(Deserialize, Serialize, Clone, Builder, Debug)]
pub struct Manifest {
pub id: String,
pub name: String,
pub app_id: String,
pub vcpu: u32,
pub memory: u32,
pub disk_size: u32,
pub image: String,
pub port_map: Vec<PortMapping>,
pub created_at_ms: u64,
#[serde(default)]
pub hugepages: bool,
#[serde(default)]
pub pin_numa: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gpus: Option<GpuConfig>,
#[serde(default)]
pub kms_urls: Vec<String>,
#[serde(default)]
pub gateway_urls: Vec<String>,
#[serde(default)]
pub no_tee: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub networking: Option<Networking>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AttachMode {
All,
#[default]
Listed,
}
impl std::fmt::Display for AttachMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AttachMode::All => write!(f, "all"),
AttachMode::Listed => write!(f, "listed"),
}
}
}
impl AttachMode {
pub fn is_all(&self) -> bool {
matches!(self, AttachMode::All)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GpuConfig {
pub attach_mode: AttachMode,
#[serde(default)]
pub gpus: Vec<GpuSpec>,
#[serde(default)]
pub bridges: Vec<GpuSpec>,
}
impl GpuConfig {
pub fn is_empty(&self) -> bool {
if self.attach_mode.is_all() {
return false;
}
self.gpus.is_empty() && self.bridges.is_empty()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GpuSpec {
#[serde(default)]
pub slot: String,
}
#[derive(Clone)]
pub struct App {
pub config: Arc<Config>,
pub supervisor: SupervisorClient,
state: Arc<Mutex<AppState>>,
forward_service: Arc<tokio::sync::Mutex<ForwardService>>,
}
impl App {
fn lock(&self) -> MutexGuard<'_, AppState> {
self.state.lock().or_panic("mutex poisoned")
}
pub(crate) fn vm_dir(&self) -> PathBuf {
self.config.run_path.clone()
}
pub(crate) fn work_dir(&self, id: &str) -> VmWorkDir {
VmWorkDir::new(self.config.run_path.join(id))
}
pub fn new(config: Config, supervisor: SupervisorClient) -> Self {
let cid_start = config.cvm.cid_start;
let cid_end = cid_start.saturating_add(config.cvm.cid_pool_size);
let cid_pool = IdPool::new(cid_start, cid_end);
Self {
supervisor: supervisor.clone(),
state: Arc::new(Mutex::new(AppState {
cid_pool,
vms: HashMap::new(),
active_forwards: HashMap::new(),
})),
config: Arc::new(config),
forward_service: Arc::new(tokio::sync::Mutex::new(ForwardService::new())),
}
}
pub async fn load_vm(
&self,
work_dir: impl AsRef<Path>,
cids_assigned: &HashMap<String, u32>,
auto_start: bool,
) -> Result<()> {
let vm_work_dir = VmWorkDir::new(work_dir.as_ref());
let manifest = vm_work_dir.manifest().context("Failed to read manifest")?;
if manifest.image.len() > 64
|| manifest.image.contains("..")
|| !manifest
.image
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '.')
{
bail!("Invalid image name");
}
let image_path = self.config.image_path.join(&manifest.image);
let image = Image::load(&image_path).context("Failed to load image")?;
let vm_id = manifest.id.clone();
let app_compose = vm_work_dir
.app_compose()
.context("Failed to read compose file")?;
{
let mut states = self.lock();
let cid = states
.get(&vm_id)
.map(|vm| vm.config.cid)
.or_else(|| cids_assigned.get(&vm_id).cloned())
.or_else(|| states.cid_pool.allocate())
.context("CID pool exhausted")?;
let vm_config = VmConfig {
manifest,
image,
cid,
workdir: vm_work_dir.path().to_path_buf(),
gateway_enabled: app_compose.gateway_enabled(),
};
match states.get_mut(&vm_id) {
Some(vm) => {
vm.config = vm_config.into();
}
None => {
states.add(VmState::new(vm_config));
}
}
};
if auto_start && vm_work_dir.started().unwrap_or_default() {
self.start_vm(&vm_id).await?;
}
Ok(())
}
pub async fn start_vm(&self, id: &str) -> Result<()> {
{
let state = self.lock();
if let Some(vm) = state.get(id) {
if vm.state.removing {
bail!("VM is being removed");
}
}
}
self.sync_dynamic_config(id)?;
let is_running = self
.supervisor
.info(id)
.await?
.is_some_and(|info| info.state.status.is_running());
self.set_started(id, true)?;
let vm_config = {
let mut state = self.lock();
let vm_state = state.get_mut(id).context("VM not found")?;
// Older images does not support for progress reporting
if vm_state.config.image.info.shared_ro {
vm_state.state.start(is_running);
} else {
vm_state.state.reset_na();
}
vm_state.config.clone()
};
if !is_running {
let work_dir = self.work_dir(id);
for path in [work_dir.serial_pty(), work_dir.qmp_socket()] {
if path.symlink_metadata().is_ok() {
fs::remove_file(path)?;
}
}
let devices = self.try_allocate_gpus(&vm_config.manifest)?;
let processes = vm_config.config_qemu(&work_dir, &self.config.cvm, &devices)?;
for process in processes {
self.supervisor
.deploy(&process)
.await
.with_context(|| format!("Failed to start process {}", process.id))?;
}
let mut state = self.lock();
let vm_state = state.get_mut(id).context("VM not found")?;
vm_state.state.devices = devices;
}
Ok(())
}
fn set_started(&self, id: &str, started: bool) -> Result<()> {
let work_dir = self.work_dir(id);
work_dir
.set_started(started)
.context("Failed to set started")
}
pub async fn stop_vm(&self, id: &str) -> Result<()> {
self.set_started(id, false)?;
self.cleanup_port_forward(id).await;
self.supervisor.stop(id).await?;
Ok(())
}
pub async fn remove_vm(&self, id: &str) -> Result<()> {
{
let mut state = self.lock();
let vm = state.get_mut(id).context("VM not found")?;
if vm.state.removing {
// Already being removed — idempotent
return Ok(());
}
vm.state.removing = true;
}
// Persist the removing marker so crash recovery can resume
let work_dir = self.work_dir(id);
if let Err(err) = work_dir.set_removing() {
warn!("Failed to write .removing marker for {id}: {err:?}");
}
// Clean up port forwarding immediately
self.cleanup_port_forward(id).await;
// Spawn background cleanup coroutine
let app = self.clone();
let id = id.to_string();
tokio::spawn(async move {
if let Err(err) = app.finish_remove_vm(&id).await {
error!("Background cleanup failed for {id}: {err:?}");
}
});
Ok(())
}
/// Background cleanup: stop supervisor process, wait for it to exit,
/// remove from supervisor, delete workdir, and free CID.
async fn finish_remove_vm(&self, id: &str) -> Result<()> {
// Stop the supervisor process (idempotent if already stopped)
if let Err(err) = self.supervisor.stop(id).await {
debug!("supervisor.stop({id}) during removal: {err:?}");
}
// Poll until the process is no longer running, then remove it.
// Some VMs take a long time to stop (e.g. 2+ hours), so we wait indefinitely.
let mut poll_count: u64 = 0;
loop {
match self.supervisor.info(id).await {
Ok(Some(info)) if info.state.status.is_running() => {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
poll_count += 1;
if poll_count.is_multiple_of(30) {
info!(
"VM {id} still running after {}m during removal, waiting...",
poll_count * 2 / 60
);
}
}
Ok(Some(_)) => {
// Not running — remove from supervisor
if let Err(err) = self.supervisor.remove(id).await {
warn!("supervisor.remove({id}) failed: {err:?}");
}
break;
}
Ok(None) => {
// Already gone from supervisor
break;
}
Err(err) => {
warn!("supervisor.info({id}) failed during removal: {err:?}");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
}
// Delete the workdir (may already be gone, e.g. manual deletion before reload)
let vm_path = self.work_dir(id);
if vm_path.path().exists() {
if let Err(err) = fs::remove_dir_all(&vm_path) {
error!("Failed to remove VM directory for {id}: {err:?}");
}
}
// Free CID and remove from memory (last step)
{
let mut state = self.lock();
if let Some(vm_state) = state.remove(id) {
state.cid_pool.free(vm_state.config.cid);
}
}
info!("VM {id} removed successfully");
Ok(())
}
/// Spawn a background task to clean up a VM (stop + remove from supervisor + delete workdir).
/// Returns false if a cleanup task is already running for this VM.
fn spawn_finish_remove(&self, id: &str) -> bool {
{
let mut state = self.lock();
if let Some(vm) = state.get_mut(id) {
if vm.state.removing {
// Already being cleaned up — skip
return false;
}
vm.state.removing = true;
}
// If VM is not in memory (e.g. orphaned supervisor process), no entry to guard
// but we still need to clean up the supervisor process and workdir.
}
let app = self.clone();
let id = id.to_string();
tokio::spawn(async move {
if let Err(err) = app.finish_remove_vm(&id).await {
error!("Background cleanup failed for {id}: {err:?}");
}
});
true
}
/// Handle a DHCP lease notification: look up VM by MAC address, persist
/// the guest IP, and reconfigure port forwarding.
pub async fn report_dhcp_lease(&self, mac: &str, ip: &str) {
use crate::app::qemu::mac_address_for_vm;
let vm_id = {
let mut state = self.lock();
let prefix = self.config.cvm.networking.mac_prefix_bytes();
let found = state
.vms
.iter_mut()
.find(|(id, _)| mac_address_for_vm(id, &prefix) == mac);
let Some((id, vm)) = found else {
debug!(mac, ip, "DHCP lease for unknown MAC, ignoring");
return;
};
let vm_id = id.clone();
let workdir = VmWorkDir::new(vm.config.workdir.clone());
if let Err(e) = workdir.set_guest_ip(ip) {
error!(mac, ip, "failed to persist guest IP: {e}");
}
vm.state.guest_ip = ip.to_string();
info!(mac, ip, id = %vm_id, "DHCP lease updated");
vm_id
};
self.reconfigure_port_forward(&vm_id).await;
}
/// Reconfigure port forwarding for a bridge-mode VM.
///
/// Computes desired rules from the VM's port_map and guest_ip, then diffs
/// against currently active rules. Only changed rules are added/removed so
/// existing connections on unchanged rules are not interrupted.
pub async fn reconfigure_port_forward(&self, id: &str) {
let info = {
let state = self.lock();
let Some(vm) = state.get(id) else {
return;
};
let networking = vm
.config
.manifest
.networking
.as_ref()
.unwrap_or(&self.config.cvm.networking);
if !networking.is_bridge() || !networking.forward_service_enabled {
return;
}
let guest_ip = vm.state.guest_ip.clone();
let port_map = vm.config.manifest.port_map.clone();
(guest_ip, port_map)
};
let (guest_ip_str, port_map) = info;
if guest_ip_str.is_empty() {
return;
}
let Ok(guest_ip) = guest_ip_str.parse::<IpAddr>() else {
warn!(id, ip = %guest_ip_str, "invalid guest IP, skipping port forward");
return;
};
let new_rules: Vec<ForwardRule> = port_map
.iter()
.map(|pm| ForwardRule {
protocol: match pm.protocol {
Protocol::Tcp => FwdProtocol::Tcp,
Protocol::Udp => FwdProtocol::Udp,
},
listen_addr: pm.address,
listen_port: pm.from,
target_ip: guest_ip,
target_port: pm.to,
})
.collect();
let old_rules = self
.lock()
.active_forwards
.get(id)
.cloned()
.unwrap_or_default();
let old_set: HashSet<_> = old_rules.iter().collect();
let new_set: HashSet<_> = new_rules.iter().collect();
let mut fwd = self.forward_service.lock().await;
// Remove rules no longer needed
for rule in old_rules.iter().filter(|r| !new_set.contains(r)) {
if let Err(e) = fwd.remove_rule(rule).await {
warn!(id, ?rule, "failed to remove forwarding rule: {e}");
}
}
// Add new rules
for rule in new_rules.iter().filter(|r| !old_set.contains(r)) {
if let Err(e) = fwd.add_rule(rule.clone()) {
warn!(id, ?rule, "failed to add forwarding rule: {e}");
}
}
drop(fwd);
self.lock()
.active_forwards
.insert(id.to_string(), new_rules);
info!(id, "port forwarding reconfigured");
}
/// Remove all port forwarding rules for a VM.
pub async fn cleanup_port_forward(&self, id: &str) {
let old_rules = self.lock().active_forwards.remove(id).unwrap_or_default();
if old_rules.is_empty() {
return;
}
let mut fwd = self.forward_service.lock().await;
for rule in &old_rules {
if let Err(e) = fwd.remove_rule(rule).await {
warn!(id, ?rule, "failed to remove forwarding rule: {e}");
}
}
info!(id, count = old_rules.len(), "port forwarding cleaned up");
}
pub async fn reload_vms(&self) -> Result<()> {
let vm_path = self.vm_dir();
let running_vms = self.supervisor.list().await.context("Failed to list VMs")?;
let running_vms: Vec<(ProcessAnnotation, _)> = running_vms
.into_iter()
.map(|p| (serde_json::from_str(&p.config.note).unwrap_or_default(), p))
.collect();
let occupied_cids = running_vms
.iter()
.filter(|(note, _)| note.is_cvm())
.flat_map(|(_, p)| p.config.cid.map(|cid| (p.config.id.clone(), cid)))
.collect::<HashMap<_, _>>();
{
let mut state = self.lock();
for cid in occupied_cids.values() {
state.cid_pool.occupy(*cid)?;
}
}
// Track VMs with .removing marker — load them but resume cleanup
let mut removing_ids = Vec::new();
if vm_path.exists() {
for entry in fs::read_dir(&vm_path).context("Failed to read VM directory")? {
let entry = entry.context("Failed to read directory entry")?;
let vm_path = entry.path();
if vm_path.is_dir() {
let workdir = VmWorkDir::new(&vm_path);
let is_removing = workdir.is_removing();
// Load all VMs into memory (including removing ones, so they show in UI)
if let Err(err) = self.load_vm(&vm_path, &occupied_cids, !is_removing).await {
error!("Failed to load VM: {err:?}");
}
if is_removing {
if let Some(id) = vm_path.file_name().and_then(|n| n.to_str()) {
info!("Found VM {id} with .removing marker, resuming cleanup");
removing_ids.push(id.to_string());
}
}
}
}
}
// Resume cleanup for VMs with .removing marker
for id in removing_ids {
self.spawn_finish_remove(&id);
}
// Clean up orphaned supervisor processes (in supervisor but not loaded as VMs)
let loaded_vm_ids: HashSet<String> = self.lock().vms.keys().cloned().collect();
for (_, process) in &running_vms {
if !loaded_vm_ids.contains(&process.config.id) {
info!(
"Cleaning up orphaned supervisor process: {}",
process.config.id
);
self.spawn_finish_remove(&process.config.id);
}
}
// Restore port forwarding for running bridge-mode VMs with persisted guest IPs
let vm_ids: Vec<String> = self.lock().vms.keys().cloned().collect();
for id in vm_ids {
let workdir = self.work_dir(&id);
if let Some(ip) = workdir.guest_ip() {
{
let mut state = self.lock();
if let Some(vm) = state.get_mut(&id) {
vm.state.guest_ip = ip;
}
}
self.reconfigure_port_forward(&id).await;
}
}
Ok(())
}
/// Reload VMs directory and sync with memory state while preserving statistics
pub async fn reload_vms_sync(&self) -> Result<ReloadVmsResponse> {
let vm_path = self.vm_dir();
let mut loaded = 0u32;
let mut updated = 0u32;
let mut removed = 0u32;
// Get running VMs to preserve CIDs and process info
let running_vms = self.supervisor.list().await.context("Failed to list VMs")?;
let running_vms_map: HashMap<String, _> = running_vms
.into_iter()
.map(|p| (p.config.id.clone(), p))
.collect();
let occupied_cids = running_vms_map
.iter()
.filter(|(_, p)| {
serde_json::from_str::<ProcessAnnotation>(&p.config.note)
.unwrap_or_default()
.is_cvm()
})
.flat_map(|(id, p)| p.config.cid.map(|cid| (id.clone(), cid)))
.collect::<HashMap<_, _>>();
// Update CID pool with running VMs
{
let mut state = self.lock();
// First clear the pool and re-occupy running VM CIDs
state.cid_pool.clear();
for cid in occupied_cids.values() {
state.cid_pool.occupy(*cid)?;
}
}
// Get VM IDs from filesystem
let mut fs_vm_ids = HashSet::new();
if vm_path.exists() {
for entry in fs::read_dir(&vm_path).context("Failed to read VM directory")? {
let entry = entry.context("Failed to read directory entry")?;
let vm_dir_path = entry.path();
if vm_dir_path.is_dir() {
// Try to get VM ID from directory name or manifest
if let Some(vm_id) = vm_dir_path.file_name().and_then(|n| n.to_str()) {
fs_vm_ids.insert(vm_id.to_string());
}
}
}
}
// Get VM IDs currently in memory and their CIDs
let (memory_vm_ids, existing_cids): (HashSet<String>, HashSet<u32>) = {
let state = self.lock();
(
state.vms.keys().cloned().collect(),
state.vms.values().map(|vm| vm.config.cid).collect(),
)
};
// Remove VMs that no longer exist in filesystem
let to_remove: Vec<String> = memory_vm_ids.difference(&fs_vm_ids).cloned().collect();
for vm_id in &to_remove {
if self.spawn_finish_remove(vm_id) {
removed += 1;
info!("VM {vm_id} scheduled for removal (directory no longer exists)");
}
}
// Load or update VMs from filesystem
let mut removing_ids = Vec::new();
if vm_path.exists() {
for entry in fs::read_dir(vm_path).context("Failed to read VM directory")? {
let entry = entry.context("Failed to read directory entry")?;
let vm_path = entry.path();
if vm_path.is_dir() {
let workdir = VmWorkDir::new(&vm_path);
let is_removing = workdir.is_removing();
// Load all VMs (including removing ones, so they show in UI)
match self
.load_or_update_vm(&vm_path, &occupied_cids, !is_removing)
.await
{
Ok(is_new) => {
if is_new {
loaded += 1;
} else {
updated += 1;
}
}
Err(err) => {
error!("Failed to load or update VM: {err:?}");
}
}
if is_removing {
if let Some(id) = vm_path.file_name().and_then(|n| n.to_str()) {
removing_ids.push(id.to_string());
}
}
}
}
}
for id in &removing_ids {
if self.spawn_finish_remove(id) {
info!("Resuming cleanup for VM {id} (.removing marker)");
}
}
// Clean up any orphaned CIDs that aren't being used
{
let mut state = self.lock();
let used_cids: HashSet<u32> = state.vms.values().map(|vm| vm.config.cid).collect();
let orphaned_cids: Vec<u32> = existing_cids.difference(&used_cids).cloned().collect();
for cid in orphaned_cids {
state.cid_pool.free(cid);
info!("Released orphaned CID {cid}");
}
}
Ok(ReloadVmsResponse {
loaded,
updated,
removed,
})
}
/// Load or update a VM, preserving existing statistics
async fn load_or_update_vm(
&self,
work_dir: impl AsRef<Path>,
cids_assigned: &HashMap<String, u32>,
auto_start: bool,
) -> Result<bool> {
let vm_work_dir = VmWorkDir::new(work_dir.as_ref());
let manifest = vm_work_dir.manifest().context("Failed to read manifest")?;
if manifest.image.len() > 64
|| manifest.image.contains("..")
|| !manifest
.image
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '.')
{
bail!("Invalid image name");
}
let image_path = self.config.image_path.join(&manifest.image);
let image = Image::load(&image_path).context("Failed to load image")?;
let vm_id = manifest.id.clone();
let already_running = cids_assigned.contains_key(&vm_id);
let app_compose = vm_work_dir
.app_compose()
.context("Failed to read compose file")?;
let mut is_new = false;
{
let mut states = self.lock();
// For existing VMs, keep their current CID
// For new VMs, try to use assigned CID or allocate a new one
let cid = if let Some(existing_vm) = states.get(&vm_id) {
// Keep existing CID
existing_vm.config.cid
} else if let Some(assigned_cid) = cids_assigned.get(&vm_id) {
// Use assigned CID from running processes
*assigned_cid
} else {
// Allocate new CID only for truly new VMs
states.cid_pool.allocate().context("CID pool exhausted")?
};
let vm_config = VmConfig {
manifest,
image,
cid,
workdir: vm_work_dir.path().to_path_buf(),
gateway_enabled: app_compose.gateway_enabled(),
};
match states.get_mut(&vm_id) {
Some(vm) => {
// Update existing VM but preserve statistics and CID
let old_state = vm.state.clone();
vm.config = vm_config.into();
vm.state = old_state; // Preserve the existing state with statistics
}
None => {
// This is a new VM, need to occupy its CID if it wasn't allocated
if !cids_assigned.contains_key(&vm_id) {
states.cid_pool.occupy(cid)?;
}
states.add(VmState::new(vm_config));
is_new = true;
}
}
};
if auto_start && vm_work_dir.started().unwrap_or_default() {
if already_running {
info!("Skipping, {vm_id} is already running");
} else {
self.start_vm(&vm_id).await?;
}
}
Ok(is_new)
}
pub async fn list_vms(&self, request: StatusRequest) -> Result<StatusResponse> {
let vms = self
.supervisor
.list()
.await
.context("Failed to list VMs")?
.into_iter()
.map(|p| (p.config.id.clone(), p))
.collect::<HashMap<_, _>>();
let mut infos = self
.lock()
.iter_vms()
.filter(|vm| {
if !request.ids.is_empty() && !request.ids.contains(&vm.config.manifest.id) {
return false;
}
if request.keyword.is_empty() {
true
} else {
vm.config.manifest.name.contains(&request.keyword)
|| vm.config.manifest.id.contains(&request.keyword)
|| vm.config.manifest.app_id.contains(&request.keyword)
|| vm.config.manifest.image.contains(&request.keyword)
}
})
.cloned()
.collect::<Vec<_>>();
infos.sort_by(|a, b| {
a.config
.manifest
.created_at_ms
.cmp(&b.config.manifest.created_at_ms)
});
let total = infos.len() as u32;
let vms = paginate(infos, request.page, request.page_size)
.map(|vm| {
vm.merged_info(
vms.get(&vm.config.manifest.id),
&self.work_dir(&vm.config.manifest.id),
)
})
.map(|info| info.to_pb(&self.config.gateway, request.brief))
.collect::<Vec<_>>();
Ok(StatusResponse {
vms,
port_mapping_enabled: self.config.cvm.port_mapping.enabled,
total,
})
}
pub fn list_images(&self) -> Result<Vec<(String, ImageInfo)>> {
let image_path = self.config.image_path.clone();
let images = fs::read_dir(image_path).context("Failed to read image directory")?;
Ok(images
.flat_map(|entry| {
let path = entry.ok()?.path();
let img = Image::load(&path).ok()?;
Some((path.file_name()?.to_string_lossy().to_string(), img.info))
})
.collect())
}
pub async fn vm_info(&self, id: &str) -> Result<Option<pb::VmInfo>> {
let proc_state = self.supervisor.info(id).await?;
let state = self.lock();
let Some(vm_state) = state.get(id) else {
return Ok(None);
};
let info = vm_state
.merged_info(proc_state.as_ref(), &self.work_dir(id))
.to_pb(&self.config.gateway, false);
Ok(Some(info))
}
pub(crate) fn vm_event_report(&self, cid: u32, event: &str, body: String) -> Result<()> {
info!(cid, event, "VM event");
if body.len() > 1024 * 4 {
error!("Event body too large, skipping");
return Ok(());
}
let mut state = self.lock();
let Some(vm) = state.vms.values_mut().find(|vm| vm.config.cid == cid) else {
bail!("VM not found");
};
vm.state.events.push_back(pb::GuestEvent {
event: event.into(),
body: body.clone(),
timestamp: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
});
while vm.state.events.len() > self.config.event_buffer_size {
vm.state.events.pop_front();
}
match event {
"boot.progress" => {
vm.state.boot_progress = body;
}
"boot.error" => {
vm.state.boot_error = body;
}
"shutdown.progress" => {
if body == "powering off" {
self.set_started(&vm.config.manifest.id, false)?;
}
vm.state.shutdown_progress = body;
}
"instance.info" => {
let workdir = VmWorkDir::new(vm.config.workdir.clone());
let instancd_info_path = workdir.instance_info_path();
safe_write::safe_write(&instancd_info_path, &body)?;
}
_ => {
error!("Guest reported unknown event: {event}");
}
}
Ok(())
}
pub(crate) fn compose_file_path(&self, id: &str) -> PathBuf {
self.shared_dir(id).join(APP_COMPOSE)
}
pub(crate) fn encrypted_env_path(&self, id: &str) -> PathBuf {
self.shared_dir(id).join(ENCRYPTED_ENV)
}
pub(crate) fn user_config_path(&self, id: &str) -> PathBuf {
self.shared_dir(id).join(USER_CONFIG)
}
pub(crate) fn shared_dir(&self, id: &str) -> PathBuf {
self.config.run_path.join(id).join("shared")
}
pub(crate) fn prepare_work_dir(
&self,
id: &str,
req: &VmConfiguration,
app_id: &str,
) -> Result<VmWorkDir> {
let work_dir = self.work_dir(id);
let shared_dir = work_dir.join("shared");
fs::create_dir_all(&shared_dir).context("Failed to create shared directory")?;
fs::write(shared_dir.join(APP_COMPOSE), &req.compose_file)
.context("Failed to write compose file")?;
if !req.encrypted_env.is_empty() {
fs::write(shared_dir.join(ENCRYPTED_ENV), &req.encrypted_env)
.context("Failed to write encrypted env")?;
}
if !req.user_config.is_empty() {
fs::write(shared_dir.join(USER_CONFIG), &req.user_config)
.context("Failed to write user config")?;
}
if !app_id.is_empty() {
let instance_info = json!({
"app_id": app_id,
});
fs::write(
shared_dir.join(INSTANCE_INFO),
serde_json::to_string(&instance_info)?,
)
.context("Failed to write vm config")?;
}
Ok(work_dir)
}
pub(crate) fn sync_dynamic_config(&self, id: &str) -> Result<()> {
let work_dir = self.work_dir(id);
let shared_dir = self.shared_dir(id);
let manifest = work_dir.manifest().context("Failed to read manifest")?;
let cfg = &self.config;
let sys_config_str = make_sys_config(cfg, &manifest)?;
fs::write(shared_dir.join(SYS_CONFIG), sys_config_str)
.context("Failed to write vm config")?;
Ok(())
}
pub(crate) fn kms_client(&self) -> Result<KmsClient<RaClient>> {
if self.config.kms_url.is_empty() {
bail!("KMS is not configured");
}
let url = format!("{}/prpc", self.config.kms_url);
let prpc_client = RaClient::new(url, true)?;
Ok(KmsClient::new(prpc_client))
}
pub(crate) fn guest_agent_client(&self, id: &str) -> Result<GuestClient> {
let cid = self.lock().get(id).context("vm not found")?.config.cid;
Ok(guest_api::client::new_client(format!(
"vsock://{cid}:8000/api"
)))
}
fn try_allocate_gpus(&self, manifest: &Manifest) -> Result<GpuConfig> {
if !self.config.cvm.gpu.enabled {
return Ok(GpuConfig::default());
}
Ok(manifest.gpus.clone().unwrap_or_default())