-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvm-copy
executable file
·218 lines (199 loc) · 4.92 KB
/
kvm-copy
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
#
# Author: Pavel Chebotarev <[email protected]>
#
# Variables definition.
IMG_USER="$(whoami)"
DIR="kvm/"
IMG_DIR_DEFAULT="/home/$DIR$DIR"
IMG_DIR=""
IMG_FILE_DEFAULT="ubuntu-server-10.04-64.img"
IMG_FILE=""
USER_DIR_DEFAULT="/$IMG_USER/$DIR"
if [ "$IMG_USER" != "root" ]; then
USER_DIR_DEFAULT="/home$USER_DIR_DEFAULT"
fi
USER_DIR=""
USER_FILE_DEFAULT="$IMG_FILE_DEFAULT"
USER_FILE=""
FORCE="--force "
DEST_FILE_PERM_DEFAULT="644"
DEST_FILE_PERM=""
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:
-s, --source Set source directory or file
(by default directory $IMG_DIR_DEFAULT
and file $IMG_FILE_DEFAULT).
-d, --destination Set destination directory or file
(by default directory $USER_DIR_DEFAULT
and file $USER_FILE_DEFAULT).
-p, --permission Set permission for destination file
(by default $DEST_FILE_PERM_DEFAULT).
-F, --not-force Set not force coping.
-v, --version Print the current version.
-h, --help Show this message.
H_E_L_P
}
# Options definition.
OPTS="$(getopt -o "s:d:p:Fvh" -l "source:,destination:,permission:,not-force,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
;;
-s|--source )
check_img "$2"
shift 2
;;
-d|--destination )
sudo stat "$2" 2>/dev/null | grep "Size:" | grep "directory" >/dev/null
if [ "$?" == "0" ]; then
USER_DIR="$(sudo bash -c "cd \"$2\" && pwd" 2>/dev/null)/"
USER_FILE="$IMG_FILE_DEFAULT"
else
echo "$2" | grep -E "$REG_SEPARATOR" >/dev/null
if [ "$?" != "0" ]; then
USER_DIR="$USER_DIR_DEFAULT"
USER_FILE="$2"
else
USER_DIR="$(dirname "$2")/"
USER_FILE="$(basename "$2")"
fi
fi
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
;;
-F|--not-force )
FORCE=""
shift
;;
-v|--version )
echo "Version: 1.2.0"
exit 0
;;
-h|--help )
help
exit 0
;;
* )
help
error_msg "Bad option(s)"
;;
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
# User image directory checking.
if [ -z "$USER_DIR" ]; then
USER_DIR="$USER_DIR_DEFAULT"
fi
# User image file checking.
if [ -z "$USER_FILE" ]; then
USER_FILE="$USER_FILE_DEFAULT"
fi
# User image file permission checking.
if [ -z "$DEST_FILE_PERM" ]; then
DEST_FILE_PERM="$DEST_FILE_PERM_DEFAULT"
fi
# Print basic information coping.
echo ""
echo "KVM:"
echo " User: $IMG_USER"
echo " Source:"
echo " Directory: $IMG_DIR"
echo " File: $IMG_FILE"
echo " Destination:"
echo " Directory: $USER_DIR"
echo " File: $USER_FILE"
echo ""
# 'Source' value and 'destination' value comparing.
if [ "$IMG_DIR$IMG_FILE" == "$USER_DIR$USER_FILE" ]; then
error_msg "'Source' value and 'destination' value are equal"
fi
# User directory making and permission setting.
if [ ! -x "$USER_DIR" ]; then
mkdir -p "$USER_DIR"
if [ "$?" == "0" ]; then
chown -R "$USER" "$USER_DIR" && chmod -R 755 "$USER_DIR"
if [ "$?" != "0" ]; then
error_msg "Can not set permission for directory '$USER_DIR'"
fi
else
error_msg "Can not create directory '$USER_DIR'"
fi
fi
# Coping.
sudo cp $FORCE"$IMG_DIR$IMG_FILE" "$USER_DIR$USER_FILE"
if [ "$?" == "0" ]; then
sudo chown "$USER" "$USER_DIR$USER_FILE" && chmod "$DEST_FILE_PERM" "$USER_DIR$USER_FILE"
if [ "$?" != "0" ]; then
error_msg "Can not set permission for file '$USER_DIR$USER_FILE'"
fi
fi
exit $?