newdisk-automator.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/sh
  2. DEVICE=$1
  3. SWAP_SIZE=$2
  4. CONDOR_SIZE=$3
  5. TEMP_SIZE=$4
  6. VM_SIZE=$5
  7. NOTEX_E=1
  8. NOTBLK_E=2
  9. ISMOUNT_E=3
  10. ISSWAP_E=4
  11. LVMUSED_E=5
  12. LVMPV_E=5
  13. # checking if the disk is used
  14. if [ ! -e ${DEVICE} ] ; then
  15. printf "$0: error: ${DEVICE} does not exist.\n" >&2
  16. exit ${NOTEX_E}
  17. fi
  18. if [ ! -b ${DEVICE} ] ; then
  19. printf "$0: error: ${DEVICE} is not a block file.\n" >&2
  20. exit ${NOTBLK_E}
  21. fi
  22. if grep --quiet "${DEVICE}" /proc/mounts ; then
  23. printf "$0: error: ${DEVICE} is mounted. Unmount it first.\n" >&2
  24. exit ${ISMOUNT_E}
  25. fi
  26. if grep --quiet "${DEVICE}" /proc/swaps ; then
  27. printf "$0: error: ${DEVICE} is used as swap. Swapoff it first.\n" >&2
  28. exit ${ISSWAP_E}
  29. fi
  30. if pvs | grep --quiet "${DEVICE}" ; then
  31. 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
  32. printf "$0: error: ${DEVICE} is part of a used lvm volume.\n" >&2
  33. exit ${LVMUSED_E}
  34. fi
  35. if [ $(vgs --noheadings --select pv_name=~"${DEVICE}" -o pv_count | uniq) -gt $(vgs --noheadings --select pv_name=~"${DEVICE}" | wc -l) ] ; then
  36. printf "$0: error: ${DEVICE} is part of a lvm volume group shared with others physical volumes.\n" >&2
  37. exit ${LVMPV_E}
  38. fi
  39. # removing lvm and other filesystem signatures)
  40. echo "Eresing disk signatures..."
  41. vgchange -an $(vgs --noheadings --select pv_name=~"${DEVICE}" -o vg_name)
  42. wipefs -a ${DEVICE}[[:digit:]]*
  43. wipefs -a ${DEVICE}
  44. fi
  45. echo "SFDISK ----------------------------------------------------"
  46. # partitioning the disk
  47. sfdisk "${DEVICE}" <<-sfdisk-script-end
  48. # Create a new partition table on the specified disk, and create a new Linux LVM partition
  49. #
  50. # partition table info
  51. label: gpt
  52. #
  53. # partitions info
  54. #start size type bootable
  55. , ,31 ,
  56. sfdisk-script-end
  57. # LVM
  58. echo "PVCREATE --------------------------------------------------"
  59. pvcreate "${DEVICE}1"
  60. echo "VGCREATE --------------------------------------------------"
  61. vgcreate localDisk "${DEVICE}1"
  62. if [ "${SWAP_SIZE}" != 0 ] ; then
  63. echo "LVCREATE SWAP --------------------------------------------------"
  64. lvcreate -W n -C y -L "${SWAP_SIZE}G" localDisk -n swap
  65. echo "MKSWAP --------------------------------------------------"
  66. mkswap /dev/localDisk/swap
  67. fi
  68. if [ "${CONDOR_SIZE}" != 0 ] ; then
  69. echo "LVCREATE CONDOR --------------------------------------------------"
  70. lvcreate -W n -L "${CONDOR_SIZE}G" localDisk -n condor
  71. echo "MKFS CONDOR --------------------------------------------------"
  72. mkfs.ext4 /dev/localDisk/condor
  73. fi
  74. if [ "${TEMP_SIZE}" != 0 ] ; then
  75. echo "LVCREATE TEMP --------------------------------------------------"
  76. lvcreate -W n -L "${TEMP_SIZE}G" localDisk -n tempdir
  77. echo "MKFS TEMP --------------------------------------------------"
  78. mkfs.ext4 /dev/localDisk/tempdir
  79. fi
  80. if [ "${VM_SIZE}" != 0 ] ; then
  81. echo "LVCREATE CALCOLO --------------------------------------------------"
  82. lvcreate -W n -L "${VM_SIZE}G" localDisk -n calcolo
  83. fi