2 releases

0.1.1 Oct 12, 2022
0.1.0 Jul 3, 2022

#1080 in Hardware support

Download history 5/week @ 2024-08-05 6/week @ 2024-08-12 13/week @ 2024-08-19 65/week @ 2024-08-26 33/week @ 2024-09-02 34/week @ 2024-09-09 13/week @ 2024-09-16 52/week @ 2024-09-23 43/week @ 2024-09-30 40/week @ 2024-10-07 55/week @ 2024-10-14 15/week @ 2024-10-21 101/week @ 2024-10-28 109/week @ 2024-11-04 32/week @ 2024-11-11

258 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.1MB
~22K SLoC