Skip to content

Commit 8317cf7

Browse files
committed
fixed bash logic mistakes
1 parent 61a8d90 commit 8317cf7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

files/virt_volume.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
# Parse options
2121
OPTIND=1
2222

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>]"
25-
exit 1
26-
fi
27-
2823
while getopts ":n:p:c:f:i:b:" opt; do
2924
case ${opt} in
3025
n) NAME=$OPTARG;;
@@ -44,10 +39,20 @@ while getopts ":n:p:c:f:i:b:" opt; do
4439
esac
4540
done
4641

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>]"
46+
exit 1
47+
fi
4748
if [[ -n $IMAGE && -n $BACKING_IMAGE ]]; then
48-
echo "Options -i and -b are mutually exclusive." >&2
49+
echo "Options -i and -b are mutually exclusive" >&2
4950
exit 1
5051
fi
52+
if [[ -z "$FORMAT" ]]; then
53+
FORMAT='qcow2'
54+
fi
55+
5156

5257
# Check whether a volume with this name exists.
5358
output=$(virsh vol-info --pool "$POOL" --vol "$NAME" 2>&1)
@@ -63,7 +68,7 @@ fi
6368

6469
# Create the volume.
6570
if [[ -n $BACKING_IMAGE ]]; then
66-
if [[ "$FORMAT" -ne "qcow2" ]]; then
71+
if [[ "$FORMAT" != 'qcow2' ]]; then
6772
echo "qcow2 format assumed for backing images, but $FORMAT format was supplied."
6873
exit 1
6974
fi
@@ -93,6 +98,8 @@ fi
9398
# these environmental variables are defined. Without doing this libvirt
9499
# cannot access the volume on RedHat based GNU/Linux distributions.
95100
if [[ -z "$VOLUME_OWNER" || -z "$VOLUME_GROUP" ]]; then
101+
# Avoid attempting to change permissions on volumes that are not file or
102+
# directory based
96103
if [[ -f "$output" || -d "$output" ]]; then
97104
existing_owner="$(stat --format '%U' "$output")"
98105
existing_group="$(stat --format '%G' "$output")"

0 commit comments

Comments
 (0)