-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm-creat.sh
executable file
·178 lines (155 loc) · 5.8 KB
/
vm-creat.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
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
#!/bin/bash -ue
#
# 'vmscripts' low-level VM management scripts - VM creator
#
# Copyright © 2015, 2016, 2017 Thilo Fromm. Released under the terms of the GNU
# GLP v3.
#
# vmscripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# vmscripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vmscripts. If not, see <http://www.gnu.org/licenses/>.#
#
vmscripts_prereq="name"
creat_shortopts="-i -d -s -M -m -c -N -n -p"
creat_longopts="--iso --disk --disk-size --move --mem --cpus --net-mode --net --ports"
# the options, including their respective defaults
disk_size="4G"
disk_image=""
iso_image=""
move=false
mem="512M"
cpus="1"
net_mode="hidden" # hidden, tap
net="172.16.10.0/24"
forward_ports="22,25,80"
creat_usage () {
echo " Usage:"
echo " vm creat <name> [<optional arguments>] - Create new VM '<name>'"
echo
echo " Optional arguments include:"
echo
echo " -d|--disk <path-to-image> Copy pre-existing harddisk image instead of creating a new empty volume."
echo " -s|--disk-size <size> Size of the harddisk volume (may be followed by K, M, G or T)"
echo " to be created for the new VM."
echo " Default: $disk_size"
echo " -i|--iso <path-to-iso-image> Path to an ISO image to use with the VM. The image will be copied."
echo " -M|--move Move source disk image and ISO instead of copying."
echo " -m|--mem <mem-size> Amount of memory (followed by M or G)."
echo " Default: $mem"
echo " -c|--cpus <nr-of-cpus> Virtual CPUs count."
echo " Default: $cpus"
echo " -N|--net-mode <hidden|tap> Networking mode."
echo " hidden No host-visible network devices; VM ports need to be forwarded (see -p)."
echo " tap VM uses a TAP device on the host. Starting the VM will require root privileges."
echo " Default: $net_mode"
echo " -n|--net <internal-network> VM-internal network (IP/MASK)."
echo " Default: $net"
echo " -p|--ports <forwarded-ports> List of ports forwarded to host ports in 'hidden' network mode,"
echo " separated by comma."
echo " Default '$forward_ports')"
}
# ----
creat_complete() {
local cword="$1"; shift
[ $cword -le 3 ] && return 1
local words=( $@ )
local cur="${words[$cword]-}"
local prev="${words[$((cword-1))]-}"
# auto-complete options with the default settings
case $prev in
-i|--iso) echo "---DEFAULT---"; return;; # complete filenames
-d|--disk) echo "---DEFAULT---"; return;; # complete filenames
-s|--disk-size) echo -n "$disk_size"; return;;
-m|--mem) echo -n "$mem"; return;;
-c|--cpus) echo -n "$cpus"; return;;
-N|--net-mode) echo -n "$net_mode"; return;;
-n|--net) echo -n "$net"; return;;
-p|--ports) echo -n "$forward_ports"; return;;
esac
return 1
}
# ----
image_and_iso() {
local name="$1"
local move="$2"
local disk_size="$3"
local disk_image="$4"
local iso_image="$5"
local op="cp"
$move && op="mv"
[ -e "$vm_path" ] && \
die "The path '$vm_path' already exists. Remove it (vm purge ${name}) or choose a different name."
mkdir "$vm_path"
local img="$vm_path/${name}.img"
if [ -e "$disk_image" ] ; then
$op -v "$disk_image" "$img"
else
qemu-img create -f raw "$img" "$disk_size"
fi
img="$vm_path/${name}.iso"
if [ -e "$iso_image" ] ; then
$op -v "$iso_image" "$img"
fi
}
# ----
write_config() {
echo "$1=\"$2\"" >> "$vm_config"
}
# ----
vm_creat() {
local opts
opts=$(getopt -o i:d:s:Mm:c:N:n:p: \
-l "iso:,disk:,disk-size:,move,mem:,cpus:,net-mode:,net:,ports:" \
-n "vm creat" -- "$@")
arg=""; name=""
for o in $opts; do
# set argument of an option
[ -n "$arg" ] && {
eval $arg="$o"; arg=""
continue
}
# iterate options
case $o in
-i|--iso) arg='iso_image';;
-d|--disk) arg='disk_image';;
-s|--disk-size) arg='disk_size';;
-m|--mem) arg='mem';;
-M|--move) move=true;;
-c|--cpus) arg='cpus';;
-N|--net-mode) arg='net_mode';;
-n|--net) arg='net';;
-p|--ports) arg='forward_ports';;
--) arg="name";;
esac
done
[ -z "$name" ] && usage "This command takes a mandatory <name> argument."
image_and_iso "$name" "$move" "$disk_size" "$disk_image" "$iso_image"
rm -f "$vm_config"
write_config "net" "$net"
write_config "netmode" "$net_mode"
write_config "cpu" "$cpus"
write_config "mem" "$mem"
write_config "forward_ports" "${forward_ports//,/ }"
mkdir -p "$vm_iodir"
echo
echo " VM $vm_name generated."
echo
}
# ----
if [ `basename "$0"` = "vm-creat.sh" ] ; then
[ "${vm_tools_initialized-NO}" != "YES" ] && {
exec $(which vm) "creat" $@
exit 1; }
vm_creat $@
else
true
fi