-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvirsh-upload-image.sh
executable file
·139 lines (125 loc) · 3.48 KB
/
virsh-upload-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
set -eo pipefail
error() {
echo >&2 "* Error: $*"
}
fatal() {
error "$@"
exit 1
}
message() {
echo "$*"
}
VIRSH_CONNECTION=
VIRSH_POOL=default
VIRSH_VOLUME_NAME=
VIRSH_DEBUG=
DELETE_VOLUME_FIRST=false
VIRSH_VOLUME_FORMAT=raw
usage() {
echo "Upload image to virsh pool"
echo
echo "$0 [options] [--] image-file-name"
echo "options:"
echo " -c, --connect=URI hypervisor connection URI"
echo " (default: '$VIRSH_CONNECTION')"
echo " --pool=NAME pool name (default: '$VIRSH_POOL')"
echo " --name=NAME new volume name"
echo " (default: input image file name)"
echo " --delete-first delete volume before creation"
echo " (default: $DELETE_VOLUME_FIRST)"
echo " --format file format type raw,bochs,qcow,qcow2,qed,vmdk"
echo " (default: $VIRSH_VOLUME_FORMAT)"
echo " -d, --debug=NUM debug level [0-4]"
echo " --help Display this help and exit"
echo " -- End of options"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--connect)
VIRSH_CONNECTION=$2
shift 2
;;
--connect=*)
VIRSH_CONNECTION=${1#*=}
shift
;;
--pool)
VIRSH_POOL=$2
shift 2
;;
--pool=*)
VIRSH_POOL=${1#*=}
shift
;;
--name)
VIRSH_VOLUME_NAME=$2
shift 2
;;
--name=*)
VIRSH_VOLUME_NAME=${1#*=}
shift
;;
--delete-first)
DELETE_VOLUME_FIRST=true
shift
;;
--format)
VIRSH_VOLUME_FORMAT=$2
shift 2
;;
--format=*)
VIRSH_VOLUME_FORMAT=${1#*=}
shift
;;
-d|--debug)
VIRSH_DEBUG=$2
shift
;;
--debug=*)
VIRSH_DEBUG=${1#*=}
shift
;;
--help)
usage
exit
;;
--)
shift
break
;;
-*)
fatal "Unknown option $1"
;;
*)
break
;;
esac
done
if [[ "$#" -ne 1 ]]; then
usage
fatal "No image file or too many image files specified"
fi
VIRSH_OPTS=()
if [[ -z "$VIRSH_VOLUME_NAME" ]]; then
VIRSH_VOLUME_NAME=$(basename "$1")
fi
if [[ -n "$VIRSH_CONNECTION" ]]; then
VIRSH_OPTS=("--connect=$VIRSH_CONNECTION")
fi
if [[ "$DELETE_VOLUME_FIRST" = "true" ]]; then
message "* Delete virsh volume $VIRSH_VOLUME_NAME in pool $VIRSH_POOL"
if ! ERR=$(virsh "${VIRSH_OPTS[@]}" vol-delete "$VIRSH_VOLUME_NAME" --pool "$VIRSH_POOL" 2>&1); then
if ! grep -q "Storage volume not found" <<<"$ERR"; then
echo >&2 "$ERR"
exit 1
else
message "* No volume $VIRSH_VOLUME_NAME in pool $VIRSH_POOL"
fi
fi
fi
FILE_SIZE=$(stat -Lc%s "$1")
message "* Create virsh volume $VIRSH_VOLUME_NAME in pool $VIRSH_POOL"
virsh "${VIRSH_OPTS[@]}" vol-create-as "$VIRSH_POOL" "$VIRSH_VOLUME_NAME" "$FILE_SIZE" --format "$VIRSH_VOLUME_FORMAT"
echo "* Upload file $1 to volume $VIRSH_VOLUME_NAME in pool $VIRSH_POOL"
virsh "${VIRSH_OPTS[@]}" vol-upload --pool "$VIRSH_POOL" "$VIRSH_VOLUME_NAME" "$1"