-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvm-update
executable file
·201 lines (183 loc) · 4.56 KB
/
kvm-update
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
#
# Author: Pavel Chebotarev <[email protected]>
#
# Variables definition.
SCRIPT_DIR="$(dirname "$0")/"
BIN_COPY="kvm-copy"
BIN_RUN="kvm-run"
IMG_USER="$(whoami)"
DIR="kvm/"
IMG_DIR_DEFAULT="/home/$DIR$DIR"
IMG_DIR=""
IMG_FILE_DEFAULT="ubuntu-server-10.04-64.img"
IMG_FILE=""
DEST_FILE_PERM_DEFAULT="400"
DEST_FILE_PERM=""
IMG_FILE_EXT_OLD=".old"
IMG_FILE_EXT_NEW=".new"
ARGS=""
ARGS_NEW=""
ARGS_NEW_STATUS=""
REG_PERMISSION="^[04567][04567][04567]$"
REG_SEPARATOR="[\/]"
# Print error message and exit.
function error_msg()
{
echo "Error: $1"
exit 1
}
# Image directory and file checking.
function check_img()
{
echo "$1" | grep -E "$REG_SEPARATOR" >/dev/null
STAT="$?"
sudo stat "$IMG_DIR_DEFAULT$1" 2>/dev/null | grep "Size:" | grep "file" >/dev/null
if [ "$?" == "0" ] && [ "$STAT" != "0" ]; then
IMG_DIR="$IMG_DIR_DEFAULT"
IMG_FILE="$1"
else
STAT="$(sudo stat "$1" 2>/dev/null | grep "Size:")"
if [ "$?" == "0" ]; then
echo "$STAT" | grep "directory" >/dev/null
if [ "$?" == "0" ]; then
IMG_DIR="$(sudo bash -c "cd \"$1\" && pwd" 2>/dev/null)/"
IMG_FILE="$IMG_FILE_DEFAULT"
else
echo "$STAT" | grep "file" >/dev/null
if [ "$?" == "0" ]; then
IMG_DIR="$(dirname "$1")"
IMG_DIR="$(sudo bash -c "cd \"$IMG_DIR\" && pwd" 2>/dev/null)/"
IMG_FILE="$(basename "$1")"
else
error_msg "No such file '$1'"
fi
fi
else
error_msg "No such file or directory '$1'"
fi
fi
STAT=""
}
# Help message definition.
function help()
{
cat <<H_E_L_P
Usage: $(basename $0) [options]
Options:
-i, --kvm-image Set file KVM VM image
(by default directory $IMG_DIR_DEFAULT
and file $IMG_FILE_DEFAULT).
-p, --permission Set permission for file after updating
(by default $DEST_FILE_PERM_DEFAULT).
-v, --version Print the current version.
-h, --help Show this message.
Note:
Other options:
-r, --ram-size,
-m, --mac-address,
-M, --no-mac-address,
-b, --bridge-interface,
-c, --cpu,
-g, --graphic,
-u, --guest-user,
-s, --guest-script-file,
-S, --no-guest-script,
-n, --guest-host-name,
-d, --guest-domain-name
to see help for kvm-run (command: kvm-run --help).
H_E_L_P
}
# Options definition.
ARGS="$@"
OPTS="$(getopt -o "i:p:r:m:Mb:c:gu:s:Sn:d:vh" -l "kvm-image:,permission:,ram-size:,mac-address:,no-mac-address,bridge-interface:,cpu:,graphic,guest-user:,guest-script-file:,no-guest-script,guest-host-name:,guest-domain-name:,version,help" -a -- "$@" 2>/dev/null)"
if [ "$?" != "0" ]; then
help
error_msg "Bad option(s) value"
fi
eval set -- "$OPTS"
# Options parsing.
while [ $# -gt 0 ]; do
case $1 in
-- )
shift
;;
-i|--kvm-image )
check_img "$2"
shift 2
;;
-p|--permission )
echo "$2" | grep "$REG_PERMISSION" >/dev/null
if [ "$?" == "0" ]; then
DEST_FILE_PERM="$2"
else
error_msg "Incorrectly permission value '$2'"
fi
shift 2
;;
-v|--version )
echo "Version: 1.1.0"
exit 0
;;
-h|--help )
help
exit 0
;;
* )
shift
;;
esac
done
# Image directory and file values checking.
if [ -z "$IMG_DIR" ] || [ -z "$IMG_FILE" ]; then
if [ -z "$IMG_DIR" ]; then
IMG_DIR="$IMG_DIR_DEFAULT"
fi
if [ -z "$IMG_FILE" ]; then
IMG_FILE="$IMG_FILE_DEFAULT"
fi
check_img "$IMG_DIR$IMG_FILE"
fi
# Image file redefinition.
for i in $ARGS; do
if [ "$ARGS_NEW_STATUS" == "yes" ]; then
ARGS_NEW="$ARGS_NEW $IMG_DIR$IMG_FILE$IMG_FILE_EXT_NEW"
ARGS_NEW_STATUS="no"
else
if [ "$i" == "-i" ] || [ "$i" == "--kvm-image" ]; then
ARGS_NEW_STATUS="yes"
fi
ARGS_NEW="$ARGS_NEW $i"
fi
done
if [ -z "$ARGS_NEW_STATUS" ]; then
ARGS_NEW="$ARGS_NEW --kvm-image \"$IMG_DIR$IMG_FILE$IMG_FILE_EXT_NEW\""
fi
# Permission option removing.
ARGS="$ARGS_NEW"
ARGS_NEW=""
ARGS_NEW_STATUS=""
for i in $ARGS; do
if [ "$ARGS_NEW_STATUS" == "yes" ]; then
ARGS_NEW_STATUS="no"
else
if [ "$i" == "-p" ] || [ "$i" == "--permission" ]; then
ARGS_NEW_STATUS="yes"
else
ARGS_NEW="$ARGS_NEW $i"
fi
fi
done
# Image destination file permission checking.
if [ -z "$DEST_FILE_PERM" ]; then
DEST_FILE_PERM="$DEST_FILE_PERM_DEFAULT"
fi
# Renaming and coping.
"$SCRIPT_DIR$BIN_COPY" --source "$IMG_DIR$IMG_FILE" --destination "$IMG_DIR$IMG_FILE$IMG_FILE_EXT_NEW"
sudo mv --force "$IMG_DIR$IMG_FILE" "$IMG_DIR$IMG_FILE$IMG_FILE_EXT_OLD"
# KVM running for updating.
"$SCRIPT_DIR$BIN_RUN" $ARGS_NEW
if [ "$?" != "0" ]; then
sudo mv --force "$IMG_DIR$IMG_FILE$IMG_FILE_EXT_NEW" "$IMG_DIR$IMG_FILE" && sudo chmod "$DEST_FILE_PERM" "$IMG_DIR$IMG_FILE"
fi
exit $?