zc2 0.0.49

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
//! The Lima backend of [`super::Runtime`] (M3.2, zc#511), macOS's first
//! choice: one zc-owned Lima VM, `zakuro`, sized to the whole node budget,
//! with containerd + nerdctl inside it. On a Mac every job runs as
//! zc -> Lima VM -> container, so Docker Desktop is not a prerequisite and
//! the VM is the isolation boundary the owner sized.
//!
//! - The VM is Apple Virtualization (`vz`) with Rosetta on Apple Silicon.
//!   cpus / memory / disk come from [`super::Budget::total`]: the VM IS the
//!   budget. Each workload inside still gets its own `--cpus` / `--memory`
//!   through [`docker::run_argv`] (nerdctl takes the Docker flags).
//! - Only the zakuro work directory is mounted, writable, at the same path
//!   in the guest. The owner's home is never mounted, and an instance that
//!   mounts it writable is refused, not adopted.
//! - zc never uses a VM the owner uses for their own containers: the name is
//!   `zakuro` (`ZAKURO_LIMA_INSTANCE` for tests), and `default` is refused.
//! - There is no GPU passthrough to a Linux guest on macOS: a GPU request is
//!   refused with that reason.
//!
//! Every `limactl` call goes through one [`LimaCli`] (argv in, stdout out),
//! so a fake drives each path in tests on a machine with no Lima.

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;

use super::lxc::SandboxSize;
use super::{docker, Budget, Gpus, HostResources, Limits, Probe, Runtime, RuntimeKind, StartSpec};
use super::{State, GIB};

/// The one Lima instance every workload runs in.
pub const INSTANCE: &str = "zakuro";
/// First Lima this was written against: 2.0 (flags `--mount-only`,
/// `--containerd`, `--rosetta`, `--tty=false`, `list --json`). Verified on
/// 2.2.1.
pub const MIN_VERSION: (u32, u32, u32) = (2, 0, 0);
/// What the CLI error starts with when the binary is not there.
pub const NOT_INSTALLED: &str = "limactl is not installed";

const MIB: u64 = 1024 * 1024;

/// The slice of `limactl` a backend needs: run one `limactl …` argv, hand
/// back its trimmed stdout, or its trimmed stderr as an error. `argv[0]` is
/// always `limactl`; the real impl swaps in the resolved binary.
pub trait LimaCli: Send + Sync {
    fn run(&self, argv: &[String]) -> Result<String, String>;
}

/// The real one: shells out.
pub struct LimaShell {
    bin: String,
}

impl LimaShell {
    /// `ZAKURO_LIMACTL`, else `limactl` on `PATH`, else the Homebrew
    /// prefixes (an agent started by launchd has no Homebrew `PATH`).
    pub fn new() -> Self {
        Self {
            bin: resolve_binary(
                "limactl",
                "ZAKURO_LIMACTL",
                &|k| std::env::var(k).ok(),
                &|p| p.is_file(),
            ),
        }
    }
}

/// The `limactl` this host runs (see [`LimaShell::new`]).
pub fn binary() -> String {
    LimaShell::new().bin
}

impl Default for LimaShell {
    fn default() -> Self {
        Self::new()
    }
}

impl LimaCli for LimaShell {
    fn run(&self, argv: &[String]) -> Result<String, String> {
        run_cli(&self.bin, "limactl", NOT_INSTALLED, argv)
    }
}

/// Shared by the Lima and Colima shells: run `argv[1..]` on `bin`.
pub(super) fn run_cli(
    bin: &str,
    name: &str,
    not_installed: &str,
    argv: &[String],
) -> Result<String, String> {
    let rest = argv
        .get(1..)
        .ok_or_else(|| format!("empty {name} command"))?;
    let out = Command::new(bin).args(rest).output().map_err(|e| {
        if e.kind() == std::io::ErrorKind::NotFound {
            format!("{not_installed} ({e})")
        } else {
            format!("{name} failed to run: {e}")
        }
    })?;
    if out.status.success() {
        Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
    } else {
        let err = String::from_utf8_lossy(&out.stderr).trim().to_string();
        Err(if err.is_empty() {
            String::from_utf8_lossy(&out.stdout).trim().to_string()
        } else {
            err
        })
    }
}

/// Where the binary `name` is: the override variable, then `PATH`, then the
/// Homebrew prefixes; the bare name (so the error is "not installed") when
/// none exists.
pub(super) fn resolve_binary(
    name: &str,
    var: &str,
    get: &dyn Fn(&str) -> Option<String>,
    is_file: &dyn Fn(&Path) -> bool,
) -> String {
    if let Some(v) = get(var)
        .map(|v| v.trim().to_string())
        .filter(|v| !v.is_empty())
    {
        return v;
    }
    let path = get("PATH").unwrap_or_default();
    let dirs = path
        .split(':')
        .filter(|d| !d.is_empty())
        .chain(["/opt/homebrew/bin", "/usr/local/bin"]);
    for d in dirs {
        let p = Path::new(d).join(name);
        if is_file(&p) {
            return p.to_string_lossy().into_owned();
        }
    }
    name.to_string()
}

pub(super) fn s(v: &[&str]) -> Vec<String> {
    v.iter().map(|a| a.to_string()).collect()
}

/// What the host contributes: platform facts and the two directories the VM
/// cares about. Injected so Linux tests cover the macOS logic.
#[derive(Debug, Clone)]
pub struct Host {
    pub macos: bool,
    pub apple_silicon: bool,
    /// The owner's home: never mounted into the VM.
    pub home: Option<PathBuf>,
    /// The zakuro state / work directory: the one writable mount.
    pub work_dir: Option<PathBuf>,
}

impl Host {
    pub fn detect() -> Self {
        Self {
            macos: cfg!(target_os = "macos"),
            apple_silicon: cfg!(all(target_os = "macos", target_arch = "aarch64")),
            home: std::env::var_os("HOME").map(PathBuf::from),
            work_dir: crate::credentials::dir(),
        }
    }
}

/// `2`, `1.5`, `0.25`: a GiB count for `--memory` (no trailing zeros).
pub(super) fn gib_flag(bytes: u64) -> String {
    let g = (bytes as f64 / GIB * 100.0).round() / 100.0;
    let t = format!("{g:.2}");
    t.trim_end_matches('0').trim_end_matches('.').to_string()
}

/// Whole cores the VM gets: a fractional budget rounds up (the fractional
/// part is enforced per workload).
pub(super) fn vm_cpus(l: &Limits) -> u32 {
    l.cpu_millis.div_ceil(1000).max(1)
}

/// Whole GiB of disk the VM gets.
pub(super) fn vm_disk_gib(size: &SandboxSize) -> u64 {
    size.disk_bytes.div_ceil(1024 * 1024 * 1024).max(1)
}

/// `limactl create …` for a new zc-owned instance.
pub fn create_argv(instance: &str, size: &SandboxSize, host: &Host) -> Vec<String> {
    let mut a = s(&[
        "limactl",
        "create",
        "--tty=false",
        "--name",
        instance,
        "--vm-type",
        "vz",
    ]);
    if host.apple_silicon {
        a.push("--rosetta".into());
    }
    a.extend(s(&["--cpus", &vm_cpus(&size.limits).to_string()]));
    a.extend(s(&["--memory", &gib_flag(size.limits.memory_bytes)]));
    a.extend(s(&["--disk", &vm_disk_gib(size).to_string()]));
    // System (rootful) containerd: nerdctl runs as root in the guest, which
    // gives workloads the same cgroup-v2 limits as on the LXC backend.
    a.extend(s(&["--containerd", "system"]));
    // Only the work directory, writable. `--mount-only` replaces the
    // template's own mounts (the owner's home).
    a.extend(s(&["--mount-type", "virtiofs"]));
    if let Some(w) = &host.work_dir {
        a.push("--mount-only".into());
        a.push(format!("{}:w", w.display()));
    } else {
        a.push("--mount-none".into());
    }
    a
}

/// `limactl edit …`: bring a stopped instance to the budget's size. The disk
/// only ever grows.
pub fn edit_argv(instance: &str, size: &SandboxSize, grow_disk: bool) -> Vec<String> {
    let mut a = s(&["limactl", "edit", "--tty=false"]);
    a.extend(s(&["--cpus", &vm_cpus(&size.limits).to_string()]));
    a.extend(s(&["--memory", &gib_flag(size.limits.memory_bytes)]));
    if grow_disk {
        a.extend(s(&["--disk", &vm_disk_gib(size).to_string()]));
    }
    a.push(instance.to_string());
    a
}

/// One instance as `limactl list --json` reports it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Info {
    pub name: String,
    pub status: String,
    pub vm_type: String,
    pub cpus: u32,
    pub memory_bytes: u64,
    pub disk_bytes: u64,
    /// `(location, writable)` of every mount.
    pub mounts: Vec<(String, bool)>,
}

/// Parse `limactl list --json`: one JSON object per line (a JSON array is
/// accepted too). Lines that are not instances are skipped.
pub fn parse_list(out: &str) -> Vec<Info> {
    let one = |v: &serde_json::Value| -> Option<Info> {
        let mounts = v["config"]["mounts"]
            .as_array()
            .map(|m| {
                m.iter()
                    .filter_map(|m| {
                        Some((
                            m["location"].as_str()?.to_string(),
                            m["writable"].as_bool().unwrap_or(false),
                        ))
                    })
                    .collect()
            })
            .unwrap_or_default();
        Some(Info {
            name: v["name"].as_str()?.to_string(),
            status: v["status"].as_str().unwrap_or("").to_string(),
            vm_type: v["vmType"].as_str().unwrap_or("").to_string(),
            cpus: v["cpus"].as_u64().unwrap_or(0) as u32,
            memory_bytes: v["memory"].as_u64().unwrap_or(0),
            disk_bytes: v["disk"].as_u64().unwrap_or(0),
            mounts,
        })
    };
    let t = out.trim();
    if t.starts_with('[') {
        if let Ok(serde_json::Value::Array(a)) = serde_json::from_str(t) {
            return a.iter().filter_map(one).collect();
        }
    }
    t.lines()
        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l.trim()).ok())
        .filter_map(|v| one(&v))
        .collect()
}

/// The first writable mount that gives the guest the owner's home (the home
/// itself, or a directory above it). Read-only mounts are not this check's
/// business.
pub(super) fn writable_home_mount(mounts: &[(String, bool)], home: &Path) -> Option<String> {
    mounts
        .iter()
        .find(|(loc, w)| *w && home.starts_with(loc.trim_end_matches('/').max("/")))
        .map(|(loc, _)| loc.clone())
}

/// `(major, minor, patch)` from `limactl version 2.2.1` /
/// `limactl version 2.0.0-beta.1`.
pub fn parse_version(out: &str) -> Option<(u32, u32, u32)> {
    let v = out.split_whitespace().last()?.trim_start_matches('v');
    let core = v.split(['-', '+']).next()?;
    let mut p = core.split('.').map(|n| n.parse::<u32>());
    Some((
        p.next()?.ok()?,
        p.next()?.ok()?,
        p.next().unwrap_or(Ok(0)).ok()?,
    ))
}

/// What differs between an existing VM and the budget.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Drift {
    pub cpu_or_memory: bool,
    pub disk_grows: bool,
}

impl Drift {
    pub fn any(&self) -> bool {
        self.cpu_or_memory || self.disk_grows
    }
}

/// Compare a VM's size with the budget's. The disk never shrinks, so a larger
/// one is not drift.
pub(super) fn drift(cpus: u32, memory_bytes: u64, disk_bytes: u64, size: &SandboxSize) -> Drift {
    Drift {
        cpu_or_memory: cpus != vm_cpus(&size.limits)
            || memory_bytes / MIB != size.limits.memory_bytes / MIB,
        disk_grows: disk_bytes < vm_disk_gib(size) * 1024 * MIB,
    }
}

/// Map each mount's host directory to its path in the guest. Lima mounts the
/// work directory at the same path, so a mount is allowed only inside it; a
/// path outside would silently be an empty directory in the VM.
pub(super) fn map_mounts(
    work_dir: Option<&Path>,
    mounts: &[(String, String)],
) -> Result<Vec<(String, String)>, String> {
    mounts
        .iter()
        .map(|(host, ctr)| {
            let w = work_dir.ok_or("no zakuro work directory to share into the VM")?;
            let p = Path::new(host);
            let escapes = p
                .components()
                .any(|c| matches!(c, std::path::Component::ParentDir));
            if escapes || !p.starts_with(w) {
                return Err(format!(
                    "mount {host:?} is outside the zakuro work directory {}: the VM \
                     shares only that directory (never the home)",
                    w.display()
                ));
            }
            Ok((host.clone(), ctr.clone()))
        })
        .collect()
}

/// A GPU request cannot be honoured: no GPU reaches a Linux guest on macOS.
pub(super) fn refuse_gpu(backend: &str, spec: &StartSpec) -> Result<(), String> {
    if spec.limits.gpus.flag_value().is_some() {
        return Err(format!(
            "{} asks for a GPU, which the {backend} backend cannot give: there is no GPU \
             passthrough to a Linux VM on macOS",
            spec.name
        ));
    }
    Ok(())
}

/// Validate the publishes. The guest publishes them and the VM layer forwards
/// the same guest port to the host, so a fixed port is needed (an ephemeral
/// one would be unknown on the host) and only tcp / udp exist.
pub(super) fn check_publish(backend: &str, publish: &[String]) -> Result<(), String> {
    for spec in publish {
        let p = super::lxc::parse_publish(spec)
            .map_err(|e| e.replace("the lxc backend", &format!("the {backend} backend")))?;
        if p.host.is_none() {
            return Err(format!(
                "publish {spec:?}: the {backend} backend needs a fixed host port"
            ));
        }
    }
    Ok(())
}

/// The guest-side script that keeps containers off the Mac and its LAN
/// (zc#511 item 4): the host gateway, private ranges and link-local are
/// rejected for traffic coming from the nerdctl bridges, the internet and DNS
/// stay reachable, and traffic inside the container range is left alone.
/// The hook is CNI's own `CNI-ADMIN` chain, which CNI's FORWARD jumps to
/// before it accepts a container's traffic (a plain FORWARD rule ends up
/// behind CNI's accept once the first container starts; verified for real).
/// Idempotent: the chain is flushed and refilled at every start, because the
/// rules do not survive a guest reboot.
///
/// `$1` is the container CIDR (nerdctl's default pool).
pub fn egress_script() -> String {
    "set -e; C=${1:-10.4.0.0/16}; \
     iptables -N ZAKURO-EGRESS 2>/dev/null || true; iptables -F ZAKURO-EGRESS; \
     iptables -A ZAKURO-EGRESS -m conntrack --ctstate ESTABLISHED,RELATED -j RETURN; \
     for ns in $(awk '/^nameserver/{print $2}' /etc/resolv.conf /run/systemd/resolve/resolv.conf 2>/dev/null | grep -E '^[0-9.]+$' | sort -u); do \
       iptables -A ZAKURO-EGRESS -d $ns -p udp --dport 53 -j RETURN; \
       iptables -A ZAKURO-EGRESS -d $ns -p tcp --dport 53 -j RETURN; \
     done; \
     iptables -A ZAKURO-EGRESS -d $C -j RETURN; \
     for r in 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 169.254.0.0/16 100.64.0.0/10; do \
       iptables -A ZAKURO-EGRESS -d $r -j REJECT; \
     done; \
     iptables -N CNI-ADMIN 2>/dev/null || true; \
     iptables -C CNI-ADMIN -s $C -j ZAKURO-EGRESS 2>/dev/null || iptables -A CNI-ADMIN -s $C -j ZAKURO-EGRESS; \
     iptables -C FORWARD -s $C -j ZAKURO-EGRESS 2>/dev/null || iptables -I FORWARD 1 -s $C -j ZAKURO-EGRESS; \
     iptables -N ZAKURO-IN 2>/dev/null || true; iptables -F ZAKURO-IN; \
     for ns in $(awk '/^nameserver/{print $2}' /etc/resolv.conf /run/systemd/resolve/resolv.conf 2>/dev/null | grep -E '^[0-9.]+$' | sort -u); do \
       iptables -A ZAKURO-IN -d $ns -p udp --dport 53 -j RETURN; \
       iptables -A ZAKURO-IN -d $ns -p tcp --dport 53 -j RETURN; \
     done; \
     iptables -A ZAKURO-IN -d $C -j RETURN; \
     iptables -A ZAKURO-IN -m addrtype --dst-type LOCAL -j REJECT; \
     iptables -C INPUT -s $C -j ZAKURO-IN 2>/dev/null || iptables -I INPUT 1 -s $C -j ZAKURO-IN"
        .to_string()
}

/// Serialises `ensure` in this process: two first starts must not both
/// create the instance.
static ENSURE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// A name zc may own: not empty, not the `default` instance of Lima (the one
/// an owner's own `limactl start` makes).
pub(super) fn owned_name(name: &str, what: &str) -> Result<(), String> {
    if name.trim().is_empty() || name == "default" {
        return Err(format!(
            "refusing to use the {what} {name:?}: zc owns its own VM and never uses one the \
             owner uses (set ZAKURO_LIMA_INSTANCE / ZAKURO_COLIMA_PROFILE to another name)"
        ));
    }
    Ok(())
}

/// Lima as a [`Runtime`].
#[derive(Clone)]
pub struct LimaRuntime {
    cli: Arc<dyn LimaCli>,
    instance: String,
    size: Result<SandboxSize, String>,
    host: Host,
    /// Creates the work directory the VM mounts (the real filesystem;
    /// a no-op in unit tests, which must not write outside a temp dir).
    mkdir: fn(&std::path::Path) -> std::io::Result<()>,
}

impl LimaRuntime {
    pub fn new(
        cli: Arc<dyn LimaCli>,
        instance: &str,
        size: Result<SandboxSize, String>,
        host: Host,
    ) -> Self {
        Self {
            cli,
            instance: instance.to_string(),
            size,
            host,
            mkdir: |p| std::fs::create_dir_all(p),
        }
    }

    /// Replace the directory creator (tests).
    #[cfg(test)]
    pub(crate) fn with_mkdir(mut self, f: fn(&std::path::Path) -> std::io::Result<()>) -> Self {
        self.mkdir = f;
        self
    }

    /// The real CLI, sized from the node budget in `get` (the environment).
    pub fn from_env(cli: Arc<dyn LimaCli>, get: &dyn Fn(&str) -> Option<String>) -> Self {
        let instance = get("ZAKURO_LIMA_INSTANCE")
            .map(|v| v.trim().to_string())
            .filter(|v| !v.is_empty())
            .unwrap_or_else(|| INSTANCE.to_string());
        let size = Budget::from_env(get)
            .and_then(|b| SandboxSize::from_budget(&b, &HostResources::detect()));
        Self::new(cli, &instance, size, Host::detect())
    }

    pub fn shell() -> Self {
        Self::from_env(Arc::new(LimaShell::new()), &|k| std::env::var(k).ok())
    }

    /// The zc-owned instance's name.
    pub fn instance(&self) -> &str {
        &self.instance
    }

    fn lima(&self, rest: &[&str]) -> Result<String, String> {
        let mut argv = vec!["limactl".to_string()];
        argv.extend(rest.iter().map(|a| a.to_string()));
        self.cli.run(&argv)
    }

    /// A Docker-shaped argv (`docker …`) run through nerdctl, as root, inside
    /// the VM. `--workdir /` so Lima does not try to enter the host's cwd.
    pub fn in_vm(&self, docker_argv: &[String]) -> Vec<String> {
        let mut a = s(&["limactl", "shell", "--workdir", "/", &self.instance, "--"]);
        a.extend(s(&["sudo", "nerdctl"]));
        let rest = match docker_argv.first().map(String::as_str) {
            Some("docker") => &docker_argv[1..],
            _ => docker_argv,
        };
        a.extend(rest.iter().cloned());
        a
    }

    fn nerdctl(&self, docker_argv: &[String]) -> Result<String, String> {
        self.cli.run(&self.in_vm(docker_argv))
    }

    /// `Some("linux/amd64")` when `image` is in the VM with no build for its
    /// own architecture: an amd64-only image (the published compute image)
    /// on an Apple-silicon Mac. nerdctl, unlike Docker, will not run a local
    /// image of a foreign platform unless told to: it goes looking for a
    /// native one in the registry instead, and fails ("pull access denied"
    /// for a local-only tag). `image inspect` prints an empty architecture
    /// for such an image. Not present, or a native one: `None`, and `run`
    /// resolves the image as usual.
    fn emulated_platform(&self, image: &str) -> Option<&'static str> {
        if !self.host.apple_silicon {
            return None;
        }
        let arch = self
            .nerdctl(&s(&[
                "docker",
                "image",
                "inspect",
                "--format",
                "{{.Architecture}}",
                image,
            ]))
            .ok()?;
        arch.trim()
            .is_empty()
            .then_some(crate::up::EMULATED_PLATFORM)
    }

    /// The instance, `None` when it does not exist. Matches by exact name.
    pub fn info(&self) -> Result<Option<Info>, String> {
        let out = self.lima(&["list", "--json"])?;
        Ok(parse_list(&out)
            .into_iter()
            .find(|i| i.name == self.instance))
    }

    fn running(&self, info: &Info) -> bool {
        info.status.eq_ignore_ascii_case("Running")
    }

    /// Why this instance cannot be adopted, if it cannot.
    fn adoption_problem(&self, info: &Info) -> Option<String> {
        if !info.vm_type.is_empty() && info.vm_type != "vz" {
            return Some(format!(
                "{} is a {} VM, not Apple Virtualization (vz); delete it \
                 (`limactl delete {}`) and zc recreates it",
                self.instance, info.vm_type, self.instance
            ));
        }
        let home = self.host.home.as_deref()?;
        writable_home_mount(&info.mounts, home).map(|m| {
            format!(
                "{} mounts {m} writable, which gives the VM the owner's home; refusing to \
                 adopt it (delete it with `limactl delete {}` and zc recreates it with only \
                 the zakuro work directory)",
                self.instance, self.instance
            )
        })
    }

    /// Whether a resize is waiting on a running VM (workloads in it).
    pub fn resize_pending(&self) -> Result<bool, String> {
        let size = self.size.clone()?;
        Ok(match self.info()? {
            Some(i) => drift(i.cpus, i.memory_bytes, i.disk_bytes, &size).any(),
            None => false,
        })
    }

    /// Create the VM, or bring an existing one to the current budget, start
    /// it, and make sure the egress rules and nerdctl are in place. Refuses an
    /// instance that mounts the owner's home writable.
    pub fn ensure(&self) -> Result<(), String> {
        let _g = ENSURE_LOCK.lock().unwrap_or_else(|p| p.into_inner());
        owned_name(&self.instance, "Lima instance")?;
        if !self.host.macos {
            return Err("the lima backend runs on macOS only".into());
        }
        let size = self.size.clone()?;
        let inst = self.instance.as_str();
        match self.info()? {
            None => {
                if let Some(w) = &self.host.work_dir {
                    (self.mkdir)(w).map_err(|e| format!("cannot create {}: {e}", w.display()))?;
                }
                self.cli
                    .run(&create_argv(inst, &size, &self.host))
                    .map_err(|e| format!("limactl create {inst} failed: {e}"))?;
                self.start_vm()?;
            }
            Some(info) => {
                if let Some(p) = self.adoption_problem(&info) {
                    return Err(p);
                }
                let d = drift(info.cpus, info.memory_bytes, info.disk_bytes, &size);
                if self.running(&info) {
                    if d.any() && self.is_idle()? {
                        // Nothing runs in it: stop, resize, start again.
                        self.lima(&["stop", inst])?;
                        self.cli.run(&edit_argv(inst, &size, d.disk_grows))?;
                        self.start_vm()?;
                    }
                    // else: workloads run; the resize waits for the next
                    // idle start ([`Self::resize_pending`] says so).
                } else if info.status.eq_ignore_ascii_case("Stopped") {
                    if d.any() {
                        self.cli.run(&edit_argv(inst, &size, d.disk_grows))?;
                    }
                    self.start_vm()?;
                } else {
                    return Err(format!(
                        "{inst} is {} (not Running or Stopped); `limactl delete -f {inst}` and \
                         zc recreates it",
                        info.status
                    ));
                }
            }
        }
        // Rules first, so a container never starts without them.
        self.cli
            .run(&self.in_vm_root(&["sh", "-c", &egress_script(), "zakuro-egress"]))
            .map_err(|e| format!("applying the guest firewall in {inst} failed: {e}"))?;
        self.cli
            .run(&self.in_vm_root(&["nerdctl", "version"]))
            .map_err(|e| format!("containerd/nerdctl is not ready in {inst}: {e}"))?;
        Ok(())
    }

    /// `limactl shell … -- sudo <argv>`.
    fn in_vm_root(&self, argv: &[&str]) -> Vec<String> {
        let mut a = s(&[
            "limactl",
            "shell",
            "--workdir",
            "/",
            &self.instance,
            "--",
            "sudo",
        ]);
        a.extend(argv.iter().map(|x| x.to_string()));
        a
    }

    fn start_vm(&self) -> Result<(), String> {
        self.lima(&["start", "--tty=false", "--timeout", "15m", &self.instance])
            .map(|_| ())
            .map_err(|e| format!("limactl start {} failed: {e}", self.instance))
    }

    fn is_idle(&self) -> Result<bool, String> {
        Ok(self.nerdctl(&s(&["ps", "-q"]))?.trim().is_empty())
    }
}

impl Runtime for LimaRuntime {
    fn kind(&self) -> RuntimeKind {
        RuntimeKind::Lima
    }

    fn probe(&self) -> Probe {
        let no = |detail: String| Probe {
            kind: RuntimeKind::Lima,
            available: false,
            detail,
        };
        if !self.host.macos {
            return no("macOS only".into());
        }
        if let Err(e) = owned_name(&self.instance, "Lima instance") {
            return no(e);
        }
        let out = match self.lima(&["--version"]) {
            Ok(v) => v,
            Err(e) if e.starts_with(NOT_INSTALLED) => {
                return no(format!(
                    "{NOT_INSTALLED} (brew install lima, or set ZAKURO_LIMACTL)"
                ))
            }
            Err(e) => {
                return no(e
                    .lines()
                    .next()
                    .unwrap_or("limactl --version failed")
                    .into())
            }
        };
        let Some(v) = parse_version(&out) else {
            return no(format!("cannot read the limactl version from {out:?}"));
        };
        if v < MIN_VERSION {
            return no(format!(
                "limactl {}.{}.{} is too old: zc needs {}.{}.{} or newer (brew upgrade lima)",
                v.0, v.1, v.2, MIN_VERSION.0, MIN_VERSION.1, MIN_VERSION.2
            ));
        }
        let vm = match self.info() {
            Err(e) => return no(e.lines().next().unwrap_or("limactl list failed").into()),
            Ok(None) => "not created yet (created at the first start)".to_string(),
            Ok(Some(i)) => {
                if let Some(p) = self.adoption_problem(&i) {
                    return no(p);
                }
                format!(
                    "{} ({} cpu, {:.1} GiB memory)",
                    i.status.to_lowercase(),
                    i.cpus,
                    i.memory_bytes as f64 / GIB
                )
            }
        };
        Probe {
            kind: RuntimeKind::Lima,
            available: true,
            detail: format!(
                "limactl {}.{}.{}, vz VM {} {vm}",
                v.0, v.1, v.2, self.instance
            ),
        }
    }

    fn verify_image(&self, image: &str, expected_digest: Option<&str>) -> Result<(), String> {
        let out = self
            .nerdctl(&s(&[
                "docker",
                "image",
                "inspect",
                "--format",
                "{{.Id}}|{{json .RepoDigests}}",
                image,
            ]))
            .map_err(|e| format!("image {image} is not in {}: {e}", self.instance))?;
        let Some(want) = expected_digest else {
            return Ok(());
        };
        let want = want.rsplit('@').next().unwrap_or(want);
        if out.contains(want) {
            Ok(())
        } else {
            Err(format!(
                "image {image} does not match the expected digest {want}"
            ))
        }
    }

    fn start(&self, spec: &StartSpec) -> Result<String, String> {
        refuse_gpu("lima", spec)?;
        check_publish("lima", &spec.publish)?;
        let mounts = map_mounts(self.host.work_dir.as_deref(), &spec.mounts)?;
        self.ensure()?;
        let mut inner = spec.clone();
        inner.mounts = mounts;
        // The workload's own limits can never exceed the VM's (the budget).
        let size = self.size.clone()?;
        inner.limits = Limits {
            cpu_millis: inner.limits.cpu_millis.min(size.limits.cpu_millis),
            memory_bytes: inner.limits.memory_bytes.min(size.limits.memory_bytes),
            gpus: Gpus::None,
        };
        // nerdctl refuses `--rm` together with `-d` (Docker accepts it): the
        // exited container stays until `stop` removes it, and only running
        // containers count against the budget.
        inner.remove_on_exit = false;
        if spec.create_network {
            if let Some(net) = &spec.network {
                // Idempotent, as in the Docker backend.
                let _ = self.nerdctl(&s(&["docker", "network", "create", net]));
            }
        }
        let mut run = vec!["docker".to_string()];
        run.extend(docker::run_argv(&inner));
        if let Some(p) = self.emulated_platform(&spec.image) {
            // Right after `run`: every flag must come before the image.
            run.splice(2..2, s(&["--platform", p]));
        }
        self.nerdctl(&run)
    }

    fn exec(&self, name: &str, argv: &[String], detach: bool) -> Result<String, String> {
        let mut full = s(&["docker", "exec"]);
        if detach {
            full.push("-d".into());
        }
        full.push(name.to_string());
        full.extend(argv.iter().cloned());
        self.nerdctl(&full)
    }

    fn stop(&self, name: &str) -> Result<(), String> {
        match self.info()? {
            Some(i) if self.running(&i) => {}
            // Nothing runs in a stopped or missing VM, and `limactl shell`
            // would start nothing useful.
            _ => return Ok(()),
        }
        match self.nerdctl(&crate::up::docker_rm_argv(name)) {
            Ok(_) => Ok(()),
            Err(e) if e.contains("no such container") || e.contains("no such object") => Ok(()),
            Err(e) => Err(e),
        }
    }

    fn limits(&self, name: &str) -> Result<Limits, String> {
        // nerdctl's inspect has no HostConfig: read the workload's cgroup.
        super::lxc::parse_cgroup_limits(&self.nerdctl(&s(&[
            "docker",
            "exec",
            name,
            "sh",
            "-c",
            "cat /sys/fs/cgroup/cpu.max /sys/fs/cgroup/memory.max; \
             ls -d /dev/nvidia[0-9]* 2>/dev/null | wc -l",
        ]))?)
    }

    fn health(&self, name: &str) -> Result<State, String> {
        let out = self.nerdctl(&crate::up::docker_state_argv(name))?;
        crate::up::parse_container_state(&out)
            .ok_or_else(|| format!("unexpected nerdctl inspect output: {out:?}"))
    }

    fn allocations(&self) -> Result<Vec<super::Allocation>, String> {
        match self.info()? {
            Some(i) if self.running(&i) => {}
            _ => return Ok(Vec::new()),
        }
        let mut argv = vec!["docker".to_string()];
        argv.extend(docker::allocations_args());
        Ok(docker::parse_allocations(&self.nerdctl(&argv)?))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    /// Records every argv; answers by the first `needle` the joined argv
    /// contains, else `Ok("")`.
    #[derive(Default)]
    struct Rec {
        calls: Mutex<Vec<String>>,
        answer: Mutex<Vec<(&'static str, Result<String, String>)>>,
    }

    impl Rec {
        fn on(&self, needle: &'static str, a: Result<&str, &str>) {
            self.answer
                .lock()
                .unwrap()
                .push((needle, a.map(String::from).map_err(String::from)));
        }
        fn calls(&self) -> Vec<String> {
            self.calls.lock().unwrap().clone()
        }
    }

    impl LimaCli for Rec {
        fn run(&self, argv: &[String]) -> Result<String, String> {
            let j = argv.join(" ");
            self.calls.lock().unwrap().push(j.clone());
            for (needle, a) in self.answer.lock().unwrap().iter() {
                if j.contains(needle) {
                    return a.clone();
                }
            }
            Ok(String::new())
        }
    }

    fn size() -> SandboxSize {
        SandboxSize {
            limits: Limits::new(1.5, 2.0, Gpus::None),
            disk_bytes: 10 * 1024 * MIB,
        }
    }

    fn host() -> Host {
        Host {
            macos: true,
            apple_silicon: true,
            home: Some("/Users/jo".into()),
            work_dir: Some("/Users/jo/.zakuro".into()),
        }
    }

    fn rt(rec: &Arc<Rec>) -> LimaRuntime {
        LimaRuntime::new(rec.clone(), INSTANCE, Ok(size()), host()).with_mkdir(|_| Ok(()))
    }

    fn spec() -> StartSpec {
        StartSpec {
            name: "zc-share-w0".into(),
            image: "img".into(),
            network: Some("net".into()),
            create_network: true,
            labels: vec![],
            mounts: vec![("/Users/jo/.zakuro/work".into(), "/work".into())],
            publish: vec!["127.0.0.1:3960:3960".into()],
            env: vec![],
            cmd: vec![],
            limits: Limits::new(0.5, 1.0, Gpus::None),
            remove_on_exit: true,
            hardened: false,
        }
    }

    fn list_line(status: &str, cpus: u32, mem_gib: u64, disk_gib: u64, mounts: &str) -> String {
        format!(
            r#"{{"name":"zakuro","status":"{status}","vmType":"vz","cpus":{cpus},"memory":{},"disk":{},"config":{{"mounts":{mounts}}}}}"#,
            mem_gib * 1024 * MIB,
            disk_gib * 1024 * MIB
        )
    }

    const WORK_MOUNT: &str = r#"[{"location":"/Users/jo/.zakuro","writable":true}]"#;

    #[test]
    fn create_argv_sizes_the_vm_from_the_budget_and_mounts_only_the_work_dir() {
        let a = create_argv(INSTANCE, &size(), &host()).join(" ");
        assert_eq!(
            a,
            "limactl create --tty=false --name zakuro --vm-type vz --rosetta --cpus 2 \
             --memory 2 --disk 10 --containerd system --mount-type virtiofs \
             --mount-only /Users/jo/.zakuro:w"
        );
        let mut intel = host();
        intel.apple_silicon = false;
        assert!(!create_argv(INSTANCE, &size(), &intel).contains(&"--rosetta".to_string()));
        let mut none = host();
        none.work_dir = None;
        let a = create_argv(INSTANCE, &size(), &none);
        assert!(a.contains(&"--mount-none".to_string()), "{a:?}");
        assert!(!a.iter().any(|x| x.contains("/Users/jo")), "{a:?}");
    }

    #[test]
    fn edit_argv_grows_the_disk_only_when_asked() {
        assert_eq!(
            edit_argv(INSTANCE, &size(), false).join(" "),
            "limactl edit --tty=false --cpus 2 --memory 2 zakuro"
        );
        assert!(edit_argv(INSTANCE, &size(), true)
            .join(" ")
            .contains("--disk 10 zakuro"));
    }

    #[test]
    fn gib_and_version_parsing() {
        assert_eq!(gib_flag(2 * 1024 * MIB), "2");
        assert_eq!(gib_flag(1536 * MIB), "1.5");
        assert_eq!(gib_flag(256 * MIB), "0.25");
        assert_eq!(parse_version("limactl version 2.2.1"), Some((2, 2, 1)));
        assert_eq!(
            parse_version("limactl version 2.0.0-beta.1"),
            Some((2, 0, 0))
        );
        assert_eq!(parse_version("limactl version v1.2"), Some((1, 2, 0)));
        assert_eq!(parse_version("nonsense"), None);
    }

    #[test]
    fn list_output_is_parsed_from_ndjson_and_arrays() {
        let one = list_line("Running", 2, 2, 10, WORK_MOUNT);
        let nd = format!("{{\"name\":\"other\",\"status\":\"Stopped\"}}\n{one}\n");
        let got = parse_list(&nd);
        assert_eq!(got.len(), 2);
        assert_eq!(got[1].cpus, 2);
        assert_eq!(got[1].mounts, vec![("/Users/jo/.zakuro".to_string(), true)]);
        assert_eq!(parse_list(&format!("[{one}]")).len(), 1);
        assert!(parse_list("").is_empty());
    }

    #[test]
    fn binary_resolution_prefers_the_override_then_path_then_homebrew() {
        let get = |k: &str| match k {
            "ZAKURO_LIMACTL" => Some(" /x/limactl ".to_string()),
            "PATH" => Some("/usr/bin:/bin".to_string()),
            _ => None,
        };
        assert_eq!(
            resolve_binary("limactl", "ZAKURO_LIMACTL", &get, &|_| false),
            "/x/limactl"
        );
        let get = |k: &str| (k == "PATH").then(|| "/usr/bin:/bin".to_string());
        assert_eq!(
            resolve_binary("limactl", "ZAKURO_LIMACTL", &get, &|p| p
                == Path::new("/opt/homebrew/bin/limactl")),
            "/opt/homebrew/bin/limactl"
        );
        assert_eq!(
            resolve_binary("limactl", "ZAKURO_LIMACTL", &get, &|_| false),
            "limactl"
        );
    }

    #[test]
    fn a_missing_instance_is_created_started_and_firewalled() {
        let d = Arc::new(Rec::default());
        rt(&d).ensure().unwrap();
        let calls = d.calls();
        assert_eq!(calls[0], "limactl list --json");
        assert!(calls[1].starts_with("limactl create --tty=false --name zakuro --vm-type vz"));
        assert_eq!(calls[2], "limactl start --tty=false --timeout 15m zakuro");
        assert!(
            calls[3].starts_with("limactl shell --workdir / zakuro -- sudo sh -c set -e;"),
            "{}",
            calls[3]
        );
        assert!(calls[3].contains("ZAKURO-EGRESS"), "{}", calls[3]);
        assert_eq!(
            calls[4],
            "limactl shell --workdir / zakuro -- sudo nerdctl version"
        );
    }

    #[test]
    fn an_existing_matching_instance_is_adopted_untouched() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        rt(&d).ensure().unwrap();
        let calls = d.calls();
        assert!(
            !calls.iter().any(|c| c.contains("create")
                || c.contains("edit")
                || c.contains("limactl stop")
                || c.contains("limactl start")),
            "{calls:#?}"
        );
    }

    #[test]
    fn a_stopped_instance_is_started_and_resized_first_when_the_budget_changed() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Stopped", 1, 1, 5, WORK_MOUNT)),
        );
        rt(&d).ensure().unwrap();
        let calls = d.calls();
        assert_eq!(
            calls[1],
            "limactl edit --tty=false --cpus 2 --memory 2 --disk 10 zakuro"
        );
        assert_eq!(calls[2], "limactl start --tty=false --timeout 15m zakuro");
    }

    #[test]
    fn a_running_idle_instance_is_stopped_resized_and_restarted() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 4, 8, 10, WORK_MOUNT)),
        );
        d.on("nerdctl ps -q", Ok(""));
        rt(&d).ensure().unwrap();
        let calls = d.calls();
        let at = |p: &str| calls.iter().position(|c| c.starts_with(p)).unwrap();
        assert!(at("limactl stop zakuro") < at("limactl edit"));
        assert!(at("limactl edit") < at("limactl start"));
        // The disk is already big enough: it is not touched (never shrinks).
        assert!(!calls[at("limactl edit")].contains("--disk"));
    }

    #[test]
    fn a_busy_instance_is_not_resized_and_says_a_resize_is_pending() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 4, 8, 10, WORK_MOUNT)),
        );
        d.on("nerdctl ps -q", Ok("abc123"));
        let r = rt(&d);
        r.ensure().unwrap();
        assert!(
            !d.calls().iter().any(|c| c.contains("limactl stop")),
            "{:#?}",
            d.calls()
        );
        assert!(r.resize_pending().unwrap());
    }

    #[test]
    fn an_instance_mounting_the_home_writable_is_refused() {
        for loc in ["/Users/jo", "/Users", "/"] {
            let d = Arc::new(Rec::default());
            let m = format!(r#"[{{"location":"{loc}","writable":true}}]"#);
            d.on("list --json", Ok(&list_line("Running", 2, 2, 10, &m)));
            let e = rt(&d).ensure().unwrap_err();
            assert!(e.contains("owner's home"), "{loc}: {e}");
            assert!(
                !d.calls().iter().any(|c| c.contains("shell")),
                "nothing runs in a refused VM"
            );
            let p = rt(&d).probe();
            assert!(!p.available, "{loc}: {}", p.detail);
        }
        // A read-only home mount is not what the rule forbids.
        let d = Arc::new(Rec::default());
        let m = r#"[{"location":"/Users/jo","writable":false}]"#;
        d.on("list --json", Ok(&list_line("Running", 2, 2, 10, m)));
        assert!(rt(&d).ensure().is_ok());
    }

    #[test]
    fn a_non_vz_instance_is_refused() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(r#"{"name":"zakuro","status":"Running","vmType":"qemu"}"#),
        );
        assert!(rt(&d)
            .ensure()
            .unwrap_err()
            .contains("not Apple Virtualization"));
    }

    #[test]
    fn the_owners_default_instance_is_never_used() {
        let d = Arc::new(Rec::default());
        let r = LimaRuntime::new(d.clone(), "default", Ok(size()), host());
        assert!(r.ensure().unwrap_err().contains("never uses one the owner"));
        assert!(!r.probe().available);
        assert!(d.calls().iter().all(|c| !c.contains("create")));
    }

    #[test]
    fn a_gpu_request_is_refused_before_anything_runs() {
        let d = Arc::new(Rec::default());
        let mut sp = spec();
        sp.limits = Limits::new(0.5, 1.0, Gpus::All);
        let e = rt(&d).start(&sp).unwrap_err();
        assert!(e.contains("no GPU passthrough"), "{e}");
        assert!(d.calls().is_empty(), "{:?}", d.calls());
    }

    #[test]
    fn start_runs_nerdctl_in_the_vm_with_the_workloads_own_limits() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        d.on("image inspect", Ok("arm64"));
        d.on("nerdctl run", Ok("cid42"));
        let id = rt(&d).start(&spec()).unwrap();
        assert_eq!(id, "cid42");
        let calls = d.calls();
        let run = calls.iter().find(|c| c.contains("nerdctl run")).unwrap();
        assert!(
            run.starts_with(
                "limactl shell --workdir / zakuro -- sudo nerdctl run -d --name zc-share-w0"
            ),
            "{run}"
        );
        // nerdctl refuses --rm with -d.
        assert!(!run.contains("--rm"), "{run}");
        assert!(
            run.contains("--cpus 0.5 --memory 1024m --memory-swap 1024m"),
            "{run}"
        );
        assert!(run.contains("-v /Users/jo/.zakuro/work:/work"), "{run}");
        assert!(run.contains("-p 127.0.0.1:3960:3960"), "{run}");
        assert!(calls
            .iter()
            .any(|c| c.contains("nerdctl network create net")));
    }

    #[test]
    fn an_amd64_only_image_runs_as_linux_amd64_on_apple_silicon() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        // nerdctl prints no architecture for an image with no native build.
        d.on("image inspect", Ok("\n"));
        rt(&d).start(&spec()).unwrap();
        let run = d
            .calls()
            .into_iter()
            .find(|c| c.contains("nerdctl run"))
            .unwrap();
        assert!(
            run.contains("sudo nerdctl run --platform linux/amd64 -d --name zc-share-w0"),
            "{run}"
        );
        assert!(run.ends_with(" img"), "{run}");
    }

    #[test]
    fn a_native_missing_or_non_apple_silicon_image_gets_no_platform() {
        for (arch, apple) in [
            (Ok("arm64"), true),
            (Err("no such image"), true),
            (Ok(""), false),
        ] {
            let d = Arc::new(Rec::default());
            d.on(
                "list --json",
                Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
            );
            d.on("image inspect", arch);
            let mut h = host();
            h.apple_silicon = apple;
            LimaRuntime::new(d.clone(), INSTANCE, Ok(size()), h)
                .with_mkdir(|_| Ok(()))
                .start(&spec())
                .unwrap();
            let run = d
                .calls()
                .into_iter()
                .find(|c| c.contains("nerdctl run"))
                .unwrap();
            assert!(!run.contains("--platform"), "{arch:?} {apple}: {run}");
        }
    }

    #[test]
    fn a_workload_is_clamped_to_the_vm_and_mounts_outside_the_work_dir_are_refused() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        let mut sp = spec();
        sp.limits = Limits::new(8.0, 64.0, Gpus::None);
        rt(&d).start(&sp).unwrap();
        let run = d
            .calls()
            .into_iter()
            .find(|c| c.contains("nerdctl run"))
            .unwrap();
        assert!(run.contains("--cpus 1.5 --memory 2048m"), "{run}");

        for bad in ["/Users/jo", "/Users/jo/.zakuro/../.ssh", "/etc"] {
            let d = Arc::new(Rec::default());
            let mut sp = spec();
            sp.mounts = vec![(bad.into(), "/x".into())];
            let e = rt(&d).start(&sp).unwrap_err();
            assert!(
                e.contains("outside the zakuro work directory"),
                "{bad}: {e}"
            );
            assert!(d.calls().is_empty());
        }
    }

    #[test]
    fn publish_needs_a_fixed_host_port_and_tcp_or_udp() {
        let d = Arc::new(Rec::default());
        let mut sp = spec();
        sp.publish = vec!["127.0.0.1::8888".into()];
        assert!(rt(&d).start(&sp).unwrap_err().contains("fixed host port"));
        sp.publish = vec!["3960:3960/sctp".into()];
        assert!(rt(&d).start(&sp).unwrap_err().contains("lima backend"));
    }

    #[test]
    fn limits_are_read_back_from_the_workloads_cgroup() {
        let d = Arc::new(Rec::default());
        d.on("exec zc-share-w0 sh -c", Ok("50000 100000\n1073741824\n0"));
        let l = rt(&d).limits("zc-share-w0").unwrap();
        assert_eq!(l.cpu_millis, 500);
        assert_eq!(l.memory_bytes, 1073741824);
        assert_eq!(l.gpus, Gpus::None);
        let d = Arc::new(Rec::default());
        d.on("exec", Ok("max 100000\n1073741824\n0"));
        assert!(rt(&d)
            .limits("w")
            .unwrap_err()
            .contains("without a cpu limit"));
    }

    #[test]
    fn stop_and_allocations_skip_a_vm_that_is_not_running() {
        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Stopped", 2, 2, 10, WORK_MOUNT)),
        );
        rt(&d).stop("w").unwrap();
        assert!(rt(&d).allocations().unwrap().is_empty());
        assert!(!d.calls().iter().any(|c| c.contains("shell")));

        let d = Arc::new(Rec::default());
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        d.on("rm -f", Err("Error: no such container: w"));
        rt(&d).stop("w").unwrap();
        d.on("nerdctl ps --filter", Ok("w|500m,1073741824"));
        let a = rt(&d).allocations().unwrap();
        assert_eq!(a.len(), 1);
        assert_eq!(a[0].cpu_millis, 500);
    }

    #[test]
    fn verify_image_checks_the_exact_digest_in_the_vm() {
        let d = Arc::new(Rec::default());
        d.on("image inspect", Ok(r#"sha256:aaa|["img@sha256:bbb"]"#));
        let r = rt(&d);
        assert!(r.verify_image("img", None).is_ok());
        assert!(r.verify_image("img", Some("img@sha256:bbb")).is_ok());
        assert!(r
            .verify_image("img", Some("sha256:ccc"))
            .unwrap_err()
            .contains("does not match"));
    }

    #[test]
    fn the_probe_says_precisely_why_it_is_unavailable() {
        let d = Arc::new(Rec::default());
        d.on("--version", Err("limactl is not installed (No such file)"));
        let p = rt(&d).probe();
        assert!(!p.available);
        assert!(p.detail.contains("brew install lima"), "{}", p.detail);

        let d = Arc::new(Rec::default());
        d.on("--version", Ok("limactl version 1.2.0"));
        assert!(rt(&d).probe().detail.contains("too old"));

        let mut h = host();
        h.macos = false;
        let r = LimaRuntime::new(Arc::new(Rec::default()), INSTANCE, Ok(size()), h);
        assert_eq!(r.probe().detail, "macOS only");

        let d = Arc::new(Rec::default());
        d.on("--version", Ok("limactl version 2.2.1"));
        let p = rt(&d).probe();
        assert!(p.available, "{}", p.detail);
        assert!(p.detail.contains("not created yet"), "{}", p.detail);
        d.on(
            "list --json",
            Ok(&list_line("Running", 2, 2, 10, WORK_MOUNT)),
        );
        let p = rt(&d).probe();
        assert!(
            p.detail.contains("running (2 cpu, 2.0 GiB memory)"),
            "{}",
            p.detail
        );
    }

    #[test]
    fn the_egress_script_blocks_the_lan_and_keeps_dns_and_the_internet() {
        let s = egress_script();
        for r in [
            "10.0.0.0/8",
            "172.16.0.0/12",
            "192.168.0.0/16",
            "169.254.0.0/16",
        ] {
            assert!(s.contains(r), "{r}");
        }
        // DNS and the container range are allowed before the rejects.
        let dns = s.find("--dport 53").unwrap();
        let ctr = s.find("-d $C -j RETURN").unwrap();
        let rej = s.find("-j REJECT").unwrap();
        assert!(dns < rej && ctr < rej);
        assert!(s.contains("ESTABLISHED,RELATED"));
        assert!(s.contains("-A CNI-ADMIN -s $C -j ZAKURO-EGRESS"));
        assert!(s.contains("iptables -F ZAKURO-EGRESS"), "idempotent refill");
    }

    /// Deletes the throwaway instance, on success or failure.
    struct Cleanup(String);
    impl Drop for Cleanup {
        fn drop(&mut self) {
            let _ = Command::new(LimaShell::new().bin.clone())
                .args(["delete", "-f", &self.0])
                .output();
            eprintln!("[real-lima] deleted instance {}", self.0);
        }
    }

    /// The real thing on this Mac: a throwaway 2 cpu / 2 GiB / 10 GiB vz VM,
    /// a tiny container with limits, the limits read back, the guest egress
    /// rules, then everything deleted. `ZAKURO_TEST_REAL_LIMA=1` and
    /// `--ignored`; never touches `zakuro` or `default`.
    #[test]
    #[ignore]
    fn real_lima_vm_runs_a_limited_container_and_blocks_the_host() {
        if std::env::var("ZAKURO_TEST_REAL_LIMA").as_deref() != Ok("1") {
            return;
        }
        let name = format!("zctest-{}", std::process::id());
        assert!(name != "zakuro" && name != "default");
        let cli: Arc<dyn LimaCli> = Arc::new(LimaShell::new());
        let before = cli.run(&s(&[
            "limactl",
            "list",
            "--format",
            "{{.Name}} {{.Status}}",
        ]));
        eprintln!("[real-lima] existing instances (untouched): {before:?}");
        let work = std::fs::canonicalize(std::env::temp_dir())
            .unwrap()
            .join(format!("{name}-work"));
        std::fs::create_dir_all(work.join("w")).unwrap();
        let _cleanup = Cleanup(name.clone());
        let mut h = Host::detect();
        h.work_dir = Some(work.clone());
        let size = SandboxSize {
            limits: Limits::new(2.0, 2.0, Gpus::None),
            disk_bytes: 10 * 1024 * MIB,
        };
        let r = LimaRuntime::new(cli.clone(), &name, Ok(size), h);
        eprintln!("[real-lima] probe before: {:?}", r.probe());
        assert!(r.probe().available);

        let spec = StartSpec {
            name: "zctest-w0".into(),
            image: "alpine:3".into(),
            network: None,
            create_network: false,
            labels: vec![],
            mounts: vec![(work.join("w").display().to_string(), "/work".into())],
            publish: vec![],
            env: vec![],
            cmd: s(&["sleep", "600"]),
            limits: Limits::new(0.5, 0.25, Gpus::None),
            remove_on_exit: false,
            hardened: true,
        };
        let id = r.start(&spec).expect("start");
        eprintln!("[real-lima] started container {id}");
        eprintln!("[real-lima] probe after: {:?}", r.probe());

        let l = r.limits("zctest-w0").expect("limits");
        eprintln!("[real-lima] limits read back from inside: {l}");
        assert_eq!(l.cpu_millis, 500);
        assert_eq!(l.memory_bytes, 256 * MIB);
        assert!(r.health("zctest-w0").unwrap().running);
        assert!(r.verify_image("alpine:3", None).is_ok());
        assert_eq!(r.allocations().unwrap().len(), 1);

        // Argv with spaces and shell characters survives `limactl shell`.
        let out = r
            .exec(
                "zctest-w0",
                &s(&["sh", "-c", "echo 'a b'; echo $((1+2)); echo hi > /work/f"]),
                false,
            )
            .unwrap();
        eprintln!("[real-lima] exec output: {out:?}");
        assert_eq!(out, "a b\n3");
        assert_eq!(
            std::fs::read_to_string(work.join("w/f")).unwrap().trim(),
            "hi"
        );

        // The guest reaches a listener on the Mac; the container does not.
        let l = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
        let port = l.local_addr().unwrap().port().to_string();
        let guest = |cmd: &str| {
            cli.run(&s(&[
                "limactl",
                "shell",
                "--workdir",
                "/",
                &name,
                "--",
                "sh",
                "-c",
                cmd,
            ]))
        };
        let gw = guest("getent hosts host.lima.internal | cut -d' ' -f1").unwrap();
        eprintln!("[real-lima] host gateway in the guest: {gw}");
        eprintln!(
            "[real-lima] guest rules: {:?}",
            cli.run(&s(&[
                "limactl",
                "shell",
                "--workdir",
                "/",
                &name,
                "--",
                "sudo",
                "sh",
                "-c",
                "iptables -S FORWARD; iptables -S ZAKURO-EGRESS; ip -4 addr | grep inet"
            ]))
        );
        let base = guest(&format!(
            "timeout 3 bash -c 'echo > /dev/tcp/{gw}/{port}' && echo reachable"
        ));
        eprintln!("[real-lima] guest -> mac listener (baseline): {base:?}");
        let ctr = r.exec(
            "zctest-w0",
            &s(&[
                "sh",
                "-c",
                &format!("nc -z -w 3 {gw} {port} && echo reachable || echo blocked"),
            ]),
            false,
        );
        eprintln!("[real-lima] container -> mac listener: {ctr:?}");
        let net = r.exec(
            "zctest-w0",
            &s(&[
                "sh",
                "-c",
                "nc -z -w 5 1.1.1.1 443 && echo internet-ok || echo internet-blocked",
            ]),
            false,
        );
        eprintln!("[real-lima] container -> 1.1.1.1:443: {net:?}");
        let dns = r.exec(
            "zctest-w0",
            &s(&[
                "sh",
                "-c",
                "nslookup example.com >/dev/null 2>&1 && echo dns-ok || echo dns-failed",
            ]),
            false,
        );
        eprintln!("[real-lima] container dns: {dns:?}");
        drop(l);
        assert_eq!(
            base.as_deref(),
            Ok("reachable"),
            "baseline: guest must reach the Mac"
        );
        assert_eq!(ctr.as_deref(), Ok("blocked"));
        assert_eq!(net.as_deref(), Ok("internet-ok"));
        assert_eq!(dns.as_deref(), Ok("dns-ok"));

        r.stop("zctest-w0").unwrap();
        assert!(r.health("zctest-w0").is_err());
        eprintln!(
            "[real-lima] stopped; allocations now {:?}",
            r.allocations().unwrap()
        );
    }
}