|
17 | 17 | # Ensure that a libvirt volume exists, optionally uploading an image. |
18 | 18 | # On success, output a JSON object with a 'changed' item. |
19 | 19 |
|
20 | | -if [[ $# -ne 4 ]] && [[ $# -ne 5 ]]; then |
21 | | - echo "Usage: $0 <name> <pool> <capacity> <format> [<image>]" |
| 20 | +# Parse options |
| 21 | +OPTIND=1 |
| 22 | + |
| 23 | +while getopts ":n:p:c:f:i:b:" opt; do |
| 24 | + case ${opt} in |
| 25 | + n) NAME=$OPTARG;; |
| 26 | + p) POOL=$OPTARG;; |
| 27 | + c) CAPACITY=$OPTARG;; |
| 28 | + f) FORMAT=$OPTARG;; |
| 29 | + i) IMAGE=$OPTARG;; |
| 30 | + b) BACKING_IMAGE=$OPTARG;; |
| 31 | + \?) |
| 32 | + echo "Invalid option: -$OPTARG" >&2 |
| 33 | + exit 1 |
| 34 | + ;; |
| 35 | + :) |
| 36 | + echo "Option -$OPTARG requires an argument." >&2 |
| 37 | + exit 1 |
| 38 | + ;; |
| 39 | + esac |
| 40 | +done |
| 41 | + |
| 42 | +# Check options |
| 43 | +if ! [[ -n $NAME && -n $POOL && -n $CAPACITY ]]; then |
| 44 | + echo "Missing manditory options" >&2 |
| 45 | + echo "Usage: $0 -n <name> -p <pool> -c <capacity> [-f <format>] [-i <source image> | -b <backing image>]" |
22 | 46 | exit 1 |
23 | 47 | fi |
| 48 | +if [[ -n $IMAGE && -n $BACKING_IMAGE ]]; then |
| 49 | + echo "Options -i and -b are mutually exclusive" >&2 |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | +if [[ -z "$FORMAT" ]]; then |
| 53 | + FORMAT='qcow2' |
| 54 | +fi |
24 | 55 |
|
25 | | -NAME=$1 |
26 | | -POOL=$2 |
27 | | -CAPACITY=$3 |
28 | | -FORMAT=$4 |
29 | | -IMAGE=$5 |
30 | 56 |
|
31 | 57 | # Check whether a volume with this name exists. |
32 | | -output=$(virsh vol-info --pool $POOL --vol $NAME 2>&1) |
| 58 | +output=$(virsh vol-info --pool "$POOL" --vol "$NAME" 2>&1) |
33 | 59 | result=$? |
34 | 60 | if [[ $result -eq 0 ]]; then |
35 | 61 | echo '{"changed": false}' |
36 | 62 | exit 0 |
37 | 63 | elif ! echo "$output" | grep 'Storage volume not found' >/dev/null 2>&1; then |
38 | | - echo "Unexpected error while getting volume info" |
| 64 | + echo "Unexpected error while getting volume info" >&2 |
39 | 65 | echo "$output" |
40 | 66 | exit $result |
41 | 67 | fi |
42 | 68 |
|
43 | 69 | # Create the volume. |
44 | | -output=$(virsh vol-create-as --pool $POOL --name $NAME --capacity $CAPACITY --format $FORMAT 2>&1) |
45 | | -result=$? |
| 70 | +if [[ -n $BACKING_IMAGE ]]; then |
| 71 | + if [[ "$FORMAT" != 'qcow2' ]]; then |
| 72 | + echo "qcow2 format assumed for backing images, but $FORMAT format was supplied." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + output=$(virsh vol-create-as --pool "$POOL" --name "$NAME" --capacity "$CAPACITY" --format "$FORMAT" --backing-vol "$BACKING_IMAGE" --backing-vol-format "$FORMAT" 2>&1) |
| 76 | + result=$? |
| 77 | +else |
| 78 | + output=$(virsh vol-create-as --pool "$POOL" --name "$NAME" --capacity "$CAPACITY" --format "$FORMAT" 2>&1) |
| 79 | + result=$? |
| 80 | +fi |
46 | 81 | if [[ $result -ne 0 ]]; then |
47 | 82 | echo "Failed to create volume" |
48 | 83 | echo "$output" |
49 | 84 | exit $result |
50 | 85 | fi |
51 | 86 |
|
52 | 87 | # Determine the path to the volume file. |
53 | | -output=$(virsh vol-key --pool $POOL --vol $NAME 2>&1) |
| 88 | +output=$(virsh vol-key --pool "$POOL" --vol "$NAME" 2>&1) |
54 | 89 | result=$? |
55 | 90 | if [[ $result -ne 0 ]]; then |
56 | 91 | echo "Failed to get volume file path" |
57 | 92 | echo "$output" |
58 | | - virsh vol-delete --pool $POOL --vol $NAME |
| 93 | + virsh vol-delete --pool "$POOL" --vol "$NAME" |
59 | 94 | exit $result |
60 | 95 | fi |
61 | 96 |
|
62 | 97 | # Change the ownership of the volume to VOLUME_OWNER:VOLUME_GROUP if |
63 | 98 | # these environmental variables are defined. Without doing this libvirt |
64 | 99 | # cannot access the volume on RedHat based GNU/Linux distributions. |
65 | | -existing_owner="$(stat --format '%U' "$output")" |
66 | | -existing_group="$(stat --format '%G' "$output")" |
67 | | -new_owner="${VOLUME_OWNER:-$existing_owner}" |
68 | | -new_group="${VOLUME_GROUP:-$existing_group}" |
69 | | -output=$(chown "$new_owner":"$new_group" $output 2>1) |
70 | | -result=$? |
71 | | -if [[ $result -ne 0 ]]; then |
72 | | - echo "Failed to change ownership of the volume to $new_owner:$new_group" |
73 | | - echo "$output" |
74 | | - virsh vol-delete --pool $POOL --vol $NAME |
75 | | - exit $result |
| 100 | +# Avoid attempting to change permissions on volumes that are not file or |
| 101 | +# directory based |
| 102 | +if [[ -f "$output" || -d "$output" ]]; then |
| 103 | + existing_owner="$(stat --format '%U' "$output")" |
| 104 | + existing_group="$(stat --format '%G' "$output")" |
| 105 | + new_owner="${VOLUME_OWNER:-$existing_owner}" |
| 106 | + new_group="${VOLUME_GROUP:-$existing_group}" |
| 107 | + output=$(chown "$new_owner":"$new_group" "$output" 2>1) |
| 108 | + result=$? |
| 109 | + if [[ $result -ne 0 ]]; then |
| 110 | + echo "Failed to change ownership of the volume to $new_owner:$new_group" |
| 111 | + echo "$output" |
| 112 | + virsh vol-delete --pool "$POOL" --vol "$NAME" |
| 113 | + exit $result |
| 114 | + fi |
76 | 115 | fi |
77 | 116 |
|
78 | 117 | if [[ -n $IMAGE ]]; then |
79 | 118 | # Upload an image to the volume. |
80 | | - output=$(virsh vol-upload --pool $POOL --vol $NAME --file $IMAGE 2>&1) |
| 119 | + output=$(virsh vol-upload --pool "$POOL" --vol "$NAME" --file "$IMAGE" 2>&1) |
81 | 120 | result=$? |
82 | 121 | if [[ $result -ne 0 ]]; then |
83 | 122 | echo "Failed to upload image $IMAGE to volume $NAME" |
84 | 123 | echo "$output" |
85 | | - virsh vol-delete --pool $POOL --vol $NAME |
| 124 | + virsh vol-delete --pool "$POOL" --vol "$NAME" |
86 | 125 | exit $result |
87 | 126 | fi |
88 | 127 |
|
89 | 128 | # Resize the volume to the requested capacity. |
90 | | - output=$(virsh vol-resize --pool $POOL --vol $NAME --capacity $CAPACITY 2>&1) |
| 129 | + output=$(virsh vol-resize --pool "$POOL" --vol "$NAME" --capacity "$CAPACITY" 2>&1) |
91 | 130 | result=$? |
92 | 131 | if [[ $result -ne 0 ]]; then |
93 | 132 | echo "Failed to resize volume $VOLUME to $CAPACITY" |
94 | 133 | echo "$output" |
95 | | - virsh vol-delete --pool $POOL --vol $NAME |
| 134 | + virsh vol-delete --pool "$POOL" --vol "$NAME" |
96 | 135 | exit $result |
97 | 136 | fi |
98 | 137 | fi |
|
0 commit comments