#!/usr/bin/env bash # Bulk volume create/delete script: Create 50 volumes, delete 50 volumes, skip on failure # Simplified version without complex resume logic set -uo pipefail # Don't exit on error (removed 'e' flag) # -------- Configuration -------- START_ID=1000 END_ID=1049 # Creates 50 volumes (1000-1049) # Base size and step for -b (soft quota) B_BASE=10 # numeric part B_UNIT="G" # K|M|G|T B_STEP=1 # increment per volume (same unit as B_UNIT) # Base size and step for -B (hard quota) BB_BASE=11 BB_UNIT="G" BB_STEP=1 # Fixed inode parameters I_SMALL="5K" # for -i I_LARGE="6K" # for -I # Mapping flags SET_UID_FROM_LOOP=true # --uid SET_GID_FROM_LOOP=true # --gid SET_PROJID_FROM_LOOP=true # --projid FIXED_UID="" # set if you want a fixed UID FIXED_GID="" FIXED_PROJID="" # Volume path pattern (use placeholder) TARGET_PATH_PATTERN="/volume_" # Command binary CMD_BIN="emf tenant volume" ENTITY="tenant1" # Dry run (true/false) DRY_RUN=false # Skip delay (seconds between operations) SKIP_DELAY=2 # -------------------------------- log() { printf '%s\n' "$*" >&2; } run() { if [[ "$DRY_RUN" == true ]]; then log "[DRY-RUN] $*" return 0 else eval "$*" return $? fi } validate_int() { local name="$1" val="$2" if ! [[ "$val" =~ ^[0-9]+$ ]]; then log "ERROR: $name must be an integer (got '$val')" exit 1 fi } validate_unit() { local unit="$1" case "$unit" in K|M|G|T) ;; *) log "ERROR: Unit must be one of K|M|G|T (got '$unit')"; exit 1;; esac } size_str() { local num="$1" unit="$2" echo "${num}${unit}" } subst_id() { local s="$1" id="$2" echo "${s///$id}" } # ---- Validation ---- validate_int "START_ID" "$START_ID" validate_int "END_ID" "$END_ID" if (( START_ID > END_ID )); then log "ERROR: START_ID must be <= END_ID" exit 1 fi validate_int "B_BASE" "$B_BASE" validate_int "B_STEP" "$B_STEP" validate_unit "$B_UNIT" validate_int "BB_BASE" "$BB_BASE" validate_int "BB_STEP" "$BB_STEP" validate_unit "$BB_UNIT" # Counters for tracking TOTAL_VOLUMES=$(( END_ID - START_ID + 1 )) CREATE_SUCCESS=0 CREATE_FAILED=0 DELETE_SUCCESS=0 DELETE_FAILED=0 # ---- Volume Creation Loop ---- log "" log "========================================" log "Creating $TOTAL_VOLUMES Volumes (Skip on Failure)" log "========================================" log "" for id in $(seq "$START_ID" "$END_ID"); do offset=$(( id - START_ID )) # Calculate quota sizes b_val=$(( B_BASE + (B_STEP * offset) )) B_ARG="$(size_str "$b_val" "$B_UNIT")" bb_val=$(( BB_BASE + (BB_STEP * offset) )) BB_ARG="$(size_str "$bb_val" "$BB_UNIT")" # Build identity flags uid_flag="" gid_flag="" projid_flag="" if [[ "$SET_UID_FROM_LOOP" == true ]]; then uid_flag="--uid $id" elif [[ -n "$FIXED_UID" ]]; then uid_flag="--uid $FIXED_UID" fi if [[ "$SET_GID_FROM_LOOP" == true ]]; then gid_flag="--gid $id" elif [[ -n "$FIXED_GID" ]]; then gid_flag="--gid $FIXED_GID" fi if [[ "$SET_PROJID_FROM_LOOP" == true ]]; then projid_flag="--projid $id" elif [[ -n "$FIXED_PROJID" ]]; then projid_flag="--projid $FIXED_PROJID" fi target_path="$(subst_id "$TARGET_PATH_PATTERN" "$id")" # Build create command cmd="$CMD_BIN create -b $B_ARG -B $BB_ARG -i $I_SMALL -I $I_LARGE $uid_flag $gid_flag $projid_flag $ENTITY \"$target_path\"" log "[$((CREATE_SUCCESS + CREATE_FAILED + 1))/$TOTAL_VOLUMES] Creating volume $id: $target_path" log " Quota: Soft=$B_ARG, Hard=$BB_ARG" # Execute command (skip on failure) if run "$cmd" 2>&1; then log " ✓ SUCCESS" CREATE_SUCCESS=$((CREATE_SUCCESS + 1)) else log " ✗ FAILED - Skipping to next volume" CREATE_FAILED=$((CREATE_FAILED + 1)) fi log "" sleep $SKIP_DELAY done log "========================================" log "Volume Creation Summary:" log " Total: $TOTAL_VOLUMES" log " Success: $CREATE_SUCCESS" log " Failed: $CREATE_FAILED" log "========================================" log "" # ---- Volume Deletion Loop ---- log "" log "========================================" log "Deleting $TOTAL_VOLUMES Volumes (Skip on Failure)" log "========================================" log "" for id in $(seq "$START_ID" "$END_ID"); do target_path="$(subst_id "$TARGET_PATH_PATTERN" "$id")" # Build projid flag (same logic as creation) projid_flag="" if [[ "$SET_PROJID_FROM_LOOP" == true ]]; then projid_flag="--projid $id" elif [[ -n "$FIXED_PROJID" ]]; then projid_flag="--projid $FIXED_PROJID" fi # Build delete command with --projid flag delete_cmd="$CMD_BIN delete $projid_flag $ENTITY \"$target_path\"" log "[$((DELETE_SUCCESS + DELETE_FAILED + 1))/$TOTAL_VOLUMES] Deleting volume $id: $target_path" if [[ -n "$projid_flag" ]]; then log " ProjID: $projid_flag" fi # Execute command (skip on failure) if run "$delete_cmd" 2>&1; then log " ✓ SUCCESS" DELETE_SUCCESS=$((DELETE_SUCCESS + 1)) else log " ✗ FAILED - Skipping to next volume" DELETE_FAILED=$((DELETE_FAILED + 1)) fi log "" sleep $SKIP_DELAY done log "========================================" log "Volume Deletion Summary:" log " Total: $TOTAL_VOLUMES" log " Success: $DELETE_SUCCESS" log " Failed: $DELETE_FAILED" log "========================================" log "" # ---- Final Summary ---- log "" log "========================================" log "FINAL SUMMARY" log "========================================" log "Total Volumes: $TOTAL_VOLUMES (ID $START_ID to $END_ID)" log "" log "Creation:" log " ✓ Success: $CREATE_SUCCESS" log " ✗ Failed: $CREATE_FAILED" log "" log "Deletion:" log " ✓ Success: $DELETE_SUCCESS" log " ✗ Failed: $DELETE_FAILED" log "" log "Overall Status: $((CREATE_SUCCESS + DELETE_SUCCESS)) successful operations, $((CREATE_FAILED + DELETE_FAILED)) failures" log "========================================" log "" # Exit with success if at least some operations succeeded if (( CREATE_SUCCESS > 0 || DELETE_SUCCESS > 0 )); then exit 0 else log "ERROR: All operations failed!" exit 1 fi