repo_server_volumes.boothook 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. #
  3. #
  4. cloud-init-per once msoc_ebs_vols sudo bash - << 'OUTEREOF'
  5. SUDO=""
  6. function get_nvme_device {
  7. local DEVICE=$1
  8. # Iterate over all the nvme devices, looking for those in /dev
  9. for blkdev in $( sudo nvme list | awk '/^\/dev/ {print $1 }' ); do
  10. # For each device grab the desired device name from the vendor data
  11. mapping=$(nvme id-ctrl --raw-binary "${blkdev}" 2>/dev/null | cut -c3073-3104 | sed 's/ //g' | sed 's#/dev/##g' )
  12. # If the desired device name is one of those currently requested
  13. if `echo $DEVICE | egrep -q "\<${mapping}\>"`; then
  14. # Repoint our device variable to the real device
  15. echo $blkdev
  16. return
  17. fi
  18. done
  19. echo ${DEVICE}_NOTFOUND
  20. }
  21. function do_the_mount
  22. {
  23. # Mount BLOCK_DEV at MOUNTPOINT, creating the directory if necessary
  24. # and formatting the filesystem.
  25. VOL_LABEL=$1
  26. BLOCK_DEV=$2
  27. MOUNTPOINT=$3
  28. # We're going to loop up to 6 times, looking for the NVME device
  29. # that matches block_dev, sleeping 30 seconds between attempts,
  30. # to give the attachment time to take place.
  31. LIMIT=6
  32. device=$( get_nvme_device ${BLOCK_DEV} )
  33. >&2 echo Looking for $device
  34. while [[ $LIMIT -gt 0 && ! -e $device ]]; do
  35. sleep 30
  36. device=$( get_nvme_device ${BLOCK_DEV} )
  37. >&2 echo Looking for $device
  38. LIMIT=$(( $LIMIT - 1 ))
  39. done
  40. if ! [[ -d ${MOUNTPOINT} ]]; then
  41. ${SUDO} mkdir -p ${MOUNTPOINT}
  42. fi
  43. if ! ${SUDO} blkid -l -t LABEL=${VOL_LABEL}; then
  44. ${SUDO} mkfs.xfs -L ${VOL_LABEL} ${device}
  45. ${SUDO} mount LABEL=${VOL_LABEL} ${MOUNTPOINT}
  46. fi
  47. if ! egrep "LABEL=${VOL_LABEL}" /etc/fstab; then
  48. ${SUDO} bash -c "cat >> /etc/fstab" <<EOF
  49. LABEL=${VOL_LABEL} ${MOUNTPOINT} xfs defaults 0 2
  50. EOF
  51. fi
  52. if ! egrep " ${MOUNTPOINT} " /proc/mounts; then
  53. ${SUDO} mount ${MOUNTPOINT}
  54. fi
  55. }
  56. do_the_mount repo_vol xvdf /var/www/html
  57. OUTEREOF