#!/usr/env/bin bash set -e # set -x # For debugging # If you ever mess this up: # $ mknod /dev/loop-control c 10 237 # $ chmod 600 /dev/loop-control # $ chown root:root /dev/loop-control # # For cleanup: # $ find /dev -maxdepth 1 -type b -name 'loop[0-9]*' -exec rm -f {} \; if [ "$(id -u)" -ne 0 ]; then echo "Run as root. We need permissions to write and setup a loop device." exit 1 fi DISKFILE="/tmp/disk.img" BINFILE="/tmp/file.bin" LOOPNUM=$((RANDOM % 156 + 100)) LOOPDEV="/dev/loop${LOOPNUM}" echo "Using device: ${LOOPDEV}" cleanup() { echo "Cleaning up..." set +e -x losetup -d ${LOOPDEV} rm ${LOOPDEV} rm ${BINFILE} ${DISKFILE} } trap cleanup EXIT INT TERM if losetup ${LOOPDEV} >/dev/null 2>&1; then echo "${LOOPDEV} already in use" >&2 cleanup exit 1 fi if [ ! -f ${DISKFILE} ]; then echo "Creating ${DISKFILE}" dd if=/dev/zero of=${DISKFILE} bs=1M count=256 fi if [ ! -f ${BINFILE} ]; then echo "Creating ${BINFILE}" dd if=/dev/urandom of=${BINFILE} bs=1M count=64 fi if [ ! -e ${LOOPDEV} ]; then mknod ${LOOPDEV} b 7 ${LOOPNUM} # busybox needs this losetup ${LOOPDEV} ${DISKFILE} fi ./writeimg -nd ${LOOPDEV} ${BINFILE} ./writeimg -vnd ${LOOPDEV} ${BINFILE} ./writeimg -nd ${LOOPDEV} ./writeimg ./writeimg -vnd ${LOOPDEV} ./writeimg ./writeimg -nd ${LOOPDEV} ./LICENSE ./writeimg -vnd ${LOOPDEV} ./LICENSE # Redirect this to avoid confusion ! ./writeimg -vnd ${LOOPDEV} ./crc32.h 2>/dev/null GREEN="\e[32m" RESET="\e[0m" echo -e "\n\n${GREEN}Looks good!${RESET}"