|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright (c) 2017 StackHPC Ltd. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +# Ensure that a libvirt volume exists, optionally uploading an image. |
| 18 | +# On success, output a JSON object with a 'changed' item. |
| 19 | + |
| 20 | +if [[ $# -ne 4 ]] && [[ $# -ne 5 ]]; then |
| 21 | + echo "Usage: $0 <name> <pool> <capacity> <format> [<image>]" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +NAME=$1 |
| 26 | +POOL=$2 |
| 27 | +CAPACITY=$3 |
| 28 | +FORMAT=$4 |
| 29 | +IMAGE=$5 |
| 30 | + |
| 31 | +# Check whether a volume with this name exists. |
| 32 | +output=$(virsh vol-info --pool $POOL --vol $NAME 2>&1) |
| 33 | +result=$? |
| 34 | +if [[ $result -eq 0 ]]; then |
| 35 | + echo '{"changed": false}' |
| 36 | + exit 0 |
| 37 | +elif ! echo "$output" | grep 'Storage volume not found' >/dev/null 2>&1; then |
| 38 | + echo "Unexpected error while getting volume info" |
| 39 | + echo "$output" |
| 40 | + exit $result |
| 41 | +fi |
| 42 | + |
| 43 | +# Create the volume. |
| 44 | +output=$(virsh vol-create-as --pool $POOL --name $NAME --capacity $CAPACITY --format $FORMAT 2>&1) |
| 45 | +result=$? |
| 46 | +if [[ $result -ne 0 ]]; then |
| 47 | + echo "Failed to create volume" |
| 48 | + echo "$output" |
| 49 | + exit $result |
| 50 | +fi |
| 51 | + |
| 52 | +# Determine the path to the volume file. |
| 53 | +output=$(virsh vol-key --pool $POOL --vol $NAME 2>&1) |
| 54 | +result=$? |
| 55 | +if [[ $result -ne 0 ]]; then |
| 56 | + echo "Failed to get volume file path" |
| 57 | + echo "$output" |
| 58 | + virsh vol-delete --pool $POOL --vol $NAME |
| 59 | + exit $result |
| 60 | +fi |
| 61 | + |
| 62 | +# Change the ownership of the volume to qemu. Without doing this libvirt cannot |
| 63 | +# access the volume. |
| 64 | +output=$(chown qemu:qemu $output 2>1) |
| 65 | +result=$? |
| 66 | +if [[ $result -ne 0 ]]; then |
| 67 | + echo "Failed to change ownership of the volume to qemu" |
| 68 | + echo "$output" |
| 69 | + virsh vol-delete --pool $POOL --vol $NAME |
| 70 | + exit $result |
| 71 | +fi |
| 72 | + |
| 73 | +if [[ -n $IMAGE ]]; then |
| 74 | + # Upload an image to the volume. |
| 75 | + output=$(virsh vol-upload --pool $POOL --vol $NAME --file $IMAGE 2>&1) |
| 76 | + result=$? |
| 77 | + if [[ $result -ne 0 ]]; then |
| 78 | + echo "Failed to upload image $IMAGE to volume $NAME" |
| 79 | + echo "$output" |
| 80 | + virsh vol-delete --pool $POOL --vol $NAME |
| 81 | + exit $result |
| 82 | + fi |
| 83 | + |
| 84 | + # Resize the volume to the requested capacity. |
| 85 | + output=$(virsh vol-resize --pool $POOL --vol $NAME --capacity $CAPACITY 2>&1) |
| 86 | + result=$? |
| 87 | + if [[ $result -ne 0 ]]; then |
| 88 | + echo "Failed to resize volume $VOLUME to $CAPACITY" |
| 89 | + echo "$output" |
| 90 | + virsh vol-delete --pool $POOL --vol $NAME |
| 91 | + exit $result |
| 92 | + fi |
| 93 | +fi |
| 94 | + |
| 95 | +echo '{"changed": true}' |
| 96 | +exit 0 |
0 commit comments