2 releases

0.1.1 Oct 12, 2022
0.1.0 Jul 3, 2022

#1101 in Hardware support

Download history 2/week @ 2024-08-01 6/week @ 2024-08-08 12/week @ 2024-08-15 40/week @ 2024-08-22 56/week @ 2024-08-29 22/week @ 2024-09-05 25/week @ 2024-09-12 29/week @ 2024-09-19 46/week @ 2024-09-26 55/week @ 2024-10-03 51/week @ 2024-10-10 20/week @ 2024-10-17 48/week @ 2024-10-24 107/week @ 2024-10-31 74/week @ 2024-11-07

253 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
~22K SLoC