99# the Business Source License, use of this software will be governed
1010# by the Apache License, Version 2.0.
1111
12- set -xeo pipefail
12+ set -xeuo pipefail
1313
14- # Volume group name
1514VG_NAME=" instance-store-vg"
15+ CLOUD_PROVIDER=" "
16+
17+ while [[ $# -gt 0 ]]; do
18+ case $1 in
19+ --cloud-provider|-c)
20+ CLOUD_PROVIDER=" $2 "
21+ shift 2
22+ ;;
23+ --vg-name|-v)
24+ VG_NAME=" $2 "
25+ shift 2
26+ ;;
27+ --help|-h)
28+ echo " Usage: $0 [options]"
29+ echo " Options:"
30+ echo " --cloud-provider, -c PROVIDER Specify cloud provider (aws, gcp, azure, generic)"
31+ echo " --vg-name, -v NAME Specify volume group name (default: instance-store-vg)"
32+ echo " --help, -h Show this help message"
33+ exit 0
34+ ;;
35+ * )
36+ echo " Unknown option: $1 "
37+ exit 1
38+ ;;
39+ esac
40+ done
1641
17- # Function to detect cloud provider
1842detect_cloud_provider () {
19- # Check for AWS
20- if curl -s -m 5 http://169.254.169.254/latest/meta-data/ > /dev/null; then
43+ # Only attempt detection if not explicitly provided
44+ if [[ -n " $CLOUD_PROVIDER " ]]; then
45+ echo " $CLOUD_PROVIDER "
46+ return
47+ fi
48+
49+ # Fall back to AWS detection if no provider is specified
50+ if curl -s -m 5 --fail http://169.254.169.254/latest/meta-data/ > /dev/null 2>&1 ; then
2151 echo " aws"
2252 return
2353 fi
2454
25- # Default to generic
55+ # Default to generic if detection fails
2656 echo " generic"
2757}
2858
29- # Cloud provider-specific device detection
59+ find_aws_bottlerocket_devices () {
60+ local nvme_devices=()
61+ local BOTTLEROCKET_ROOT=" /.bottlerocket/rootfs"
62+
63+ mapfile -t SSD_NVME_DEVICE_LIST < <( lsblk --json --output-all | \
64+ jq -r ' .blockdevices[] | select(.model // empty | contains("Amazon EC2 NVMe Instance Storage")) | .path' )
65+
66+ for device in " ${SSD_NVME_DEVICE_LIST[@]} " ; do
67+ nvme_devices+=(" $BOTTLEROCKET_ROOT$device " )
68+ done
69+
70+ echo " ${nvme_devices[@]} "
71+ }
72+
73+ find_aws_standard_devices () {
74+ lsblk --json --output-all | \
75+ jq -r ' .blockdevices[] | select(.model // empty | contains("Amazon EC2 NVMe Instance Storage")) | .path'
76+ }
77+
78+ find_aws_devices () {
79+ local nvme_devices=()
80+
81+ # Check if we're running in Bottlerocket
82+ if [[ -d " /.bottlerocket" ]]; then
83+ # Use mapfile to properly handle the output
84+ mapfile -t nvme_devices < <( find_aws_bottlerocket_devices)
85+ else
86+ # Use mapfile to properly handle the output
87+ mapfile -t nvme_devices < <( find_aws_standard_devices)
88+ fi
89+
90+ echo " ${nvme_devices[@]} "
91+ }
92+
93+ find_generic_devices () {
94+ lsblk --json --output-all | \
95+ jq -r ' .blockdevices[] | select(.name | startswith("nvme")) | select(.mountpoint == null and (.children | length == 0)) | .path'
96+ }
97+
3098find_nvme_devices () {
3199 local cloud=$1
32100 local nvme_devices=()
33101
34102 case $cloud in
35103 aws)
36- # Handle both standard Linux and Bottlerocket paths
37- if [ -d " /.bottlerocket" ]; then
38- # Bottlerocket specific path
39- BOTTLEROCKET_ROOT=" /.bottlerocket/rootfs"
40- mapfile -t SSD_NVME_DEVICE_LIST < <( lsblk --json --output-all | jq -r ' .blockdevices[] | select(.model // empty | contains("Amazon EC2 NVMe Instance Storage")) | .path' )
41- for device in " ${SSD_NVME_DEVICE_LIST[@]} " ; do
42- nvme_devices+=(" $BOTTLEROCKET_ROOT$device " )
43- done
44- else
45- # Standard EC2 instances
46- mapfile -t nvme_devices < <( lsblk --json --output-all | jq -r ' .blockdevices[] | select(.model // empty | contains("Amazon EC2 NVMe Instance Storage")) | .path' )
47- fi
104+ # Use mapfile to properly handle the output
105+ mapfile -t nvme_devices < <( find_aws_devices)
48106 ;;
49- # Add more cloud providers here
107+ # Add more cloud providers here as we support them
108+ # gcp)
109+ # mapfile -t nvme_devices < <(find_gcp_devices)
110+ # ;;
111+ # azure)
112+ # mapfile -t nvme_devices < <(find_azure_devices)
113+ # ;;
50114 * )
51- # Generic approach - find all NVMe devices that are not mounted and don't have children (partitions)
52- mapfile -t nvme_devices < <( lsblk --json --output-all | jq -r ' .blockdevices[] | select(.name | startswith("nvme")) | select(.mountpoint == null and (.children | length == 0)) | .path ' )
115+ # Generic approach for any other cloud or environment
116+ mapfile -t nvme_devices < <( find_generic_devices )
53117 ;;
54118 esac
55119
@@ -60,7 +124,7 @@ find_nvme_devices() {
60124setup_lvm () {
61125 local -a devices=(" $@ " )
62126
63- if [ ${# devices[@]} -eq 0 ]; then
127+ if [[ ${# devices[@]} -eq 0 ] ]; then
64128 echo " No suitable NVMe devices found"
65129 exit 1
66130 fi
@@ -89,27 +153,19 @@ setup_lvm() {
89153 return 0
90154}
91155
92- # Main execution
93156echo " Starting NVMe disk configuration..."
94157
95- # Detect cloud provider
158+ # Detect or use provided cloud provider
96159CLOUD_PROVIDER=$( detect_cloud_provider)
97- echo " Detected cloud provider: $CLOUD_PROVIDER "
160+ echo " Using cloud provider: $CLOUD_PROVIDER "
98161
99162# Find NVMe devices
100163mapfile -t NVME_DEVICES < <( find_nvme_devices " $CLOUD_PROVIDER " )
101164
102165# Setup LVM
103166if setup_lvm " ${NVME_DEVICES[@]} " ; then
104167 echo " NVMe disk configuration completed successfully"
105- # Call taint management script to remove the taint
106- /usr/local/bin/manage-taints.sh remove
107-
108- # Keep the container running
109- echo " Setup complete. Container will now stay running for monitoring purposes."
110- while true ; do
111- sleep 3600
112- done
168+ exit 0
113169else
114170 echo " NVMe disk configuration failed"
115171 exit 1
0 commit comments