parse-arguments.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. parse_arguments()
  2. {
  3. FORCE=0
  4. HELP=0
  5. SCRIPT_OUTPUT=/proc/self/fd/1
  6. PROGRAM_OUTPUT=/dev/null
  7. # Parse options
  8. while getopts ":fhvq" o; do
  9. case $o in
  10. f) FORCE=1;;
  11. h) HELP=1;;
  12. v) PROGRAM_OUTPUT=/proc/self/fd/1;;
  13. q) SCRIPT_OUTPUT=/dev/null;;
  14. \?) usage && exit;;
  15. esac
  16. done
  17. shift $(( $OPTIND - 1 ))
  18. if [ $HELP -eq 1 ]; then
  19. help
  20. exit
  21. fi
  22. # The 1st argument is the device. If no other argument is passed, the default mode is used.
  23. # If other 4 arguments are passed, the manual mode is used (the arguments are the sizes of the partitions)
  24. # Otherwise, the script exits
  25. if [ $# -eq 1 ]; then
  26. MODE=default
  27. elif [ $# -eq 5 ] && [ $FORCE -ne 1 ]; then
  28. MODE=manual
  29. else
  30. usage && exit
  31. fi
  32. DEVICE="$1"
  33. check_device
  34. DEVICE_SIZE=$(blockdev --getsz "${DEVICE}")
  35. RAM_SIZE=$(to_sectors $(grep MemTotal: /proc/meminfo | awk '{print $2}')K)
  36. $MODE $@
  37. }
  38. default()
  39. {
  40. MIN_SIZE=$(( $(to_sectors 100G) + $RAM_SIZE + $(to_sectors 2G) ))
  41. BEST_SIZE=$(( $(to_sectors 200G) + 2 * $RAM_SIZE ))
  42. if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then
  43. fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(sectors_to_gibibytes ${MIN_SIZE})GB".
  44. elif [ $DEVICE_SIZE -lt $BEST_SIZE ] && [ $FORCE -ne 1 ]; then
  45. fatal ${BESTSIZE_E} "to use default mode, it is reccomended that the disk have at least $(sectors_to_gibibytes ${BEST_SIZE})GB. If you know what you are doing, you can force default mode with -f".
  46. fi
  47. VM_SIZE=$(to_sectors 100G)
  48. SWAP_SIZE=$(( $RAM_SIZE + $(to_sectors 1G) ))
  49. FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 ))
  50. CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 ))
  51. TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 ))
  52. }
  53. manual()
  54. {
  55. TEMP_SIZE=$(to_sectors $2)
  56. CONDOR_SIZE=$(to_sectors $3)
  57. SWAP_SIZE=$(to_sectors $4)
  58. VM_SIZE=$(to_sectors $5)
  59. if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then
  60. fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(sectors_to_gibibytes ${DEVICE_SIZE})G"
  61. fi
  62. }
  63. usage()
  64. {
  65. echo "Usage:"
  66. echo "${PROGRAM_NAME} [-qvf] device"
  67. echo "${PROGRAM_NAME} [-qv] device temp_size condor_size swap_size vm_size"
  68. echo "${PROGRAM_NAME} -h[v]"
  69. exit USAGE_E
  70. }
  71. help()
  72. {
  73. cat <<-help-end
  74. newdisk automator
  75. This program can be used in two mode: automatic and manual.
  76. For help, use -h. For info about error codes, use both help and verbose flags.
  77. Automatic:
  78. newdisk-automator [-qvf] device
  79. device is partitioned according to the following scheme:
  80. - calcolo: 100GB
  81. - swap: ram size + 1GB
  82. - free space: 10% of the total
  83. - condor: 40% of remaining space
  84. - tempdir: 60% of remaining space
  85. If the size of the disk is less than 102GB + ram size, this mode cannot be used.
  86. 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.
  87. 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.
  88. Manual:
  89. newdisk-automator [-qv] device temp_size condor_size swap_size vm_size
  90. 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.
  91. If the total passed with the command line is greater than the size of the disk, the program stops.
  92. help-end
  93. if [ "$PROGRAM_OUTPUT" = "/proc/self/fd/1" ]; then
  94. cat <<-error-end
  95. Error codes:
  96. 10 Usage error
  97. 11 Size units error
  98. 20 Device file does not exist
  99. 21 Device is not a block file
  100. 22 Device is mounted
  101. 23 Device is used as swap
  102. 24 Device is part of a used lvm volume
  103. 25 Device is part of a lvm volume group shared with other physical volumes
  104. 26 Device is not a regular disk (it may be a partition or another type of block file)
  105. 30 Device is too small to use default mode
  106. 31 Device does not have the reccomended size. Force with -f to use default mode
  107. 32 Device is smaller than the manually specified size
  108. 40 One of the programs called from the script returned a non 0 value. Check the stderr to get the code
  109. error-end
  110. fi
  111. }