├── .cargo └── config.toml ├── .github ├── renovate.json └── workflows │ └── ci.yaml ├── .gitignore ├── .rustfmt.toml ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── src └── bin │ ├── wd_copier.rs │ └── writedisk.rs └── xtask ├── Cargo.toml └── src ├── main.rs ├── pseudo_sudo └── vmtest.rs /.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [alias] 2 | xtask = "run --package xtask --" 3 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:base", 5 | "schedule:monthly" 6 | ], 7 | "lockFileMaintenance": { 8 | "enabled": true, 9 | "automerge": true 10 | }, 11 | "rangeStrategy": "update-lockfile" 12 | } 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | pull_request: 6 | schedule: 7 | - cron: '0 0 * * 0' 8 | 9 | name: CI 10 | 11 | jobs: 12 | check: 13 | name: Check 14 | runs-on: ubuntu-24.04 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: Swatinem/rust-cache@v2 18 | - run: cargo check --workspace 19 | 20 | test: 21 | name: Test Suite 22 | runs-on: ubuntu-24.04 23 | steps: 24 | - uses: actions/checkout@v4 25 | - uses: Swatinem/rust-cache@v2 26 | - run: cargo test --workspace 27 | 28 | fmt: 29 | name: Rustfmt 30 | runs-on: ubuntu-24.04 31 | steps: 32 | - uses: actions/checkout@v4 33 | - run: cargo fmt --all -- --check 34 | 35 | clippy: 36 | name: Clippy 37 | runs-on: ubuntu-24.04 38 | steps: 39 | - uses: actions/checkout@v4 40 | - uses: Swatinem/rust-cache@v2 41 | - run: cargo clippy --workspace -- -D warnings 42 | 43 | vmtest: 44 | name: VM test 45 | runs-on: ubuntu-24.04 46 | steps: 47 | - uses: actions/checkout@v4 48 | - run: | 49 | sudo apt-get update 50 | sudo apt-get install qemu-system-x86 51 | - run: rustup target add x86_64-unknown-linux-musl 52 | - uses: Swatinem/rust-cache@v2 53 | - run: cargo xtask vmtest --disable-kvm 54 | env: 55 | RUST_BACKTRACE: 1 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | 3 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 80 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "1.1.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "anstream" 16 | version = "0.6.19" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" 19 | dependencies = [ 20 | "anstyle", 21 | "anstyle-parse", 22 | "anstyle-query", 23 | "anstyle-wincon", 24 | "colorchoice", 25 | "is_terminal_polyfill", 26 | "utf8parse", 27 | ] 28 | 29 | [[package]] 30 | name = "anstyle" 31 | version = "1.0.11" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" 34 | 35 | [[package]] 36 | name = "anstyle-parse" 37 | version = "0.2.7" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" 40 | dependencies = [ 41 | "utf8parse", 42 | ] 43 | 44 | [[package]] 45 | name = "anstyle-query" 46 | version = "1.1.3" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" 49 | dependencies = [ 50 | "windows-sys", 51 | ] 52 | 53 | [[package]] 54 | name = "anstyle-wincon" 55 | version = "3.0.9" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" 58 | dependencies = [ 59 | "anstyle", 60 | "once_cell_polyfill", 61 | "windows-sys", 62 | ] 63 | 64 | [[package]] 65 | name = "anyhow" 66 | version = "1.0.98" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" 69 | 70 | [[package]] 71 | name = "argh" 72 | version = "0.1.13" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | checksum = "34ff18325c8a36b82f992e533ece1ec9f9a9db446bd1c14d4f936bac88fcd240" 75 | dependencies = [ 76 | "argh_derive", 77 | "argh_shared", 78 | "rust-fuzzy-search", 79 | ] 80 | 81 | [[package]] 82 | name = "argh_derive" 83 | version = "0.1.13" 84 | source = "registry+https://github.com/rust-lang/crates.io-index" 85 | checksum = "adb7b2b83a50d329d5d8ccc620f5c7064028828538bdf5646acd60dc1f767803" 86 | dependencies = [ 87 | "argh_shared", 88 | "proc-macro2", 89 | "quote", 90 | "syn", 91 | ] 92 | 93 | [[package]] 94 | name = "argh_shared" 95 | version = "0.1.13" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "a464143cc82dedcdc3928737445362466b7674b5db4e2eb8e869846d6d84f4f6" 98 | dependencies = [ 99 | "serde", 100 | ] 101 | 102 | [[package]] 103 | name = "autocfg" 104 | version = "1.4.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 107 | 108 | [[package]] 109 | name = "bitflags" 110 | version = "2.9.1" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" 113 | 114 | [[package]] 115 | name = "camino" 116 | version = "1.1.10" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "0da45bc31171d8d6960122e222a67740df867c1dd53b4d51caa297084c185cab" 119 | 120 | [[package]] 121 | name = "cfg-if" 122 | version = "1.0.0" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 125 | 126 | [[package]] 127 | name = "cfg_aliases" 128 | version = "0.2.1" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" 131 | 132 | [[package]] 133 | name = "clap" 134 | version = "4.5.39" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "fd60e63e9be68e5fb56422e397cf9baddded06dae1d2e523401542383bc72a9f" 137 | dependencies = [ 138 | "clap_builder", 139 | "clap_derive", 140 | ] 141 | 142 | [[package]] 143 | name = "clap_builder" 144 | version = "4.5.39" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "89cc6392a1f72bbeb820d71f32108f61fdaf18bc526e1d23954168a67759ef51" 147 | dependencies = [ 148 | "anstream", 149 | "anstyle", 150 | "clap_lex", 151 | "strsim", 152 | ] 153 | 154 | [[package]] 155 | name = "clap_derive" 156 | version = "4.5.32" 157 | source = "registry+https://github.com/rust-lang/crates.io-index" 158 | checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" 159 | dependencies = [ 160 | "heck", 161 | "proc-macro2", 162 | "quote", 163 | "syn", 164 | ] 165 | 166 | [[package]] 167 | name = "clap_lex" 168 | version = "0.7.4" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" 171 | 172 | [[package]] 173 | name = "colorchoice" 174 | version = "1.0.4" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" 177 | 178 | [[package]] 179 | name = "comma" 180 | version = "1.0.0" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "55b672471b4e9f9e95499ea597ff64941a309b2cdbffcc46f2cc5e2d971fd335" 183 | 184 | [[package]] 185 | name = "command-run" 186 | version = "1.1.2" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "e2b24ecd5c3263edf426b0244b128fc9e7e703353dbea9b49a40b9a225382cab" 189 | dependencies = [ 190 | "log", 191 | "os_pipe", 192 | ] 193 | 194 | [[package]] 195 | name = "errno" 196 | version = "0.3.12" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" 199 | dependencies = [ 200 | "libc", 201 | "windows-sys", 202 | ] 203 | 204 | [[package]] 205 | name = "fastrand" 206 | version = "2.3.0" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" 209 | 210 | [[package]] 211 | name = "fs-err" 212 | version = "3.1.1" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | checksum = "88d7be93788013f265201256d58f04936a8079ad5dc898743aa20525f503b683" 215 | dependencies = [ 216 | "autocfg", 217 | ] 218 | 219 | [[package]] 220 | name = "getrandom" 221 | version = "0.3.3" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" 224 | dependencies = [ 225 | "cfg-if", 226 | "libc", 227 | "r-efi", 228 | "wasi", 229 | ] 230 | 231 | [[package]] 232 | name = "heck" 233 | version = "0.5.0" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 236 | 237 | [[package]] 238 | name = "hex" 239 | version = "0.4.3" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" 242 | 243 | [[package]] 244 | name = "is_terminal_polyfill" 245 | version = "1.70.1" 246 | source = "registry+https://github.com/rust-lang/crates.io-index" 247 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" 248 | 249 | [[package]] 250 | name = "libc" 251 | version = "0.2.172" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" 254 | 255 | [[package]] 256 | name = "linux-raw-sys" 257 | version = "0.4.15" 258 | source = "registry+https://github.com/rust-lang/crates.io-index" 259 | checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" 260 | 261 | [[package]] 262 | name = "linux-raw-sys" 263 | version = "0.9.4" 264 | source = "registry+https://github.com/rust-lang/crates.io-index" 265 | checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" 266 | 267 | [[package]] 268 | name = "log" 269 | version = "0.4.27" 270 | source = "registry+https://github.com/rust-lang/crates.io-index" 271 | checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" 272 | 273 | [[package]] 274 | name = "memchr" 275 | version = "2.7.4" 276 | source = "registry+https://github.com/rust-lang/crates.io-index" 277 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 278 | 279 | [[package]] 280 | name = "nix" 281 | version = "0.30.1" 282 | source = "registry+https://github.com/rust-lang/crates.io-index" 283 | checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" 284 | dependencies = [ 285 | "bitflags", 286 | "cfg-if", 287 | "cfg_aliases", 288 | "libc", 289 | ] 290 | 291 | [[package]] 292 | name = "once_cell" 293 | version = "1.21.3" 294 | source = "registry+https://github.com/rust-lang/crates.io-index" 295 | checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" 296 | 297 | [[package]] 298 | name = "once_cell_polyfill" 299 | version = "1.70.1" 300 | source = "registry+https://github.com/rust-lang/crates.io-index" 301 | checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" 302 | 303 | [[package]] 304 | name = "os_pipe" 305 | version = "1.2.2" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "db335f4760b14ead6290116f2427bf33a14d4f0617d49f78a246de10c1831224" 308 | dependencies = [ 309 | "libc", 310 | "windows-sys", 311 | ] 312 | 313 | [[package]] 314 | name = "proc-macro2" 315 | version = "1.0.95" 316 | source = "registry+https://github.com/rust-lang/crates.io-index" 317 | checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" 318 | dependencies = [ 319 | "unicode-ident", 320 | ] 321 | 322 | [[package]] 323 | name = "procfs" 324 | version = "0.17.0" 325 | source = "registry+https://github.com/rust-lang/crates.io-index" 326 | checksum = "cc5b72d8145275d844d4b5f6d4e1eef00c8cd889edb6035c21675d1bb1f45c9f" 327 | dependencies = [ 328 | "bitflags", 329 | "hex", 330 | "procfs-core", 331 | "rustix 0.38.44", 332 | ] 333 | 334 | [[package]] 335 | name = "procfs-core" 336 | version = "0.17.0" 337 | source = "registry+https://github.com/rust-lang/crates.io-index" 338 | checksum = "239df02d8349b06fc07398a3a1697b06418223b1c7725085e801e7c0fc6a12ec" 339 | dependencies = [ 340 | "bitflags", 341 | "hex", 342 | ] 343 | 344 | [[package]] 345 | name = "progress" 346 | version = "0.2.0" 347 | source = "registry+https://github.com/rust-lang/crates.io-index" 348 | checksum = "17b820305721858696053a7fd0215cfeeee16ecaaf96b7a209945428e02f1c44" 349 | dependencies = [ 350 | "terminal_size", 351 | ] 352 | 353 | [[package]] 354 | name = "quote" 355 | version = "1.0.40" 356 | source = "registry+https://github.com/rust-lang/crates.io-index" 357 | checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" 358 | dependencies = [ 359 | "proc-macro2", 360 | ] 361 | 362 | [[package]] 363 | name = "r-efi" 364 | version = "5.2.0" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 367 | 368 | [[package]] 369 | name = "regex" 370 | version = "1.11.1" 371 | source = "registry+https://github.com/rust-lang/crates.io-index" 372 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 373 | dependencies = [ 374 | "aho-corasick", 375 | "memchr", 376 | "regex-automata", 377 | "regex-syntax", 378 | ] 379 | 380 | [[package]] 381 | name = "regex-automata" 382 | version = "0.4.9" 383 | source = "registry+https://github.com/rust-lang/crates.io-index" 384 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" 385 | dependencies = [ 386 | "aho-corasick", 387 | "memchr", 388 | "regex-syntax", 389 | ] 390 | 391 | [[package]] 392 | name = "regex-syntax" 393 | version = "0.8.5" 394 | source = "registry+https://github.com/rust-lang/crates.io-index" 395 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 396 | 397 | [[package]] 398 | name = "rexpect" 399 | version = "0.6.1" 400 | source = "registry+https://github.com/rust-lang/crates.io-index" 401 | checksum = "7fac38595028b44c3b3ac4bd87c126636a2d84d6d7c18f626779c6d5117d3cf1" 402 | dependencies = [ 403 | "comma", 404 | "nix", 405 | "regex", 406 | "tempfile", 407 | "thiserror", 408 | ] 409 | 410 | [[package]] 411 | name = "rust-fuzzy-search" 412 | version = "0.1.1" 413 | source = "registry+https://github.com/rust-lang/crates.io-index" 414 | checksum = "a157657054ffe556d8858504af8a672a054a6e0bd9e8ee531059100c0fa11bb2" 415 | 416 | [[package]] 417 | name = "rustix" 418 | version = "0.38.44" 419 | source = "registry+https://github.com/rust-lang/crates.io-index" 420 | checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" 421 | dependencies = [ 422 | "bitflags", 423 | "errno", 424 | "libc", 425 | "linux-raw-sys 0.4.15", 426 | "windows-sys", 427 | ] 428 | 429 | [[package]] 430 | name = "rustix" 431 | version = "1.0.7" 432 | source = "registry+https://github.com/rust-lang/crates.io-index" 433 | checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" 434 | dependencies = [ 435 | "bitflags", 436 | "errno", 437 | "libc", 438 | "linux-raw-sys 0.9.4", 439 | "windows-sys", 440 | ] 441 | 442 | [[package]] 443 | name = "serde" 444 | version = "1.0.219" 445 | source = "registry+https://github.com/rust-lang/crates.io-index" 446 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 447 | dependencies = [ 448 | "serde_derive", 449 | ] 450 | 451 | [[package]] 452 | name = "serde_derive" 453 | version = "1.0.219" 454 | source = "registry+https://github.com/rust-lang/crates.io-index" 455 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 456 | dependencies = [ 457 | "proc-macro2", 458 | "quote", 459 | "syn", 460 | ] 461 | 462 | [[package]] 463 | name = "strsim" 464 | version = "0.11.1" 465 | source = "registry+https://github.com/rust-lang/crates.io-index" 466 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 467 | 468 | [[package]] 469 | name = "syn" 470 | version = "2.0.101" 471 | source = "registry+https://github.com/rust-lang/crates.io-index" 472 | checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" 473 | dependencies = [ 474 | "proc-macro2", 475 | "quote", 476 | "unicode-ident", 477 | ] 478 | 479 | [[package]] 480 | name = "tempfile" 481 | version = "3.20.0" 482 | source = "registry+https://github.com/rust-lang/crates.io-index" 483 | checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" 484 | dependencies = [ 485 | "fastrand", 486 | "getrandom", 487 | "once_cell", 488 | "rustix 1.0.7", 489 | "windows-sys", 490 | ] 491 | 492 | [[package]] 493 | name = "terminal_size" 494 | version = "0.1.17" 495 | source = "registry+https://github.com/rust-lang/crates.io-index" 496 | checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" 497 | dependencies = [ 498 | "libc", 499 | "winapi", 500 | ] 501 | 502 | [[package]] 503 | name = "thiserror" 504 | version = "2.0.12" 505 | source = "registry+https://github.com/rust-lang/crates.io-index" 506 | checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" 507 | dependencies = [ 508 | "thiserror-impl", 509 | ] 510 | 511 | [[package]] 512 | name = "thiserror-impl" 513 | version = "2.0.12" 514 | source = "registry+https://github.com/rust-lang/crates.io-index" 515 | checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" 516 | dependencies = [ 517 | "proc-macro2", 518 | "quote", 519 | "syn", 520 | ] 521 | 522 | [[package]] 523 | name = "unicode-ident" 524 | version = "1.0.18" 525 | source = "registry+https://github.com/rust-lang/crates.io-index" 526 | checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 527 | 528 | [[package]] 529 | name = "utf8parse" 530 | version = "0.2.2" 531 | source = "registry+https://github.com/rust-lang/crates.io-index" 532 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 533 | 534 | [[package]] 535 | name = "wasi" 536 | version = "0.14.2+wasi-0.2.4" 537 | source = "registry+https://github.com/rust-lang/crates.io-index" 538 | checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" 539 | dependencies = [ 540 | "wit-bindgen-rt", 541 | ] 542 | 543 | [[package]] 544 | name = "winapi" 545 | version = "0.3.9" 546 | source = "registry+https://github.com/rust-lang/crates.io-index" 547 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 548 | dependencies = [ 549 | "winapi-i686-pc-windows-gnu", 550 | "winapi-x86_64-pc-windows-gnu", 551 | ] 552 | 553 | [[package]] 554 | name = "winapi-i686-pc-windows-gnu" 555 | version = "0.4.0" 556 | source = "registry+https://github.com/rust-lang/crates.io-index" 557 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 558 | 559 | [[package]] 560 | name = "winapi-x86_64-pc-windows-gnu" 561 | version = "0.4.0" 562 | source = "registry+https://github.com/rust-lang/crates.io-index" 563 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 564 | 565 | [[package]] 566 | name = "windows-sys" 567 | version = "0.59.0" 568 | source = "registry+https://github.com/rust-lang/crates.io-index" 569 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 570 | dependencies = [ 571 | "windows-targets", 572 | ] 573 | 574 | [[package]] 575 | name = "windows-targets" 576 | version = "0.52.6" 577 | source = "registry+https://github.com/rust-lang/crates.io-index" 578 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 579 | dependencies = [ 580 | "windows_aarch64_gnullvm", 581 | "windows_aarch64_msvc", 582 | "windows_i686_gnu", 583 | "windows_i686_gnullvm", 584 | "windows_i686_msvc", 585 | "windows_x86_64_gnu", 586 | "windows_x86_64_gnullvm", 587 | "windows_x86_64_msvc", 588 | ] 589 | 590 | [[package]] 591 | name = "windows_aarch64_gnullvm" 592 | version = "0.52.6" 593 | source = "registry+https://github.com/rust-lang/crates.io-index" 594 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 595 | 596 | [[package]] 597 | name = "windows_aarch64_msvc" 598 | version = "0.52.6" 599 | source = "registry+https://github.com/rust-lang/crates.io-index" 600 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 601 | 602 | [[package]] 603 | name = "windows_i686_gnu" 604 | version = "0.52.6" 605 | source = "registry+https://github.com/rust-lang/crates.io-index" 606 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 607 | 608 | [[package]] 609 | name = "windows_i686_gnullvm" 610 | version = "0.52.6" 611 | source = "registry+https://github.com/rust-lang/crates.io-index" 612 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 613 | 614 | [[package]] 615 | name = "windows_i686_msvc" 616 | version = "0.52.6" 617 | source = "registry+https://github.com/rust-lang/crates.io-index" 618 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 619 | 620 | [[package]] 621 | name = "windows_x86_64_gnu" 622 | version = "0.52.6" 623 | source = "registry+https://github.com/rust-lang/crates.io-index" 624 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 625 | 626 | [[package]] 627 | name = "windows_x86_64_gnullvm" 628 | version = "0.52.6" 629 | source = "registry+https://github.com/rust-lang/crates.io-index" 630 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 631 | 632 | [[package]] 633 | name = "windows_x86_64_msvc" 634 | version = "0.52.6" 635 | source = "registry+https://github.com/rust-lang/crates.io-index" 636 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 637 | 638 | [[package]] 639 | name = "wit-bindgen-rt" 640 | version = "0.39.0" 641 | source = "registry+https://github.com/rust-lang/crates.io-index" 642 | checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" 643 | dependencies = [ 644 | "bitflags", 645 | ] 646 | 647 | [[package]] 648 | name = "writedisk" 649 | version = "1.4.0" 650 | dependencies = [ 651 | "clap", 652 | "procfs", 653 | "progress", 654 | ] 655 | 656 | [[package]] 657 | name = "xtask" 658 | version = "0.0.0" 659 | dependencies = [ 660 | "anyhow", 661 | "argh", 662 | "camino", 663 | "command-run", 664 | "fs-err", 665 | "rexpect", 666 | "tempfile", 667 | ] 668 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "writedisk" 3 | version = "1.4.0" 4 | authors = ["Nicholas Bishop "] 5 | edition = "2024" 6 | repository = "https://github.com/nicholasbishop/writedisk" 7 | license = "Apache-2.0" 8 | description = "Utility for writing a disk image to a USB drive" 9 | readme = "README.md" 10 | categories = ["command-line-utilities"] 11 | keywords = ["disk", "usb", "image", "burn"] 12 | default-run = "writedisk" 13 | 14 | [dependencies] 15 | clap = { version = "4.4.6", features = ["derive"] } 16 | procfs = { version = "0.17.0", default-features = false } 17 | progress = { version = "0.2.0", default-features = false } 18 | 19 | [workspace] 20 | members = ["xtask"] 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # writedisk 2 | 3 | Small utility for writing a disk image to a USB drive. 4 | 5 | Usage: `writedisk ` 6 | 7 | This will scan for connected USB disks and prompt for you to select 8 | one. Then the input file will be copied to the drive. The copying 9 | operation is done with a small `wd_copier` binary that is 10 | automatically invoked with `sudo`. 11 | 12 | Linux only for now. 13 | 14 | ## Installation 15 | 16 | ### Cargo 17 | 18 | ```shell 19 | cargo install writedisk 20 | ``` 21 | 22 | ### Nix/NixOS 23 | 24 | Per user: 25 | 26 | ```shell 27 | nix-env --install writedisk 28 | ``` 29 | 30 | System-wide: 31 | 32 | ```shell 33 | environment.systemPackages = with pkgs; [ writedisk ]; 34 | ``` 35 | 36 | ## License 37 | 38 | Apache 2.0 39 | -------------------------------------------------------------------------------- /src/bin/wd_copier.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::pedantic)] 2 | 3 | use clap::Parser; 4 | use procfs::Current; 5 | use std::convert::TryInto; 6 | use std::io::{Read, Write}; 7 | use std::path::PathBuf; 8 | use std::sync::mpsc; 9 | use std::time::Duration; 10 | use std::{fs, thread}; 11 | 12 | #[derive(Debug, Parser)] 13 | struct Opt { 14 | src: PathBuf, 15 | dst: PathBuf, 16 | } 17 | 18 | /// Get OS dirty byte count using [`procfs::Meminfo`]. 19 | fn get_dirty_bytes() -> u64 { 20 | match procfs::Meminfo::current() { 21 | Ok(o) => o.dirty, 22 | Err(_e) => 0, 23 | } 24 | } 25 | 26 | struct DirtyInfo { 27 | /// Dirty bytes before the copy. This is the "goal". 28 | before_copy: u64, 29 | /// Dirty bytes after the copy. 30 | after_copy: u64, 31 | /// Current number of dirty bytes. 32 | current: u64, 33 | } 34 | 35 | impl DirtyInfo { 36 | /// Estimate the percent completion (between 0 and 100) of the sync 37 | /// operation. 38 | /// 39 | /// The estimate is based on the idea that the number of dirty bytes 40 | /// will be close to the value it was before the copy operation once 41 | /// sync has completed. After the copy completes, the `current` 42 | /// value will be the same as `after_copy`, and it should decrease 43 | /// as the sync is underway until it reaches `before_copy`. 44 | fn calc_sync_percent(&self) -> i32 { 45 | let current = self.current.saturating_sub(self.before_copy); 46 | let max = self.after_copy.saturating_sub(self.before_copy); 47 | 48 | // Flip the value because a lower number of dirty pages is 49 | // closer to completion. 50 | 100 - calc_percent(current, max) 51 | } 52 | } 53 | 54 | #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)] 55 | fn calc_percent(current: u64, max: u64) -> i32 { 56 | // Prevent division by zero. 57 | if max == 0 { 58 | return 0; 59 | } 60 | 61 | let percent = (current as f64) / (max as f64) * 100_f64; 62 | let percent = percent as i32; 63 | if percent > 100 { 100 } else { percent } 64 | } 65 | 66 | /// Draws a progress bar for a disk sync. 67 | /// 68 | /// Uses the dirty value before our copy as our 'goal' by reading from 69 | /// `/proc/meminfo`. This isn't an exact science and is just a rough estimate 70 | /// of our completion. 71 | /// 72 | /// Meant to be run on a thread parallel to the actual sync process and exits 73 | /// after receiving a signal from main that the sync is complete. 74 | fn sync_progress_bar( 75 | rx: &mpsc::Receiver<()>, 76 | mut progress_bar: progress::Bar, 77 | mut dirty: DirtyInfo, 78 | ) { 79 | progress_bar.set_job_title("syncing... (2/2)"); 80 | loop { 81 | dirty.current = get_dirty_bytes(); 82 | progress_bar.reach_percent(dirty.calc_sync_percent()); 83 | thread::sleep(Duration::from_millis(500)); 84 | if matches!( 85 | rx.try_recv(), 86 | Ok(()) | Err(mpsc::TryRecvError::Disconnected) 87 | ) { 88 | return; 89 | } 90 | } 91 | } 92 | 93 | #[allow(clippy::read_zero_byte_vec)] 94 | fn main() { 95 | let opt = Opt::parse(); 96 | 97 | let mut dirty = DirtyInfo { 98 | before_copy: get_dirty_bytes(), 99 | after_copy: 0, 100 | current: 0, 101 | }; 102 | 103 | let mut progress_bar = progress::Bar::new(); 104 | progress_bar.set_job_title("copying... (1/2)"); 105 | 106 | let mut src = fs::File::open(opt.src).unwrap(); 107 | let src_size = src.metadata().unwrap().len(); 108 | 109 | let mut dst = fs::OpenOptions::new().write(true).open(&opt.dst).unwrap(); 110 | 111 | let mut remaining = src_size; 112 | let mut bytes_written: u64 = 0; 113 | let chunk_size: u64 = 1024 * 1024; // TODO 114 | let mut buf = Vec::new(); 115 | while remaining > 0 { 116 | let percent = calc_percent(bytes_written, src_size); 117 | progress_bar.reach_percent(percent); 118 | 119 | let read_size = if chunk_size > remaining { 120 | remaining 121 | } else { 122 | chunk_size 123 | }; 124 | buf.resize(read_size.try_into().unwrap(), 0); 125 | 126 | src.read_exact(&mut buf).unwrap(); 127 | dst.write_all(&buf).unwrap(); 128 | 129 | remaining -= read_size; 130 | bytes_written += read_size; 131 | } 132 | 133 | let (tx, rx) = mpsc::channel(); 134 | dirty.after_copy = get_dirty_bytes() - dirty.before_copy; 135 | 136 | // If we can't get dirty bytes info we can just print 'syncing...' to the screen 137 | if dirty.after_copy == 0 { 138 | println!("syncing... (2/2)"); 139 | } else { 140 | thread::spawn(move || { 141 | sync_progress_bar(&rx, progress_bar, dirty); 142 | }); 143 | } 144 | 145 | dst.sync_data().unwrap(); 146 | tx.send(()).unwrap(); 147 | 148 | println!("finished"); 149 | } 150 | 151 | #[cfg(test)] 152 | mod tests { 153 | use super::*; 154 | 155 | #[test] 156 | fn test_calc_percent() { 157 | assert_eq!(calc_percent(0, 20), 0); 158 | assert_eq!(calc_percent(1, 20), 5); 159 | assert_eq!(calc_percent(20, 20), 100); 160 | 161 | // Check clamping. 162 | assert_eq!(calc_percent(100, 20), 100); 163 | 164 | // Check for division by zero. 165 | assert_eq!(calc_percent(100, 0), 0); 166 | } 167 | 168 | #[test] 169 | fn test_dirty_calc_percent() { 170 | let mut dirty = DirtyInfo { 171 | before_copy: 100, 172 | after_copy: 120, 173 | current: 120, 174 | }; 175 | assert_eq!(dirty.calc_sync_percent(), 0); 176 | 177 | dirty.current = 105; 178 | assert_eq!(dirty.calc_sync_percent(), 75); 179 | 180 | dirty.current = 100; 181 | assert_eq!(dirty.calc_sync_percent(), 100); 182 | 183 | // Check clamping. 184 | dirty.current = 0; 185 | assert_eq!(dirty.calc_sync_percent(), 100); 186 | dirty.current = 200; 187 | assert_eq!(dirty.calc_sync_percent(), 0); 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/bin/writedisk.rs: -------------------------------------------------------------------------------- 1 | #![warn(clippy::pedantic)] 2 | 3 | use clap::Parser; 4 | use std::io::{self, Write}; 5 | use std::path::{Path, PathBuf}; 6 | use std::{env, fs, process}; 7 | 8 | #[derive(Clone, Debug)] 9 | struct UsbBlockDevice { 10 | /// The device path, e.g. "/dev/sdc" 11 | device: PathBuf, 12 | 13 | manufacturer: String, 14 | product: String, 15 | serial: String, 16 | } 17 | 18 | /// Try to determine whether this is a USB device or not by searching 19 | /// upwards for a directory name starting with "usb". 20 | fn is_usb_in_path(path: &Path) -> bool { 21 | for path in path.ancestors() { 22 | if let Some(name) = path.file_name() { 23 | if let Some(name) = name.to_str() { 24 | if name.starts_with("usb") { 25 | return true; 26 | } 27 | } 28 | } 29 | } 30 | false 31 | } 32 | 33 | /// Search upwards for a directory containing device info 34 | /// (manufacturer, product, and serial). 35 | fn find_usb_info(path: &Path) -> Option { 36 | for path in path.ancestors() { 37 | if path.join("manufacturer").exists() 38 | && path.join("product").exists() 39 | && path.join("serial").exists() 40 | { 41 | return Some(path.into()); 42 | } 43 | } 44 | None 45 | } 46 | 47 | impl UsbBlockDevice { 48 | fn get_all() -> io::Result> { 49 | let mut result = Vec::new(); 50 | for entry in fs::read_dir("/sys/block")? { 51 | let entry = entry?; 52 | let path = entry.path(); 53 | let device_path = path.join("device"); 54 | if !device_path.exists() { 55 | continue; 56 | } 57 | 58 | // This will give a very long path such as: 59 | // /sys/devices/pci0000:00/0000:00:01.2/0000:02:00.0/ 60 | // 0000:03:08.0/0000:08:00.3/usb4/4-3/4-3.2/4-3.2:1.0/ 61 | // host7/target7:0:0/7:0:0:0 62 | let device_path = device_path.canonicalize()?; 63 | 64 | // Skip non-USB devices 65 | if !is_usb_in_path(&device_path) { 66 | continue; 67 | } 68 | 69 | if let Some(info_path) = find_usb_info(&device_path) { 70 | let read = |name| -> io::Result { 71 | let path = info_path.join(name); 72 | let contents = fs::read_to_string(path)?; 73 | Ok(contents.trim().into()) 74 | }; 75 | 76 | result.push(UsbBlockDevice { 77 | device: Path::new("/dev").join(entry.file_name()), 78 | manufacturer: read("manufacturer")?, 79 | product: read("product")?, 80 | serial: read("serial")?, 81 | }); 82 | } 83 | } 84 | Ok(result) 85 | } 86 | 87 | fn summary(&self) -> String { 88 | format!( 89 | "[{}] {} {} {}", 90 | self.device.display(), 91 | &self.manufacturer, 92 | &self.product, 93 | &self.serial, 94 | ) 95 | } 96 | } 97 | 98 | fn choose_device() -> UsbBlockDevice { 99 | let devices = UsbBlockDevice::get_all().unwrap(); 100 | 101 | if devices.is_empty() { 102 | println!("no devices found"); 103 | process::exit(1); 104 | } 105 | 106 | for (index, device) in devices.iter().enumerate() { 107 | println!("{index}: {}", device.summary()); 108 | } 109 | 110 | print!("select device: "); 111 | io::stdout().flush().unwrap(); 112 | let mut input = String::new(); 113 | io::stdin().read_line(&mut input).unwrap(); 114 | 115 | let index = match input.trim().parse::() { 116 | Ok(i) => i, 117 | Err(_e) => { 118 | println!("invalid input"); 119 | process::exit(1); 120 | } 121 | }; 122 | 123 | if index >= devices.len() { 124 | println!("invalid index"); 125 | process::exit(1); 126 | } 127 | 128 | devices[index].clone() 129 | } 130 | 131 | #[derive(Debug, Parser)] 132 | #[command(about = "Write a disk image to a USB disk.", version)] 133 | struct Opt { 134 | /// Disk image 135 | input: PathBuf, 136 | } 137 | 138 | fn main() { 139 | let opt = Opt::parse(); 140 | 141 | // Check if the input file exists before doing anything else. 142 | if !opt.input.exists() { 143 | eprintln!("file not found: {}", opt.input.display()); 144 | process::exit(1); 145 | } 146 | 147 | let device = choose_device(); 148 | 149 | let copier_path = env::current_exe() 150 | .expect("failed to get current exe path") 151 | .parent() 152 | .expect("failed to get current exe directory") 153 | .join("wd_copier"); 154 | 155 | println!( 156 | "sudo {} {} {}", 157 | copier_path.display(), 158 | opt.input.display(), 159 | device.device.display() 160 | ); 161 | let status = process::Command::new("sudo") 162 | .args([&copier_path, &opt.input, &device.device]) 163 | .status() 164 | .expect("failed to run command"); 165 | if !status.success() { 166 | println!("copy failed"); 167 | process::exit(1); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /xtask/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "xtask" 3 | version = "0.0.0" 4 | edition = "2024" 5 | publish = false 6 | 7 | [dependencies] 8 | anyhow = "1.0.75" 9 | argh = "0.1.12" 10 | camino = "1.1.6" 11 | command-run = "1.1.1" 12 | fs-err = "3.0.0" 13 | rexpect = "0.6.0" 14 | tempfile = "3.8.0" 15 | -------------------------------------------------------------------------------- /xtask/src/main.rs: -------------------------------------------------------------------------------- 1 | mod vmtest; 2 | 3 | use anyhow::Result; 4 | use argh::FromArgs; 5 | 6 | #[derive(FromArgs)] 7 | /// Tasks for writedisk. 8 | struct Opt { 9 | #[argh(subcommand)] 10 | action: Action, 11 | } 12 | 13 | #[derive(FromArgs)] 14 | #[argh(subcommand)] 15 | enum Action { 16 | VmTest(ActionVmTest), 17 | } 18 | 19 | /// Test writedisk in a VM. 20 | #[derive(FromArgs)] 21 | #[argh(subcommand, name = "vmtest")] 22 | pub struct ActionVmTest { 23 | /// don't enable KVM 24 | #[argh(switch)] 25 | disable_kvm: bool, 26 | } 27 | 28 | fn main() -> Result<()> { 29 | let opt: Opt = argh::from_env(); 30 | 31 | match &opt.action { 32 | Action::VmTest(action) => vmtest::run(action), 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /xtask/src/pseudo_sudo: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | $@ 4 | -------------------------------------------------------------------------------- /xtask/src/vmtest.rs: -------------------------------------------------------------------------------- 1 | use crate::ActionVmTest; 2 | use anyhow::{Result, anyhow}; 3 | use camino::Utf8PathBuf; 4 | use command_run::Command; 5 | use fs_err as fs; 6 | use rexpect::session::PtySession; 7 | use std::env; 8 | 9 | struct AlpineVersion { 10 | major: u32, 11 | minor: u32, 12 | patch: u32, 13 | } 14 | 15 | impl AlpineVersion { 16 | fn new(major: u32, minor: u32, patch: u32) -> AlpineVersion { 17 | AlpineVersion { 18 | major, 19 | minor, 20 | patch, 21 | } 22 | } 23 | 24 | fn iso_file_name(&self) -> String { 25 | format!( 26 | "alpine-virt-{}.{}.{}-x86_64.iso", 27 | self.major, self.minor, self.patch 28 | ) 29 | } 30 | 31 | fn iso_url(&self) -> String { 32 | format!( 33 | "https://dl-cdn.alpinelinux.org/alpine/v{}.{}/releases/x86_64/{}", 34 | self.major, 35 | self.minor, 36 | self.iso_file_name() 37 | ) 38 | } 39 | } 40 | 41 | struct Vm { 42 | qemu: String, 43 | iso_path: Utf8PathBuf, 44 | usb_backing_file: Utf8PathBuf, 45 | data_path: Utf8PathBuf, 46 | } 47 | 48 | fn session_run_cmd(p: &mut PtySession, cmd: &str) -> Result<()> { 49 | let prompt = "localhost:~# "; 50 | println!("{cmd}"); 51 | p.exp_string(prompt)?; 52 | p.send_line(cmd)?; 53 | Ok(()) 54 | } 55 | 56 | fn session_verify_success(p: &mut PtySession) -> Result<()> { 57 | session_run_cmd(p, "echo $?")?; 58 | // Skip the echo of the command. 59 | p.read_line()?; 60 | let exit_code = p.read_line()?; 61 | let exit_code = exit_code.trim(); 62 | if exit_code != "0" { 63 | panic!("command exited non-zero: {}", exit_code); 64 | } 65 | Ok(()) 66 | } 67 | 68 | impl Vm { 69 | fn run(&self, action: &ActionVmTest) -> Result<()> { 70 | let qemu_cmd = [ 71 | self.qemu.as_str(), 72 | if action.disable_kvm { 73 | "" 74 | } else { 75 | "-enable-kvm" 76 | }, 77 | "-display none", 78 | &format!("-drive format=raw,file={}", self.iso_path.as_str()), 79 | "-m 512", 80 | "-serial stdio", 81 | // Add a disk backed by the data subdirectory, will appear as sdb1. 82 | &format!("-drive format=raw,file=fat:rw:{}", self.data_path), 83 | // Add a USB disk device backed by a file. 84 | &format!( 85 | "-drive if=none,id=stick,format=raw,file={}", 86 | self.usb_backing_file 87 | ), 88 | "-device nec-usb-xhci,id=xhci", 89 | "-device usb-storage,bus=xhci.0,drive=stick", 90 | ] 91 | .join(" "); 92 | 93 | println!("{qemu_cmd}"); 94 | 95 | // Use a fairly long timeout to avoid failing in the CI. 96 | let p = &mut rexpect::spawn(&qemu_cmd, Some(100_000))?; 97 | 98 | // Log in. 99 | println!("waiting to log in..."); 100 | p.exp_string("localhost login: ")?; 101 | println!("logging in"); 102 | p.send_line("root")?; 103 | 104 | // Mount the data dir. 105 | session_run_cmd(p, "mkdir /mnt/data")?; 106 | session_verify_success(p)?; 107 | session_run_cmd(p, "mount /dev/sdb1 /mnt/data")?; 108 | session_verify_success(p)?; 109 | 110 | // Run writedisk. Set the path to the data dir. This is necessary so 111 | // that the pseudo sudo executable will be found. 112 | session_run_cmd(p, "PATH=/mnt/data writedisk /mnt/data/test_file")?; 113 | 114 | // Skip the echo of the command. 115 | p.read_line()?; 116 | 117 | // Expect one USB device to be found. 118 | let choice_0 = p.read_line()?; 119 | if !choice_0.starts_with("0: [/dev/sdc] QEMU QEMU USB HARDDRIVE") { 120 | panic!("unexpected output: {}", choice_0); 121 | } 122 | 123 | // Choose the first disk and start the copy. 124 | println!("starting copy..."); 125 | p.send_line("0")?; 126 | 127 | // Check the exit code. 128 | session_verify_success(p)?; 129 | 130 | // Shut down. 131 | session_run_cmd(p, "poweroff")?; 132 | p.exp_eof()?; 133 | 134 | Ok(()) 135 | } 136 | } 137 | 138 | /// Get the absolute path of the repo. Assumes that this executable is 139 | /// located at /target//. 140 | fn get_repo_path() -> Result { 141 | let exe = Utf8PathBuf::from_path_buf(env::current_exe()?) 142 | .map_err(|_| anyhow!("exe path is not utf-8"))?; 143 | Ok(exe 144 | .parent() 145 | .and_then(|path| path.parent()) 146 | .and_then(|path| path.parent()) 147 | .ok_or_else(|| anyhow!("not enough parents: {}", exe))? 148 | .into()) 149 | } 150 | 151 | pub fn run(action: &ActionVmTest) -> Result<()> { 152 | let repo_path = get_repo_path()?; 153 | 154 | let tmp_dir = tempfile::tempdir()?; 155 | let tmp_path = Utf8PathBuf::from_path_buf(tmp_dir.path().into()).unwrap(); 156 | 157 | let alpine_version = AlpineVersion::new(3, 14, 2); 158 | 159 | // Download the Alpine ISO. 160 | let iso_path = tmp_path.join(alpine_version.iso_file_name()); 161 | Command::with_args( 162 | "curl", 163 | ["--output", iso_path.as_str(), &alpine_version.iso_url()], 164 | ) 165 | .run()?; 166 | 167 | // Create a file to back the virtual USB device. 168 | let usb_backing_file = tmp_path.join("usb_data"); 169 | Command::with_args( 170 | "truncate", 171 | ["--size", "10MiB", usb_backing_file.as_str()], 172 | ) 173 | .run()?; 174 | 175 | // Create the data dir. 176 | let data_path = tmp_path.join("data"); 177 | fs::create_dir(&data_path)?; 178 | 179 | // Copy the pseudo sudo executable to the data dir. 180 | fs::copy( 181 | repo_path.join("xtask/src/pseudo_sudo"), 182 | data_path.join("sudo"), 183 | )?; 184 | 185 | // Build a statically-linked writedisk and copy to the data dir. 186 | Command::with_args( 187 | "cargo", 188 | [ 189 | "build", 190 | "--release", 191 | "--target", 192 | "x86_64-unknown-linux-musl", 193 | ], 194 | ) 195 | .run()?; 196 | let build_path = repo_path.join("target/x86_64-unknown-linux-musl/release"); 197 | fs::copy(build_path.join("writedisk"), data_path.join("writedisk"))?; 198 | fs::copy(build_path.join("wd_copier"), data_path.join("wd_copier"))?; 199 | 200 | // Write out a test file that writedisk will use as input. 201 | let test_disk_data = b"This is the content of the test file"; 202 | let test_data_path = data_path.join("test_file"); 203 | fs::write(test_data_path, test_disk_data)?; 204 | 205 | // Run the VM. 206 | let vm = Vm { 207 | qemu: "qemu-system-x86_64".into(), 208 | iso_path, 209 | usb_backing_file: usb_backing_file.clone(), 210 | data_path, 211 | }; 212 | vm.run(action).unwrap(); 213 | 214 | // Verify the correct data was written. It should start with the 215 | // string in `test_disk_data` and the rest should be zeroes. 216 | let actual = fs::read(&usb_backing_file)?; 217 | assert_eq!(actual.len(), 10 * 1024 * 1024); 218 | assert_eq!(&actual[..test_disk_data.len()], test_disk_data); 219 | for byte in &actual[test_disk_data.len()..] { 220 | assert_eq!(*byte, 0); 221 | } 222 | 223 | println!("test passed"); 224 | 225 | Ok(()) 226 | } 227 | --------------------------------------------------------------------------------