2020# Parse options
2121OPTIND=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-
2823while 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
4540done
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
4748if [[ -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
5051fi
52+ if [[ -z " $FORMAT " ]]; then
53+ FORMAT=' qcow2'
54+ fi
55+
5156
5257# Check whether a volume with this name exists.
5358output=$( virsh vol-info --pool " $POOL " --vol " $NAME " 2>&1 )
6368
6469# Create the volume.
6570if [[ -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
9398# these environmental variables are defined. Without doing this libvirt
9499# cannot access the volume on RedHat based GNU/Linux distributions.
95100if [[ -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