parse_arguments() { FORCE=0 HELP=0 SCRIPT_OUTPUT=/proc/self/fd/1 PROGRAM_OUTPUT=/dev/null # Parse options while getopts ":fhvq" o; do case $o in f) FORCE=1;; h) HELP=1;; v) PROGRAM_OUTPUT=/proc/self/fd/1;; q) SCRIPT_OUTPUT=/dev/null;; \?) usage && exit;; esac done shift $(( $OPTIND - 1 )) if [ $HELP -eq 1 ]; then help exit fi # The 1st argument is the device. If no other argument is passed, the default mode is used. # If other 4 arguments are passed, the manual mode is used (the arguments are the sizes of the partitions) # Otherwise, the script exits if [ $# -eq 1 ]; then MODE=default elif [ $# -eq 5 ] && [ $FORCE -ne 1 ]; then MODE=manual else usage && exit fi DEVICE="$1" check_device DEVICE_SIZE=$(blockdev --getsz "${DEVICE}") RAM_SIZE=$(to_sectors $(grep MemTotal: /proc/meminfo | awk '{print $2}')K) $MODE $@ } default() { MIN_SIZE=$(( $(to_sectors 100G) + $RAM_SIZE + $(to_sectors 2G) )) BEST_SIZE=$(( $(to_sectors 200G) + 2 * $RAM_SIZE )) if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(sectors_to_gibibytes ${MIN_SIZE})GB". elif [ $DEVICE_SIZE -lt $BEST_SIZE ] && [ $FORCE -ne 1 ]; then 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". fi VM_SIZE=$(to_sectors 100G) SWAP_SIZE=$(( $RAM_SIZE + $(to_sectors 1G) )) FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 )) CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 )) TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 )) } manual() { TEMP_SIZE=$(to_sectors $2) CONDOR_SIZE=$(to_sectors $3) SWAP_SIZE=$(to_sectors $4) VM_SIZE=$(to_sectors $5) if [ $(( $TEMP_SIZE + $CONDOR_SIZE + $SWAP_SIZE + $VM_SIZE )) -gt $DEVICE_SIZE ]; then fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(sectors_to_gibibytes ${DEVICE_SIZE})G" fi } usage() { echo "Usage:" echo "${PROGRAM_NAME} [-qvf] device" echo "${PROGRAM_NAME} [-qv] device temp_size condor_size swap_size vm_size" echo "${PROGRAM_NAME} -h[v]" exit $USAGE_E } help() { cat <<-help-end newdisk automator This program can be used in two mode: automatic and manual. For help, use -h. For info about error codes, use both help and verbose flags. WARNING! This program does not mount the new partitions, nor update the fstab: these operations have to be performed afterwards. Automatic: newdisk-automator [-qvf] device device is partitioned according to the following scheme: - calcolo: 100GB - swap: ram size + 1GB - free space: 10% of the total - condor: 40% of remaining space - tempdir: 60% of remaining space If the size of the disk is less than 102GB + ram size, this mode cannot be used. 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. 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. Manual: newdisk-automator [-qv] device temp_size condor_size swap_size vm_size 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. If the total passed with the command line is greater than the size of the disk, the program stops. help-end if [ "$PROGRAM_OUTPUT" = "/proc/self/fd/1" ]; then cat <<-error-end Error codes: 10 Usage error 11 Size units error 20 Device file does not exist 21 Device is not a block file 22 Device is mounted 23 Device is used as swap 24 Device is part of a used lvm volume 25 Device is part of a lvm volume group shared with other physical volumes 26 Device is not a regular disk (it may be a partition or another type of block file) 30 Device is too small to use default mode 31 Device does not have the reccomended size. Force with -f to use default mode 32 Device is smaller than the manually specified size 40 One of the programs called from the script returned a non 0 value. Check the stderr to get the code 41 The effective user id of the calling user is not 0. Only root can run this tool error-end fi }