parse-arguments.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. parse_arguments()
  2. {
  3. FORCE=0
  4. SCRIPT_OUTPUT=/proc/self/fd/1
  5. PROGRAM_OUTPUT=/dev/null
  6. while getopts ":fhvq" o; do
  7. case $o in
  8. f) FORCE=1;;
  9. h) help && exit 0;;
  10. v) SCRIPT_OUTPUT=/proc/self/fd/1 && PROGRAM_OUTPUT=/proc/self/fd/1;;
  11. q) SCRIPT_OUTPUT=/dev/null && PROGRAM_OUTPUT=/dev/null;;
  12. \?) usage && exit;;
  13. esac
  14. done
  15. shift $(( $OPTIND - 1 ))
  16. if [ $# -eq 1 ]; then
  17. MODE=default
  18. elif [ $# -eq 5 ] && [ $FORCE -ne 1 ]; then
  19. MODE=manual
  20. else
  21. usage && exit
  22. fi
  23. DEVICE="$1"
  24. check_device
  25. DEVICE_SIZE=$(blockdev --getsize64 "${DEVICE}")
  26. RAM_SIZE=$(kibibytes_to_bytes $(grep MemTotal: /proc/meminfo | awk '{print $2}'))
  27. $MODE $@
  28. }
  29. default()
  30. {
  31. MIN_SIZE=$(( $(gibibytes_to_bytes 100) + $RAM_SIZE + 2 ))
  32. BEST_SIZE=$(( $(gibibytes_to_bytes 200) + 2 * $RAM_SIZE ))
  33. if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then
  34. fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(bytes_to_gibibytes ${MIN_SIZE})GB".
  35. elif [ $DEVICE_SIZE -lt $BEST_SIZE ] && [ $FORCE -ne 1 ]; then
  36. 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".
  37. fi
  38. VM_SIZE=$(gibibytes_to_bytes 100)
  39. SWAP_SIZE=$(( $RAM_SIZE + $(gibibytes_to_bytes 1) ))
  40. FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 ))
  41. CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 ))
  42. TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 ))
  43. }
  44. manual()
  45. {
  46. TEMP_SIZE=$(to_bytes $2)
  47. if [ $(($TEMP_SIZE % 512)) -ne 0 ] ; then
  48. fatal ${CHUNK_E} "size must be multiple of 512 bytes"
  49. fi
  50. CONDOR_SIZE=$(to_bytes $3)
  51. SWAP_SIZE=$(to_bytes $4)
  52. VM_SIZE=$(to_bytes $5)
  53. if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then
  54. fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(bytes_to_gibibytes ${DEVICE_SIZE})G"
  55. fi
  56. }
  57. usage()
  58. {
  59. echo -e "Usage:\n${PROGRAM_NAME} [-qvf] device\n${PROGRAM_NAME} [-qv] device temp_size condor_size swap_size vm_size"
  60. echo "Try -h for help"
  61. }
  62. help()
  63. {
  64. cat <<-help-end
  65. newdisk automator
  66. This program can be used in two mode: automatic and manual.
  67. Automatic:
  68. newdisk-automator [-f] device
  69. device is partitioned according to the following scheme:
  70. - calcolo: 100GB
  71. - swap: ram size + 1GB
  72. - free space: 10% of the total
  73. - condor: 40% of remaining space
  74. - tempdir: 60% of remaining space
  75. If the size of the disk is less than 102GB + ram size, this mode cannot be used.
  76. If the size of the disk is greater than the minimum size, but still less than (100GB + ram size) * 2, to use this mode the -f option must be passed.
  77. Be careful: in the automatic mode, calcolo and swap size are fixed, and forcing it with -f could result in having very small tempdir and condor partitions.
  78. Manual:
  79. newdisk-automator device temp_size condor_size swap_size vm_size
  80. The partitions are assigned the size passed on the command line. b, K, M, G can be appended to the sizes for bytes, kibibytes, mebibytes and gibibytes. The default is bytes.
  81. If the total passed with the command line is greater than the size of the disk, the program stops.
  82. help-end
  83. }