-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmware
More file actions
executable file
·159 lines (148 loc) · 4.96 KB
/
vmware
File metadata and controls
executable file
·159 lines (148 loc) · 4.96 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
#!/usr/bin/env python
import argparse
import collections
import datetime
import os
import signal
import subprocess
import sys
Instance = collections.namedtuple("Instance", "path name")
vmrun_cmd = "/usr/bin/vmplayer"
ovftool_cmd = "/usr/bin/ovftool"
instances = []
# Argparse setup
parser = argparse.ArgumentParser(description="IncludeOS VMmware runner")
parser.add_argument("-n", "--instances", dest="instances",
type=int, help="Number of instances to launch")
parser.add_argument("vm_filename", type=str, help="Filename of VM")
args = parser.parse_args()
if args.instances == None:
args.instances = 1
def handler(signal, frame):
stop_instances()
sys.exit(0)
def stop_instances():
for instance in instances:
stop_instance(instance)
def start_instance(instance):
serial_path = os.path.join(instance.path, "includeos-serial.txt")
vmxpath = os.path.join(instance.path, instance.name)
return subprocess.call([vmrun_cmd, vmxpath])
def stop_instance(instance):
full_path = os.path.join(instance.path, instance.name)
print "Stopping vm {path}".format(path=full_path)
subprocess.call([vmrun_cmd, "stop", full_path, "hard"])
def convert_image(servicename, destination_path):
vmdkfile = "{filename}.vmdk".format(filename=servicename)
vmdk_path = os.path.join(destination_path, vmdkfile)
convert_cmd = "qemu-img convert -O vmdk {filename}.img {dest}".format(dest=vmdk_path, filename=servicename)
try:
os.makedirs(destination_path)
except:
pass
return subprocess.call(convert_cmd, shell=True)
def write_vmx(instance_path, servicename):
vmdkfile = "{filename}.vmdk".format(filename=servicename)
vmdk_full_path = os.path.join(instance_path, vmdkfile)
vmxfile = "{filename}.vmx".format(filename=servicename)
vmx_full_path = os.path.join(instance_path, vmxfile)
vmx = """.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "11"
memsize = "64"
ide0:0.present = "TRUE"
ide0:0.fileName = "{path}"
ide1:0.present = "TRUE"
ide1:0.autodetect = "TRUE"
ide1:0.deviceType = "cdrom-raw"
ide1:0.startConnected = "FALSE"
ethernet0.present = "TRUE"
ethernet0.connectionType = "hostonly"
ethernet0.wakeOnPcktRcv = "FALSE"
ethernet0.addressType = "generated"
ethernet0.linkStatePropagation.enable = "TRUE"
sound.present = "TRUE"
sound.fileName = "-1"
sound.autodetect = "TRUE"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
vmci0.present = "TRUE"
vprobe.enable = "TRUE"
hpet0.present = "TRUE"
tools.syncTime = "TRUE"
displayName = "IncludeOS"
guestOS = "other"
nvram = "IncludeOS.nvram"
virtualHW.productCompatibility = "hosted"
tools.upgrade.policy = "upgradeAtPowerCycle"
powerType.powerOff = "soft"
powerType.powerOn = "soft"
powerType.suspend = "soft"
powerType.reset = "soft"
extendedConfigFile = "IncludeOS.vmxf"
floppy0.present = "FALSE"
serial0.present = "TRUE"
serial0.fileType = "file"
serial0.fileName = "includeos-serial.txt"
serial0.tryNoRxLoss = "FALSE"
ethernet0.virtualDev = "vmxnet3"
""".format(path=vmdk_full_path)
try:
os.makedirs(vmx_output_path)
except:
pass
with open(vmx_full_path, "w") as f:
f.write(vmx)
instance = Instance(path=instance_path, name=vmxfile)
return instance
def write_ova(instance_path, servicename):
vmxfile = "{filename}.vmx".format(filename=servicename)
vmx_full_path = os.path.join(instance_path, vmxfile)
ovafile = "{filename}.ova".format(filename=servicename)
ova_full_path = os.path.join(instance_path, ovafile)
subprocess.call([ovftool_cmd, vmx_full_path, ova_full_path])
return
if __name__ == "__main__":
timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
servicename = args.vm_filename
path, _ = os.path.split(os.path.abspath(args.vm_filename))
root_path = os.path.join(path, timestamp)
for i in range(args.instances):
instance_path = os.path.join(root_path, str(i))
# VMWare wants an exclusive lock on the disk file, so every instance
# needs their own copy :(
res = convert_image(servicename, instance_path)
if res == 0:
instance = write_vmx(instance_path, servicename)
if os.path.isfile(ovftool_cmd):
write_ova(instance_path, servicename)
try:
res = start_instance(instance)
except KeyboardInterrupt:
sys.exit(0)
sys.exit(0)
if res == 0:
instances.append(instance)
if len(instances) != args.instances:
print "One or more instances failed to launch, stopping all"
stop_instances()
else:
signal.signal(signal.SIGINT, handler)
tail_cmd = ["tail", "-f"]
# Feel free to substitute your preferred tail variant
#tail_cmd = ["multitail", "-s", "2"]
for instance in instances:
log_path = os.path.join(instance.path, "includeos-serial.txt")
tail_cmd.append(log_path)
subprocess.call(tail_cmd)