Browse Source

Added functions to get device and ram size

Matteo Zeccoli Marazzini 5 years ago
parent
commit
079c4f1147
4 changed files with 56 additions and 26 deletions
  1. 26 0
      check-device.sh
  2. 8 24
      newdisk-automator.sh
  3. 13 2
      parse-arguments.sh
  4. 9 0
      utils.sh

+ 26 - 0
check-device.sh

@@ -0,0 +1,26 @@
+check_device()
+{
+	# checking if the disk is used
+	if [ ! -e ${DEVICE} ] ; then
+		fatal ${NOTEX_E} "${DEVICE} does not exist"
+	fi
+	if [ ! -b ${DEVICE} ] ; then
+		fatal ${NOTBLK_E} "${DEVICE} is not a block file"
+	fi
+	if grep --quiet "${DEVICE}" /proc/mounts ; then
+		fatal ${ISMOUNT_E} "${DEVICE} is mounted"
+	fi
+	if grep --quiet "${DEVICE}" /proc/swaps ; then
+		fatal ${ISSWAP_E} "${DEVICE} is udes as swap"
+	fi
+
+	if pvs | grep --quiet "${DEVICE}" ; then
+		if [ $(lvs --noheadings --select vg_name="$(pvs --noheadings --select pv_name=~"${DEVICE}" -o vg_name)" --select lv_device_open!=0 | wc -l) -ne 0 ] ; then
+			fatal ${LVMUSED_E} "${DEVICE} is part of a used lvm volume"
+		fi
+		if [ $(vgs --noheadings --select pv_name=~"${DEVICE}" -o pv_count | uniq) -gt $(vgs --noheadings --select pv_name=~"${DEVICE}" | wc -l) ] ; then
+			fatal ${LVMPV_E} "${DEVICE} is part of a lvm volume group shared with other physical volumes"
+		fi
+		LVM=1
+	fi
+}

+ 8 - 24
newdisk-automator.sh

@@ -4,10 +4,15 @@ DIR="/home/ravioli/newdisk-automator"
 
 . "${DIR}/error.sh"
 . "${DIR}/parse-arguments.sh"
+. "${DIR}/utils.sh"
+. "${DIR}/check-device.sh"
 
 PROGRAM_NAME="$0"
 
 parse_arguments $@
+echo $DEVICE_SIZE
+echo $RAM_SIZE
+exit
 
 DEVICE=$1
 SWAP_SIZE=$2
@@ -16,30 +21,9 @@ TEMP_SIZE=$4
 VM_SIZE=$5
 
 
-# checking if the disk is used
-if [ ! -e ${DEVICE} ] ; then
-	fatal ${NOTEX_E} "${DEVICE} does not exist"
-fi
-if [ ! -b ${DEVICE} ] ; then
-	fatal ${NOTBLK_E} "${DEVICE} is not a block file"
-fi
-if grep --quiet "${DEVICE}" /proc/mounts ; then
-	fatal ${ISMOUNT_E} "${DEVICE} is mounted"
-fi
-if grep --quiet "${DEVICE}" /proc/swaps ; then
-	fatal ${ISSWAP_E} "${DEVICE} is udes as swap"
-fi
-
-if pvs | grep --quiet "${DEVICE}" ; then
-	if [ $(lvs --noheadings --select vg_name="$(pvs --noheadings --select pv_name=~"${DEVICE}" -o vg_name)" --select lv_device_open!=0 | wc -l) -ne 0 ] ; then
-		fatal ${LVMUSED_E} "${DEVICE} is part of a used lvm volume"
-	fi
-	if [ $(vgs --noheadings --select pv_name=~"${DEVICE}" -o pv_count | uniq) -gt $(vgs --noheadings --select pv_name=~"${DEVICE}" | wc -l) ] ; then
-		fatal ${LVMPV_E} "${DEVICE} is part of a lvm volume group shared with other physical volumes"
-	fi
-
-	# removing lvm and other filesystem signatures)
-	echo "Eresing disk signatures..."
+# 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}

+ 13 - 2
parse-arguments.sh

@@ -10,10 +10,21 @@ parse_arguments()
 	shift `expr $OPTIND - 1`
 	
 	if [ $# -eq 1 ]; then
-		default
+		MODE=default
 	elif [ $# -eq 5 ] && [ $FORCE -ne 1 ]; then
-		manual
+		MODE=manual
 	else
 		usage && exit
 	fi
+	DEVICE="$1"
+	check_device
+	DEVICE_SIZE=$(blockdev --getsize64 "${DEVICE}")
+	RAM_SIZE=$(kibibytes_to_bytes $(grep MemTotal: /proc/meminfo | awk '{print $2}'))
+	$MODE $@
 }
+
+default()
+{
+	return
+}
+	

+ 9 - 0
utils.sh

@@ -0,0 +1,9 @@
+gibibytes_to_bytes()
+{
+	expr 1024 \* 1024 \* 1024 \* $1
+}
+
+kibibytes_to_bytes()
+{
+	expr 1024 \* $1
+}