nvme-setup.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. #
  3. # Assumptions:
  4. #
  5. # 1] we're on aws
  6. # 2] any ephemeral devices are ours to take
  7. # 3] if no ephemeral devices are available then there's a list
  8. # of hard-coded block devices to use for the splunk hot VG
  9. #
  10. #--------------------------------------------------------------------
  11. exec > /dev/console
  12. exec 2>&1
  13. HOT_VG_NAME="vg_splunkhot"
  14. HOT_LV_NAME="lv_splunkhot"
  15. # These are the *HARD-CODED* volumes that we will use when no
  16. # ephemeral disks are available
  17. HOT_EBS_VOLUMES="xvdg xvdh"
  18. CURL="curl -f --connect-timeout 1 --silent"
  19. declare -A EBSMAP
  20. # Yes heavy assumption we're on AWS
  21. INSTANCE_TYPE="`${CURL} http://169.254.169.254/latest/meta-data/instance-type`"
  22. if [[ "$INSTANCE_TYPE" == "" ]]; then
  23. echo "Could not figure out instance type, giving up"
  24. exit 1
  25. fi
  26. # Build a map of EBS NVMe disks from their AWS-API-name to their NVMe name
  27. # this makes an associative array (like a python hash) of the
  28. # sdX/xvdX name you'd set in AWS API to the corresponding nvmeX name
  29. # Thanks Fred for the awesome id-ctrl stuff I'd never seen before
  30. #
  31. # One interesting side effect observed: the id-ctrl output is different when
  32. # volumes are attached at boot time (no /dev/) versus attached after the OS
  33. # is started (includes /dev/)
  34. function make_nve_ebs_map {
  35. for DEVICE in $( lsblk -d -o NAME,MODEL -n | egrep "Elastic Block Store" | awk '{ print $1 }' ); do
  36. UNDERLYING=$( nvme id-ctrl --raw-binary /dev/${DEVICE} 2>/dev/null | cut -c 3073-3104 | tr -d ' ' | sed "s#/dev/##" )
  37. EBSMAP[$UNDERLYING]=$DEVICE
  38. UNDERLYING2=$( echo $UNDERLYING | sed "s/sd/xvd/" )
  39. EBSMAP[$UNDERLYING2]=$DEVICE
  40. done
  41. }
  42. DEVS=""
  43. # Look for ephemeral NVMe disks
  44. EPHEMERAL_DISKS=$( lsblk -d -o NAME,SIZE,TYPE,MODEL,SERIAL | egrep "Amazon EC2 NVMe Instance Storage" | awk '{ print "/dev/"$1 }' )
  45. if [[ "${EPHEMERAL_DISKS}" != "" ]]; then
  46. # We have some ephemeral disks lets use them
  47. # This is the happy path
  48. DEVS="${EPHEMERAL_DISKS}"
  49. else
  50. # Looking for the hard-coded volumes above to come attached. They
  51. # could be attached immediately, or it could take a "couple of minutes"
  52. # as they are created and attached by terraform.
  53. # Checking for both the normal attachment and the NVMe form
  54. for VOLUME in $HOT_EBS_VOLUMES; do
  55. DONE=0
  56. TRIES=0
  57. while [[ $DONE -ne 1 ]] && [[ $TRIES -lt 20 ]]; do
  58. echo "Looking for $VOLUME to come attached"
  59. make_nve_ebs_map
  60. #echo "------- current nvme/ebs map -------"
  61. #for K in "${!EBSMAP[@]}"; do echo $K = ${EBSMAP[$K]} ; done
  62. #echo "------- end current nvme/ebs map -------"
  63. if [[ -b /dev/$VOLUME ]]; then
  64. DEVS="/dev/$VOLUME $DEVS"
  65. DONE=1
  66. elif [[ -b /dev/${EBSMAP[$VOLUME]} ]]; then
  67. DEVS="/dev/${EBSMAP[$VOLUME]} $DEVS"
  68. DONE=1
  69. else
  70. sleep 10
  71. TRIES=$(( $TRIES + 1 ))
  72. fi
  73. done
  74. done
  75. fi
  76. if [[ "$DEVS" == "" ]]; then
  77. echo "Failed to enumerate possible devices, oops"
  78. exit 1
  79. fi
  80. DEVCOUNT=`echo $DEVS | wc -w`
  81. # See if the volume group already exists - if not let's make it
  82. if ! vgs --noheadings ${HOT_VG_NAME} >/dev/null 2>&1; then
  83. echo "Making VG on devices ${DEVS}"
  84. vgcreate ${HOT_VG_NAME} ${DEVS}
  85. fi
  86. # See if the logical volume already exists - if not make it
  87. # and also make the filesystem
  88. if ! lvs --noheadings ${HOT_VG_NAME}/${HOT_LV_NAME} >/dev/null 2>&1; then
  89. echo "Making LV"
  90. lvcreate -l 100%FREE --stripes $DEVCOUNT --name ${HOT_LV_NAME} ${HOT_VG_NAME}
  91. mkfs -t ext4 /dev/${HOT_VG_NAME}/${HOT_LV_NAME}
  92. fi
  93. if ! egrep -q "/dev/${HOT_VG_NAME}/${HOT_LV_NAME}" /etc/fstab; then
  94. echo "Adding to fstab"
  95. echo "/dev/vg_splunkhot/lv_splunkhot /opt/splunkdata/hot ext4 nofail,noatime 0 0" >> /etc/fstab
  96. fi
  97. if [[ ! -d /opt/splunkdata/hot ]]; then
  98. echo "Creating mount directories"
  99. mkdir -p /opt/splunkdata/hot
  100. fi
  101. if ! mountpoint /opt/splunkdata/hot >/dev/null 2>&1; then
  102. echo "Mounting it"
  103. mount /opt/splunkdata/hot
  104. fi
  105. # Looking for splunk user, trying to fix up ownerships in case they got lost
  106. # This commonly happens when an ephemeral storage box loses its ephemeral storage
  107. # but everything else survives
  108. if getent passwd splunk | egrep -q splunk; then
  109. echo "Changing ownership of /opt/splunkdata and /opt/splunkdata/hot"
  110. chown splunk:splunk /opt/splunkdata
  111. chown splunk:splunk /opt/splunkdata/hot
  112. fi