arcbox-agent 0.9.0

Guest agent for ArcBox VMs
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
//! Crash-safe migrate-then-retire state machine for the ext4 metadata volume.
//!
//! The Linux agent (`agent/linux/metadata_volume.rs`) moves the fsync-hot
//! boltdb metadata directories from the btrfs data volume onto a small ext4
//! volume and bind-mounts them back over their canonical paths. This module
//! owns the on-disk state machine — copy, retire, mountpoint stub — and is
//! kept free of mount syscalls so every crash window stays unit-testable on
//! any host. Design: ../company/engineering/arcbox/plans/ext4-metadata-volume.md.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// What a migration entry is on disk.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryKind {
    /// A directory bind-mounted as a whole.
    Dir,
    /// A single file (boltdb) bind-mounted over its canonical path.
    File,
}

/// How [`prepare_entry`] left the volume side.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Prepared {
    /// Existing btrfs-side content was copied onto the volume.
    Migrated,
    /// No source content existed; the volume entry was created empty.
    Fresh,
    /// The volume entry already existed (nothing copied).
    Existing,
}

/// Suffix of the retired btrfs-side source after a successful migration.
///
/// Renaming the source — rather than leaving it shadowed under the bind — is
/// what prevents a later blank metadata volume (deleted image, recreated by
/// the host) from silently re-migrating stale pre-upgrade state against a
/// data store that has moved on. The renamed copy doubles as a manual
/// recovery artifact for the (unsupported) downgrade path.
pub const RETIRED_SUFFIX: &str = ".pre-ext4";

/// Suffix of an in-progress copy on the volume; never bound, discarded and
/// redone on re-entry.
const PARTIAL_SUFFIX: &str = ".partial";

/// Prepares one metadata entry on the volume and retires its btrfs-side
/// source. Idempotent and crash-safe:
///
/// 1. If `<volume_root>/<name>` is absent and a retired source exists, fail.
///    Otherwise, discard any stale `.partial`, then copy the source into
///    `.partial` and atomically rename it into place, or create the entry
///    empty when no source content exists. Propagate inspection errors.
/// 2. If the final entry exists while the source still has content at its
///    canonical path, rename the source to `*.pre-ext4` (also covers a crash
///    between the copy and this retire).
/// 3. Ensure an empty mountpoint stub exists at the canonical path.
///
/// Every interruption re-converges: a torn copy is redone from scratch, a
/// completed copy is never redone, and the retire is retried until it lands.
pub fn prepare_entry(
    volume_root: &Path,
    target: &Path,
    name: &str,
    kind: EntryKind,
) -> io::Result<Prepared> {
    let final_path = volume_root.join(name);
    let partial = volume_root.join(format!("{name}{PARTIAL_SUFFIX}"));

    let prepared = if final_path.try_exists()? {
        Prepared::Existing
    } else {
        if retired_exists(target)? {
            return Err(io::Error::other(format!(
                "metadata entry {} is missing after migration; restore the paired volumes",
                final_path.display()
            )));
        }
        remove_existing(&partial)?;
        if has_content(target, kind)? {
            copy_entry(target, &partial, kind)?;
            fs::rename(&partial, &final_path)?;
            // The publish rename MUST be durable before the retire below:
            // the two renames live on different filesystems (volume vs
            // btrfs source), so without this barrier a crash could persist
            // the retire while losing the publish. The next boot would then
            // require recovery because the metadata entry is missing.
            fs::File::open(volume_root)?.sync_all()?;
            Prepared::Migrated
        } else {
            create_entry(&final_path, kind)?;
            Prepared::Fresh
        }
    };

    if has_content(target, kind)? {
        fs::rename(target, free_retired_path(target)?)?;
    }
    ensure_stub(target, kind)?;
    Ok(prepared)
}

/// Whether the canonical source still carries data worth migrating/retiring:
/// a non-empty directory or a non-empty file.
fn has_content(path: &Path, kind: EntryKind) -> io::Result<bool> {
    let result = match kind {
        EntryKind::Dir => fs::read_dir(path)
            .and_then(|mut dir| dir.next().transpose().map(|entry| entry.is_some())),
        EntryKind::File => fs::metadata(path).map(|meta| meta.len() > 0),
    };
    match result {
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        other => other,
    }
}

/// Verifies populated original metadata before a Btrfs-only upgrade.
///
/// Empty mountpoint stubs do not distinguish a new legacy install from a lost
/// metadata image. Both original database kinds must provide positive evidence.
///
/// # Errors
/// Returns an error for retired sources, missing evidence, or unreadable entries.
pub fn verify_legacy_sources(entries: &[(&Path, EntryKind)]) -> io::Result<()> {
    let mut populated_directory = false;
    let mut populated_file = false;
    for &(path, kind) in entries {
        if retired_exists(path)? {
            return Err(io::Error::other(format!(
                "{} was already migrated; its metadata volume is missing",
                path.display()
            )));
        }
        if has_content(path, kind)? {
            match kind {
                EntryKind::Dir => populated_directory = true,
                EntryKind::File => populated_file = true,
            }
        }
    }
    if !populated_directory || !populated_file {
        return Err(io::Error::other(
            "original metadata cannot be distinguished from empty migrated mountpoints",
        ));
    }
    Ok(())
}

/// Verifies that an older pair completed migration before adopting its mappings.
///
/// Old empty entries left no retired marker. A populated source can therefore
/// be stale data from a later Btrfs-only boot, not proof of an unfinished copy.
///
/// # Errors
/// Returns an error for missing destinations or inspection failures.
pub fn verify_legacy_pair(volume: &Path, entries: &[&str]) -> io::Result<()> {
    for name in entries {
        if !volume.join(name).try_exists()? {
            return Err(io::Error::other(format!(
                "legacy metadata entry {name} is missing; source provenance is unknown; preserve both volumes for explicit recovery"
            )));
        }
    }
    Ok(())
}

fn retired_exists(target: &Path) -> io::Result<bool> {
    let Some(parent) = target.parent() else {
        return Ok(false);
    };
    let retired = path_with_suffix(target, RETIRED_SUFFIX);
    let retired_name = retired
        .file_name()
        .ok_or_else(|| io::Error::other("metadata target has no filename"))?
        .to_string_lossy();
    let entries = match fs::read_dir(parent) {
        Ok(entries) => entries,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),
        Err(error) => return Err(error),
    };
    for entry in entries {
        let name = entry?.file_name();
        let name = name.to_string_lossy();
        if name == retired_name || name.starts_with(&format!("{retired_name}.")) {
            return Ok(true);
        }
    }
    Ok(false)
}

fn remove_existing(path: &Path) -> io::Result<()> {
    match path.symlink_metadata() {
        Ok(meta) if meta.is_dir() => fs::remove_dir_all(path),
        Ok(_) => fs::remove_file(path),
        Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e),
    }
}

fn create_entry(path: &Path, kind: EntryKind) -> io::Result<()> {
    match kind {
        EntryKind::Dir => fs::create_dir_all(path),
        EntryKind::File => {
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(path)
                .map(|_| ())
        }
    }
}

/// Creates the (empty) mountpoint at the canonical path without touching
/// existing content — an existing empty file/dir is reused as the stub.
fn ensure_stub(target: &Path, kind: EntryKind) -> io::Result<()> {
    create_entry(target, kind)
}

fn copy_entry(src: &Path, dst: &Path, kind: EntryKind) -> io::Result<()> {
    match kind {
        EntryKind::Dir => copy_dir_synced(src, dst),
        EntryKind::File => {
            if let Some(parent) = dst.parent() {
                fs::create_dir_all(parent)?;
            }
            copy_file_synced(src, dst)
        }
    }
}

fn copy_dir_synced(src: &Path, dst: &Path) -> io::Result<()> {
    fs::create_dir_all(dst)?;
    fs::set_permissions(dst, fs::metadata(src)?.permissions())?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let to = dst.join(entry.file_name());
        let file_type = entry.file_type()?;
        if file_type.is_dir() {
            copy_dir_synced(&entry.path(), &to)?;
        } else if file_type.is_symlink() {
            std::os::unix::fs::symlink(fs::read_link(entry.path())?, &to)?;
        } else {
            copy_file_synced(&entry.path(), &to)?;
        }
    }
    // Per-file fsync does not contractually persist this directory's
    // entries; sync the dir itself so the published tree can never be
    // durable-but-hollow (replay trusts the final path's existence).
    fs::File::open(dst)?.sync_all()
}

/// `fs::copy` + fsync: the copy must be durable before the rename that
/// publishes it, or a crash could publish a hollow entry.
fn copy_file_synced(src: &Path, dst: &Path) -> io::Result<()> {
    fs::copy(src, dst)?;
    fs::File::open(dst)?.sync_all()
}

/// First free `*.pre-ext4[.N]` sibling for retiring the source. A numbered
/// fallback covers state re-accumulated at the canonical path by boots that
/// ran without the metadata volume.
fn free_retired_path(target: &Path) -> io::Result<PathBuf> {
    let base = path_with_suffix(target, RETIRED_SUFFIX);
    if !base.try_exists()? {
        return Ok(base);
    }
    for n in 1..100u32 {
        let candidate = path_with_suffix(target, &format!("{RETIRED_SUFFIX}.{n}"));
        if !candidate.try_exists()? {
            return Ok(candidate);
        }
    }
    Err(io::Error::other(format!(
        "no free {RETIRED_SUFFIX} backup name next to {}",
        target.display()
    )))
}

fn path_with_suffix(path: &Path, suffix: &str) -> PathBuf {
    let mut os = path.as_os_str().to_owned();
    os.push(suffix);
    PathBuf::from(os)
}

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

    #[test]
    fn older_pair_with_complete_destinations_can_be_adopted() {
        let (_tmp, volume, data) = setup();
        let first = data.join("bolt");
        let second = data.join("metadata.db");
        seed_dir(&first);
        fs::write(&second, b"current snapshot metadata").unwrap();
        prepare_entry(&volume, &first, "bolt", EntryKind::Dir).unwrap();
        prepare_entry(&volume, &second, "snapshot", EntryKind::File).unwrap();
        let entries = ["bolt", "snapshot"];
        verify_legacy_pair(&volume, &entries).unwrap();
        assert_eq!(
            fs::read(volume.join("snapshot")).unwrap(),
            b"current snapshot metadata"
        );
        fs::remove_file(volume.join("snapshot")).unwrap();
        fs::write(&second, b"stale state from a later btrfs-only boot").unwrap();
        assert!(verify_legacy_pair(&volume, &entries).is_err());
        assert!(!volume.join("snapshot").exists());
        assert_eq!(
            fs::read(data.join("metadata.db.pre-ext4")).unwrap(),
            b"current snapshot metadata"
        );
    }

    #[test]
    fn older_pair_does_not_adopt_reaccumulated_sources_without_retired_markers() {
        let (_tmp, volume, data) = setup();
        let target = data.join("metadata.db");
        assert_eq!(
            prepare_entry(&volume, &target, "snapshot", EntryKind::File).unwrap(),
            Prepared::Fresh
        );
        fs::write(volume.join("snapshot"), b"authoritative metadata").unwrap();
        fs::write(&target, b"reaccumulated stale source").unwrap();
        assert!(!retired_exists(&target).unwrap());
        fs::remove_file(volume.join("snapshot")).unwrap();
        assert!(verify_legacy_pair(&volume, &["snapshot"]).is_err());
        assert!(!volume.join("snapshot").exists());
        assert_eq!(fs::read(target).unwrap(), b"reaccumulated stale source");
    }

    #[test]
    fn older_pair_does_not_initialize_entries_without_source_provenance() {
        let (_tmp, volume, data) = setup();
        let target = data.join("metadata.db");
        for source in [
            None,
            Some(b"".as_slice()),
            Some(b"possible original data".as_slice()),
        ] {
            if let Some(bytes) = source {
                fs::write(&target, bytes).unwrap();
            }
            assert!(verify_legacy_pair(&volume, &["snapshot"]).is_err());
            assert!(!volume.join("snapshot").exists());
        }
    }

    #[test]
    fn legacy_upgrade_requires_original_databases_and_rejects_retired_sources() {
        let tmp = tempfile::tempdir().unwrap();
        let directory = tmp.path().join("bolt");
        let database = tmp.path().join("metadata.db");
        fs::create_dir(&directory).unwrap();
        fs::write(directory.join("meta.db"), b"original bolt").unwrap();
        fs::write(&database, b"original snapshot metadata").unwrap();
        let entries = [(&*directory, EntryKind::Dir), (&*database, EntryKind::File)];
        verify_legacy_sources(&entries).unwrap();

        fs::create_dir(tmp.path().join("bolt.pre-ext4")).unwrap();
        assert!(
            verify_legacy_sources(&entries)
                .unwrap_err()
                .to_string()
                .contains("already migrated")
        );
    }

    #[test]
    fn empty_migration_stubs_do_not_authorize_a_new_metadata_volume() {
        let tmp = tempfile::tempdir().unwrap();
        let directory = tmp.path().join("bolt");
        let database = tmp.path().join("metadata.db");
        fs::create_dir(&directory).unwrap();
        fs::write(&database, b"").unwrap();
        assert!(
            verify_legacy_sources(&[(&directory, EntryKind::Dir), (&database, EntryKind::File)])
                .is_err()
        );
    }

    #[test]
    fn source_inspection_errors_do_not_create_empty_metadata() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        std::os::unix::fs::symlink(&target, &target).unwrap();
        let error = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap_err();
        assert_ne!(error.kind(), io::ErrorKind::NotFound);
        assert!(!volume.join("docker-network").exists());
        assert!(verify_legacy_sources(&[(&target, EntryKind::Dir)]).is_err());
    }

    fn setup() -> (tempfile::TempDir, PathBuf, PathBuf) {
        let tmp = tempfile::tempdir().unwrap();
        let volume = tmp.path().join("volume");
        let data = tmp.path().join("data");
        fs::create_dir_all(&volume).unwrap();
        fs::create_dir_all(&data).unwrap();
        (tmp, volume, data)
    }

    fn seed_dir(dir: &Path) {
        fs::create_dir_all(dir.join("sub")).unwrap();
        fs::write(dir.join("meta.db"), b"bolt").unwrap();
        fs::write(dir.join("sub/inner.json"), b"{}").unwrap();
    }

    #[test]
    fn fresh_install_creates_empty_entries() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        assert_eq!(out, Prepared::Fresh);
        assert!(volume.join("docker-network").is_dir());
        assert!(target.is_dir(), "mountpoint stub must exist");

        let file_target = data.join("overlayfs/metadata.db");
        let out = prepare_entry(
            &volume,
            &file_target,
            "snapshotter-metadata.db",
            EntryKind::File,
        )
        .unwrap();
        assert_eq!(out, Prepared::Fresh);
        assert_eq!(
            fs::metadata(volume.join("snapshotter-metadata.db"))
                .unwrap()
                .len(),
            0
        );
        assert_eq!(fs::metadata(&file_target).unwrap().len(), 0);
    }

    #[test]
    fn populated_source_is_migrated_and_retired() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        assert_eq!(out, Prepared::Migrated);
        let migrated = volume.join("docker-network");
        assert_eq!(fs::read(migrated.join("meta.db")).unwrap(), b"bolt");
        assert_eq!(fs::read(migrated.join("sub/inner.json")).unwrap(), b"{}");
        // Source retired, empty stub in its place.
        assert!(data.join("network.pre-ext4").join("meta.db").exists());
        assert!(fs::read_dir(&target).unwrap().next().is_none());
    }

    #[test]
    fn torn_copy_is_discarded_and_redone() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);
        // A crashed previous copy left a partial with garbage.
        fs::create_dir_all(volume.join("docker-network.partial")).unwrap();
        fs::write(volume.join("docker-network.partial/meta.db"), b"torn").unwrap();

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        assert_eq!(out, Prepared::Migrated);
        assert!(!volume.join("docker-network.partial").exists());
        assert_eq!(
            fs::read(volume.join("docker-network/meta.db")).unwrap(),
            b"bolt"
        );
    }

    #[test]
    fn crash_between_copy_and_retire_converges() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);
        // The copy landed (final exists) but the retire never ran.
        fs::create_dir_all(volume.join("docker-network")).unwrap();
        fs::write(volume.join("docker-network/meta.db"), b"bolt").unwrap();

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        // Nothing re-copied, but the retire and stub landed.
        assert_eq!(out, Prepared::Existing);
        assert!(data.join("network.pre-ext4/meta.db").exists());
        assert!(fs::read_dir(&target).unwrap().next().is_none());
    }

    #[test]
    fn replay_after_full_migration_is_a_noop() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);
        prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        assert_eq!(out, Prepared::Existing);
        assert!(
            !data.join("network.pre-ext4.1").exists(),
            "no second backup"
        );
    }

    #[test]
    fn blank_volume_after_retire_requires_recovery() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);
        prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        // The metadata image was deleted and recreated blank: volume side
        // empty, retired backup still on btrfs.
        fs::remove_dir_all(volume.join("docker-network")).unwrap();

        let error = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap_err();

        // The retired backup must NOT be resurrected — that state predates
        // whatever the data store has since moved on to.
        assert!(error.to_string().contains("restore the paired volumes"));
        assert!(!volume.join("docker-network").exists());
        assert_eq!(
            fs::read(data.join("network.pre-ext4/meta.db")).unwrap(),
            b"bolt"
        );
    }

    #[test]
    fn interim_state_retires_under_numbered_backup() {
        let (_tmp, volume, data) = setup();
        let target = data.join("network");
        seed_dir(&target);
        prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        // Boots without the volume re-accumulated state at the canonical path.
        fs::write(target.join("meta.db"), b"interim").unwrap();

        let out = prepare_entry(&volume, &target, "docker-network", EntryKind::Dir).unwrap();

        assert_eq!(out, Prepared::Existing);
        assert_eq!(
            fs::read(data.join("network.pre-ext4.1/meta.db")).unwrap(),
            b"interim"
        );
        assert!(fs::read_dir(&target).unwrap().next().is_none());
    }

    #[test]
    fn file_entry_migrates_and_stubs() {
        let (_tmp, volume, data) = setup();
        let target = data.join("overlayfs/metadata.db");
        fs::create_dir_all(target.parent().unwrap()).unwrap();
        fs::write(&target, b"boltdb-pages").unwrap();
        // A torn file copy is discarded too.
        fs::write(volume.join("snapshotter-metadata.db.partial"), b"torn").unwrap();

        let out =
            prepare_entry(&volume, &target, "snapshotter-metadata.db", EntryKind::File).unwrap();

        assert_eq!(out, Prepared::Migrated);
        assert!(!volume.join("snapshotter-metadata.db.partial").exists());
        assert_eq!(
            fs::read(volume.join("snapshotter-metadata.db")).unwrap(),
            b"boltdb-pages"
        );
        assert_eq!(
            fs::read(data.join("overlayfs/metadata.db.pre-ext4")).unwrap(),
            b"boltdb-pages"
        );
        assert_eq!(fs::metadata(&target).unwrap().len(), 0, "0-byte stub");
    }
}