Browse Source

All sizes are now in sectors

Matteo Zeccoli Marazzini 5 years ago
parent
commit
739d507e25
3 changed files with 36 additions and 24 deletions
  1. 5 0
      conversion.sh
  2. 15 13
      newdisk-automator
  3. 16 11
      parse-arguments.sh

+ 5 - 0
utils.sh → conversion.sh

@@ -39,3 +39,8 @@ to_sectors()
 {
 	bytes_to_sectors $( to_bytes "${1}" )
 }
+
+sectors_to_gibibytes()
+{
+	echo $(bytes_to_gibibytes $(( 512 * "${1}" )) )
+}

+ 15 - 13
newdisk-automator

@@ -5,11 +5,9 @@ PROGRAM_NAME="$0"
 
 . "${DIR}/error.sh"
 . "${DIR}/parse-arguments.sh"
-. "${DIR}/utils.sh"
+. "${DIR}/conversion.sh"
 . "${DIR}/check-device.sh"
 
-LVM_SUPPRESS_FD_WARNINGS=1
-
 parse_arguments $@
 
 # removing lvm and other filesystem signatures
@@ -17,31 +15,34 @@ if [ $LVM == 1 ]; then
 	vgchange -an $(vgs --noheadings --select pv_name=~"${DEVICE}" -o vg_name) >${PROGRAM_OUTPUT} 2>&1 
 fi
 
+# Erase all disk signatures form the device and - if they exist - from all its partitions
 echo "Erasing disk signatures..." >${SCRIPT_OUTPUT}
 if [ -e ${DEVICE}1 ]; then
 	wipefs -a ${DEVICE}[[:digit:]]* >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "wipefs returned $?"
 fi
 wipefs -a ${DEVICE} >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "wipefs returned $%"
 
+# Partition the disk creating a gpt and a single partition occupying the whole remaining disk space
 echo "Partitioning the disk..." >${SCRIPT_OUTPUT}
-# partitioning the disk
 sfdisk "${DEVICE}" <<-sfdisk-script-end >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "sfdisk returned $?"
-# Create a new partition table on the specified disk, and create a new Linux LVM partition
-#
-# partition table info
-label: gpt
-#
-# partitions info
-#start	size	type	bootable
-,	,31	,
+	# Create a new partition table on the specified disk, and create a new Linux LVM partition
+	#
+	# partition table info
+	label: gpt
+	#
+	# partitions info
+	#start	size	type	bootable
+		,	,31	,
 sfdisk-script-end
 
-# LVM
+# Create physical volume on device and then volume group localDisk
 echo "Creating physical volume ${DEVICE}1..." >${SCRIPT_OUTPUT}
 pvcreate "${DEVICE}1" >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "pvcreate returned $?"
 echo "Creating volume group localDisk..." >${SCRIPT_OUTPUT}
 vgcreate localDisk "${DEVICE}1" >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "vgcreate returned $?"
 
+# Create local volumes and partition them.
+# If the specified size is 0, it means that the volume must not be created
 echo ${SWAP_SIZE}
 if [ "${SWAP_SIZE}" != 0 ] ; then
 	echo "Creating logical volume swap..." >${SCRIPT_OUTPUT}
@@ -64,4 +65,5 @@ fi
 if [ "${VM_SIZE}" != 0 ] ; then
 	echo "Creating logical volume calcolo..." >${SCRIPT_OUTPUT}
 	lvcreate -W n --size "${VM_SIZE}S" localDisk -n calcolo >${PROGRAM_OUTPUT} 2>&1 || fatal ${PROG_E} "lvcreate returned $? for vm lv"
+	# The vm logical volume is not partitioned
 fi

+ 16 - 11
parse-arguments.sh

@@ -3,6 +3,8 @@ parse_arguments()
 	FORCE=0
 	SCRIPT_OUTPUT=/proc/self/fd/1
 	PROGRAM_OUTPUT=/dev/null
+
+	# Parse options
 	while getopts ":fhvq" o; do
 		case $o in
 		f) FORCE=1;;
@@ -14,6 +16,9 @@ parse_arguments()
 	done
 	shift $(( $OPTIND - 1 ))
 
+	# 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
@@ -23,26 +28,26 @@ parse_arguments()
 	fi
 	DEVICE="$1"
 	check_device
-	DEVICE_SIZE=$(blockdev --getsize64 "${DEVICE}")
-	RAM_SIZE=$(kibibytes_to_bytes $(grep MemTotal: /proc/meminfo | awk '{print $2}'))
+	DEVICE_SIZE=$(blockdev --getsz "${DEVICE}")
+	RAM_SIZE=$(to_sectors $(grep MemTotal: /proc/meminfo | awk '{print $2}')K)
 	$MODE $@
 }
 
 default()
 {
-	MIN_SIZE=$(( $(gibibytes_to_bytes 100) + $RAM_SIZE + 2 ))
-	BEST_SIZE=$(( $(gibibytes_to_bytes 200) + 2 * $RAM_SIZE ))
+	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 $(bytes_to_gibibytes ${MIN_SIZE})GB".
+		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 $(bytes_to_gibibytes ${BEST_SIZE})GB. If you know what you are doing, you can force default mode with -f".
+		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=$(to_sectors $(( $RAM_SIZE + $(gibibytes_to_bytes 1) )) )
+	SWAP_SIZE=$(( $RAM_SIZE + $(to_sectors 1G) ))
 	FREE_SIZE=$(( ( $DEVICE_SIZE - $VM_SIZE - $SWAP_SIZE ) / 10 ))
-	CONDOR_SIZE=$(to_sectors $(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 )) )
-	TEMP_SIZE=$(to_sectors $(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 )) )
+	CONDOR_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 4 ))
+	TEMP_SIZE=$(( ( $DEVICE_SIZE - $FREE_SIZE ) / 10 * 6 ))
 }
 
 manual()
@@ -51,8 +56,8 @@ manual()
 	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 $(to_sectors $DEVICE_SIZE) ]; then
-		fatal ${MANUALSIZE_E} "can't use this sizes: ${DEVICE} has only $(bytes_to_gibibytes ${DEVICE_SIZE})G"
+	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
 }