File: connect-command.rs

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (38 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download | duplicates (2)
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
//! This example shows how to run an NBD server
//! (nbdkit) as a subprocess of libnbd.

fn main() -> libnbd::Result<()> {
    // Create the libnbd handle.
    let handle = libnbd::Handle::new()?;

    // Run nbdkit as a subprocess.
    let args = [
        "nbdkit",
        // You must use ‘-s’ (which tells nbdkit to serve
        // a single connection on stdin/stdout).
        "-s",
        // It is recommended to use ‘--exit-with-parent’
        // to ensure nbdkit is always cleaned up even
        // if the main program crashes.
        "--exit-with-parent",
        // Use this to enable nbdkit debugging.
        "-v",
        // The nbdkit plugin name - this is a RAM disk.
        "memory",
        "size=1M",
    ];
    handle.connect_command(&args)?;

    // Write some random data to the first sector.
    let wbuf: Vec<u8> = (0..512).into_iter().map(|i| (i % 13) as u8).collect();
    handle.pwrite(&wbuf, 0, None)?;

    // Read the first sector back.
    let mut rbuf = [0; 512];
    handle.pread(&mut rbuf, 0, None)?;

    // What was read must be exactly the same as what was written.
    assert_eq!(wbuf.as_slice(), rbuf.as_slice());

    Ok(())
}