Skip to content

Instantly share code, notes, and snippets.

@jpouellet
Created March 31, 2017 22:51

Revisions

  1. jpouellet created this gist Mar 31, 2017.
    23 changes: 23 additions & 0 deletions jpo.WriteUSB
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #!/bin/bash

    if [ $# -eq 0 ]; then
    dev=/dev/sda
    else
    # Protected by arg-specific qubes-rpc policy.
    dev=/dev/"$1"
    fi

    if ! [ -b "$dev" ]; then
    echo "${0##*/}: $dev: No such block device" >&2
    exit 1
    fi

    # Keep track of exactly how much we wrote to know where to stop reading later.
    len=$(sudo tee -- "$dev" | wc -c)
    echo "${0##*/}: Done writing $len bytes to $dev. Verifying..." >&2

    # Read it back from actual media, only once, passing it to all checksums.
    # Wait for all hashes to be done, and guarantee consistent ordering.
    sudo head -c "$len" -- "$dev" | (
    tee >(sha1sum --tag) >(sha256sum --tag) > >(sha512sum --tag)
    ) | sort