OpenStack: Glance (Image)

Deaktivate image

IMAGE_NAME="Ubuntu 14.04"
openstack image list --status active --name "${IMAGE_NAME}" -c ID -f value | \
  xargs openstack image set --deactivate --private
 
openstack image list --status active

List the images

openstack image list

Delete image

IMAGE_NAME="Ubuntu 16.04"
openstack image set --unprotected ${IMAGE_NAME}
openstack image delete ${IMAGE_NAME}
 
# delete all images
for IMAGE in $(openstack image list -c ID -f value); do
    openstack image set --unprotected ${IMAGE}
    openstack image delete ${IMAGE}
done

Deploy ISO image

openstack image create grml \
  --file grml64-small_2018.12.iso \
  --disk-format iso \
  --container-format bare

Find images used by server

for IMAGE in $(openstack image list | grep ubuntu | cut -d" " -f2); do
    echo "IMAGE: ${IMAGE}"
    openstack server list --all-projects --image ${IMAGE}
    echo
done

Get server image project relation

export OS_CLOUD=dev
echo SERVER_ID IMAGE PROJECT_ID
for IMAGE in $(openstack image list -c ID -c Name -f value | grep -i "Windows Server" | cut -d" " -f1); do
    for SERVER in $(openstack server list --all-projects --image ${IMAGE} -c ID -f value); do
        openstack server show ${SERVER} -f value -c id -c image -c project_id | paste - - -
        # openstack project show sd32sd0fsdfj9sdf9sdf0sda7fsda -f value -c name 
    done
done
 
openstack project show XXX --os-cloud=dev  -f value -c name
openstack server show XXX --os-cloud=dev -f value -c name

Shared images

openstack image list --shared
glance member-list --image-id ${IMAGE_ID}

cloudbase-Init (Windows)
https://cloudbase.it/openstack-windows-nano-server/

Find image usage by volume
Fix: The image cannot be deleted because it is in use through the backend store outside of Glance

IMAGE_ID=d9ccd517-da5a-493e-b56c-6968d7d650ea
 
echo "Image:"
openstack image show ${IMAGE_ID} -c id -c name
 
IMAGE_PROJECT_ID=$(openstack image show ${IMAGE_ID} -c owner -f value)
VOLUME_IDS=$(openstack volume list --project ${IMAGE_PROJECT_ID} -c ID -f value)
for VOLUME_ID in ${VOLUME_IDS}; do
    VOLUME_IMAGE_METADATA_JSON=$(openstack volume show ${VOLUME_ID} -c volume_image_metadata -f json 2>/dev/null)
    if [ ! -z "${VOLUME_IMAGE_METADATA_JSON}" ]; then
        VOLUME_IMAGE_ID=$(echo ${VOLUME_IMAGE_METADATA_JSON} | jq -r '.volume_image_metadata.image_id')
        if [ "${VOLUME_IMAGE_ID}" == "${IMAGE_ID}" ]; then
            echo "Used by Volume:"
            openstack volume show ${VOLUME_ID} -c id -c name -c status
            echo "Server:"
            SERVER_ID=$(openstack volume show ${VOLUME_ID} -c attachments -f json | jq -r '.attachments[].server_id')
            openstack server show ${SERVER_ID} -c id -c name
        fi
    fi
done

Links
https://docs.openstack.org/python-openstackclient/latest/cli/command-objects/image.html
https://docs.openstack.org/glance/latest/admin/manage-images.html