Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # This script is launched by devd to flash an image file
- # to a flash disk.
- # Copyright 2015 Lars Engels <lme@FreeBSD.org>
- # BSD-style license applies
- image_dir="/ISOs"
- notify_tool="/usr/local/bin/zenity"
- pipeview="/usr/bin/dpv"
- title="Img2Flash"
- _usage() {
- echo "Usage: $(basename $0) [-I <image_dir>] <-d devicename>"
- exit 2
- }
- _goodbye() {
- ${notify_tool} \
- --error \
- --title "${title}" \
- --text 'Operation cancelled or an error occured!'
- exit 1
- }
- _check_dependency() {
- if [ ! -f ${1} ]; then
- echo "Cannot find ${1}!" >/dev/stderr
- exit 3
- fi
- }
- args=$(getopt d:I: $*)
- if [ $? -ne 0 ]; then
- _usage
- fi
- for dep in "$notify_tool" "$pipeview"; do
- _check_dependency $dep
- done
- set -- $args
- while true; do
- case "$1" in
- -d)
- devicename="$2"
- shift; shift
- ;;
- -I)
- image_dir="$2"
- shift; shift
- ;;
- --)
- shift; break
- ;;
- esac
- done
- if [ -z "${devicename}" ]; then
- _usage
- fi
- if [ ! -d "${image_dir}" ]; then
- ${notify_tool} \
- --error \
- --title "${title}" \
- --text "Cannot access \"${image_dir}\"!"
- exit 3
- fi
- cd ${image_dir}
- image_file="$(cd ${image_dir} && find . -type f -print0 | \
- xargs -0 -I % basename % | ${notify_tool} \
- --list \
- --column "Files" \
- --title "${title}" \
- --text='Select an image file to write to flash disk')"
- if [ -z "${image_file}" ]; then
- _goodbye
- fi
- if [ ! -r "${image_dir}/${image_file}" ]; then
- ${notify_tool} \
- --error \
- --title "${title}" \
- --text "Cannot access \"${image_dir}/${image_file}\"!"
- exit 3
- fi
- ${notify_tool} \
- --question \
- --title "${title}" \
- --text "Write ${image_file} to ${devicename}?"
- rc=$?
- if [ ${rc} -ne 0 ]; then
- ${notify_tool} \
- --info \
- --title "${title}" \
- --text "Goodbye!"
- exit 1
- fi
- # Write to flashdisk
- dpv -dX -o ${devicename} -m `stat -f '%z' "${image_dir}/${image_file}"`:" \
- ${devicename}" "${image_dir}/${image_file}" |
- ${notify_tool} --progress \
- --title "${title}" \
- --text "Writing ${image_file} to ${devicename}" \
- --auto-close
- rc=$?
- if [ ${rc} -ne 0 ]; then
- _goodbye
- else
- ${notify_tool} \
- --info \
- --title "${title}" \
- --text "Image written successfully!"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement