parse-arguments.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. parse_arguments()
  2. {
  3. FORCE=0
  4. while getopts ":fh" o; do
  5. case $o in
  6. f) FORCE=1;;
  7. h) help && 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. echo "min size: $MIN_SIZE"
  29. BEST_SIZE=$(( $(gibibytes_to_bytes 200) + 2 * $RAM_SIZE ))
  30. echo "best size: $BEST_SIZE"
  31. if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then
  32. fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(bytes_to_gibibytes ${MIN_SIZE})GB".
  33. elif [ $DEVICE_SIZE -lt $BEST_SIZE ] && [ $FORCE -ne 1 ]; then
  34. 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".
  35. fi
  36. VM_SIZE=$(gibibytes_to_bytes 100)
  37. SWAP_SIZE=$(( $RAM_SIZE + $(gibibytes_to_bytes 1) ))
  38. FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 ))
  39. CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 ))
  40. TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 ))
  41. }
  42. manual()
  43. {
  44. TEMP_SIZE=$(gibibytes_to_bytes $2)
  45. CONDOR_SIZE=$(gibibytes_to_bytes $3)
  46. SWAP_SIZE=$(gibibytes_to_bytes $4)
  47. VM_SIZE=$(gibibytes_to_bytes $5)
  48. if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then
  49. fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(bytes_to_gibibytes ${DEVICE_SIZE})G"
  50. fi
  51. }