parse-arguments.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 ))
  50. ## Condor partition is 40% of remaining space
  51. CONDOR_SIZE=$(( $FREE_SIZE * 4 / 10 ))
  52. ## Scratch partition is 60% of remaining space
  53. TEMP_SIZE=$(( $FREE_SIZE * 6 / 10 ))
  54. }
  55. manual()
  56. {
  57. TEMP_SIZE=$(to_sectors $2)
  58. CONDOR_SIZE=$(to_sectors $3)
  59. SWAP_SIZE=$(to_sectors $4)
  60. VM_SIZE=$(to_sectors $5)
  61. if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then
  62. fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(sectors_to_gibibytes ${DEVICE_SIZE})G"
  63. fi
  64. }
  65. usage()
  66. {
  67. echo "Usage:"
  68. echo "${PROGRAM_NAME} [-qvf] device"
  69. echo "${PROGRAM_NAME} [-qv] device temp_size condor_size swap_size vm_size"
  70. echo "${PROGRAM_NAME} -h[v]"
  71. exit $USAGE_E
  72. }
  73. help()
  74. {
  75. cat <<-help-end
  76. newdisk automator
  77. This program can be used in two mode: automatic and manual.
  78. For help, use -h. For info about error codes, use both help and verbose flags.
  79. WARNING! This program does not mount the new partitions, nor update the fstab: these operations have to be performed afterwards.
  80. Automatic:
  81. newdisk-automator [-qvf] device
  82. device is partitioned according to the following scheme:
  83. - calcolo: 100GB
  84. - swap: ram size + 1GB
  85. - free space: 10% of the total
  86. - condor: 40% of remaining space
  87. - tempdir: 60% of remaining space
  88. If the size of the disk is less than 102GB + ram size, this mode cannot be used.
  89. 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.
  90. 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.
  91. Manual:
  92. newdisk-automator [-qv] device temp_size condor_size swap_size vm_size
  93. 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.
  94. If the total passed with the command line is greater than the size of the disk, the program stops.
  95. help-end
  96. if [ "$PROGRAM_OUTPUT" = "/proc/self/fd/1" ]; then
  97. cat <<-error-end
  98. Error codes:
  99. 10 Usage error
  100. 11 Size units error
  101. 20 Device file does not exist
  102. 21 Device is not a block file
  103. 22 Device is mounted
  104. 23 Device is used as swap
  105. 24 Device is part of a used lvm volume
  106. 25 Device is part of a lvm volume group shared with other physical volumes
  107. 26 Device is not a regular disk (it may be a partition or another type of block file)
  108. 30 Device is too small to use default mode
  109. 31 Device does not have the reccomended size. Force with -f to use default mode
  110. 32 Device is smaller than the manually specified size
  111. 40 One of the programs called from the script returned a non 0 value. Check the stderr to get the code
  112. 41 The effective user id of the calling user is not 0. Only root can run this tool
  113. error-end
  114. fi
  115. }