Advertisement
Guest User

ceph_partition_disk.sh

a guest
Aug 24th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.97 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # This script is a wrapper around ceph-disk to create ceph OSD disk. It can
  4. # handle creation of OSDs on partitions and whole disks.
  5. #
  6. # The companion script 'disk_check.sh' checks if a disk|partition is allready
  7. # an OSD and does some extra checking.
  8. #
  9. # Written by Richard Arends, mainly for puppet-ceph:
  10. #   https://github.com/stackforge/puppet-ceph
  11. #
  12. # Exit codes:
  13. #   100: $DISK_CHECK_PROG returned an exit code other then '0'
  14. #   101: No arguments received
  15. #   102: Invalid argument to option -t (type).
  16. #   103: Invalid argument. Only -d and -t are allowed
  17. #   999: An error was triggered, but we are not sure where it came from
  18. #  
  19.  
  20. TYPE="unknown"
  21. DEVICE="unknown"
  22. CLUSTER_NAME="ceph"
  23. CLUSTER_OPTION=""
  24.  
  25. DISK_CHECK_PROG="/path/to/disk_check.sh"
  26.  
  27. Usage() {
  28.     echo " "
  29.     echo "Usage: $0 -t [partition|disk] -d [device] -j <journal> -c <cluster>"
  30.     echo " "
  31.     echo "Example:"
  32.     echo "           $0 -t disk -d /dev/sdd"
  33.     echo "           $0 -t partition -d /dev/sdg"
  34. }
  35.  
  36. Error() {
  37.     ERROR_MESSAGE=$1
  38.     E_CODE=$2
  39.  
  40.     if [ ! -z "${ERROR_MESSAGE}" ]; then
  41.         echo "ERROR: ${ERROR_MESSAGE}"
  42.     else
  43.         echo "ERROR"
  44.     fi
  45.  
  46.     if [ $E_CODE ] && [ $E_CODE -gt 0 ]; then
  47.         exit $E_CODE
  48.     else
  49.         exit 999
  50.     fi
  51. }
  52.  
  53. while getopts ":t:d:j:c:" OPT; do
  54.     case "${OPT}" in
  55.         c)
  56.             CLUSTER_NAME="${OPTARG}"
  57.             CLUSTER_OPTION="--cluster ${CLUSTER_NAME}"
  58.         ;;
  59.         j)
  60.             JOURNAL="${OPTARG}"
  61.         ;;
  62.         d)
  63.             DEVICE="${OPTARG}"
  64.         ;;
  65.         t)
  66.             TYPE="${OPTARG}"
  67.         ;;
  68.         \?)
  69.             echo "Invalid option: -$OPTARG" >&2
  70.             exit 103
  71.         ;;
  72.     esac
  73. done
  74.  
  75. if [ "${TYPE}" == "unknown" ] || [ "${DEVICE}" == "unknown" ]; then
  76.     Usage
  77.     exit 101
  78. fi
  79.  
  80. case "${TYPE}" in
  81.     partition)
  82.         case "$(${DISK_CHECK_PROG} ${DEVICE})" in
  83.             1)
  84.                 PARTITION=$(sgdisk --print "${DEVICE}"|tail -1|awk '{print $1+1}')
  85.                 OSD_UUID=$(uuidgen -r)
  86.                 PTYPE_UUID="4fbd7e29-9d25-41b8-afd0-062c0ceff05d"
  87.  
  88.                 sgdisk --largest-new=${PARTITION} \
  89.                   --change-name="${PARTITION}:ceph data" \
  90.                   --partition-guid=${PARTITION}:${OSD_UUID} \
  91.                   --typecode=${PARTITION}:${PTYPE_UUID} -- "${DEVICE}" || Error
  92.  
  93.                 partprobe || Error
  94.  
  95.                 ceph-disk prepare ${CLUSTER_OPTION} ${DEVICE}${PARTITION} || Error
  96.                 ceph-disk activate ${DEVICE}${PARTITION} || Error
  97.             ;;
  98.             0)
  99.                 echo "An 'ceph data' partition is allready present on device ${DEVICE}"
  100.             ;;
  101.             *)
  102.                 echo "${DISK_CHECK_PROG} returned an exit code other then '1' or '0'. An error happend?"
  103.                 exit 100
  104.             ;;
  105.         esac
  106.     ;;
  107.     disk)
  108.         case "$(${DISK_CHECK_PROG} ${DEVICE})" in
  109.             1)
  110.                 ## ???  mkdir -p ${data}
  111.                 ceph-disk prepare ${CLUSTER_OPTION} ${DEVICE} ${JOURNAL} || Error
  112.                 ceph-disk activate ${DEVICE} || Error
  113.             ;;
  114.             0)
  115.                 echo "Device ${DEVICE} is allready a ceph OSD"
  116.             ;;
  117.             *)
  118.                 echo "${DISK_CHECK_PROG} returned an exit code other then '1' or '0'. An error happend?"
  119.                 exit 100
  120.             ;;
  121.         esac
  122.     ;;
  123.     *)
  124.         echo "Invalid argument ${TYPE} to option -t (type). Must be set to <partition> or <disk>"
  125.         Usage
  126.         exit 102
  127.     ;;
  128. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement