Export server volume as image

SERVER_IDS="
dd799bc6-ded0-4f20-8f24-3e5af5250fd3
46562d71-ba00-47b7-872a-cd759abd014c
5e517453-c87f-4426-b705-96ffc9afe4ce
"
 
function save_image() {
    IMAGE_ID="${1}"
    IMAGE_NAME="${2}"
 
    echo "IMAGE_ID: ${IMAGE_ID}"
    openstack image set --private ${IMAGE_ID}    
 
    echo "Save image as ${IMAGE_NAME}.qcow2 ..."
    openstack image save ${IMAGE_ID} --file ${IMAGE_NAME}.qcow2
 
    openstack image show ${IMAGE_ID} -c size -f value
    ls -l ${IMAGE_NAME}.qcow2
    md5sum ${IMAGE_NAME}.qcow2 > ${IMAGE_NAME}.qcow2.md5sum
 
    openstack image show ${IMAGE_ID} -f json > ${IMAGE_NAME}.json
 
    echo "Delete image ${IMAGE_NAME}"
    openstack image delete ${IMAGE_ID}
}
 
 
for SERVER_ID in ${SERVER_IDS}; do
    echo "SERVER_ID: ${SERVER_ID}"
    SERVER_JSON=$(openstack server show ${SERVER_ID} -f json)
 
    SERVER_NAME=$(echo ${SERVER_JSON} | jq -r .name | tr " " "_")
    echo "SERVER_NAME: ${SERVER_NAME}"
    SERVER_STATUS=$(echo ${SERVER_JSON} | jq -r .status)
    echo "SERVER_STATUS: ${SERVER_STATUS}"
    SERVER_IMAGE=$(echo ${SERVER_JSON} | jq -r .image)
    echo "SERVER_IMAGE: ${SERVER_IMAGE}"
 
    openstack server show ${SERVER_ID} -f json > ${SERVER_ID}.json
 
    if [ "${SERVER_IMAGE}" != "N/A (booted from volume)" ]; then
        FLAVOR_ID=$(echo ${SERVER_JSON} | jq -r .flavor | cut -d "(" -f2 | cut -d")" -f1)
        FLAVOR_DISK=$(openstack flavor show ${FLAVOR_ID} -c disk -f value)
        echo "FLAVOR_DISK: ${FLAVOR_DISK}"
 
        IMAGE_NAME="${SERVER_ID}_image_$(date -I)"
 
        echo "Create image ${IMAGE_NAME} ..."
        IMAGE_ID=$(openstack server image create --wait --name ${IMAGE_NAME} ${SERVER_ID} -c id -f value)
 
        save_image "${IMAGE_ID}" "${IMAGE_NAME}"
    fi
 
    VOLUME_IDS=$(openstack server show ${SERVER_ID} -c volumes_attached -f json | jq -r .volumes_attached[].id)
    for VOLUME_ID in ${VOLUME_IDS}; do
        VOLUME_NAME=$(openstack volume show ${VOLUME_ID} -c name -f value)
        [ -z ${VOLUME_NAME} ] && VOLUME_NAME=${VOLUME_ID}
        echo "VOLUME_NAME: ${VOLUME_NAME}"
 
        VOLUME_SIZE=$(openstack volume show ${VOLUME_ID} -c size -f value)
        echo "VOLUME_SIZE: ${VOLUME_SIZE}"
 
        IMAGE_NAME="${VOLUME_NAME}_image_$(date -I)"
 
        echo "Create snapshot ${IMAGE_NAME} ..."
        TMP_SNAPSHOT_ID=$(openstack volume snapshot create --volume ${VOLUME_ID} ${IMAGE_NAME} -c id -f value --force)
        echo "TMP_SNAPSHOT_ID: ${TMP_SNAPSHOT_ID}"
        TMP_VOLUME_ID=$(openstack volume create --snapshot ${TMP_SNAPSHOT_ID} ${IMAGE_NAME} -c id -f value)
        echo "TMP_VOLUME_ID: ${TMP_VOLUME_ID}"
        echo "Wait for volume status available ..."
        while ! [ "$(openstack volume show ${TMP_VOLUME_ID} -c status -f value)" == "available" ]; do sleep 1; done
        echo "Create image ${IMAGE_NAME} ..."
        IMAGE_ID=$(openstack image create --volume ${TMP_VOLUME_ID} ${IMAGE_NAME} -c image_id -f value)
        echo "IMAGE_ID: ${IMAGE_ID}"
        echo "Wait for volume status available"
        while ! [ "$(openstack volume show ${TMP_VOLUME_ID} -c status -f value)" == "available" ]; do sleep 1; done
        save_image "${IMAGE_ID}" "${IMAGE_NAME}"
 
        # cleanup
        openstack volume delete ${TMP_VOLUME_ID}
        openstack volume snapshot delete ${TMP_SNAPSHOT_ID}
    done
 
    echo 
done

Import image

openstack image create --disk-format qcow2 --container-format bare  --file img1.qcow2 my_image