parse-arguments.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. parse_arguments()
  2. {
  3. FORCE=0
  4. while getopts ":fh" o; do
  5. case $o in
  6. f) FORCE=1;;
  7. h) cat ${DIR}/helpfile && exit 0;;
  8. \?) usage && exit;;
  9. esac
  10. done
  11. shift $(( $OPTIND - 1 ))
  12. if [ $# -eq 1 ]; then
  13. MODE=default
  14. elif [ $# -eq 5 ] && [ $FORCE -ne 1 ]; then
  15. MODE=manual
  16. else
  17. usage && exit
  18. fi
  19. DEVICE="$1"
  20. check_device
  21. DEVICE_SIZE=$(blockdev --getsize64 "${DEVICE}")
  22. RAM_SIZE=$(kibibytes_to_bytes $(grep MemTotal: /proc/meminfo | awk '{print $2}'))
  23. $MODE $@
  24. }
  25. default()
  26. {
  27. MIN_SIZE=$(( $(gibibytes_to_bytes 100) + $RAM_SIZE + 2 ))
  28. BEST_SIZE=$(( $(gibibytes_to_bytes 200) + 2 * $RAM_SIZE ))
  29. if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then
  30. fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(bytes_to_gibibytes ${MIN_SIZE})GB".
  31. elif [ $DEVICE_SIZE -lt $BEST_SIZE ] && [ $FORCE -ne 1 ]; then
  32. fatal ${BESTSIZE_E} "to use default mode, it is reccomended that the disk have at least $(bytes_to_gibibytes ${BEST_SIZE})GB. If you know what you are doing, you can force default mode with -f".
  33. fi
  34. VM_SIZE=$(gibibytes_to_bytes 100)
  35. SWAP_SIZE=$(( $RAM_SIZE + $(gibibytes_to_bytes 1) ))
  36. FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 ))
  37. CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 ))
  38. TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 ))
  39. }
  40. manual()
  41. {
  42. TEMP_SIZE=$(to_bytes $2)
  43. if [ $(($TEMP_SIZE % 512)) -ne 0 ] ; then
  44. fatal ${CHUNK_E} "size must be multiple of 512 bytes"
  45. fi
  46. CONDOR_SIZE=$(to_bytes $3)
  47. SWAP_SIZE=$(to_bytes $4)
  48. VM_SIZE=$(to_bytes $5)
  49. if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then
  50. fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(bytes_to_gibibytes ${DEVICE_SIZE})G"
  51. fi
  52. }
  53. usage()
  54. {
  55. echo "Usage: ${PROGRAM_NAME} [-f] device | device temp condor swap vm"
  56. echo "Try -h for help"
  57. }