add VMHelper.py

This commit is contained in:
2013-04-05 02:55:01 +02:00
parent 577438a24c
commit 216eae05dd

61
mc-vm-manager/VMHelper.py Normal file
View File

@@ -0,0 +1,61 @@
class VMHelper:
def createArguments(self, config):
args = ""
if ('cpu' in config):
args += " -cpu " + config['cpu']
if ('smp' in config):
args += " -smp " + str(config['smp'])
if ('memory' in config):
args += " -m " + str(config['memory'])
if ('disk' in config):
disk = config['disk']
if ('file' in disk):
args += " -drive file=" + disk['file']
if ('hw' in disk):
args += ",if=" + disk['hw']
if ('cdrom' in config):
args += " -cdrom " + config['cdrom']
if ('network' in config):
net = config['network']
args += " -net nic,vlan=1"
if ('mac' in net):
args += ",macaddr=" + net['mac']
if ('hw' in net):
args += ",model=" + net['hw']
args += " -net tap,vlan=1"
if ('dev' in net):
args += ",ifname=" + net['dev']
args += ",script=no"
if ('vnc' in config):
vnc = config['vnc']
if ('display' in vnc):
args += " -vnc 127.0.0.1:" + str(vnc['display'])
if ('keyboard' in config):
args += " -k " + config['keyboard']
if ('kernel' in config):
args += " -kernel " + config['kernel']
if ('append' in config):
args += " -append \"" + config['append'] + "\""
return args
def setupNetwork(self, config):
foo = ""
if ('network' in config):
net = config['network']
if ('dev' in net):
foo += "tunctl -d " + net['dev'] + "\n"
foo += "ip link set dev " + net['dev'] +" up\n"
foo += "ip addr add dev " + net['dev'] +" 0.0.0.0\n"
if ('ip' in net):
for ip in net['ip']:
foo += "ip route add " + ip + " dev " + net['dev'] + "\n"
# TODO create extra chain
foo += "iptables -A FORWARD -i " + net['dev'] + " -s " + ip + " -j ACCEPT\n"
foo += "iptables -A FORWARD -i " + net['dev'] + " -j REJECT --reject-with icmp-admin-prohibited\n"
if ('ipv6' in net):
for ipv6 in net['ipv6']:
foo += "ip -6 route add " + ipv6 + " dev " + net['dev'] + "\n"
foo += "ip6tables -A FORWARD -i " + net['dev'] + " -s " + ipv6 + " -j ACCEPT\n"
foo += "iptables -A FORWARD -i " + net['dev'] + " -j REJECT --reject-with icmp6-adm-prohibited\n"
return foo