-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch-vm
More file actions
executable file
·255 lines (216 loc) · 8.09 KB
/
launch-vm
File metadata and controls
executable file
·255 lines (216 loc) · 8.09 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
issu(){
if [ "$EUID" -ne 0 ]; then
msg "${YELLOW}Please run using sudo (sudo ./launch-vm)${NC}"
msg "For help run: ./launch-vm -h"
exit
fi
return 0
}
multipassdriver(){
driver=$(sudo multipass get local.driver)
if [[ $driver != "virtualbox" ]]; then
msg "Looks like your multipass driver is set to ${YELLOW}$driver${NC}"
msg "Whould like it to be changed to virtualbox? (y/n)"
select yn in "Yes" "Exit"; do
case $yn in
Yes ) sudo multipass set local.driver=virtualbox; break;;
Exit ) exit;;
esac
done
fi
}
checkforcmd(){
cmd="${1-}"
version="${2-}"
if ! command -v $cmd &> /dev/null
then
msg "${RED}$cmd could not be found!!${NC}"
msg "Please install $cmd and try again"
msg "https://www.oracle.com/virtualization/technologies/vm/downloads/virtualbox-downloads.html"
msg "https://multipass.run/download/macos"
exit
fi
msg "${CYAN}Great, looks like you have $cmd installed${NC}"
$cmd $version
}
mysleep(){
x="${1-}"
z="${2-}"
while [ $x -gt 0 ]
do
sleep 1
text=$(sed "s/Xs/$x/g" <<< $z )
msg "\e[1A\e[${YELLOW} $text ${NC}"
x=$(( $x - 1 ))
done
}
petname(){
name="snow-$(shuf -n 1 names/names.txt)-$(shuf -n 1 names/adjectives.txt)"
}
createNetwork(){
VBoxManage hostonlyif create
vlan=$(VBoxManage list hostonlyifs | grep ^Name: | tail -n 1 | awk '{print $2}')
msg "${YELLOW}New vlan created named: $vlan ${NC}"
VBoxManage hostonlyif ipconfig $vlan --ip 192.168.56.1
VBoxManage dhcpserver add --ifname $vlan --ip 192.168.56.1 --netmask 255.255.255.0 --lowerip 192.168.56.100 --upperip 192.168.56.200
VBoxManage dhcpserver modify --ifname $vlan --enable
}
help(){
cat <<EOF
Usage: sudo ./$(basename "${BASH_SOURCE[0]}") [-h] [-v] -n name -f cloud-init.yaml -m 1GB -c 2 -l vboxnet0
Launch a multipass VM with cloud init
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-n, --name Name of the instance
-f, --cloud-init cloud init file path
-m, --mem memory for instance, defualt to 4GB if empty
-d, --hd drive size for instance, defualt to 20GB if empty
-c, --cpu number of vCPU, defualt to 2 if empty
-l, --vlan name of the virtual network, defualt to vboxnet0 if empty
-t, --timeout timeout for launch script, defualt to 900 if empty
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NC='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
# backup this way does not work as the image get currupted
backupinstace(){
instance="${1-}"
newinstance="${2-}"
n_mem=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/multipassd-vm-instances.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$instance']['mem_size'])")
n_cpu=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/multipassd-vm-instances.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$instance']['num_cores'])")
n_disk=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/multipassd-vm-instances.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$instance']['disk_space'])")
n_image=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/vault/multipassd-instance-image-records.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$instance']['image']['original_release'])")
instance_iso=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/vault/multipassd-instance-image-records.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$instance']['image']['path'])")
multipass launch -c $n_cpu -d $n_disk -n $newinstance -m $n_mem # for now use latest $n_image
multipass stop $newinstance
new_instance_iso=$(cat /var/root/Library/Application\ Support/multipassd/virtualbox/vault/multipassd-instance-image-records.json | python3 -c "import sys, json; print(json.load(sys.stdin)['$newinstance']['image']['path'])")
sudo cp -f "/var/root/Library/Application Support/multipassd/virtualbox/vault/instances/${instance}/cloud-init-config.iso" "/var/root/Library/Application Support/multipassd/virtualbox/vault/instances/${newinstance}/cloud-init-config.iso"
sudo cp -f "${instance_iso}" "${new_instance_iso}"
multipass start $newinstance
}
#backupinstace "k8-kind" "k8-backup"
parse_params() {
# defaults
cloudinit=cloud-init-base.yaml
memory=4G
drive=20G
cpu=2
vlan=vboxnet0
timeout=900
debug=""
while :; do
case "${1-}" in
-h | --help) help ;;
-v | --verbose) set -x
debug="-vvvv";;
--no-color) NO_COLOR=1 ;;
-n | --name) name="${2-}"
shift;;
-f | --cloud-init) cloudinit="${2-}"
shift;;
-m | --mem) memory="${2-}"
shift;;
-d | --hd) drive="${2-}"
shift;;
-c | --cpu) cpu="${2-}"
shift;;
-l | --vlan) vlan="${2-}"
shift;;
-t | --timeout) timeout="${2-}"
shift;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
return 0
}
setup_colors # setup colors
checkforcmd "VBoxManage" "-v" # check for virtualbox
checkforcmd "multipass" "version" # check for multipass
multipassdriver # check for multipass driver
parse_params "$@" # parse command line
issu # check if user is root
if [[ -z "${name-}" ]]; then
petname
msg "You enter no name, so I generated one for you, I will call him: ${YELLOW}$name${NC}, is this fine? (y/n)"
select yn in "Yes" "Enter Name" "Exit"; do
case $yn in
Yes ) break;;
"Enter Name" ) read -p "Enter name: " name; break;;
Exit ) exit;;
esac
done
fi
if ! VBoxManage list hostonlyifs | grep ^Name: | grep -q $vlan; then
msg "vlan does not exist? this is the list of vlans:"
msg "${YELLOW}$(VBoxManage list hostonlyifs | grep ^Name:)${NC}"
msg "Create a new vlan? (y/n)"
select yn in "Yes" "Enter Name" "Exit"; do
case $yn in
Yes ) createNetwork; break;;
"Enter Name" ) read -p "Enter name: " vlan; break;;
Exit ) exit;;
esac
done
fi
msg "${YELLOW}The multipass command that I'm about to run:${NC}"
msg "${CYAN}multipass launch -c $cpu -d $drive -n $name -m $memory --cloud-init $cloudinit --timeout $timeout $debug${NC}"
multipass launch -c $cpu -d $drive -n $name -m $memory --cloud-init $cloudinit --timeout $timeout $debug
mysleep "60" "I am going to sleep for Xs seconds before stopping the Vm just in case..."
cloud_init_status=0
while [ $cloud_init_status != "1" ]
do
sleep 1
cloud_init_status=$(multipass exec $name - cloud-init status | grep -c "done")
msg "$name cloud-init status: $cloud_init_status"
done
multipass stop $name
vm_status=0
while [ $vm_status != "Stopped" ]
do
sleep 1
vm_status=$(multipass list --format csv | grep "k8-minikube" | awk -F '"*,"*' '{print $2}')
msg "$name status: $vm_status"
done
vlan_status=0
while [ $vlan_status != "1" ]
do
msg "${YELLOW}Running: sudo VBoxManage modifyvm $name --nic2 hostonly --hostonlyadapter2 $vlan${NC}"
sudo VBoxManage modifyvm $name --nic2 hostonly --hostonlyadapter2 $vlan
sleep 1
vlan_status=$(sudo VBoxManage showvminfo $name | grep "NIC 2" | grep -c "$vlan")
msg "$name vlan $vlan status: $vlan_status"
done
multipass start $name
multipass exec $name sudo netplan apply
mkdir -p ~/multipass-mounts/$name
touch ~/multipass-mounts/$name/SUCCESS
multipass mount ~/multipass-mounts $name:/home/ubuntu/mac-mount
exit
#multipass mount /Users/$SUDO_USER $name:/home/$SUDO_USER
#multipass mount ~/ $name:/home/$SUDO_USER