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