create-bluestore-partitions.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env bash
  2. # Copyright 2021 The Rook Authors. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -ex
  16. #############
  17. # VARIABLES #
  18. #############
  19. SIZE=2048M
  20. #############
  21. # FUNCTIONS #
  22. #############
  23. function usage {
  24. echo "Use me like this to create bluestore partitions:"
  25. echo "$0 --disk /dev/sda --bluestore-type block.db"
  26. echo ""
  27. echo "Use me like this to create multiple OSDs:"
  28. echo "$0 --disk /dev/sda --osd-count 2"
  29. echo ""
  30. }
  31. function wipe_disk {
  32. sudo sgdisk --zap-all -- "$DISK"
  33. sudo dd if=/dev/zero of="$DISK" bs=1M count=10
  34. if [ -n "$WIPE_ONLY" ]; then
  35. # `parted "$DISK -s print" exits with 1 if the partition label doesn't exist.
  36. # It's no problem in "--wipe-only" mode
  37. sudo parted "$DISK" -s print || :
  38. return
  39. fi
  40. set +e
  41. sudo parted -s "$DISK" mklabel gpt
  42. set -e
  43. sudo partprobe -d "$DISK"
  44. sudo udevadm settle
  45. sudo parted "$DISK" -s print
  46. }
  47. function create_partition {
  48. sudo sgdisk --new=0:0:+"$SIZE" --change-name=0:"$1" --mbrtogpt -- "$DISK"
  49. }
  50. function create_block_partition {
  51. local osd_count=$1
  52. if [ "$osd_count" -eq 1 ]; then
  53. sudo sgdisk --largest-new=0 --change-name=0:'block' --mbrtogpt -- "$DISK"
  54. elif [ "$osd_count" -gt 1 ]; then
  55. SIZE=6144M
  56. for osd in $(seq 1 "$osd_count"); do
  57. echo "$osd"
  58. create_partition osd-"$osd"
  59. echo "SUBSYSTEM==\"block\", ATTR{size}==\"12582912\", ATTR{partition}==\"$osd\", ACTION==\"add\", RUN+=\"/bin/chown 167:167 ${DISK}${osd}\"" | sudo tee -a /etc/udev/rules.d/01-rook-"$osd".rules
  60. done
  61. fi
  62. }
  63. ########
  64. # MAIN #
  65. ########
  66. if [ ! "$#" -ge 1 ]; then
  67. exit 1
  68. fi
  69. while [ "$1" != "" ]; do
  70. case $1 in
  71. --disk)
  72. shift
  73. DISK="$1"
  74. ;;
  75. --bluestore-type)
  76. shift
  77. BLUESTORE_TYPE="$1"
  78. ;;
  79. --osd-count)
  80. shift
  81. OSD_COUNT="$1"
  82. ;;
  83. --wipe-only)
  84. WIPE_ONLY=1
  85. ;;
  86. -h | --help)
  87. usage
  88. exit
  89. ;;
  90. *)
  91. usage
  92. exit 1
  93. ;;
  94. esac
  95. shift
  96. done
  97. # First wipe the disk
  98. wipe_disk
  99. if [ -n "$WIPE_ONLY" ]; then
  100. exit
  101. fi
  102. if [ -z "$WIPE_ONLY" ]; then
  103. if [ -n "$BLUESTORE_TYPE" ]; then
  104. case "$BLUESTORE_TYPE" in
  105. block.db)
  106. create_partition block.db
  107. ;;
  108. block.wal)
  109. create_partition block.db
  110. create_partition block.wal
  111. ;;
  112. *)
  113. printf "invalid bluestore configuration %q" "$BLUESTORE_TYPE" >&2
  114. exit 1
  115. ;;
  116. esac
  117. fi
  118. # Create final block partitions
  119. create_block_partition "$OSD_COUNT"
  120. fi
  121. # Inform the kernel of partition table changes
  122. sudo partprobe "$DISK"
  123. # Wait the udev event queue, and exits if all current events are handled.
  124. sudo udevadm settle
  125. # Print drives
  126. sudo lsblk
  127. sudo parted "$DISK" -s print