#!/bin/bash # funcs set_status(){ echo "$$" > "$OUTPUT_FILE" echo "$1" >> "$OUTPUT_FILE" } mount_list(){ mount |sed -ne "\%^$USB_DEV0[[:digit:]][[:blank:]]%s%[[:blank:]].*$%%p" } write_image(){ set_status "writing" for m in $(mount_list) ; do umount "$m" || true done if [ -d "$IMAGE_FILE" ] ; then # dir mode # create mountpoint MOUNT_POINT="$(mktemp -d)" mount "$USB_DEV" "$MOUNT_POINT" || return 1 rsync -rx "$IMAGE_FILE/" "$MOUNT_POINT/" || return 1 # install syslinux # find syslinux file and get syslinux dir SYSLINUX_FILE="$(find "$MOUNT_POINT" -name syslinux.cfg -printf "%P\n" 2>/dev/null)" SYSLINUX_DIR="" if [ "$SYSLINUX_FILE" ] ; then SYSLINUX_DIR="$(dirname "$SYSLINUX_FILE")" fi rm -f "$MOUNT_POINT/$SYSLINUX_DIR/ldlinux.sys" [ -z "$SYSLINUX_DIR" ] || SYSLINUX_DIR="-d $SYSLINUX_DIR" syslinux $SYSLINUX_DIR "$USB_DEV" || return 1 # mbr cat "$MBR_FILE" > "$USB_DEV0" # set_status "cat $MBR_FILE > $USB_DEV0" # sleep 5 # boot parted -s "$USB_DEV0" set $USB_PART boot on || true umount "$MOUNT_POINT" || true else # block mode # do dd dd if="$IMAGE_FILE" of="$USB_DEV" || return 1 fi sleep 1 for m in $(mount_list) ; do umount "$m" || true done return 0 } # main INPUT_FILE="$1" MBR_FILE="/usr/lib/syslinux/mbr.bin" [ -r "$MBR_FILE" ] || return 0 while true; do [ -s "$INPUT_FILE" ] || exit 0 USB_DEV="$(sed -ne "1p" "$INPUT_FILE")" OUTPUT_FILE="$(sed -ne "2p" "$INPUT_FILE")" CONTROL_FILE="$(sed -ne "3p" "$INPUT_FILE")" [ "$USB_DEV" -a "$OUTPUT_FILE" -a "$CONTROL_FILE" -a -s "$CONTROL_FILE" ] || exit 0 USB_DEV0="$(echo "$USB_DEV" |sed -e "s%[[:digit:]]$%%")" USB_PART=${USB_DEV##$USB_DEV0} IMAGE_FILE="$(sed -ne "1p" "$CONTROL_FILE")" [ -r "$IMAGE_FILE" ] || exit 0 if [ -b "$USB_DEV" ] ; then if write_image ; then STATUS="COMPLETED. Please, remove USB" set_status "$STATUS" # wait to remove while [ -s "$CONTROL_FILE" -a -b "$USB_DEV" ] ; do sleep 1 done STATUS="iddle" else STATUS="ERROR" fi else STATUS="iddle" fi set_status "$STATUS" sleep 2 done exit 0