parse-arguments.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 102G) + $RAM_SIZE ))
  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 $((1 + $(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 $((1 + $(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. WARNING! This program does not mount the new partitions, nor update the fstab: these operations have to be performed afterwards.
  78. Automatic:
  79. newdisk-automator [-qvf] device
  80. device is partitioned according to the following scheme:
  81. - calcolo: 100GB
  82. - swap: ram size + 1GB
  83. - free space: 10% of the total
  84. - condor: 40% of remaining space
  85. - tempdir: 60% of remaining space
  86. If the size of the disk is less than 102GB + ram size, this mode cannot be used.
  87. 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.
  88. 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.
  89. Manual:
  90. newdisk-automator [-qv] device temp_size condor_size swap_size vm_size
  91. The partitions are assigned the size passed on the command line. S, b, K, M, G can be appended to the sizes for 512-byte-sectors, bytes, kibibytes, mebibytes and gibibytes. The default is bytes.
  92. If the total passed with the command line is greater than the size of the disk, the program stops.
  93. help-end
  94. if [ "$PROGRAM_OUTPUT" = "/proc/self/fd/1" ]; then
  95. cat <<-error-end
  96. Error codes:
  97. 10 Usage error
  98. 11 Size units error
  99. 20 Device file does not exist
  100. 21 Device is not a block file
  101. 22 Device is mounted
  102. 23 Device is used as swap
  103. 24 Device is part of a used lvm volume
  104. 25 Device is part of a lvm volume group shared with other physical volumes
  105. 26 Device is not a regular disk (it may be a partition or another type of block file)
  106. 30 Device is too small to use default mode
  107. 31 Device does not have the reccomended size. Force with -f to use default mode
  108. 32 Device is smaller than the manually specified size
  109. 40 One of the programs called from the script returned a non 0 value. Check the stderr to get the code
  110. 41 The effective user id of the calling user is not 0. Only root can run this tool
  111. error-end
  112. fi
  113. }