OpenStack: cinder (volume)

# List your volumes
openstack volume list
 
openstack volume type list --public --long
openstack volume backend pool list
cinder --os-volume-api-version 3.50 attachment-delete <attachment_id>
 
openstack volume set --non-bootable <VOLUME_ID>

Search for multiple volume attachment ids

# search for double attachments
openstack volume list --all-projects -c ID -c "Attached to" | egrep "Attached.*Attached"
 
# search in DB
DB_PASS=$(grep nova_database_password /etc/kolla/passwords.yml | cut -d " " -f2)
 
mysql -h db.service.example.com --password=${DB_PASS} -P 3306 -u nova -D nova -e "
    select *
    FROM block_device_mapping
    where attachment_id in (
        SELECT attachment_id
        FROM block_device_mapping
        where attachment_id is not null
        and deleted = 0
        group by attachment_id
        having count(*) > 1
    )
"
 
# SQL
mysql -h db.service.example.com --password=${DB_PASS} -P 3306 -u nova -D nova -e "select * from block_device_mapping"  | grep 1234567890-1111-33333-a400-111111111111
mysql -h db.service.example.com --password=${DB_PASS} -P 3306 -u nova -D nova -e "select * from block_device_mapping where volume_id = '1234567890-1111-33333-a400-111111111111"
 
SELECT volume_id
FROM block_device_mapping
where volume_id is not null
and deleted = 0
group by volume_id
having count(*) > 1

(Force) delete volume

cinder reset-state --state available ${VOLUME_ID}
cinder reset-state --attach-status detached ${VOLUME_ID}
openstack volume delete ${VOLUME_ID}

Backup / Snapshot
https://docs.openstack.org/python-openstackclient/latest/cli/command-objects/volume-snapshot.html

for ID in {1..3}; do
  openstack volume backup create --force --name dev-ceph${ID}-$(date -I) dev-ceph${ID}
done
openstack volume backup list

Search for non existing Server

comm -1 -3 \
    <(openstack server list --all-projects -c ID -f value --sort-column ID) \
    <(openstack volume list --all-projects -c "Attached to" -f json | jq -r '.[]."Attached to"[].server_id' | sort -u)

Search for server with non existing volumes

openstack volume list --all-projects -c ID -f value > /tmp/all_volumes
SERVER_IDS=$(openstack server list --all-projects -c ID -f value)
for SERVER_ID in ${SERVER_IDS}; do
    VOLUME_IDS=$(openstack server show ${SERVER_ID} -c volumes_attached -f json | jq -r .volumes_attached[].id)
    for VOLUME_ID in ${VOLUME_IDS}; do
        if [ ! $(grep ${VOLUME_ID} /tmp/all_volumes) ]; then
            echo "Missing volume ${VOLUME_ID} on server ${SERVER_ID}"
            openstack server show -c name -c id -c status -c project_id ${SERVER_ID}
            echo
 
            EXIT_CODE=1
        fi
    done
done

Remove volume with non existing server

# find volumes with non existing server
SERVER_IDS=$(comm -1 -3 <(openstack server list --all-projects -c ID -f value --sort-column ID) <(openstack volume list --all-projects -c "Attached to" -f json | jq -r '.[]."Attached to"[].server_id' | sort -u))
for SERVER_ID in ${SERVER_IDS}; do
    openstack volume list --all-projects | grep ${SERVER_ID}
done
 
# detach volume
VOLUME_ID=0bbc58dd-7f7e-4de0-9ae9-01228546e781
VOLUME_ATTACHMENT_ID=$(openstack volume show ${VOLUME_ID} -c attachments -f json | jq -r .attachments[].attachment_id)
cinder --os-volume-api-version 3.50 attachment-delete ${VOLUME_ATTACHMENT_ID}
openstack volume delete ${VOLUME_ID}

Fix disabled server in cinder

mysql cinder -e "update volumes set host='ctl3-dev@ceph1-ec-1#ceph1-ec-1' where host='ctl5-dev@ceph1-ec-1#ceph1-ec-1' and deleted_at is null;"

Get volume status from DB

mysql cinder -e "select id,display_name,project_id,host,status,attach_status,previous_status,updated_at from volumes where deleted_at is null and status!='available' and status!='in-use';"

Resize Cinder based Volume attached to an Instance in OpenStack as admin
https://tuxfixer.com/resize-cinder-based-volume-attached-to-an-instance-in-openstack/

# Cannot detach a root device volume (HTTP 400) (Request-ID: req-359c3743-adff-4e97-93f8-a6c2309a09f3)
 
# Power off the VM
openstack server stop ${SERVER_ID}
 
# Find volume ID
openstack server show ${SERVER_ID} | grep volumes_attached
 
# Change volume status to available
cinder reset-state --state available ${VOLUME_ID}
 
# Resize the volume
openstack volume set ${VOLUME_ID} --size 40
 
# After resizing OpenStack automatically sets volume status back to in-use
 
# Power on the Instance
openstack server start ${SERVER_ID}

Force delete (error) volume

rbd remove dev_cinder_ec_metadata/volume-91469e16-4580-48b7-bf69-2c02bdda131e
openstack volume set --state available 91469e16-4580-48b7-bf69-2c02bdda131e
openstack volume delete 91469e16-4580-48b7-bf69-2c02bdda131e

Migrate volume between AZ (volume migrate)
https://docs.openstack.org/cinder/latest/admin/blockstorage-volume-migration.html

Move volume between AZs
VOLUME_ID=a41c9b4d-0b40-4026-993f-9cf7485a7f6c
 
#openstack volume migrate ${VOLUME_ID} --host az2-ctl1-prod@az2-ceph1-ec-1#az1-ceph1-ec-1
openstack volume migrate ${VOLUME_ID} --host az2-ctl1-prod@az2-ceph1-ec-1
 
# --force-host-copy
 
# Debug
cinder get-pools
openstack volume backend pool list
 
# change visibility
openstack image set --community ${VOLUME_ID}