This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhyveManager.sh
executable file
·170 lines (154 loc) · 4.72 KB
/
xhyveManager.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
#!/bin/bash
#This script can create, start, stop and delete and edit xhyve or hyperkit VMs.
#It requires dd and/or qemu-img and needs the following file structures.
# $HOME/VMs/xhyve-hyperkit
# /images
# /kernels
# /vms
#
#It creates .ini files for the VM configurations.
#Folders to use. Change if you want to.
BASE="$HOME/VMs/xhyve-hyperkit"
folders=( "$HOME/VMs" \
"$BASE" \
"$BASE/images" \
"$BASE/kernels" \
"$BASE/vms" \
)
#Mapping between Questions, Variables and possible values.
#Question-Variable separator ';'
#Value separator '|'
questions=( \
"How many CPU cores? Enter number: ;CPU" \
"How much RAM? Enter number: ;MEM" \
"Harddrive type raw? (raw [y], qcow2 [n]);QCOW;0|1" \
"Harddrive name: ;IMG" \
"Use Network Interace? (y,n);USENET;0|1" \
"Use RND interface? (y,n);USERND;0|1" \
"Use ACPI? (y,n);USEACPI;0|1" \
"Use CD? (y,n);USECD;0|1" \
"CD Image path: ;CD" \
)
linuxquestions=( \
"Kernel: ;KERNEL" \
"Intird: ;INITRD" \
"cmdline: ;CMDLINE" \
)
BSDquestions=( \
"APICx2 (y,n);APICx2;0|1" \
"KERNELENV:;KERNELENV" \
"BOOTVOLUME:;BOOTVOLUME" \
"USERBOOT (default [1], custom [2]):;USERBOOT;${folders[4]}/userboot.so"
)
typeQuestion="Type of VM is linux? (linux [y], freebsd [n]);VMTYPE;kexec|fbsd"
#Declare what hypervisor to use. If Docker is installed which hyperkit returns true.
hypervisor=xhyve
which hyperkit &>/dev/null
[ $? ] && hypervisor=hyperkit
function checkFileStructure() {
#echo "Checking folder structure..."
for i in ${folders[@]}; do
#echo -n "$i...";
[ -d $i ] || echo "$i does not exist. creating..."; mkdir -p $i
done
}
#Depends on $value and $key and userinput existing in calling function
function setKeyVal(){
key=$(echo "$1"|cut -d ";" -f 2)
echo "$1"|cut -d ";" -f 1
read userinput
if [[ $(echo $1|awk -F";" '{print NF}') -eq 3 ]]; then
defaults=$(echo "$1"|cut -d ";" -f 3)
if [ "$userinput" == "y" ]; then
value=$(echo "$defaults"|cut -d "|" -f 1)
else
value=$(echo "$defaults"|cut -d "|" -f 2)
fi
else
value="$userinput"
fi
}
function write2config(){
setKeyVal "$1"
echo "$key=\"$value\"">>"${folders[5]}/$vmname.ini"
}
#gets run when xhyveManager create VM-name is run.
function createVM(){
vmname=$1
declare userinput
declare value
declare key
declare defaults
touch "${folders[5]}/$vmname.ini"
echo "UUID=\"$(uuidgen)\"" >>"${folders[5]}/$vmname.ini"
write2config "$typeQuestion"
if [ "$value" == "kexec" ]; then
for i in "${linuxquestions[@]}"; do
write2config "$i"
done
else
for i in "${BSDquestions[@]}"; do
write2config "$i"
done
fi
for i in "${questions[@]}"; do
write2config "$i"
done
}
#runs VM after its config has been loaded.
function startVM() {
RAM="-m $MEM"
SMP="-c $CPU"
[ $USENET ] && NET="-s 2:0,virtio-net"
[ $USECD -eq 0 ] && IMG_CD="-s 3:0,ahci-cd,$CD"
[ $QCOW -eq 0 ] && IMG_HDD="-s 4:0,virtio-blk,$IMG" || IMG_HDD="-s 4:0,ahci-hd,$IMG"
[ $USERND ] && VND_RND="-s 5:0,virtio-rnd"
PCI_DEV="-s 0:0,hostbridge -s 31,lpc"
LPC_DEV="-l com1,stdio"
[ $USEACPI ] && ACPI="-A"
[ $APICx2 ] && APICx2="-x"
if [ $VMTYPE == "kexec" ]; then
KERNELLINE="kexec,$KERNEL,$INITRD,$CMDLINE"
else
KERNELLINE="fbsd,$USERBOOT,$BOOTVOLUME,$KERNELENV"
fi
printf "sudo $hypervisor -P -H -u \n$APICx2 \n$ACPI \n$RAM \n$SMP \n$PCI_DEV \n$LPC_DEV \n$NET \n$IMG_CD \n$IMG_HDD \n$VND_RND \n-U $UUID \n-f $KERNELLINE\n"
sudo $hypervisor -P -H -u $APICx2 $ACPI $RAM $SMP $PCI_DEV $LPC_DEV $NET $IMG_CD $IMG_HDD $VND_RND -U $UUID -f $KERNELLINE
}
#Reads Configfile of supplied name and evals it's lines to variables for later use.
#Configfile contents of form CD=bootcd.iso => variable $CD with value "bootcd.iso" after eval.
function readConfig(){
configfile=$1.ini
source ${folders[5]}/$configfile
}
#createVM test;
function listVMs(){
for i in ${folders[5]}/*.ini; do
echo ${i%.*}
done
}
#deleteVM
function deleteVM(){
[ "$KERNEL" != "userboot.so" ] && mv -v $KERNEL ~/.Trash/
[ -e ${folders[4]}/$INITRD ] && mv -v ${folders[4]}/$INITRD ~/.Trash/
[ -e ${folders[3]}/$IMG ] && mv -v ${folders[3]}/$IMG ~/.Trash/
mv -v ${folders[5]}/$1.ini ~/.Trash/
}
checkFileStructure
case $1 in
"")
echo "help"
;;
"create")
createVM "$2"
;;
"start")
readConfig "$2" ; startVM
;;
"list")
listVMs
;;
"delete")
readConfig "$2" ; deleteVM "$2";
;;
esac