fstool 0.4.21

Build disk images and filesystems (ext2/3/4, MBR, GPT) from a directory tree and TOML spec, in the spirit of genext2fs.
Documentation
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
//! qcow2 backend validation against real `qemu-img`-produced images.
//! Each test skips silently when `qemu-img` isn't on PATH.

use std::io::Read as _;
use std::process::Command;

use fstool::block::{BlockDevice, Qcow2Backend};
use tempfile::NamedTempFile;

fn which(tool: &str) -> bool {
    Command::new("sh")
        .arg("-c")
        .arg(format!("command -v {tool}"))
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// `qemu-img create -f qcow2 …` produces an empty image whose virtual
/// size and cluster_size we should parse correctly.
#[test]
fn opens_qemu_img_created_image() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let tmp = NamedTempFile::new().unwrap();
    let out = Command::new("qemu-img")
        .args(["create", "-q", "-f", "qcow2"])
        .arg(tmp.path())
        .arg("64M")
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "qemu-img create failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let back = Qcow2Backend::open(tmp.path()).unwrap();
    assert_eq!(back.total_size(), 64 * 1024 * 1024);
    assert_eq!(back.header().cluster_size(), 65536);
    // qemu-img defaults to v3 (`compat=1.1`).
    assert_eq!(back.header().version, 3);
}

/// Read-back invariant: write a pattern into a raw image, convert it to
/// qcow2 via qemu-img, and read it through Qcow2Backend. Bytes must
/// match the original pattern.
#[test]
fn read_back_pattern_via_qemu_img_convert() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }

    // Build a 4 MiB raw image with a known pattern at a few offsets.
    let raw = NamedTempFile::new().unwrap();
    {
        use std::io::Write as _;
        let mut f = std::fs::File::create(raw.path()).unwrap();
        f.set_len(4 * 1024 * 1024).unwrap();
        f.write_all(b"hello qcow2 reader\n").unwrap();
        // Pattern straddling a 64 KiB cluster boundary.
        use std::io::Seek as _;
        use std::io::SeekFrom;
        f.seek(SeekFrom::Start(65500)).unwrap();
        f.write_all(&[0xAB; 200]).unwrap();
        // Pattern in the middle of a cluster.
        f.seek(SeekFrom::Start(2 * 1024 * 1024)).unwrap();
        f.write_all(b"halfway through\n").unwrap();
        f.sync_all().unwrap();
    }

    // Convert raw → qcow2.
    let qcow = NamedTempFile::new().unwrap();
    let out = Command::new("qemu-img")
        .args(["convert", "-f", "raw", "-O", "qcow2"])
        .arg(raw.path())
        .arg(qcow.path())
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "qemu-img convert failed:\n{}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Read through Qcow2Backend; bytes should match what we wrote.
    let mut back = Qcow2Backend::open(qcow.path()).unwrap();
    assert_eq!(back.total_size(), 4 * 1024 * 1024);

    let mut head = [0u8; 32];
    back.read_at(0, &mut head).unwrap();
    assert_eq!(&head[..19], b"hello qcow2 reader\n");

    let mut straddle = [0u8; 200];
    back.read_at(65500, &mut straddle).unwrap();
    assert!(straddle.iter().all(|&b| b == 0xAB));

    let mut mid = [0u8; 16];
    back.read_at(2 * 1024 * 1024, &mut mid).unwrap();
    assert_eq!(&mid, b"halfway through\n");

    // Unallocated tail reads as zeros.
    let mut tail = [0xffu8; 4096];
    back.read_at(3 * 1024 * 1024, &mut tail).unwrap();
    assert!(tail.iter().all(|&b| b == 0), "tail should be zero");

    // Stream the whole thing via Read.
    use std::io::Seek as _;
    use std::io::SeekFrom;
    back.seek(SeekFrom::Start(0)).unwrap();
    let mut all = Vec::new();
    back.read_to_end(&mut all).unwrap();
    assert_eq!(all.len(), 4 * 1024 * 1024);
    assert_eq!(&all[..19], b"hello qcow2 reader\n");
}

/// Build a compressible 4 MiB raw source with a few recognizable regions.
#[cfg(test)]
fn compressible_source() -> (NamedTempFile, Vec<u8>) {
    let mut data = vec![0u8; 4 * 1024 * 1024];
    // Highly compressible text spanning the first cluster.
    let text = b"The quick brown fox jumps over the lazy dog.\n";
    for (i, b) in data.iter_mut().take(90_000).enumerate() {
        *b = text[i % text.len()];
    }
    // A less-compressible region straddling a later cluster boundary.
    for (i, b) in data[2_000_000..2_050_000].iter_mut().enumerate() {
        *b = (i * 7 % 256) as u8;
    }
    let raw = NamedTempFile::new().unwrap();
    std::fs::write(raw.path(), &data).unwrap();
    (raw, data)
}

/// Read back a **zlib**-compressed qcow2 (`qemu-img convert -c`), byte-exact.
#[test]
fn read_back_zlib_compressed() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let (raw, expect) = compressible_source();
    let qcow = NamedTempFile::new().unwrap();
    let out = Command::new("qemu-img")
        .args(["convert", "-f", "raw", "-O", "qcow2", "-c"])
        .arg(raw.path())
        .arg(qcow.path())
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "qemu-img convert -c failed:\n{}",
        String::from_utf8_lossy(&out.stderr)
    );

    let mut back = Qcow2Backend::open(qcow.path()).unwrap();
    assert_eq!(back.total_size(), expect.len() as u64);
    use std::io::Seek as _;
    use std::io::SeekFrom;
    back.seek(SeekFrom::Start(0)).unwrap();
    let mut all = Vec::new();
    back.read_to_end(&mut all).unwrap();
    assert_eq!(all, expect, "zlib-compressed read mismatch");
}

/// Read back a **zstd**-compressed qcow2 (sets the COMPRESSION_TYPE incompat
/// bit), byte-exact.
#[test]
fn read_back_zstd_compressed() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let (raw, expect) = compressible_source();
    let qcow = NamedTempFile::new().unwrap();
    let out = Command::new("qemu-img")
        .args([
            "convert",
            "-f",
            "raw",
            "-O",
            "qcow2",
            "-c",
            "-o",
            "compression_type=zstd",
        ])
        .arg(raw.path())
        .arg(qcow.path())
        .output()
        .unwrap();
    if !out.status.success() {
        // Old qemu without zstd support — skip rather than fail.
        eprintln!(
            "skipping: qemu-img has no zstd compression: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        return;
    }

    let mut back = Qcow2Backend::open(qcow.path()).unwrap();
    assert_eq!(back.header().compression_type, 1, "should be zstd");
    let mut all = Vec::new();
    use std::io::Read as _;
    back.read_to_end(&mut all).unwrap();
    assert_eq!(all, expect, "zstd-compressed read mismatch");
}

/// Writing into a compressed cluster copies it out to a plain cluster
/// (COW): the edit sticks, untouched compressed clusters survive byte-exact,
/// and `qemu-img check` stays clean (refcounts intact).
#[test]
fn cow_write_into_compressed_cluster() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let (raw, mut expect) = compressible_source();
    let qcow = NamedTempFile::new().unwrap();
    let out = Command::new("qemu-img")
        .args(["convert", "-f", "raw", "-O", "qcow2", "-c"])
        .arg(raw.path())
        .arg(qcow.path())
        .output()
        .unwrap();
    assert!(out.status.success(), "convert -c failed");

    // Overwrite a 100-byte window inside the first (compressed) cluster and a
    // window inside the later compressed region at 2 MiB.
    let patch = [0x5Au8; 100];
    {
        let mut back = Qcow2Backend::open(qcow.path()).unwrap();
        back.write_at(10, &patch).unwrap();
        back.write_at(2_000_100, &patch).unwrap();
        back.sync().unwrap();
    }
    expect[10..110].copy_from_slice(&patch);
    expect[2_000_100..2_000_200].copy_from_slice(&patch);

    // qemu-img check: structural + refcount validation.
    let check = Command::new("qemu-img")
        .arg("check")
        .arg(qcow.path())
        .output()
        .unwrap();
    assert!(
        check.status.success(),
        "qemu-img check failed after COW:\n{}\n{}",
        String::from_utf8_lossy(&check.stdout),
        String::from_utf8_lossy(&check.stderr)
    );

    // Reopen and confirm the whole image matches (edits applied, rest intact).
    let mut back = Qcow2Backend::open(qcow.path()).unwrap();
    let mut all = Vec::new();
    use std::io::Read as _;
    back.read_to_end(&mut all).unwrap();
    assert_eq!(all, expect, "post-COW image mismatch");
}

/// Produce a compressed qcow2 with our serializer; qemu-img must validate
/// it (check clean) and decode it back to the original bytes.
fn write_compressed_roundtrip(ctype: u8) {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let (raw, expect) = compressible_source();
    let src_dev = fstool::block::FileBackend::open(raw.path()).unwrap();
    let out = NamedTempFile::new().unwrap();
    let mut src: Box<dyn BlockDevice> = Box::new(src_dev);
    let written = fstool::block::qcow2::compress::write_compressed_image(
        src.as_mut(),
        out.path(),
        65536,
        ctype,
        6,
    )
    .unwrap();
    assert!(
        written < expect.len() as u64,
        "compressed output ({written}) should be smaller than the {} source",
        expect.len()
    );

    // qemu-img check: structural + refcount validation.
    let check = Command::new("qemu-img")
        .arg("check")
        .arg(out.path())
        .output()
        .unwrap();
    assert!(
        check.status.success(),
        "qemu-img check failed:\n{}\n{}",
        String::from_utf8_lossy(&check.stdout),
        String::from_utf8_lossy(&check.stderr)
    );

    // qemu decodes it back to the original raw bytes.
    let back = NamedTempFile::new().unwrap();
    let conv = Command::new("qemu-img")
        .args(["convert", "-O", "raw"])
        .arg(out.path())
        .arg(back.path())
        .output()
        .unwrap();
    assert!(
        conv.status.success(),
        "qemu-img convert -O raw failed:\n{}",
        String::from_utf8_lossy(&conv.stderr)
    );
    let got = std::fs::read(back.path()).unwrap();
    assert_eq!(got, expect, "qemu round-trip mismatch");

    // Our own reader also reads it byte-exact.
    let mut ours = Qcow2Backend::open(out.path()).unwrap();
    let mut all = Vec::new();
    use std::io::Read as _;
    ours.read_to_end(&mut all).unwrap();
    assert_eq!(
        all, expect,
        "our reader mismatch on our own compressed image"
    );
}

#[test]
fn write_compressed_zlib_roundtrip() {
    write_compressed_roundtrip(0);
}

#[test]
fn write_compressed_zstd_roundtrip() {
    write_compressed_roundtrip(1);
}

/// Qcow2Backend::create makes a fresh image that qemu-img validates.
#[test]
fn create_then_qemu_img_check() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let tmp = NamedTempFile::new().unwrap();
    {
        let mut back = Qcow2Backend::create(tmp.path(), 64 * 1024 * 1024, 65536).unwrap();
        // Write a few patterns through the allocator.
        back.write_at(0, b"hello fresh qcow2\n").unwrap();
        back.write_at(1024 * 1024, &[0xCDu8; 128]).unwrap();
        back.write_at(63 * 1024 * 1024, &[0xEFu8; 4096]).unwrap();
        back.sync().unwrap();
    }

    // qemu-img info: parses as a real qcow2 v3 with the expected size.
    let info = Command::new("qemu-img")
        .args(["info", "--output=json"])
        .arg(tmp.path())
        .output()
        .unwrap();
    assert!(
        info.status.success(),
        "qemu-img info failed:\n{}",
        String::from_utf8_lossy(&info.stderr)
    );
    let s = String::from_utf8_lossy(&info.stdout);
    assert!(s.contains("\"virtual-size\": 67108864"), "info:\n{s}");
    assert!(s.contains("\"format\": \"qcow2\""), "info:\n{s}");

    // qemu-img check: structural validation.
    let check = Command::new("qemu-img")
        .arg("check")
        .arg(tmp.path())
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&check.stdout);
    let stderr = String::from_utf8_lossy(&check.stderr);
    assert!(
        check.status.success(),
        "qemu-img check failed:\nstdout:\n{stdout}\nstderr:\n{stderr}"
    );

    // Reopen through our reader and verify the patterns came back.
    let mut back = Qcow2Backend::open(tmp.path()).unwrap();
    let mut head = [0u8; 32];
    back.read_at(0, &mut head).unwrap();
    assert_eq!(&head[..18], b"hello fresh qcow2\n");
    let mut mid = [0u8; 128];
    back.read_at(1024 * 1024, &mut mid).unwrap();
    assert!(mid.iter().all(|&b| b == 0xCD));
    let mut tail = [0u8; 4096];
    back.read_at(63 * 1024 * 1024, &mut tail).unwrap();
    assert!(tail.iter().all(|&b| b == 0xEF));

    // Unallocated cluster reads as zeros.
    let mut zeros = [0xffu8; 1024];
    back.read_at(8 * 1024 * 1024, &mut zeros).unwrap();
    assert!(zeros.iter().all(|&b| b == 0));
}

/// `fstool create -t ext4 src -o out.qcow2` produces a valid qcow2
/// carrying an ext4 image. Verified with qemu-img check + (after
/// convert-to-raw) e2fsck.
#[test]
fn ext_build_into_qcow2() {
    if !which("qemu-img") || !which("e2fsck") {
        eprintln!("skipping: qemu-img or e2fsck missing");
        return;
    }

    let srcdir = tempfile::tempdir().unwrap();
    std::fs::write(srcdir.path().join("hello"), b"in qcow2\n").unwrap();
    std::fs::create_dir(srcdir.path().join("etc")).unwrap();
    std::fs::write(srcdir.path().join("etc/conf"), b"k=v\n").unwrap();

    let dir = tempfile::tempdir().unwrap();
    let out = dir.path().join("disk.qcow2");
    let bin = env!("CARGO_BIN_EXE_fstool");
    let r = Command::new(bin)
        .args(["create", "-t", "ext4"])
        .arg(srcdir.path())
        .arg("-o")
        .arg(&out)
        .output()
        .unwrap();
    assert!(
        r.status.success(),
        "create failed:\n{}",
        String::from_utf8_lossy(&r.stderr)
    );

    // qemu-img check on the qcow2.
    let chk = Command::new("qemu-img")
        .arg("check")
        .arg(&out)
        .output()
        .unwrap();
    assert!(
        chk.status.success(),
        "qemu-img check failed:\n{}",
        String::from_utf8_lossy(&chk.stdout)
    );

    // Convert to raw and e2fsck.
    let raw = dir.path().join("disk.raw");
    let cv = Command::new("qemu-img")
        .args(["convert", "-O", "raw"])
        .arg(&out)
        .arg(&raw)
        .output()
        .unwrap();
    assert!(cv.status.success(), "qemu-img convert failed");
    let fsck = Command::new("e2fsck")
        .arg("-fn")
        .arg(&raw)
        .output()
        .unwrap();
    assert!(
        fsck.status.success(),
        "e2fsck on converted ext4 failed:\n{}",
        String::from_utf8_lossy(&fsck.stdout)
    );

    // fstool's own ls/cat works on the qcow2 directly.
    let ls = Command::new(bin)
        .arg("ls")
        .arg(&out)
        .arg("/")
        .output()
        .unwrap();
    assert!(ls.status.success());
    let s = String::from_utf8_lossy(&ls.stdout);
    assert!(s.contains("hello"));
    assert!(s.contains("etc"));

    let cat = Command::new(bin)
        .arg("cat")
        .arg(&out)
        .arg("/etc/conf")
        .output()
        .unwrap();
    assert!(cat.status.success());
    assert_eq!(cat.stdout, b"k=v\n");
}

/// `fstool build spec -o disk.qcow2` produces a GPT-partitioned qcow2
/// with two filesystems. The partition target syntax (`disk.qcow2:N`)
/// walks each partition cleanly.
#[test]
fn build_partitioned_qcow2() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }

    let srcdir = tempfile::tempdir().unwrap();
    std::fs::write(srcdir.path().join("hello"), b"in partition 2\n").unwrap();

    let dir = tempfile::tempdir().unwrap();
    let spec_path = dir.path().join("spec.toml");
    std::fs::write(
        &spec_path,
        format!(
            r#"
            [image]
            size = "128MiB"
            partition_table = "gpt"

            [[partitions]]
            name = "EFI"
            type = "esp"
            size = "48MiB"

            [partitions.filesystem]
            type = "fat32"
            volume_label = "EFI"

            [[partitions]]
            name = "root"
            type = "linux"
            size = "remaining"

            [partitions.filesystem]
            type = "ext4"
            source = "{}"
            block_size = 1024
            "#,
            srcdir.path().display()
        ),
    )
    .unwrap();

    let out = dir.path().join("disk.qcow2");
    let bin = env!("CARGO_BIN_EXE_fstool");
    let r = Command::new(bin)
        .arg("build")
        .arg(&spec_path)
        .arg("-o")
        .arg(&out)
        .output()
        .unwrap();
    assert!(
        r.status.success(),
        "build failed:\n{}",
        String::from_utf8_lossy(&r.stderr)
    );

    let chk = Command::new("qemu-img")
        .arg("check")
        .arg(&out)
        .output()
        .unwrap();
    assert!(
        chk.status.success(),
        "qemu-img check failed:\n{}",
        String::from_utf8_lossy(&chk.stdout)
    );

    // info on the qcow2 lists the table.
    let info = Command::new(bin).arg("info").arg(&out).output().unwrap();
    assert!(info.status.success());
    let s = String::from_utf8_lossy(&info.stdout);
    assert!(s.contains("partition table:"));
    assert!(s.contains("EFI"));
    assert!(s.contains("root"));

    // :2 walks the ext4 partition.
    let mut p2 = std::ffi::OsString::from(&out);
    p2.push(":2");
    let ls = Command::new(bin)
        .arg("ls")
        .arg(&p2)
        .arg("/")
        .output()
        .unwrap();
    assert!(ls.status.success(), "ls :2 failed");
    let s = String::from_utf8_lossy(&ls.stdout);
    assert!(s.contains("hello"));
}

/// Writing zeros to an unallocated cluster (whether via `write_at` or
/// `zero_range`) must NOT allocate the cluster on disk — the qcow2
/// allocator already treats unmapped clusters as zero, so the backing
/// file should stay small. Regression: previously the ext formatter's
/// `dev.zero_range(0, total_bytes)` upfront in `format_with` allocated
/// every cluster of the virtual image, turning an 8 GiB repacked
/// qcow2 into an 8 GiB file on disk.
#[test]
fn zero_writes_stay_sparse() {
    let tmp = NamedTempFile::new().unwrap();
    let mut back = Qcow2Backend::create(tmp.path(), 1024 * 1024 * 1024, 65536).unwrap();
    // Zero the entire 1 GiB virtual region.
    back.zero_range(0, 1024 * 1024 * 1024).unwrap();
    // A write of all-zero bytes through write_at is the same situation.
    back.write_at(512 * 1024 * 1024, &[0u8; 4096]).unwrap();
    back.sync().unwrap();
    drop(back);
    let on_disk = std::fs::metadata(tmp.path()).unwrap().len();
    // A fresh 1 GiB qcow2 with cluster_size=64 KiB only needs the
    // header + refcount + L1; well under 1 MiB. Allow a generous bound.
    assert!(
        on_disk < 8 * 1024 * 1024,
        "zero writes bloated the file: {on_disk} bytes on disk",
    );
    // The virtual contents must still read back as zero.
    let mut buf = [0xffu8; 4096];
    let mut back = Qcow2Backend::open(tmp.path()).unwrap();
    back.read_at(0, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0));
    back.read_at(512 * 1024 * 1024, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0));
}

/// Writing zeros to an *already allocated* cluster must overwrite the
/// existing data (not silently skip), so a later read returns zero.
#[test]
fn zero_writes_clear_allocated_clusters() {
    let tmp = NamedTempFile::new().unwrap();
    let mut back = Qcow2Backend::create(tmp.path(), 4 * 1024 * 1024, 65536).unwrap();
    // Allocate a cluster by writing a non-zero pattern.
    back.write_at(0, &[0xABu8; 4096]).unwrap();
    // Now write zeros to the same range; the read-back must be zero.
    back.write_at(0, &[0u8; 4096]).unwrap();
    let mut buf = [0xffu8; 4096];
    back.read_at(0, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0), "stale data persisted");

    // Same via zero_range.
    back.write_at(1024 * 1024, &[0xCDu8; 4096]).unwrap();
    back.zero_range(1024 * 1024, 4096).unwrap();
    back.read_at(1024 * 1024, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0), "zero_range left stale data");
}

/// fstool::block::open_image dispatches to Qcow2Backend on qcow2 magic.
#[test]
fn open_image_dispatches_to_qcow2() {
    if !which("qemu-img") {
        eprintln!("skipping: qemu-img not installed");
        return;
    }
    let tmp = NamedTempFile::new().unwrap();
    Command::new("qemu-img")
        .args(["create", "-q", "-f", "qcow2"])
        .arg(tmp.path())
        .arg("32M")
        .output()
        .unwrap();

    let mut dev = fstool::block::open_image(tmp.path()).unwrap();
    assert_eq!(dev.total_size(), 32 * 1024 * 1024);
    // Read returns zeros.
    let mut buf = [0xffu8; 1024];
    dev.read_at(0, &mut buf).unwrap();
    assert!(buf.iter().all(|&b| b == 0));
}