2 releases

0.1.1 Oct 12, 2022
0.1.0 Jul 3, 2022

#1000 in Hardware support

Download history 39/week @ 2024-02-08 4/week @ 2024-02-15 35/week @ 2024-02-22 21/week @ 2024-02-29 10/week @ 2024-03-07 11/week @ 2024-03-14

77 downloads per month

MIT license

10KB
141 lines

mkswap in pure Rust

Initialize a swap device or file in Rust.

For example:

```rust
use std::io::Cursor;

use mkswap::SwapWriter;

let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![0; 40 * 1024]);
let size = SwapWriter::new()
    .label("🔀".into())
    .unwrap()
    .write(&mut buffer)
    .unwrap();


lib.rs:

Create swap files and devices in pure Rust.

This library will construct this header, copied from the Linux kernel:

// Note that this code snippet is licensed GPL-2.0, matching include/linux/swap.h in the Linux Kernel.
union swap_header {
    struct {
        char reserved[PAGE_SIZE - 10];
        char magic[10];                 /* SWAP-SPACE or SWAPSPACE2 */
    } magic;
    struct {
        char            bootbits[1024]; /* Space for disklabel etc. */
        __u32           version;
        __u32           last_page;
        __u32           nr_badpages;
        unsigned char   sws_uuid[16];
        unsigned char   sws_volume[16];
        __u32           padding[117];
        __u32           badpages[1];
    } info;
};
use std::io::Cursor;

use mkswap::SwapWriter;

let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![0; 40 * 1024]);
let size = SwapWriter::new()
    .label("🔀".into())
    .unwrap()
    .write(&mut buffer)
    .unwrap();

Notes

This library will seek around the file, including back to position 0. If this isn't desirable, consider use fscommon::StreamSlice or sending a pull request.

Dependencies

~0.5–1.2MB
~24K SLoC