#!/bin/bash
# ============================================================================
# cinder-volumes-loop.sh —— 挂接 loop 设备并确保 cinder-volumes 卷组就绪
#   由 cinder-volumes-loop.service 调用(幂等)。
#   卷文件容量可调:/etc/sysconfig/cinder-volumes-loop 里写 CINDER_VOLUME_SIZE=200G
# ============================================================================
set -e

IMG=/data/openstack/cinder/volumes.img
SIZE=${CINDER_VOLUME_SIZE:-150G}

[ -f "$IMG" ] || truncate -s "$SIZE" "$IMG"

LOOP=$(losetup -j "$IMG" | cut -d: -f1)
if [ -z "$LOOP" ]; then
    LOOP=$(losetup -fP --show "$IMG")
fi

vgscan --mknodes >/dev/null 2>&1 || true
if ! vgs cinder-volumes >/dev/null 2>&1; then
    pvcreate -f "$LOOP" >/dev/null
    vgcreate cinder-volumes "$LOOP" >/dev/null
    lvcreate --type thin-pool -l 100%FREE -n cinder-volumes-pool cinder-volumes >/dev/null
fi