Browse Source

Added checks on device

Matteo Zeccoli Marazzini 5 years ago
parent
commit
351319c73b
1 changed files with 60 additions and 2 deletions
  1. 60 2
      newdisk-automator.sh

+ 60 - 2
newdisk-automator.sh

@@ -4,8 +4,37 @@ CONDOR_SIZE=$3
 TEMP_SIZE=$4
 VM_SIZE=$5
 
-# oartitioning the disk
-sfdisk $DEVICE <<-sfdisk-script-end
+NOTEX_E=1
+NOTBLK_E=2
+ISMOUNT_E=3
+ISSWAP_E=4
+ISLVM_E=5
+
+# checking if the disk is used
+if [ ! -e ${DEVICE} ] ; then
+	printf "$0: error: ${DEVICE} does not exist.\n" >&2
+	exit ${NOTEX_E}
+fi
+if [ ! -b ${DEVICE} ] ; then
+	printf "$0: error: ${DEVICE} is not a block file.\n" >&2
+	exit ${NOTBLK_E}
+fi
+if grep --quiet "${DEVICE}" /proc/mounts ; then
+	printf "$0: error: ${DEVICE} is mounted. Unmount it first.\n" >&2
+	exit ${ISMOUNT_E}
+fi
+if grep --quiet "${DEVICE}" /proc/swaps ; then
+	printf "$0: error: ${DEVICE} is used as swap. Swapoff it first.\n" >&2
+	exit ${ISSWAP_E}
+fi
+if pvs --select pv_in_use=1 | grep --quiet "${DEVICE}" ; then
+	printf "$0: error: ${DEVICE} is part of a LVM volume group.\n" >&2
+	exit ${ISLVM_E}
+fi
+
+echo "SFDISK ----------------------------------------------------"
+# partitioning the disk
+sfdisk "${DEVICE}" <<-sfdisk-script-end
 	# Create a new partition table on the specified disk, and create a new Linux LVM partition
 	#
 	# partition table info
@@ -15,3 +44,32 @@ sfdisk $DEVICE <<-sfdisk-script-end
 	#start	size	type	bootable
 		,	,31	,
 sfdisk-script-end
+
+# LVM
+echo "PVCREATE --------------------------------------------------"
+pvcreate "${DEVICE}1"
+echo "VGCREATE --------------------------------------------------"
+vgcreate localDisk "${DEVICE}1"
+
+if [ "${SWAP_SIZE}" != 0 ] ; then
+	echo "LVCREATE  SWAP --------------------------------------------------"
+	lvcreate -C y -L "${SWAP_SIZE}G" localDisk -n swap
+	echo "MKSWAP --------------------------------------------------"
+	mkswap /dev/localDisk/swap
+fi
+if [ "${CONDOR_SIZE}" != 0 ] ; then
+	echo "LVCREATE CONDOR --------------------------------------------------"
+	lvcreate -L "${CONDOR_SIZE}G" localDisk -n condor
+	echo "MKFS CONDOR --------------------------------------------------"
+	mkfs.ext4 /dev/localDisk/condor
+fi
+if [ "${TEMP_SIZE}" != 0 ] ; then
+	echo "LVCREATE TEMP --------------------------------------------------"
+	lvcreate -L "${TEMP_SIZE}G" localDisk -n tempdir
+	echo "MKFS TEMP --------------------------------------------------"
+	mkfs.ext4 /dev/localDisk/tempdir
+fi
+if [ "${VM_SIZE}" != 0 ] ; then
+	echo "LVCREATE CALCOLO --------------------------------------------------"
+	lvcreate -L "${VM_SIZE}G" localDisk -n calcolo
+fi