Browse Source

Implemented functions to decide partition sizes

Matteo Zeccoli Marazzini 5 years ago
parent
commit
87f8f19405
4 changed files with 47 additions and 21 deletions
  1. 5 0
      error.sh
  2. 7 16
      newdisk-automator.sh
  3. 28 3
      parse-arguments.sh
  4. 7 2
      utils.sh

+ 5 - 0
error.sh

@@ -9,6 +9,11 @@ ISSWAP_E=4
 LVMUSED_E=5
 LVMPV_E=6
 
+# size errors
+MINSIZE_E=10
+BESTSIZE_E=11
+MANUALSIZE_E=12
+
 fatal()
 {
 	STRING=${2:-"generic error"}

+ 7 - 16
newdisk-automator.sh

@@ -10,25 +10,16 @@ DIR="/home/ravioli/newdisk-automator"
 PROGRAM_NAME="$0"
 
 parse_arguments $@
-echo $DEVICE_SIZE
-echo $RAM_SIZE
-exit
-
-DEVICE=$1
-SWAP_SIZE=$2
-CONDOR_SIZE=$3
-TEMP_SIZE=$4
-VM_SIZE=$5
-
 
 # removing lvm and other filesystem signatures)
 if [ $LVM == 1]
 	echo "Erasing disk signatures..."
 	vgchange -an $(vgs --noheadings --select pv_name=~"${DEVICE}" -o vg_name)
-	wipefs -a ${DEVICE}[[:digit:]]*
-	wipefs -a ${DEVICE}
 fi
 
+wipefs -a ${DEVICE}[[:digit:]]*
+wipefs -a ${DEVICE}
+
 echo "SFDISK ----------------------------------------------------"
 # partitioning the disk
 sfdisk "${DEVICE}" <<-sfdisk-script-end
@@ -50,23 +41,23 @@ vgcreate localDisk "${DEVICE}1"
 
 if [ "${SWAP_SIZE}" != 0 ] ; then
 	echo "LVCREATE  SWAP --------------------------------------------------"
-	lvcreate -W n -C y -L "${SWAP_SIZE}G" localDisk -n swap
+	lvcreate -W n -C y -L "${SWAP_SIZE}B" localDisk -n swap
 	echo "MKSWAP --------------------------------------------------"
 	mkswap /dev/localDisk/swap
 fi
 if [ "${CONDOR_SIZE}" != 0 ] ; then
 	echo "LVCREATE CONDOR --------------------------------------------------"
-	lvcreate -W n -L "${CONDOR_SIZE}G" localDisk -n condor
+	lvcreate -W n -L "${CONDOR_SIZE}B" localDisk -n condor
 	echo "MKFS CONDOR --------------------------------------------------"
 	mkfs.ext4 /dev/localDisk/condor
 fi
 if [ "${TEMP_SIZE}" != 0 ] ; then
 	echo "LVCREATE TEMP --------------------------------------------------"
-	lvcreate -W n -L "${TEMP_SIZE}G" localDisk -n tempdir
+	lvcreate -W n -L "${TEMP_SIZE}B" localDisk -n tempdir
 	echo "MKFS TEMP --------------------------------------------------"
 	mkfs.ext4 /dev/localDisk/tempdir
 fi
 if [ "${VM_SIZE}" != 0 ] ; then
 	echo "LVCREATE CALCOLO --------------------------------------------------"
-	lvcreate -W n -L "${VM_SIZE}G" localDisk -n calcolo
+	lvcreate -W n -L "${VM_SIZE}B" localDisk -n calcolo
 fi

+ 28 - 3
parse-arguments.sh

@@ -1,5 +1,6 @@
 parse_arguments()
 {
+	FORCE=0
 	while getopts ":fh" o; do
 		case $o in
 		f) FORCE=1;;
@@ -7,7 +8,7 @@ parse_arguments()
 		\?) usage && exit;;
 		esac
 	done
-	shift `expr $OPTIND - 1`
+	shift $(( $OPTIND - 1 ))
 	
 	if [ $# -eq 1 ]; then
 		MODE=default
@@ -25,6 +26,30 @@ parse_arguments()
 
 default()
 {
-	return
+	MIN_SIZE=$(( $(gibibytes_to_bytes 100) + $RAM_SIZE + 2 ))
+	echo "min size: $MIN_SIZE"
+	BEST_SIZE=$(( $(gibibytes_to_bytes 200) + 2 * $RAM_SIZE ))
+	echo "best size: $BEST_SIZE"
+
+	if [ $DEVICE_SIZE -lt $MIN_SIZE ]; then
+		fatal ${MINSIZE_E} "to use default mode, the disk must have at least $(bytes_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 $(bytes_to_gibibytes ${BEST_SIZE})GB. If you know what you are doing, you can force default mode with -f".
+	fi
+	VM_SIZE=$(gibibytes_to_bytes 100)
+	SWAP_SIZE=$(( $RAM_SIZE + $(gibibytes_to_bytes 1) ))
+	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=$(gibibytes_to_bytes $2)
+	CONDOR_SIZE=$(gibibytes_to_bytes $3)
+	SWAP_SIZE=$(gibibytes_to_bytes $4)
+	VM_SIZE=$(gibibytes_to_bytes $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 $(bytes_to_gibibytes ${DEVICE_SIZE})G"
+	fi
 }
-	

+ 7 - 2
utils.sh

@@ -1,9 +1,14 @@
 gibibytes_to_bytes()
 {
-	expr 1024 \* 1024 \* 1024 \* $1
+	echo $(( 1024 * 1024 * 1024 * $1 ))
 }
 
 kibibytes_to_bytes()
 {
-	expr 1024 \* $1
+	echo $(( 1024 * $1 ))
+}
+
+bytes_to_gibibytes()
+{
+	echo $(( $1 / 1024 / 1024 / 1024 ))
 }