#!/bin/sh # notjunk USB stick maker for macOS and Linux. # # curl -fsSL https://notjunk.si/install.sh | sh # # Downloads the notjunk image, checks its SHA-256, lists only USB and removable # disks, asks you to type the disk's name, writes the image, reads it back to # check it, then ejects the stick. # # Options (after `sh -s --` when piped): # --disk NAME preselect the stick (sdb, disk4). You still type it to confirm. # --image FILE use an image file you already have (still checked). # --download-only download and check the image, write nothing. # --list list the disks it would offer, write nothing. # # Starting a computer from the finished stick installs notjunk on its disk. # Guide: https://notjunk.si/install set -eu VERSION="c99479f" IMG_URL="https://github.com/thomasdisney/notjunk-releases/releases/download/v0.1.0-c99479f/notjunk.iso" IMG_SHA="27f5339d9d3226f3f784ea22b5fd01dc4f86157112bb4c6325973ad40532ec3b" IMG_BYTES=1506377728 MAX_BYTES=$((512 * 1000 * 1000 * 1000)) # bigger than 512 GB is a backup drive, not a stick DISK=""; IMAGE=""; MODE="write" while [ $# -gt 0 ]; do case "$1" in --disk) DISK="${2:-}"; shift 2 ;; --disk=*) DISK="${1#*=}"; shift ;; --image) IMAGE="${2:-}"; shift 2 ;; --image=*) IMAGE="${1#*=}"; shift ;; --download-only) MODE=download; shift ;; --list) MODE=list; shift ;; -h|--help) sed -n '2,20p' "$0" 2>/dev/null || echo "See https://notjunk.si/install"; exit 0 ;; *) echo "Unknown option: $1 (see https://notjunk.si/install)" >&2; exit 2 ;; esac done say() { printf '%s\n' "$*"; } die() { printf '\nStopped: %s\nNothing was written.\n' "$*" >&2; exit 1; } # Tests only: NJ_INSTALL_TEST=1 swaps the system's disks for plain files listed in # NJ_TEST_DISKS ("name|bytes|model|removable|system|path" per line). Plain files only, # never a device, so this can't reach a real disk. TEST=0 if [ "${NJ_INSTALL_TEST:-}" = 1 ]; then TEST=1; fi OS=$(uname -s) case "$OS" in Darwin) OS=mac ;; Linux) OS=linux ;; *) die "this command is for macOS and Linux. On Windows, use balenaEtcher: https://notjunk.si/install#windows" ;; esac # Questions come from the keyboard even when the script arrives through a pipe. ask() { if [ -n "${NJ_TEST_ANSWER+x}" ] && [ "$TEST" = 1 ]; then REPLY="$NJ_TEST_ANSWER"; return 0; fi if [ -r /dev/tty ] && (exec < /dev/tty) 2>/dev/null; then read -r REPLY < /dev/tty || REPLY="" else die "no keyboard to ask you which disk. Run it in a terminal window."; fi } sha_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum | cut -d' ' -f1 else shasum -a 256 | cut -d' ' -f1; fi } gb() { awk -v b="$1" 'BEGIN { if (b >= 1e9) printf "%.1f GB", b / 1e9; else printf "%d MB", b / 1e6 }'; } run_root() { if [ "$TEST" = 1 ] || [ "$(id -u)" = 0 ]; then "$@"; else sudo "$@"; fi } # ---------- the image ---------- get_image() { if [ -n "$IMAGE" ]; then [ -f "$IMAGE" ] || die "no file at $IMAGE" else dir="${NJ_DOWNLOAD_DIR:-$HOME/Downloads}" [ -d "$dir" ] || dir="${TMPDIR:-/tmp}" IMAGE="$dir/notjunk-$VERSION.iso" if [ -f "$IMAGE" ] && [ "$(wc -c < "$IMAGE" | tr -d ' ')" = "$IMG_BYTES" ]; then say "Found $IMAGE, checking it." else command -v curl >/dev/null 2>&1 || die "this needs curl" say "Downloading notjunk $VERSION ($(gb $IMG_BYTES))..." curl -fL --retry 3 --progress-bar -o "$IMAGE.part" "$IMG_URL" || die "the download failed. Check the internet and run it again." mv -f "$IMAGE.part" "$IMAGE" fi fi got=$(sha_of < "$IMAGE") if [ "$got" != "$IMG_SHA" ]; then [ -z "${NJ_KEEP_BAD:-}" ] && case "$IMAGE" in *"/notjunk-$VERSION.iso") rm -f "$IMAGE" ;; esac die "the image's fingerprint is wrong (got $got). Run the command again to download a fresh copy." fi say "Image checked: SHA-256 $IMG_SHA" } # ---------- disks: one line each, "name|bytes|model|removable|system|path" ---------- disks_linux() { root_src=$(findmnt -no SOURCE / 2>/dev/null || true) root_disk="" [ -n "$root_src" ] && root_disk=$(lsblk -no PKNAME "$root_src" 2>/dev/null | head -1 || true) lsblk -dbnpo NAME,TYPE,TRAN,RM,SIZE,RO 2>/dev/null | while read -r dev type tran rm size ro; do [ "$type" = disk ] || continue name=${dev#/dev/} case "$name" in loop*|ram*|zram*|sr*|nbd*|md*|dm-*) continue ;; esac model=$(lsblk -dno VENDOR,MODEL "$dev" 2>/dev/null | tr -s ' ' | sed 's/^ //;s/ $//;s/|/-/g') # USB or marked removable only. Not "hotplug": internal SATA ports can say hotplug. removable=0; { [ "$tran" = usb ] || [ "$rm" = 1 ]; } && removable=1 [ "$ro" = 1 ] && removable=0 system=0 [ "$name" = "$root_disk" ] && system=1 # any mounted system path or active swap on it if lsblk -lnpo MOUNTPOINT "$dev" 2>/dev/null | grep -qE '^(/|/boot|/boot/efi|/efi|/usr|/var|/home|\[SWAP\])$'; then system=1; fi echo "$name|$size|${model:-disk}|$removable|$system|$dev" done } disks_mac() { boot=$(diskutil info / 2>/dev/null | awk -F': *' '/Part of Whole/ {print $2; exit}') for d in $(diskutil list 2>/dev/null | awk '/^\/dev\/disk[0-9]+ / {sub("/dev/","",$1); print $1}'); do info=$(diskutil info "$d" 2>/dev/null) || continue echo "$info" | grep -qE '^ *Virtual: *Yes' && continue echo "$info" | grep -qE 'Disk Image' && continue size=$(echo "$info" | sed -n 's/.*Disk Size:.*(\([0-9]*\) Bytes).*/\1/p' | head -1) model=$(echo "$info" | awk -F': *' '/Device \/ Media Name/ {print $2; exit}' | sed 's/|/-/g') removable=0 echo "$info" | grep -qE 'Device Location: *External|Removable Media: *(Removable|Yes)|Protocol: *USB' && removable=1 echo "$info" | grep -qE 'Device Location: *Internal' && ! echo "$info" | grep -qE 'Removable Media: *(Removable|Yes)' && removable=0 system=0 [ "$d" = "$boot" ] && system=1 # APFS containers: the physical store holding the boot volume is the system disk diskutil apfs list 2>/dev/null | awk -v d="$d" '/Physical Store/ {ps=$NF} /Mount Point: *\/$/ && ps ~ "^"d"s" {print "sys"}' | grep -q sys && system=1 echo "$d|${size:-0}|${model:-disk}|$removable|$system|/dev/$d" done } all_disks() { if [ "$TEST" = 1 ]; then printf '%s\n' "${NJ_TEST_DISKS:-}" | grep -v '^$' || true; return; fi if [ "$OS" = mac ]; then disks_mac; else disks_linux; fi } why_not() { # prints a reason, or nothing if the disk can be used _size=$2; _rem=$4; _sys=$5 if [ "$_sys" = 1 ]; then echo "this computer runs from it"; return; fi if [ "$_rem" != 1 ]; then echo "it's an internal disk"; return; fi if [ "${_size:-0}" -lt "$IMG_BYTES" ]; then echo "too small"; return; fi if [ "${_size:-0}" -gt "$MAX_BYTES" ]; then echo "over 512 GB, so probably a backup drive"; return; fi } list_disks() { OK_LIST="" say "" say "Disks on this computer:" all_disks > "${TMPDIR:-/tmp}/nj-disks.$$" while IFS='|' read -r name size model rem sys path; do [ -n "$name" ] || continue reason=$(why_not "$name" "$size" "$model" "$rem" "$sys") if [ -z "$reason" ]; then printf ' %-10s %9s %s\n' "$name" "$(gb "$size")" "$model" OK_LIST="$OK_LIST $name" else printf ' %-10s %9s %s (not offered: %s)\n' "$name" "$(gb "$size")" "$model" "$reason" fi done < "${TMPDIR:-/tmp}/nj-disks.$$" rm -f "${TMPDIR:-/tmp}/nj-disks.$$" } disk_line() { all_disks | awk -F'|' -v n="$1" '$1 == n {print; exit}'; } # ---------- write, read back, eject ---------- unmount_disk() { if [ "$TEST" = 1 ]; then return 0; fi if [ "$OS" = mac ]; then diskutil unmountDisk force "$1" >/dev/null || die "couldn't unmount $1. Close any windows showing the stick and try again." else for p in $(lsblk -lnpo NAME "$1" | tail -n +2); do if lsblk -no MOUNTPOINT "$p" 2>/dev/null | grep -q .; then run_root umount "$p" || die "couldn't unmount $p. Close any windows showing the stick and try again."; fi done fi } write_image() { # $1 = target path out=$1 [ "$OS" = mac ] && [ "$TEST" = 0 ] && out=$(echo "$1" | sed 's#/dev/disk#/dev/rdisk#') say "Writing $(gb $IMG_BYTES) to $out. This takes a few minutes." if [ "$TEST" = 1 ]; then dd if="$IMAGE" of="$out" bs=1048576 conv=notrunc 2>/dev/null elif [ "$OS" = mac ]; then run_root dd if="$IMAGE" of="$out" bs=4m || die "writing failed. Try another USB port or stick." else run_root dd if="$IMAGE" of="$out" bs=4M conv=fsync oflag=direct status=progress || die "writing failed. Try another USB port or stick." fi if [ "$TEST" = 0 ]; then sync; fi } read_back() { # $1 = target path; sha of the first IMG_BYTES bytes, read from the stick itself in=$1; blocks=$(( (IMG_BYTES + 1048575) / 1048576 )) if [ "$TEST" = 1 ]; then dd if="$in" bs=1048576 count=$blocks 2>/dev/null | head -c "$IMG_BYTES" | sha_of; return; fi if [ "$OS" = mac ]; then in=$(echo "$1" | sed 's#/dev/disk#/dev/rdisk#') run_root dd if="$in" bs=1m count=$blocks 2>/dev/null | head -c "$IMG_BYTES" | sha_of else run_root blockdev --flushbufs "$in" 2>/dev/null || true run_root dd if="$in" bs=1M count=$blocks iflag=direct 2>/dev/null | head -c "$IMG_BYTES" | sha_of fi } eject_disk() { if [ "$TEST" = 1 ]; then return 0; fi if [ "$OS" = mac ]; then diskutil eject "$1" >/dev/null 2>&1 || true else command -v udisksctl >/dev/null 2>&1 && udisksctl power-off -b "$1" >/dev/null 2>&1 && return 0 command -v eject >/dev/null 2>&1 && run_root eject "$1" >/dev/null 2>&1 || true fi } # ---------- main ---------- say "notjunk USB stick maker ($VERSION)" if [ "$MODE" = list ]; then list_disks; [ -n "$OK_LIST" ] || say "No USB stick found."; exit 0; fi for t in dd awk; do command -v "$t" >/dev/null 2>&1 || die "this needs $t"; done if [ "$TEST" = 0 ] && [ "$OS" = linux ]; then command -v lsblk >/dev/null 2>&1 || die "this needs lsblk (util-linux)"; fi get_image if [ "$MODE" = download ]; then say "Saved at $IMAGE"; exit 0; fi list_disks if [ -z "$OK_LIST" ]; then die "no USB stick found. Plug one in (2 GB or more; everything on it is erased) and run it again."; fi if [ -n "$DISK" ]; then DISK=${DISK#/dev/} case " $OK_LIST " in *" $DISK "*) ;; *) die "$DISK isn't a USB stick this computer can use. Pick one from the list." ;; esac say "" say "You picked $DISK. Everything on it will be erased." printf 'Type %s again to confirm: ' "$DISK" else say "" say "Everything on the stick you pick will be erased." printf 'Type the name of your stick (like %s), or press Return to stop: ' "$(echo $OK_LIST | cut -d' ' -f1)" fi ask pick=$(printf '%s' "$REPLY" | tr -d ' \t\r') pick=${pick#/dev/} [ -n "$pick" ] || die "you didn't pick a disk." if [ -n "$DISK" ] && [ "$pick" != "$DISK" ]; then die "that didn't match $DISK."; fi case " $OK_LIST " in *" $pick "*) ;; *) die "\"$pick\" isn't on the list of sticks." ;; esac # Check again right before writing: the stick must still be there, unchanged. line=$(disk_line "$pick") [ -n "$line" ] || die "$pick is gone. Plug it back in and run it again." size=$(echo "$line" | cut -d'|' -f2); rem=$(echo "$line" | cut -d'|' -f4); sys=$(echo "$line" | cut -d'|' -f5); path=$(echo "$line" | cut -d'|' -f6) reason=$(why_not "$pick" "$size" x "$rem" "$sys") [ -z "$reason" ] || die "$pick can't be used: $reason." if [ "$TEST" = 1 ]; then [ -f "$path" ] && [ ! -b "$path" ] && [ ! -c "$path" ] || die "test disks must be plain files" else [ -b "$path" ] || [ -c "$path" ] || die "$path isn't a disk." fi [ "$TEST" = 0 ] && [ "$(id -u)" != 0 ] && say "Your computer password may be asked for next (sudo)." unmount_disk "$path" write_image "$path" say "Checking the stick..." back=$(read_back "$path") if [ "$back" != "$IMG_SHA" ]; then die "the stick doesn't read back correctly (got $back). The stick may be faulty: try another one." fi eject_disk "$path" say "" say "Done. The stick checked out and is ejected. Pull it out." say "" say "Next: plug it into the spare computer and start it from the stick." say " Mac: hold Option while it starts, then pick EFI Boot." say " PC: tap the boot menu key (often F12) and pick the USB stick." say "It installs notjunk on that computer's disk (replacing everything on it)," say "then shows two words: type them on your phone at notjunk.si/enter." say "Guide: https://notjunk.si/install"