Browse Source

Addec checks to test if disk is used and added automatic deletion of lvm

Matteo Zeccoli Marazzini 5 years ago
parent
commit
b904698235
1 changed files with 30 additions and 16 deletions
  1. 30 16
      newdisk-automator.sh

+ 30 - 16
newdisk-automator.sh

@@ -1,3 +1,5 @@
+#!/bin/sh
+
 DEVICE=$1
 SWAP_SIZE=$2
 CONDOR_SIZE=$3
@@ -8,7 +10,8 @@ NOTEX_E=1
 NOTBLK_E=2
 ISMOUNT_E=3
 ISSWAP_E=4
-ISLVM_E=5
+LVMUSED_E=5
+LVMPV_E=5
 
 # checking if the disk is used
 if [ ! -e ${DEVICE} ] ; then
@@ -27,22 +30,33 @@ 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}
+
+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
+		printf "$0: error: ${DEVICE} is part of a used lvm volume.\n" >&2
+		exit ${LVMUSED_E}
+	fi
+	if [ $(vgs --noheadings --select pv_name=~"${DEVICE}" -o pv_count | uniq) -gt $(vgs --noheadings --select pv_name=~"${DEVICE}" | wc -l) ] ; then
+		printf "$0: error: ${DEVICE} is part of a lvm volume group shared with others physical volumes.\n" >&2
+		exit ${LVMPV_E}
+	fi
+
+	# removing lvm
+	vgremove -f $(vgs --noheadings --select pv_name=~"${DEVICE}" -o vg_name)
+	pvremove $(pvs --noheadings --select pv_name=~"${DEVICE}" -o pv_name)
 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
-	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
@@ -53,23 +67,23 @@ vgcreate localDisk "${DEVICE}1"
 
 if [ "${SWAP_SIZE}" != 0 ] ; then
 	echo "LVCREATE  SWAP --------------------------------------------------"
-	lvcreate -C y -L "${SWAP_SIZE}G" localDisk -n swap
+	lvcreate -W n -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
+	lvcreate -W n -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
+	lvcreate -W n -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
+	lvcreate -W n -L "${VM_SIZE}G" localDisk -n calcolo
 fi