Advertisement
devinteske

image2flash.sh by @laemodost (lme@)

Aug 24th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.25 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # This script is launched by devd to flash an image file
  4. # to a flash disk.
  5.  
  6. # Copyright 2015 Lars Engels <lme@FreeBSD.org>
  7. # BSD-style license applies
  8.  
  9. image_dir="/ISOs"
  10.  
  11. notify_tool="/usr/local/bin/zenity"
  12. pipeview="/usr/bin/dpv"
  13.  
  14. title="Img2Flash"
  15.  
  16. _usage() {
  17.     echo "Usage: $(basename $0) [-I <image_dir>] <-d devicename>"
  18.     exit 2
  19. }
  20.  
  21. _goodbye() {
  22.     ${notify_tool} \
  23.       --error \
  24.       --title "${title}" \
  25.       --text 'Operation cancelled or an error occured!'
  26.     exit 1
  27. }
  28.  
  29. _check_dependency() {
  30.     if [ ! -f ${1} ]; then
  31.         echo "Cannot find ${1}!" >/dev/stderr
  32.         exit 3
  33.     fi
  34. }
  35.  
  36. args=$(getopt d:I: $*)
  37.  
  38. if [ $? -ne 0 ]; then
  39.     _usage
  40. fi
  41.  
  42. for dep in "$notify_tool" "$pipeview"; do
  43.     _check_dependency $dep
  44. done
  45.  
  46. set -- $args
  47.  
  48. while true; do
  49.     case "$1" in
  50.     -d)
  51.         devicename="$2"
  52.         shift; shift
  53.         ;;
  54.     -I)
  55.         image_dir="$2"
  56.         shift; shift
  57.         ;;
  58.         --)
  59.         shift; break
  60.         ;;
  61.         esac
  62. done
  63.  
  64. if [ -z "${devicename}" ]; then
  65.     _usage
  66. fi
  67.  
  68. if [ ! -d "${image_dir}" ]; then
  69.     ${notify_tool} \
  70.       --error \
  71.       --title "${title}" \
  72.       --text "Cannot access \"${image_dir}\"!"
  73.     exit 3
  74. fi
  75.  
  76. cd ${image_dir}
  77.  
  78. image_file="$(cd ${image_dir} && find . -type f -print0 | \
  79.  xargs -0 -I % basename % | ${notify_tool} \
  80.  --list \
  81.  --column "Files" \
  82.  --title "${title}" \
  83.  --text='Select an image file to write to flash disk')"
  84.  
  85. if [ -z "${image_file}" ]; then
  86.     _goodbye
  87. fi
  88.  
  89. if [ ! -r "${image_dir}/${image_file}" ]; then
  90.     ${notify_tool} \
  91.       --error \
  92.       --title "${title}" \
  93.       --text "Cannot access \"${image_dir}/${image_file}\"!"
  94.     exit 3
  95. fi
  96.  
  97. ${notify_tool} \
  98.   --question \
  99.   --title "${title}" \
  100.   --text "Write ${image_file} to ${devicename}?"
  101.  
  102. rc=$?
  103.  
  104. if [ ${rc} -ne 0 ]; then
  105.     ${notify_tool} \
  106.       --info \
  107.       --title "${title}" \
  108.       --text "Goodbye!"
  109.       exit 1
  110. fi
  111.  
  112. # Write to flashdisk
  113. dpv -dX -o ${devicename} -m `stat -f '%z' "${image_dir}/${image_file}"`:" \
  114.  ${devicename}" "${image_dir}/${image_file}" |
  115.   ${notify_tool} --progress \
  116.   --title "${title}" \
  117.   --text "Writing ${image_file} to ${devicename}" \
  118.   --auto-close
  119.  
  120. rc=$?
  121.  
  122. if [ ${rc} -ne 0 ]; then
  123.     _goodbye
  124. else
  125.     ${notify_tool} \
  126.       --info \
  127.       --title "${title}" \
  128.       --text "Image written successfully!"
  129. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement