You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cloudstack.apache.org by David Nalley <da...@gnsa.us> on 2012/06/28 08:53:55 UTC
Re: [2/3] add clouddev
Edison,
When you instantiate a new file can you be sure to include the license header
http://www.apache.org/legal/src-headers.html
--David
On Thu, Jun 28, 2012 at 2:29 AM, <ed...@apache.org> wrote:
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmops
> ----------------------------------------------------------------------
> diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmops b/scripts/vm/hypervisor/xenserver/xcposs/vmops
> new file mode 100644
> index 0000000..e4a2fdc
> --- /dev/null
> +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmops
> @@ -0,0 +1,1519 @@
> +#!/usr/bin/python
> +# Version 2.2.8.2012-02-28T23:50:03Z
> +#
> +# A plugin for executing script needed by vmops cloud
> +
> +import os, sys, time
> +import XenAPIPlugin
> +sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
> +import base64
> +import hostvmstats
> +import socket
> +import stat
> +import tempfile
> +import util
> +import subprocess
> +import zlib
> +from util import CommandException
> +
> +def echo(fn):
> + def wrapped(*v, **k):
> + name = fn.__name__
> + util.SMlog("#### VMOPS enter %s ####" % name )
> + res = fn(*v, **k)
> + util.SMlog("#### VMOPS exit %s ####" % name )
> + return res
> + return wrapped
> +
> +@echo
> +def gethostvmstats(session, args):
> + collect_host_stats = args['collectHostStats']
> + consolidation_function = args['consolidationFunction']
> + interval = args['interval']
> + start_time = args['startTime']
> + result = hostvmstats.get_stats(session, collect_host_stats, consolidation_function, interval, start_time)
> + return result
> +
> +@echo
> +def setup_iscsi(session, args):
> + uuid=args['uuid']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/setup_iscsi.sh", uuid]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + return '> DONE <'
> +
> +@echo
> +def getvncport(session, args):
> + domid = args['domID']
> + hvm = args['hvm']
> + version = args['version']
> + if hvm == 'true':
> + path1 = "/local/domain/" + domid + "/qemu-pid"
> + path2 = "/local/domain/" + domid + "/console/vnc-port"
> + else:
> + if version[:3] == '6.0':
> + path1 = "/local/domain/" + domid + "/vncterm-pid"
> + path2 = "/local/domain/" + domid + "/console/vnc-port"
> + else:
> + path1 = "/local/domain/" + domid + "/serial/0/vncterm-pid"
> + path2 = "/local/domain/" + domid + "/serial/0/vnc-port"
> + try:
> + cmd = ["xenstore-read", path1]
> + pid = util.pread2(cmd)
> + pid = pid.strip()
> + cmd = ["ps", pid]
> + util.pread2(cmd)
> + cmd = ["xenstore-read", path2]
> + vncport = util.pread2(cmd)
> + vncport = vncport.strip()
> + return vncport
> + except:
> + return ''
> +
> +@echo
> +def getgateway(session, args):
> + mgmt_ip = args['mgmtIP']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/network_info.sh", "-g", mgmt_ip]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def preparemigration(session, args):
> + uuid = args['uuid']
> + try:
> + cmd = ["/usr/lib/xcp/bin/make_migratable.sh", uuid]
> + util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog("Catch prepare migration exception" )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def setIptables(session, args):
> + try:
> + '''cmd = ["/bin/bash", "/usr/lib/xcp/bin/setupxenserver.sh"]
> + txt = util.pread2(cmd)'''
> + txt = 'success'
> + except:
> + util.SMlog(" setIptables execution failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def pingdomr(session, args):
> + host = args['host']
> + port = args['port']
> + socket.setdefaulttimeout(3)
> + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> + try:
> + s.connect((host,int(port)))
> + txt = 'success'
> + except:
> + txt = ''
> +
> + s.close()
> +
> + return txt
> +
> +@echo
> +def kill_copy_process(session, args):
> + namelabel = args['namelabel']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/kill_copy_process.sh", namelabel]
> + txt = util.pread2(cmd)
> + except:
> + txt = 'false'
> + return txt
> +
> +@echo
> +def pingxenserver(session, args):
> + txt = 'success'
> + return txt
> +
> +@echo
> +def ipassoc(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/ipassoc.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" ip associate failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def vm_data(session, args):
> + router_ip = args.pop('routerIP')
> + vm_ip = args.pop('vmIP')
> +
> + util.SMlog(" adding vmdata for VM with IP: " + vm_ip + " to router with IP: " + router_ip)
> +
> + for pair in args:
> + pairList = pair.split(',')
> + vmDataFolder = pairList[0]
> + vmDataFile = pairList[1]
> + vmDataValue = args[pair]
> + cmd = ["/bin/bash", "/usr/lib/xcp/bin/vm_data.sh", "-r", router_ip, "-v", vm_ip, "-F", vmDataFolder, "-f", vmDataFile]
> +
> + fd = None
> + tmp_path = None
> +
> + try:
> + fd,tmp_path = tempfile.mkstemp()
> + tmpfile = open(tmp_path, 'w')
> +
> + if vmDataFolder == "userdata" and vmDataValue != "none":
> + vmDataValue = base64.urlsafe_b64decode(vmDataValue)
> +
> + if vmDataValue != "none":
> + tmpfile.write(vmDataValue)
> +
> + tmpfile.close()
> + cmd.append("-d")
> + cmd.append(tmp_path)
> + except:
> + util.SMlog(" vmdata failed to write tempfile " )
> + os.close(fd)
> + os.remove(tmp_path)
> + return ''
> +
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" vmdata failed with folder: " + vmDataFolder + " and file: " + vmDataFile)
> + txt = ''
> +
> + if (fd != None):
> + os.close(fd)
> + os.remove(tmp_path)
> +
> + return txt
> +
> +def pingtest(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/pingtest.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" pingtest failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def savePassword(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/save_password_to_domr.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" save password to domr failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def saveDhcpEntry(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/dhcp_entry.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" save dhcp entry failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def lt2p_vpn(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/l2tp_vpn.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog("l2tp vpn failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def setLinkLocalIP(session, args):
> + brName = args['brName']
> + try:
> + cmd = ["ip", "route", "del", "169.254.0.0/16"]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + try:
> + cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
> + txt = util.pread2(cmd)
> + except:
> +
> + try:
> + cmd = ["brctl", "addbr", brName]
> + txt = util.pread2(cmd)
> + except:
> + pass
> +
> + try:
> + cmd = ["ifconfig", brName, "169.254.0.1", "netmask", "255.255.0.0"]
> + txt = util.pread2(cmd)
> + except:
> + pass
> + try:
> + cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName, "src", "169.254.0.1"]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + txt = 'success'
> + return txt
> +
> +@echo
> +def setFirewallRule(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/call_firewall.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" set firewall rule failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def setLoadBalancerRule(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/call_loadbalancer.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog(" set loadbalancer rule failed " )
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def createFile(session, args):
> + file_path = args['filepath']
> + file_contents = args['filecontents']
> +
> + try:
> + f = open(file_path, "w")
> + f.write(file_contents)
> + f.close()
> + txt = 'success'
> + except:
> + util.SMlog(" failed to create HA proxy cfg file ")
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def deleteFile(session, args):
> + file_path = args["filepath"]
> +
> + try:
> + if os.path.isfile(file_path):
> + os.remove(file_path)
> + txt = 'success'
> + except:
> + util.SMlog(" failed to remove HA proxy cfg file ")
> + txt = ''
> +
> + return txt
> +
> +
> +@echo
> +def networkUsage(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/networkUsage.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + except:
> + util.SMlog(" network usage error " )
> + txt = ''
> +
> + return txt
> +
> +def get_private_nic(session, args):
> + vms = session.xenapi.VM.get_all()
> + host_uuid = args.get('host_uuid')
> + host = session.xenapi.host.get_by_uuid(host_uuid)
> + piflist = session.xenapi.host.get_PIFs(host)
> + mgmtnic = 'eth0'
> + for pif in piflist:
> + pifrec = session.xenapi.PIF.get_record(pif)
> + network = pifrec.get('network')
> + nwrec = session.xenapi.network.get_record(network)
> + if nwrec.get('name_label') == 'cloud-guest':
> + return pifrec.get('device')
> + if pifrec.get('management'):
> + mgmtnic = pifrec.get('device')
> +
> + return mgmtnic
> +
> +def chain_name(vm_name):
> + if vm_name.startswith('i-') or vm_name.startswith('r-'):
> + if vm_name.endswith('untagged'):
> + return '-'.join(vm_name.split('-')[:-1])
> + return vm_name
> +
> +def chain_name_def(vm_name):
> + if vm_name.startswith('i-'):
> + if vm_name.endswith('untagged'):
> + return '-'.join(vm_name.split('-')[:-2]) + "-def"
> + return '-'.join(vm_name.split('-')[:-1]) + "-def"
> + return vm_name
> +
> +def egress_chain_name(vm_name):
> + return chain_name(vm_name) + "-eg"
> +
> +@echo
> +def can_bridge_firewall(session, args):
> + try:
> + util.pread2(['ebtables', '-V'])
> + util.pread2(['ipset', '-V'])
> + except:
> + return 'false'
> +
> + host_uuid = args.get('host_uuid')
> + try:
> + util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
> + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
> + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '-p', 'udp', '--dport', '67', '--sport', '68', '-j', 'ACCEPT'])
> + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '-p', 'udp', '--dport', '68', '--sport', '67', '-j', 'ACCEPT'])
> + util.pread2(['iptables', '-D', 'FORWARD', '-j', 'RH-Firewall-1-INPUT'])
> + except:
> + util.SMlog('Chain BRIDGE-FIREWALL already exists')
> + privnic = get_private_nic(session, args)
> + result = 'true'
> + try:
> + util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
> + except:
> + try:
> + util.pread2(['iptables', '-I', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
> + util.pread2(['iptables', '-A', 'FORWARD', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j', 'ACCEPT'])
> + util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
> + except:
> + return 'false'
> + default_ebtables_rules()
> + allow_egress_traffic(session)
> + if not os.path.exists('/var/run/cloud'):
> + os.makedirs('/var/run/cloud')
> + if not os.path.exists('/var/cache/cloud'):
> + os.makedirs('/var/cache/cloud')
> + #get_ipset_keyword()
> +
> + cleanup_rules_for_dead_vms(session)
> + cleanup_rules(session, args)
> +
> + return result
> +
> +@echo
> +def default_ebtables_rules():
> + try:
> + util.pread2(['ebtables', '-N', 'DEFAULT_EBTABLES'])
> + util.pread2(['ebtables', '-A', 'FORWARD', '-j' 'DEFAULT_EBTABLES'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'ACCEPT'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
> + # deny mac broadcast and multicast
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Broadcast', '-j', 'DROP'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-d', 'Multicast', '-j', 'DROP'])
> + # deny ip broadcast and multicast
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv4', '-j', 'RETURN'])
> + # deny ipv6
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', 'IPv6', '-j', 'DROP'])
> + # deny vlan
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p', '802_1Q', '-j', 'DROP'])
> + # deny all others (e.g., 802.1d, CDP)
> + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-j', 'DROP'])
> + except:
> + util.SMlog('Chain DEFAULT_EBTABLES already exists')
> +
> +
> +@echo
> +def allow_egress_traffic(session):
> + devs = []
> + for pif in session.xenapi.PIF.get_all():
> + pif_rec = session.xenapi.PIF.get_record(pif)
> + vlan = pif_rec.get('VLAN')
> + dev = pif_rec.get('device')
> + if vlan == '-1':
> + devs.append(dev)
> + else:
> + devs.append(dev + "." + vlan)
> + for d in devs:
> + try:
> + util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD | grep '%s '" % d])
> + except:
> + try:
> + util.pread2(['iptables', '-I', 'FORWARD', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
> + except:
> + util.SMlog("Failed to add FORWARD rule through to %s" % d)
> + return 'false'
> + return 'true'
> +
> +
> +def ipset(ipsetname, proto, start, end, ips):
> + try:
> + util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
> + except:
> + util.SMlog("ipset chain already exists" + ipsetname)
> +
> + result = True
> + ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) + str(int(time.time()) % 1000)
> +
> + try:
> + util.pread2(['ipset', '-N', ipsettmp, 'iptreemap'])
> + except:
> + util.SMlog("Failed to create temp ipset, reusing old name= " + ipsettmp)
> + try:
> + util.pread2(['ipset', '-F', ipsettmp])
> + except:
> + util.SMlog("Failed to clear old temp ipset name=" + ipsettmp)
> + return False
> +
> + try:
> + for ip in ips:
> + try:
> + util.pread2(['ipset', '-A', ipsettmp, ip])
> + except CommandException, cex:
> + if cex.reason.rfind('already in set') == -1:
> + raise
> + except:
> + util.SMlog("Failed to program ipset " + ipsetname)
> + util.pread2(['ipset', '-F', ipsettmp])
> + util.pread2(['ipset', '-X', ipsettmp])
> + return False
> +
> + try:
> + util.pread2(['ipset', '-W', ipsettmp, ipsetname])
> + except:
> + util.SMlog("Failed to swap ipset " + ipsetname)
> + result = False
> +
> + try:
> + util.pread2(['ipset', '-F', ipsettmp])
> + util.pread2(['ipset', '-X', ipsettmp])
> + except:
> + # if the temporary name clashes next time we'll just reuse it
> + util.SMlog("Failed to delete temp ipset " + ipsettmp)
> +
> + return result
> +
> +@echo
> +def destroy_network_rules_for_vm(session, args):
> + vm_name = args.pop('vmName')
> + vmchain = chain_name(vm_name)
> + vmchain_egress = egress_chain_name(vm_name)
> + vmchain_default = chain_name_def(vm_name)
> +
> + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> + if vm_name.startswith('i-') or vm_name.startswith('r-') or vm_name.startswith('l-'):
> + try:
> + util.pread2(['iptables', '-F', vmchain_default])
> + util.pread2(['iptables', '-X', vmchain_default])
> + except:
> + util.SMlog("Ignoring failure to delete chain " + vmchain_default)
> +
> + destroy_ebtables_rules(vmchain)
> +
> + try:
> + util.pread2(['iptables', '-F', vmchain])
> + util.pread2(['iptables', '-X', vmchain])
> + except:
> + util.SMlog("Ignoring failure to delete ingress chain " + vmchain)
> +
> +
> + try:
> + util.pread2(['iptables', '-F', vmchain_egress])
> + util.pread2(['iptables', '-X', vmchain_egress])
> + except:
> + util.SMlog("Ignoring failure to delete egress chain " + vmchain_egress)
> +
> + remove_rule_log_for_vm(vm_name)
> +
> + if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
> + return 'true'
> +
> + try:
> + setscmd = "ipset --save | grep " + vmchain + " | grep '^-N' | awk '{print $2}'"
> + setsforvm = util.pread2(['/bin/bash', '-c', setscmd]).split('\n')
> + for set in setsforvm:
> + if set != '':
> + util.pread2(['ipset', '-F', set])
> + util.pread2(['ipset', '-X', set])
> + except:
> + util.SMlog("Failed to destroy ipsets for %" % vm_name)
> +
> +
> + return 'true'
> +
> +@echo
> +def destroy_ebtables_rules(vm_chain):
> +
> + delcmd = "ebtables-save | grep " + vm_chain + " | sed 's/-A/-D/'"
> + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> + delcmds.pop()
> + for cmd in delcmds:
> + try:
> + dc = cmd.split(' ')
> + dc.insert(0, 'ebtables')
> + util.pread2(dc)
> + except:
> + util.SMlog("Ignoring failure to delete ebtables rules for vm " + vm_chain)
> + try:
> + util.pread2(['ebtables', '-F', vm_chain])
> + util.pread2(['ebtables', '-X', vm_chain])
> + except:
> + util.SMlog("Ignoring failure to delete ebtables chain for vm " + vm_chain)
> +
> +@echo
> +def destroy_arptables_rules(vm_chain):
> + delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
> + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> + delcmds.pop()
> + for cmd in delcmds:
> + try:
> + dc = cmd.split(' ')
> + dc.insert(0, 'arptables')
> + dc.insert(1, '-D')
> + dc.insert(2, 'FORWARD')
> + util.pread2(dc)
> + except:
> + util.SMlog("Ignoring failure to delete arptables rules for vm " + vm_chain)
> +
> + try:
> + util.pread2(['arptables', '-F', vm_chain])
> + util.pread2(['arptables', '-X', vm_chain])
> + except:
> + util.SMlog("Ignoring failure to delete arptables chain for vm " + vm_chain)
> +
> +@echo
> +def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
> + if vm_mac == 'ff:ff:ff:ff:ff:ff':
> + util.SMlog("Ignoring since mac address is not valid")
> + return 'true'
> +
> + try:
> + util.pread2(['ebtables', '-N', vm_chain])
> + except:
> + try:
> + util.pread2(['ebtables', '-F', vm_chain])
> + except:
> + util.SMlog("Failed to create ebtables antispoof chain, skipping")
> + return 'true'
> +
> + # note all rules for packets into the bridge (-i) precede all output rules (-o)
> + # always start after the first rule in the FORWARD chain that jumps to DEFAULT_EBTABLES chain
> + try:
> + for vif in vifs:
> + util.pread2(['ebtables', '-I', 'FORWARD', '2', '-i', vif, '-j', vm_chain])
> + util.pread2(['ebtables', '-A', 'FORWARD', '-o', vif, '-j', vm_chain])
> + except:
> + util.SMlog("Failed to program default ebtables FORWARD rules for %s" % vm_chain)
> + return 'false'
> +
> + try:
> + for vif in vifs:
> + # only allow source mac that belongs to the vm
> + util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s', '!', vm_mac, '-j', 'DROP'])
> + # do not allow fake dhcp responses
> + util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
> + # do not allow snooping of dhcp requests
> + util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
> + except:
> + util.SMlog("Failed to program default ebtables antispoof rules for %s" % vm_chain)
> + return 'false'
> +
> + return 'true'
> +
> +@echo
> +def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
> + if vm_mac == 'ff:ff:ff:ff:ff:ff':
> + util.SMlog("Ignoring since mac address is not valid")
> + return 'true'
> +
> + try:
> + util.pread2(['arptables', '-N', vm_chain])
> + except:
> + try:
> + util.pread2(['arptables', '-F', vm_chain])
> + except:
> + util.SMlog("Failed to create arptables rule, skipping")
> + return 'true'
> +
> + # note all rules for packets into the bridge (-i) precede all output rules (-o)
> + try:
> + for vif in vifs:
> + util.pread2(['arptables', '-I', 'FORWARD', '-i', vif, '-j', vm_chain])
> + util.pread2(['arptables', '-A', 'FORWARD', '-o', vif, '-j', vm_chain])
> + except:
> + util.SMlog("Failed to program default arptables rules in FORWARD chain vm=" + vm_chain)
> + return 'false'
> +
> + try:
> + for vif in vifs:
> + #accept arp replies into the bridge as long as the source mac and ips match the vm
> + util.pread2(['arptables', '-A', vm_chain, '-i', vif, '--opcode', 'Reply', '--source-mac', vm_mac, '--source-ip', vm_ip, '-j', 'ACCEPT'])
> + #accept any arp requests from this vm. In the future this can be restricted to deny attacks on hosts
> + #also important to restrict source ip and src mac in these requests as they can be used to update arp tables on destination
> + util.pread2(['arptables', '-A', vm_chain, '-i', vif, '--opcode', 'Request', '--source-mac', vm_mac, '--source-ip', vm_ip, '-j', 'RETURN'])
> + #accept any arp requests to this vm as long as the request is for this vm's ip
> + util.pread2(['arptables', '-A', vm_chain, '-o', vif, '--opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])
> + #accept any arp replies to this vm as long as the mac and ip matches
> + util.pread2(['arptables', '-A', vm_chain, '-o', vif, '--opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip', vm_ip, '-j', 'ACCEPT'])
> + util.pread2(['arptables', '-A', vm_chain, '-j', 'DROP'])
> +
> + except:
> + util.SMlog("Failed to program default arptables rules")
> + return 'false'
> +
> + return 'true'
> +
> +@echo
> +def default_network_rules_systemvm(session, args):
> + vm_name = args.pop('vmName')
> + try:
> + vm = session.xenapi.VM.get_by_name_label(vm_name)
> + if len(vm) != 1:
> + return 'false'
> + vm_rec = session.xenapi.VM.get_record(vm[0])
> + vm_vifs = vm_rec.get('VIFs')
> + vifnums = [session.xenapi.VIF.get_record(vif).get('device') for vif in vm_vifs]
> + domid = vm_rec.get('domid')
> + except:
> + util.SMlog("### Failed to get domid or vif list for vm ##" + vm_name)
> + return 'false'
> +
> + if domid == '-1':
> + util.SMlog("### Failed to get domid for vm (-1): " + vm_name)
> + return 'false'
> +
> + vifs = ["vif" + domid + "." + v for v in vifnums]
> + #vm_name = '-'.join(vm_name.split('-')[:-1])
> + vmchain = chain_name(vm_name)
> +
> +
> + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> +
> + try:
> + util.pread2(['iptables', '-N', vmchain])
> + except:
> + util.pread2(['iptables', '-F', vmchain])
> +
> + allow_egress_traffic(session)
> +
> + for vif in vifs:
> + try:
> + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
> + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '4', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', vmchain])
> + util.pread2(['iptables', '-I', vmchain, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
> + except:
> + util.SMlog("Failed to program default rules")
> + return 'false'
> +
> +
> + util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
> +
> + if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid, '_initial_', '-1') == False:
> + util.SMlog("Failed to log default network rules for systemvm, ignoring")
> + return 'true'
> +
> +
> +@echo
> +def default_network_rules(session, args):
> + vm_name = args.pop('vmName')
> + vm_ip = args.pop('vmIP')
> + vm_id = args.pop('vmID')
> + vm_mac = args.pop('vmMAC')
> +
> + try:
> + vm = session.xenapi.VM.get_by_name_label(vm_name)
> + if len(vm) != 1:
> + util.SMlog("### Failed to get record for vm " + vm_name)
> + return 'false'
> + vm_rec = session.xenapi.VM.get_record(vm[0])
> + domid = vm_rec.get('domid')
> + except:
> + util.SMlog("### Failed to get domid for vm " + vm_name)
> + return 'false'
> + if domid == '-1':
> + util.SMlog("### Failed to get domid for vm (-1): " + vm_name)
> + return 'false'
> +
> + vif = "vif" + domid + ".0"
> + tap = "tap" + domid + ".0"
> + vifs = [vif]
> + try:
> + util.pread2(['ifconfig', tap])
> + vifs.append(tap)
> + except:
> + pass
> +
> + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> +
> +
> + vmchain = chain_name(vm_name)
> + vmchain_egress = egress_chain_name(vm_name)
> + vmchain_default = chain_name_def(vm_name)
> +
> + destroy_ebtables_rules(vmchain)
> +
> +
> + try:
> + util.pread2(['iptables', '-N', vmchain])
> + except:
> + util.pread2(['iptables', '-F', vmchain])
> +
> + try:
> + util.pread2(['iptables', '-N', vmchain_egress])
> + except:
> + util.pread2(['iptables', '-F', vmchain_egress])
> +
> + try:
> + util.pread2(['iptables', '-N', vmchain_default])
> + except:
> + util.pread2(['iptables', '-F', vmchain_default])
> +
> + try:
> + for v in vifs:
> + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
> + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
> + #allow dhcp
> + for v in vifs:
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-p', 'udp', '--dport', '67', '--sport', '68', '-j', 'ACCEPT'])
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-p', 'udp', '--dport', '68', '--sport', '67', '-j', 'ACCEPT'])
> +
> + #don't let vm spoof its ip address
> + for v in vifs:
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', '!', vm_ip, '-j', 'DROP'])
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '--destination', '!', vm_ip, '-j', 'DROP'])
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip, '-j', vmchain_egress])
> +
> + for v in vifs:
> + util.pread2(['iptables', '-A', vmchain_default, '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain])
> + except:
> + util.SMlog("Failed to program default rules for vm " + vm_name)
> + return 'false'
> +
> + default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
> + default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
> +
> + if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, '_initial_', '-1', vm_mac) == False:
> + util.SMlog("Failed to log default network rules, ignoring")
> +
> + util.SMlog("Programmed default rules for vm " + vm_name)
> + return 'true'
> +
> +@echo
> +def check_domid_changed(session, vmName):
> + curr_domid = '-1'
> + try:
> + vm = session.xenapi.VM.get_by_name_label(vmName)
> + if len(vm) != 1:
> + util.SMlog("### Could not get record for vm ## " + vmName)
> + else:
> + vm_rec = session.xenapi.VM.get_record(vm[0])
> + curr_domid = vm_rec.get('domid')
> + except:
> + util.SMlog("### Failed to get domid for vm ## " + vmName)
> +
> +
> + logfilename = "/var/run/cloud/" + vmName +".log"
> + if not os.path.exists(logfilename):
> + return ['-1', curr_domid]
> +
> + lines = (line.rstrip() for line in open(logfilename))
> +
> + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> + for line in lines:
> + try:
> + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac] = line.split(',')
> + except ValueError,v:
> + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] = line.split(',')
> + break
> +
> + return [curr_domid, old_domid]
> +
> +@echo
> +def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
> + vm_name = vmName
> + vmchain = chain_name_def(vm_name)
> +
> + delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep " + vmchain + " | sed 's/-A/-D/'"
> + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> + delcmds.pop()
> + for cmd in delcmds:
> + try:
> + dc = cmd.split(' ')
> + dc.insert(0, 'iptables')
> + dc.pop()
> + util.pread2(filter(None, dc))
> + except:
> + util.SMlog("Ignoring failure to delete rules for vm " + vmName)
> +
> +
> +@echo
> +def network_rules_for_rebooted_vm(session, vmName):
> + vm_name = vmName
> + [curr_domid, old_domid] = check_domid_changed(session, vm_name)
> +
> + if curr_domid == old_domid:
> + return True
> +
> + if old_domid == '-1':
> + return True
> +
> + if curr_domid == '-1':
> + return True
> +
> + util.SMlog("Found a rebooted VM -- reprogramming rules for " + vm_name)
> +
> + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> + if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-'] ]:
> + default_network_rules_systemvm(session, {"vmName":vm_name})
> + return True
> +
> + vif = "vif" + curr_domid + ".0"
> + tap = "tap" + curr_domid + ".0"
> + vifs = [vif]
> + try:
> + util.pread2(['ifconfig', tap])
> + vifs.append(tap)
> + except:
> + pass
> + vmchain = chain_name(vm_name)
> + vmchain_default = chain_name_def(vm_name)
> +
> + for v in vifs:
> + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m', 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain_default])
> + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j', vmchain_default])
> +
> + #change antispoof rule in vmchain
> + try:
> + delcmd = "iptables-save | grep '\-A " + vmchain_default + "' | grep physdev-in | sed 's/-A/-D/'"
> + delcmd2 = "iptables-save | grep '\-A " + vmchain_default + "' | grep physdev-out | sed 's/-A/-D/'"
> + inscmd = "iptables-save | grep '\-A " + vmchain_default + "' | grep physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
> + inscmd2 = "iptables-save| grep '\-A " + vmchain_default + "' | grep physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
> + inscmd3 = "iptables-save | grep '\-A " + vmchain_default + "' | grep physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/' "
> + inscmd4 = "iptables-save| grep '\-A " + vmchain_default + "' | grep physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/' "
> +
> + ipts = []
> + for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3, inscmd4]:
> + cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
> + cmds.pop()
> + for c in cmds:
> + ipt = c.split(' ')
> + ipt.insert(0, 'iptables')
> + ipt.pop()
> + ipts.append(ipt)
> +
> + for ipt in ipts:
> + try:
> + util.pread2(filter(None,ipt))
> + except:
> + util.SMlog("Failed to rewrite antispoofing rules for vm " + vm_name)
> +
> + util.pread2(['/bin/bash', '-c', 'iptables -D ' + vmchain_default + " -j " + vmchain])
> + util.pread2(['/bin/bash', '-c', 'iptables -A ' + vmchain_default + " -j " + vmchain])
> + except:
> + util.SMlog("No rules found for vm " + vm_name)
> +
> + destroy_ebtables_rules(vmchain)
> + destroy_arptables_rules(vmchain)
> + [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
> + default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
> + default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
> + rewrite_rule_log_for_vm(vm_name, curr_domid)
> + return True
> +
> +def rewrite_rule_log_for_vm(vm_name, new_domid):
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> + if not os.path.exists(logfilename):
> + return
> + lines = (line.rstrip() for line in open(logfilename))
> +
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
> + for line in lines:
> + try:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
> + break
> + except ValueError,v:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
> +
> + write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid, _signature, '-1', _vmMac)
> +
> +def get_rule_log_for_vm(session, vmName):
> + vm_name = vmName;
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> + if not os.path.exists(logfilename):
> + return ''
> +
> + lines = (line.rstrip() for line in open(logfilename))
> +
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> + for line in lines:
> + try:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
> + break
> + except ValueError,v:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
> +
> + return ','.join([_vmName, _vmID, _vmIP, _domID, _signature, _seqno])
> +
> +@echo
> +def get_vm_mac_ip_from_log(vm_name):
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> + if not os.path.exists(logfilename):
> + return ['_', '_']
> +
> + lines = (line.rstrip() for line in open(logfilename))
> + for line in lines:
> + try:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = line.split(',')
> + break
> + except ValueError,v:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
> +
> + return [ _vmIP, _vmMac]
> +
> +@echo
> +def get_rule_logs_for_vms(session, args):
> + host_uuid = args.pop('host_uuid')
> + try:
> + thishost = session.xenapi.host.get_by_uuid(host_uuid)
> + hostrec = session.xenapi.host.get_record(thishost)
> + vms = hostrec.get('resident_VMs')
> + except:
> + util.SMlog("Failed to get host from uuid " + host_uuid)
> + return ' '
> +
> + result = []
> + try:
> + for name in [session.xenapi.VM.get_name_label(x) for x in vms]:
> + if 1 not in [ name.startswith(c) for c in ['r-', 's-', 'v-', 'i-', 'l-'] ]:
> + continue
> + network_rules_for_rebooted_vm(session, name)
> + if name.startswith('i-'):
> + log = get_rule_log_for_vm(session, name)
> + result.append(log)
> + except:
> + util.SMlog("Failed to get rule logs, better luck next time!")
> +
> + return ";".join(result)
> +
> +@echo
> +def cleanup_rules_for_dead_vms(session):
> + try:
> + vms = session.xenapi.VM.get_all()
> + cleaned = 0
> + for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
> + if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-'] ]:
> + vm = session.xenapi.VM.get_by_name_label(vm_name)
> + if len(vm) != 1:
> + continue
> + vm_rec = session.xenapi.VM.get_record(vm[0])
> + state = vm_rec.get('power_state')
> + if state != 'Running' and state != 'Paused':
> + util.SMlog("vm " + vm_name + " is not running, cleaning up")
> + destroy_network_rules_for_vm(session, {'vmName':vm_name})
> + cleaned = cleaned+1
> +
> + util.SMlog("Cleaned up rules for " + str(cleaned) + " vms")
> + except:
> + util.SMlog("Failed to cleanup rules for dead vms!")
> +
> +
> +@echo
> +def cleanup_rules(session, args):
> + instance = args.get('instance')
> + if not instance:
> + instance = 'VM'
> + resident_vms = []
> + try:
> + hostname = util.pread2(['/bin/bash', '-c', 'hostname']).split('\n')
> + if len(hostname) < 1:
> + raise Exception('Could not find hostname of this host')
> + thishost = session.xenapi.host.get_by_name_label(hostname[0])
> + if len(thishost) < 1:
> + raise Exception("Could not find host record from hostname %s of this host"%hostname[0])
> + hostrec = session.xenapi.host.get_record(thishost[0])
> + vms = hostrec.get('resident_VMs')
> + resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
> + util.SMlog('cleanup_rules: found %s resident vms on this host %s' % (len(resident_vms)-1, hostname[0]))
> +
> + chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
> + chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
> + vmchains = [ch for ch in chains if 1 in [ ch.startswith(c) for c in ['r-', 'i-', 's-', 'v-', 'l-']]]
> + util.SMlog('cleanup_rules: found %s iptables chains for vms on this host %s' % (len(vmchains), hostname[0]))
> + cleaned = 0
> + cleanup = []
> + for chain in vmchains:
> + vm = session.xenapi.VM.get_by_name_label(chain)
> + if len(vm) != 1:
> + vm = session.xenapi.VM.get_by_name_label(chain + "-untagged")
> + if len(vm) != 1:
> + util.SMlog("chain " + chain + " does not correspond to a vm, cleaning up")
> + cleanup.append(chain)
> + continue
> + if chain not in resident_vms:
> + util.SMlog("vm " + chain + " is not running, cleaning up")
> + cleanup.append(chain)
> +
> + for vm_name in cleanup:
> + destroy_network_rules_for_vm(session, {'vmName':vm_name})
> +
> + util.SMlog("Cleaned up rules for " + str(len(cleanup)) + " chains")
> + return str(len(cleanup))
> + except Exception, ex:
> + util.SMlog("Failed to cleanup rules, reason= " + str(ex))
> + return '-1';
> +
> +@echo
> +def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno):
> + vm_name = vmName;
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> + if not os.path.exists(logfilename):
> + util.SMlog("Failed to find logfile %s" %logfilename)
> + return [True, True, True]
> +
> + lines = (line.rstrip() for line in open(logfilename))
> +
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> + try:
> + for line in lines:
> + try:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno, _vmMac] = line.split(',')
> + except ValueError,v:
> + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] = line.split(',')
> + break
> + except:
> + util.SMlog("Failed to parse log file for vm " + vmName)
> + remove_rule_log_for_vm(vmName)
> + return [True, True, True]
> +
> + reprogramDefault = False
> + if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
> + util.SMlog("Change in default info set of vm %s" % vmName)
> + return [True, True, True]
> + else:
> + util.SMlog("No change in default info set of vm %s" % vmName)
> +
> + reprogramChain = False
> + rewriteLog = True
> + if (int(seqno) > int(_seqno)):
> + if (_signature != signature):
> + reprogramChain = True
> + util.SMlog("Seqno increased from %s to %s: reprogamming "\
> + "ingress rules for vm %s" % (_seqno, seqno, vmName))
> + else:
> + util.SMlog("Seqno increased from %s to %s: but no change "\
> + "in signature for vm: skip programming ingress "\
> + "rules %s" % (_seqno, seqno, vmName))
> + elif (int(seqno) < int(_seqno)):
> + util.SMlog("Seqno decreased from %s to %s: ignoring these "\
> + "ingress rules for vm %s" % (_seqno, seqno, vmName))
> + rewriteLog = False
> + elif (signature != _signature):
> + util.SMlog("Seqno %s stayed the same but signature changed from "\
> + "%s to %s for vm %s" % (seqno, _signature, signature, vmName))
> + rewriteLog = True
> + reprogramChain = True
> + else:
> + util.SMlog("Seqno and signature stayed the same: %s : ignoring these "\
> + "ingress rules for vm %s" % (seqno, vmName))
> + rewriteLog = False
> +
> + return [reprogramDefault, reprogramChain, rewriteLog]
> +
> +
> +@echo
> +def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature, seqno, vmMac='ff:ff:ff:ff:ff:ff'):
> + vm_name = vmName
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> + util.SMlog("Writing log to " + logfilename)
> + logf = open(logfilename, 'w')
> + output = ','.join([vmName, vmID, vmIP, domID, signature, seqno, vmMac])
> + result = True
> + try:
> + logf.write(output)
> + logf.write('\n')
> + except:
> + util.SMlog("Failed to write to rule log file " + logfilename)
> + result = False
> +
> + logf.close()
> +
> + return result
> +
> +@echo
> +def remove_rule_log_for_vm(vmName):
> + vm_name = vmName
> + logfilename = "/var/run/cloud/" + vm_name +".log"
> +
> + result = True
> + try:
> + os.remove(logfilename)
> + except:
> + util.SMlog("Failed to delete rule log file " + logfilename)
> + result = False
> +
> + return result
> +
> +@echo
> +def inflate_rules (zipped):
> + return zlib.decompress(base64.b64decode(zipped))
> +
> +@echo
> +def cache_ipset_keyword():
> + tmpname = 'ipsetqzvxtmp'
> + try:
> + util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + ' iptreemap'])
> + except:
> + util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
> +
> + try:
> + util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
> + util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --set ' + tmpname + ' src' + ' -j ACCEPT'])
> + keyword = 'set'
> + except:
> + keyword = 'match-set'
> +
> + try:
> + util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
> + except:
> + pass
> +
> + cachefile = "/var/cache/cloud/ipset.keyword"
> + util.SMlog("Writing ipset keyword to " + cachefile)
> + cachef = open(cachefile, 'w')
> + try:
> + cachef.write(keyword)
> + cachef.write('\n')
> + except:
> + util.SMlog("Failed to write to cache file " + cachef)
> +
> + cachef.close()
> + return keyword
> +
> +@echo
> +def get_ipset_keyword():
> + cachefile = "/var/cache/cloud/ipset.keyword"
> + keyword = 'match-set'
> +
> + if not os.path.exists(cachefile):
> + util.SMlog("Failed to find ipset keyword cachefile %s" %cachefile)
> + keyword = cache_ipset_keyword()
> + else:
> + lines = (line.rstrip() for line in open(cachefile))
> + for line in lines:
> + keyword = line
> + break
> +
> + return keyword
> +
> +@echo
> +def network_rules(session, args):
> + try:
> + vm_name = args.get('vmName')
> + vm_ip = args.get('vmIP')
> + vm_id = args.get('vmID')
> + vm_mac = args.get('vmMAC')
> + signature = args.pop('signature')
> + seqno = args.pop('seqno')
> + deflated = 'false'
> + if 'deflated' in args:
> + deflated = args.pop('deflated')
> +
> + try:
> + vm = session.xenapi.VM.get_by_name_label(vm_name)
> + if len(vm) != 1:
> + util.SMlog("### Could not get record for vm ## " + vm_name)
> + return 'false'
> + vm_rec = session.xenapi.VM.get_record(vm[0])
> + domid = vm_rec.get('domid')
> + except:
> + util.SMlog("### Failed to get domid for vm ## " + vm_name)
> + return 'false'
> + if domid == '-1':
> + util.SMlog("### Failed to get domid for vm (-1): " + vm_name)
> + return 'false'
> +
> + vif = "vif" + domid + ".0"
> + tap = "tap" + domid + ".0"
> + vifs = [vif]
> + try:
> + util.pread2(['ifconfig', tap])
> + vifs.append(tap)
> + except:
> + pass
> +
> +
> + reason = 'seqno_change_or_sig_change'
> + [reprogramDefault, reprogramChain, rewriteLog] = \
> + check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid, signature, seqno)
> +
> + if not reprogramDefault and not reprogramChain:
> + util.SMlog("No changes detected between current state and received state")
> + reason = 'seqno_same_sig_same'
> + if rewriteLog:
> + reason = 'seqno_increased_sig_same'
> + write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac)
> + util.SMlog("Programming network rules for vm %s seqno=%s signature=%s guestIp=%s,"\
> + " do nothing, reason=%s" % (vm_name, seqno, signature, vm_ip, reason))
> + return 'true'
> +
> + if not reprogramChain:
> + util.SMlog("###Not programming any ingress rules since no changes detected?")
> + return 'true'
> +
> + if reprogramDefault:
> + util.SMlog("Change detected in vmId or vmIp or domId, resetting default rules")
> + default_network_rules(session, args)
> + reason = 'domid_change'
> +
> + rules = args.pop('rules')
> + if deflated.lower() == 'true':
> + rules = inflate_rules (rules)
> + keyword = '--' + get_ipset_keyword()
> + lines = rules.split(' ')
> +
> + util.SMlog("Programming network rules for vm %s seqno=%s numrules=%s signature=%s guestIp=%s,"\
> + " update iptables, reason=%s" % (vm_name, seqno, len(lines), signature, vm_ip, reason))
> +
> + cmds = []
> + egressrules = 0
> + for line in lines:
> + tokens = line.split(':')
> + if len(tokens) != 5:
> + continue
> + type = tokens[0]
> + protocol = tokens[1]
> + start = tokens[2]
> + end = tokens[3]
> + cidrs = tokens.pop();
> + ips = cidrs.split(",")
> + ips.pop()
> + allow_any = False
> +
> + if type == 'E':
> + vmchain = egress_chain_name(vm_name)
> + action = "RETURN"
> + direction = "dst"
> + egressrules = egressrules + 1
> + else:
> + vmchain = chain_name(vm_name)
> + action = "ACCEPT"
> + direction = "src"
> + if '0.0.0.0/0' in ips:
> + i = ips.index('0.0.0.0/0')
> + del ips[i]
> + allow_any = True
> + range = start + ":" + end
> + if ips:
> + ipsetname = vmchain + "_" + protocol + "_" + start + "_" + end
> + if start == "-1":
> + ipsetname = vmchain + "_" + protocol + "_any"
> +
> + if ipset(ipsetname, protocol, start, end, ips) == False:
> + util.SMlog(" failed to create ipset for rule " + str(tokens))
> +
> + if protocol == 'all':
> + iptables = ['iptables', '-I', vmchain, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
> + elif protocol != 'icmp':
> + iptables = ['iptables', '-I', vmchain, '-p', protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
> + else:
> + range = start + "/" + end
> + if start == "-1":
> + range = "any"
> + iptables = ['iptables', '-I', vmchain, '-p', 'icmp', '--icmp-type', range, '-m', 'set', keyword, ipsetname, direction, '-j', action]
> +
> + cmds.append(iptables)
> + util.SMlog(iptables)
> +
> + if allow_any and protocol != 'all':
> + if protocol != 'icmp':
> + iptables = ['iptables', '-I', vmchain, '-p', protocol, '-m', protocol, '--dport', range, '-m', 'state', '--state', 'NEW', '-j', action]
> + else:
> + range = start + "/" + end
> + if start == "-1":
> + range = "any"
> + iptables = ['iptables', '-I', vmchain, '-p', 'icmp', '--icmp-type', range, '-j', action]
> + cmds.append(iptables)
> + util.SMlog(iptables)
> +
> + vmchain = chain_name(vm_name)
> + util.pread2(['iptables', '-F', vmchain])
> + egress_vmchain = egress_chain_name(vm_name)
> + util.pread2(['iptables', '-F', egress_vmchain])
> +
> + for cmd in cmds:
> + util.pread2(cmd)
> +
> + if egressrules == 0 :
> + util.pread2(['iptables', '-A', egress_vmchain, '-j', 'RETURN'])
> + else:
> + util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
> +
> + util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
> +
> + if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature, seqno, vm_mac) == False:
> + return 'false'
> +
> + return 'true'
> + except:
> + util.SMlog("Failed to network rule !")
> +
> +@echo
> +def checkRouter(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/getRouterStatus.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + except:
> + util.SMlog(" check router status fail! ")
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def bumpUpPriority(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/bumpUpPriority.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + txt = 'success'
> + except:
> + util.SMlog("bump up priority fail! ")
> + txt = ''
> +
> + return txt
> +
> +@echo
> +def createISOVHD(session, args):
> + #hack for XCP on ubuntu 12.04, as can't attach iso to a vm
> + vdis = session.xenapi.VDI.get_by_name_label("systemvm-vdi");
> + util.SMlog(vdis)
> + if len(vdis) > 0:
> + vdi_record = session.xenapi.VDI.get_record(vdis[0])
> + vdi_uuid = vdi_record['uuid']
> + return vdi_uuid
> + localsrUUid = args['uuid'];
> + sr = session.xenapi.SR.get_by_uuid(localsrUUid)
> + data = {'name_label': "systemvm-vdi",
> + 'SR': sr,
> + 'virtual_size': '50000000',
> + 'type': 'user',
> + 'sharable':False,
> + 'read_only':False,
> + 'other_config':{},
> + }
> + vdi = session.xenapi.VDI.create(data);
> + vdi_record = session.xenapi.VDI.get_record(vdi)
> +
> + vdi_uuid = vdi_record['uuid']
> +
> + vms = session.xenapi.VM.get_all()
> + ctrldom = None
> + for vm in vms:
> + dom0 = session.xenapi.VM.get_is_control_domain(vm)
> + if dom0 is False:
> + continue
> + else:
> + ctrldom = vm
> +
> + if ctrldom is None:
> + return "Failed"
> +
> + vbds = session.xenapi.VM.get_VBDs(ctrldom)
> + if len(vbds) == 0:
> + vbd = session.xenapi.VBD.create({"VDI": vdi, "VM": ctrldom, "type":"Disk", "device": "xvda4", "bootable": False, "mode": "RW", "userdevice": "4", "empty":False,
> + "other_config":{}, "qos_algorithm_type":"", "qos_algorithm_params":{}})
> + else:
> + vbd = vbds[0]
> +
> + vbdr = session.xenapi.VBD.get_record(vbd)
> + if session.xenapi.VBD.get_currently_attached(vbd) is False:
> + session.xenapi.VBD.plug(vbd)
> + vbdr = session.xenapi.VBD.get_record(vbd)
> + util.pread2(["dd", "if=/usr/share/xcp/packages/iso/systemvm.iso", "of=" + "/dev/" + vbdr["device"]])
> + session.xenapi.VBD.unplug(vbd)
> + session.xenapi.VBD.destroy(vbd)
> + return vdi_uuid
> +
> +@echo
> +def getDomRVersion(session, args):
> + sargs = args['args']
> + cmd = sargs.split(' ')
> + cmd.insert(0, "/usr/lib/xcp/bin/getDomRVersion.sh")
> + cmd.insert(0, "/bin/bash")
> + try:
> + txt = util.pread2(cmd)
> + except:
> + util.SMlog(" get domR version fail! ")
> + txt = ''
> +
> + return txt
> +
> +if __name__ == "__main__":
> + XenAPIPlugin.dispatch({"pingtest": pingtest, "setup_iscsi":setup_iscsi, "gethostvmstats": gethostvmstats,
> + "getvncport": getvncport, "getgateway": getgateway, "preparemigration": preparemigration,
> + "setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,
> + "ipassoc": ipassoc, "vm_data": vm_data, "savePassword": savePassword,
> + "saveDhcpEntry": saveDhcpEntry, "setFirewallRule": setFirewallRule,
> + "setLoadBalancerRule": setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile,
> + "networkUsage": networkUsage, "network_rules":network_rules,
> + "can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
> + "destroy_network_rules_for_vm":destroy_network_rules_for_vm,
> + "default_network_rules_systemvm":default_network_rules_systemvm,
> + "get_rule_logs_for_vms":get_rule_logs_for_vms,
> + "setLinkLocalIP":setLinkLocalIP, "lt2p_vpn":lt2p_vpn,
> + "cleanup_rules":cleanup_rules, "checkRouter":checkRouter,
> + "bumpUpPriority":bumpUpPriority, "getDomRVersion":getDomRVersion,
> + "kill_copy_process":kill_copy_process,
> + "createISOVHD":createISOVHD})
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> ----------------------------------------------------------------------
> diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot b/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> new file mode 100644
> index 0000000..15ea62f
> --- /dev/null
> +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> @@ -0,0 +1,552 @@
> +#!/usr/bin/python
> +# Version @VERSION@
> +#
> +# A plugin for executing script needed by vmops cloud
> +
> +import os, sys, time
> +import XenAPIPlugin
> +sys.path.append("/usr/lib/xcp/sm/")
> +import SR, VDI, SRCommand, util, lvutil
> +from util import CommandException
> +import vhdutil
> +import shutil
> +import lvhdutil
> +import errno
> +import subprocess
> +import xs_errors
> +import cleanup
> +import stat
> +import random
> +
> +VHD_UTIL = 'vhd-util'
> +VHD_PREFIX = 'VHD-'
> +CLOUD_DIR = '/run/cloud_mount'
> +
> +def echo(fn):
> + def wrapped(*v, **k):
> + name = fn.__name__
> + util.SMlog("#### VMOPS enter %s ####" % name )
> + res = fn(*v, **k)
> + util.SMlog("#### VMOPS exit %s ####" % name )
> + return res
> + return wrapped
> +
> +
> +@echo
> +def create_secondary_storage_folder(session, args):
> + local_mount_path = None
> +
> + util.SMlog("create_secondary_storage_folder, args: " + str(args))
> +
> + try:
> + try:
> + # Mount the remote resource folder locally
> + remote_mount_path = args["remoteMountPath"]
> + local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
> + mount(remote_mount_path, local_mount_path)
> +
> + # Create the new folder
> + new_folder = local_mount_path + "/" + args["newFolder"]
> + if not os.path.isdir(new_folder):
> + current_umask = os.umask(0)
> + os.makedirs(new_folder)
> + os.umask(current_umask)
> + except OSError, (errno, strerror):
> + errMsg = "create_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + except:
> + errMsg = "create_secondary_storage_folder failed."
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + finally:
> + if local_mount_path != None:
> + # Unmount the local folder
> + umount(local_mount_path)
> + # Remove the local folder
> + os.system("rmdir " + local_mount_path)
> +
> + return "1"
> +
> +@echo
> +def delete_secondary_storage_folder(session, args):
> + local_mount_path = None
> +
> + util.SMlog("delete_secondary_storage_folder, args: " + str(args))
> +
> + try:
> + try:
> + # Mount the remote resource folder locally
> + remote_mount_path = args["remoteMountPath"]
> + local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
> + mount(remote_mount_path, local_mount_path)
> +
> + # Delete the specified folder
> + folder = local_mount_path + "/" + args["folder"]
> + if os.path.isdir(folder):
> + os.system("rm -f " + folder + "/*")
> + os.system("rmdir " + folder)
> + except OSError, (errno, strerror):
> + errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + except:
> + errMsg = "delete_secondary_storage_folder failed."
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + finally:
> + if local_mount_path != None:
> + # Unmount the local folder
> + umount(local_mount_path)
> + # Remove the local folder
> + os.system("rmdir " + local_mount_path)
> +
> + return "1"
> +
> +@echo
> +def post_create_private_template(session, args):
> + local_mount_path = None
> + try:
> + try:
> + # get local template folder
> + templatePath = args["templatePath"]
> + local_mount_path = os.path.join(CLOUD_DIR, util.gen_uuid())
> + mount(templatePath, local_mount_path)
> + # Retrieve args
> + filename = args["templateFilename"]
> + name = args["templateName"]
> + description = args["templateDescription"]
> + checksum = args["checksum"]
> + file_size = args["size"]
> + virtual_size = args["virtualSize"]
> + template_id = args["templateId"]
> +
> + # Create the template.properties file
> + template_properties_install_path = local_mount_path + "/template.properties"
> + f = open(template_properties_install_path, "w")
> + f.write("filename=" + filename + "\n")
> + f.write("vhd=true\n")
> + f.write("id=" + template_id + "\n")
> + f.write("vhd.filename=" + filename + "\n")
> + f.write("public=false\n")
> + f.write("uniquename=" + name + "\n")
> + f.write("vhd.virtualsize=" + virtual_size + "\n")
> + f.write("virtualsize=" + virtual_size + "\n")
> + f.write("checksum=" + checksum + "\n")
> + f.write("hvm=true\n")
> + f.write("description=" + description + "\n")
> + f.write("vhd.size=" + str(file_size) + "\n")
> + f.write("size=" + str(file_size) + "\n")
> + f.close()
> + util.SMlog("Created template.properties file")
> +
> + # Set permissions
> + permissions = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
> + os.chmod(template_properties_install_path, permissions)
> + util.SMlog("Set permissions on template and template.properties")
> +
> + except:
> + errMsg = "post_create_private_template failed."
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + finally:
> + if local_mount_path != None:
> + # Unmount the local folder
> + umount(local_mount_path)
> + # Remove the local folder
> + os.system("rmdir " + local_mount_path)
> + return "1"
> +
> +def isfile(path, isISCSI):
> + errMsg = ''
> + exists = True
> + if isISCSI:
> + exists = checkVolumeAvailablility(path)
> + else:
> + exists = os.path.isfile(path)
> +
> + if not exists:
> + errMsg = "File " + path + " does not exist."
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + return errMsg
> +
> +def copyfile(fromFile, toFile, isISCSI):
> + util.SMlog("Starting to copy " + fromFile + " to " + toFile)
> + errMsg = ''
> + try:
> + cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
> + txt = util.pread2(cmd)
> + except:
> + try:
> + os.system("rm -f " + toFile)
> + except:
> + txt = ''
> + txt = ''
> + errMsg = "Error while copying " + fromFile + " to " + toFile + " in secondary storage"
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + util.SMlog("Successfully copied " + fromFile + " to " + toFile)
> + return errMsg
> +
> +def chdir(path):
> + try:
> + os.chdir(path)
> + except OSError, (errno, strerror):
> + errMsg = "Unable to chdir to " + path + " because of OSError with errno: " + str(errno) + " and strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + util.SMlog("Chdired to " + path)
> + return
> +
> +def scanParent(path):
> + # Do a scan for the parent for ISCSI volumes
> + # Note that the parent need not be visible on the XenServer
> + parentUUID = ''
> + try:
> + lvName = os.path.basename(path)
> + dirname = os.path.dirname(path)
> + vgName = os.path.basename(dirname)
> + vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid, vgName)
> + parentUUID = vhdInfo.parentUuid
> + except:
> + errMsg = "Could not get vhd parent of " + path
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + return parentUUID
> +
> +def getParent(path, isISCSI):
> + parentUUID = ''
> + try :
> + if isISCSI:
> + parentUUID = vhdutil.getParent(path, lvhdutil.extractUuid)
> + else:
> + parentUUID = vhdutil.getParent(path, cleanup.FileVDI.extractUuid)
> + except:
> + errMsg = "Could not get vhd parent of " + path
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + return parentUUID
> +
> +def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
> + snapshotVHD = getVHD(snapshotUuid, isISCSI)
> + snapshotPath = os.path.join(primarySRPath, snapshotVHD)
> +
> + baseCopyUuid = ''
> + if isISCSI:
> + checkVolumeAvailablility(snapshotPath)
> + baseCopyUuid = scanParent(snapshotPath)
> + else:
> + baseCopyUuid = getParent(snapshotPath, isISCSI)
> +
> + util.SMlog("Base copy of snapshotUuid: " + snapshotUuid + " is " + baseCopyUuid)
> + return baseCopyUuid
> +
> +def setParent(parent, child):
> + try:
> + cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
> + txt = util.pread2(cmd)
> + except:
> + errMsg = "Unexpected error while trying to set parent of " + child + " to " + parent
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + util.SMlog("Successfully set parent of " + child + " to " + parent)
> + return
> +
> +def rename(originalVHD, newVHD):
> + try:
> + os.rename(originalVHD, newVHD)
> + except OSError, (errno, strerror):
> + errMsg = "OSError while renaming " + origiinalVHD + " to " + newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + return
> +
> +def makedirs(path):
> + if not os.path.isdir(path):
> + try:
> + os.makedirs(path)
> + except OSError, (errno, strerror):
> + umount(path)
> + if os.path.isdir(path):
> + return
> + errMsg = "OSError while creating " + path + " with errno: " + str(errno) + " and strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + return
> +
> +def mount(remoteDir, localDir):
> + makedirs(localDir)
> + options = "soft,tcp,timeo=133,retrans=1"
> + try:
> + cmd = ['mount', '-o', options, remoteDir, localDir]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + errMsg = "Unexpected error while trying to mount " + remoteDir + " to " + localDir
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> + util.SMlog("Successfully mounted " + remoteDir + " to " + localDir)
> +
> + return
> +
> +def umount(localDir):
> + try:
> + cmd = ['umount', localDir]
> + util.pread2(cmd)
> + except CommandException:
> + errMsg = "CommandException raised while trying to umount " + localDir
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + util.SMlog("Successfully unmounted " + localDir)
> + return
> +
> +def mountSnapshotsDir(secondaryStorageMountPath, relativeDir, dcId, accountId, instanceId):
> + # The aim is to mount secondaryStorageMountPath on
> + # And create <accountId>/<instanceId> dir on it, if it doesn't exist already.
> + # Assuming that secondaryStorageMountPath exists remotely
> +
> + # Alex's suggestion and currently implemented:
> + # Just mount secondaryStorageMountPath/<relativeDir> everytime
> + # Never unmount.
> + snapshotsDir = os.path.join(secondaryStorageMountPath, relativeDir)
> +
> + # Mkdir local mount point dir, if it doesn't exist.
> + localMountPointPath = os.path.join(CLOUD_DIR, dcId)
> + localMountPointPath = os.path.join(localMountPointPath, relativeDir)
> +
> + makedirs(localMountPointPath)
> + # if something is not mounted already on localMountPointPath,
> + # mount secondaryStorageMountPath on localMountPath
> + if os.path.ismount(localMountPointPath):
> + # There is only one secondary storage per zone.
> + # And we are mounting each sec storage under a zone-specific directory
> + # So two secondary storage snapshot dirs will never get mounted on the same point on the same XenServer.
> + util.SMlog("The remote snapshots directory has already been mounted on " + localMountPointPath)
> + else:
> + mount(snapshotsDir, localMountPointPath)
> +
> + # Create accountId/instanceId dir on localMountPointPath, if it doesn't exist
> + backupsDir = os.path.join(localMountPointPath, accountId)
> + backupsDir = os.path.join(backupsDir, instanceId)
> + makedirs(backupsDir)
> + return backupsDir
> +
> +@echo
> +def unmountSnapshotsDir(session, args):
> + dcId = args['dcId']
> + localMountPointPath = os.path.join(CLOUD_DIR, dcId)
> + localMountPointPath = os.path.join(localMountPointPath, "snapshots")
> + try:
> + umount(localMountPointPath)
> + except:
> + util.SMlog("Ignoring the error while trying to unmount the snapshots dir.")
> +
> + return "1"
> +
> +def getPrimarySRPath(session, primaryStorageSRUuid, isISCSI):
> + sr = session.xenapi.SR.get_by_uuid(primaryStorageSRUuid)
> + srrec = session.xenapi.SR.get_record(sr)
> + srtype = srrec["type"]
> + if srtype == "file":
> + pbd = session.xenapi.SR.get_PBDs(sr)[0]
> + pbdrec = session.xenapi.PBD.get_record(pbd)
> + primarySRPath = pbdrec["device_config"]["location"]
> + return primarySRPath
> + if isISCSI:
> + primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
> + return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
> + else:
> + return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
> +
> +def getBackupVHD(UUID):
> + return UUID + '.' + SR.DEFAULT_TAP
> +
> +def getVHD(UUID, isISCSI):
> + if isISCSI:
> + return VHD_PREFIX + UUID
> + else:
> + return UUID + '.' + SR.DEFAULT_TAP
> +
> +def getIsTrueString(stringValue):
> + booleanValue = False
> + if (stringValue and stringValue == 'true'):
> + booleanValue = True
> + return booleanValue
> +
> +def makeUnavailable(uuid, primarySRPath, isISCSI):
> + if not isISCSI:
> + return
> + VHD = getVHD(uuid, isISCSI)
> + path = os.path.join(primarySRPath, VHD)
> + manageAvailability(path, '-an')
> + return
> +
> +def manageAvailability(path, value):
> + if path.__contains__("/var/run/sr-mount"):
> + return
> + util.SMlog("Setting availability of " + path + " to " + value)
> + try:
> + cmd = ['/usr/sbin/lvchange', value, path]
> + util.pread2(cmd)
> + except: #CommandException, (rc, cmdListStr, stderr):
> + #errMsg = "CommandException thrown while executing: " + cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
> + errMsg = "Unexpected exception thrown by lvchange"
> + util.SMlog(errMsg)
> + if value == "-ay":
> + # Raise an error only if we are trying to make it available.
> + # Just warn if we are trying to make it unavailable after the
> + # snapshot operation is done.
> + raise xs_errors.XenError(errMsg)
> + return
> +
> +
> +def checkVolumeAvailablility(path):
> + try:
> + if not isVolumeAvailable(path):
> + # The VHD file is not available on XenSever. The volume is probably
> + # inactive or detached.
> + # Do lvchange -ay to make it available on XenServer
> + manageAvailability(path, '-ay')
> + except:
> + errMsg = "Could not determine status of ISCSI path: " + path
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + success = False
> + i = 0
> + while i < 6:
> + i = i + 1
> + # Check if the vhd is actually visible by checking for the link
> + # set isISCSI to true
> + success = isVolumeAvailable(path)
> + if success:
> + util.SMlog("Made vhd: " + path + " available and confirmed that it is visible")
> + break
> +
> + # Sleep for 10 seconds before checking again.
> + time.sleep(10)
> +
> + # If not visible within 1 min fail
> + if not success:
> + util.SMlog("Could not make vhd: " + path + " available despite waiting for 1 minute. Does it exist?")
> +
> + return success
> +
> +def isVolumeAvailable(path):
> + # Check if iscsi volume is available on this XenServer.
> + status = "0"
> + try:
> + p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path + " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
> + status = p.communicate()[0].strip("\n")
> + except:
> + errMsg = "Could not determine status of ISCSI path: " + path
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + return (status == "1")
> +
> +def getVhdParent(session, args):
> + util.SMlog("getParent with " + str(args))
> + primaryStorageSRUuid = args['primaryStorageSRUuid']
> + snapshotUuid = args['snapshotUuid']
> + isISCSI = getIsTrueString(args['isISCSI'])
> +
> + primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
> + util.SMlog("primarySRPath: " + primarySRPath)
> +
> + baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
> +
> + return baseCopyUuid
> +
> +
> +def backupSnapshot(session, args):
> + util.SMlog("Called backupSnapshot with " + str(args))
> + primaryStorageSRUuid = args['primaryStorageSRUuid']
> + dcId = args['dcId']
> + accountId = args['accountId']
> + volumeId = args['volumeId']
> + secondaryStorageMountPath = args['secondaryStorageMountPath']
> + snapshotUuid = args['snapshotUuid']
> + prevBackupUuid = args['prevBackupUuid']
> + backupUuid = args['backupUuid']
> + isISCSI = getIsTrueString(args['isISCSI'])
> +
> + primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid, isISCSI)
> + util.SMlog("primarySRPath: " + primarySRPath)
> +
> + baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI)
> + baseCopyVHD = getVHD(baseCopyUuid, isISCSI)
> + baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
> + util.SMlog("Base copy path: " + baseCopyPath)
> +
> +
> + # Mount secondary storage mount path on XenServer along the path
> + # /var/run/sr-mount/<dcId>/snapshots/ and create <accountId>/<volumeId> dir
> + # on it.
> + backupsDir = mountSnapshotsDir(secondaryStorageMountPath, "snapshots", dcId, accountId, volumeId)
> + util.SMlog("Backups dir " + backupsDir)
> +
> + # Check existence of snapshot on primary storage
> + isfile(baseCopyPath, isISCSI)
> + if prevBackupUuid:
> + # Check existence of prevBackupFile
> + prevBackupVHD = getBackupVHD(prevBackupUuid)
> + prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
> + isfile(prevBackupFile, False)
> +
> + # copy baseCopyPath to backupsDir with new uuid
> + backupVHD = getBackupVHD(backupUuid)
> + backupFile = os.path.join(backupsDir, backupVHD)
> + util.SMlog("Back up " + baseCopyUuid + " to Secondary Storage as " + backupUuid)
> + copyfile(baseCopyPath, backupFile, isISCSI)
> + vhdutil.setHidden(backupFile, False)
> +
> + # Because the primary storage is always scanned, the parent of this base copy is always the first base copy.
> + # We don't want that, we want a chain of VHDs each of which is a delta from the previous.
> + # So set the parent of the current baseCopyVHD to prevBackupVHD
> + if prevBackupUuid:
> + # If there was a previous snapshot
> + setParent(prevBackupFile, backupFile)
> +
> + txt = "1#" + backupUuid
> + return txt
> +
> +@echo
> +def deleteSnapshotBackup(session, args):
> + util.SMlog("Calling deleteSnapshotBackup with " + str(args))
> + dcId = args['dcId']
> + accountId = args['accountId']
> + volumeId = args['volumeId']
> + secondaryStorageMountPath = args['secondaryStorageMountPath']
> + backupUUID = args['backupUUID']
> +
> + backupsDir = mountSnapshotsDir(secondaryStorageMountPath, "snapshots", dcId, accountId, volumeId)
> + # chdir to the backupsDir for convenience
> + chdir(backupsDir)
> +
> + backupVHD = getBackupVHD(backupUUID)
> + util.SMlog("checking existence of " + backupVHD)
> +
> + # The backupVHD is on secondary which is NFS and not ISCSI.
> + if not os.path.isfile(backupVHD):
> + util.SMlog("backupVHD " + backupVHD + "does not exist. Not trying to delete it")
> + return "1"
> + util.SMlog("backupVHD " + backupVHD + " exists.")
> +
> + # Just delete the backupVHD
> + try:
> + os.remove(backupVHD)
> + except OSError, (errno, strerror):
> + errMsg = "OSError while removing " + backupVHD + " with errno: " + str(errno) + " and strerr: " + strerror
> + util.SMlog(errMsg)
> + raise xs_errors.XenError(errMsg)
> +
> + return "1"
> +
> +if __name__ == "__main__":
> + XenAPIPlugin.dispatch({"getVhdParent":getVhdParent, "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir})
> +
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> ----------------------------------------------------------------------
> diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium b/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> new file mode 100644
> index 0000000..ce6cdbc
> --- /dev/null
> +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> @@ -0,0 +1,129 @@
> +#!/usr/bin/python
> +# Version @VERSION@
> +#
> +# A plugin for executing script needed by vmops cloud
> +
> +import os, sys, time
> +import XenAPIPlugin
> +sys.path.append("/usr/lib/xcp/sm/")
> +import util
> +import socket
> +
> +def echo(fn):
> + def wrapped(*v, **k):
> + name = fn.__name__
> + util.SMlog("#### VMOPS enter %s ####" % name )
> + res = fn(*v, **k)
> + util.SMlog("#### VMOPS exit %s ####" % name )
> + return res
> + return wrapped
> +
> +@echo
> +def forceShutdownVM(session, args):
> + domId = args['domId']
> + try:
> + cmd = ["/usr/lib/xcp/debug/xenops", "destroy_domain", "-domid", domId]
> + txt = util.pread2(cmd)
> + except:
> + txt = '10#failed'
> + return txt
> +
> +
> +@echo
> +def create_privatetemplate_from_snapshot(session, args):
> + templatePath = args['templatePath']
> + snapshotPath = args['snapshotPath']
> + tmpltLocalDir = args['tmpltLocalDir']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir]
> + txt = util.pread2(cmd)
> + except:
> + txt = '10#failed'
> + return txt
> +
> +@echo
> +def upgrade_snapshot(session, args):
> + templatePath = args['templatePath']
> + snapshotPath = args['snapshotPath']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/upgrate_snapshot.sh",snapshotPath, templatePath]
> + txt = util.pread2(cmd)
> + except:
> + txt = '10#failed'
> + return txt
> +
> +@echo
> +def copy_vhd_to_secondarystorage(session, args):
> + mountpoint = args['mountpoint']
> + vdiuuid = args['vdiuuid']
> + sruuid = args['sruuid']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid, sruuid]
> + txt = util.pread2(cmd)
> + except:
> + txt = '10#failed'
> + return txt
> +
> +@echo
> +def copy_vhd_from_secondarystorage(session, args):
> + mountpoint = args['mountpoint']
> + sruuid = args['sruuid']
> + namelabel = args['namelabel']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/copy_vhd_from_secondarystorage.sh", mountpoint, sruuid, namelabel]
> + txt = util.pread2(cmd)
> + except:
> + txt = '10#failed'
> + return txt
> +
> +@echo
> +def setup_heartbeat_sr(session, args):
> + host = args['host']
> + sr = args['sr']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_sr.sh", host, sr]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + return txt
> +
> +@echo
> +def setup_heartbeat_file(session, args):
> + host = args['host']
> + sr = args['sr']
> + add = args['add']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_file.sh", host, sr, add]
> + txt = util.pread2(cmd)
> + except:
> + txt = ''
> + return txt
> +
> +@echo
> +def check_heartbeat(session, args):
> + host = args['host']
> + interval = args['interval']
> + try:
> + cmd = ["bash", "/usr/lib/xcp/bin/check_heartbeat.sh", host, interval]
> + txt = util.pread2(cmd)
> + except:
> + txt=''
> + return txt
> +
> +
> +@echo
> +def heartbeat(session, args):
> + '''
> + host = args['host']
> + interval = args['interval']
> + try:
> + cmd = ["/bin/bash", "/usr/lib/xcp/bin/launch_hb.sh", host, interval]
> + txt = util.pread2(cmd)
> + except:
> + txt='fail'
> + '''
> + return '> DONE <'
> +
> +if __name__ == "__main__":
> + XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM, "upgrade_snapshot":upgrade_snapshot, "create_privatetemplate_from_snapshot":create_privatetemplate_from_snapshot, "copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage, "copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage, "setup_heartbeat_sr":setup_heartbeat_sr, "setup_heartbeat_file":setup_heartbeat_file, "check_heartbeat":check_heartbeat, "heartbeat": heartbeat})
> +
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> ----------------------------------------------------------------------
> diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> index b6c1057..f15e45a 100755
> --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> @@ -1508,7 +1508,9 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx
> throw new ConfigurationException(msg);
> }
> } else {
> - _serviceOffering = new ServiceOfferingVO("System Offering For Console Proxy", 1, DEFAULT_PROXY_VM_RAMSIZE, DEFAULT_PROXY_VM_CPUMHZ, 0, 0, false, null, useLocalStorage, true, null, true, VirtualMachine.Type.ConsoleProxy, true);
> + int ramSize = NumbersUtil.parseInt(_configDao.getValue("console.ram.size"), DEFAULT_PROXY_VM_RAMSIZE);
> + int cpuFreq = NumbersUtil.parseInt(_configDao.getValue("console.cpu.mhz"), DEFAULT_PROXY_VM_CPUMHZ);
> + _serviceOffering = new ServiceOfferingVO("System Offering For Console Proxy", 1, ramSize, cpuFreq, 0, 0, false, null, useLocalStorage, true, null, true, VirtualMachine.Type.ConsoleProxy, true);
> _serviceOffering.setUniqueName(ServiceOffering.consoleProxyDefaultOffUniqueName);
> _serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering);
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
> ----------------------------------------------------------------------
> diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
> index d044d4f..fc8c0f7 100755
> --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
> +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
> @@ -603,8 +603,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
> final Map<String, String> configs = _configDao.getConfiguration("AgentManager", params);
>
> _mgmt_host = configs.get("host");
> - _routerRamSize = NumbersUtil.parseInt(configs.get("router.ram.size"), DEFAULT_ROUTER_VM_RAMSIZE);
> - _routerCpuMHz = NumbersUtil.parseInt(configs.get("router.cpu.mhz"), DEFAULT_ROUTER_CPU_MHZ);
> + _routerRamSize = NumbersUtil.parseInt(_configDao.getValue("router.ram.size"), DEFAULT_ROUTER_VM_RAMSIZE);
> + _routerCpuMHz = NumbersUtil.parseInt(_configDao.getValue("router.cpu.mhz"), DEFAULT_ROUTER_CPU_MHZ);
>
> _routerExtraPublicNics = NumbersUtil.parseInt(_configDao.getValue(Config.RouterExtraPublicNics.key()), 2);
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
> ----------------------------------------------------------------------
> diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
> index 29a9782..0573b65 100755
> --- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
> +++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
> @@ -841,8 +841,10 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
> throw new ConfigurationException(msg);
> }
> } else {
> + int ramSize = NumbersUtil.parseInt(_configDao.getValue("ssvm.ram.size"), DEFAULT_SS_VM_RAMSIZE);
> + int cpuFreq = NumbersUtil.parseInt(_configDao.getValue("ssvm.cpu.mhz"), DEFAULT_SS_VM_CPUMHZ);
> _useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));
> - _serviceOffering = new ServiceOfferingVO("System Offering For Secondary Storage VM", 1, DEFAULT_SS_VM_RAMSIZE, DEFAULT_SS_VM_CPUMHZ, null, null, false, null, _useLocalStorage, true, null, true, VirtualMachine.Type.SecondaryStorageVm, true);
> + _serviceOffering = new ServiceOfferingVO("System Offering For Secondary Storage VM", 1, ramSize, cpuFreq, null, null, false, null, _useLocalStorage, true, null, true, VirtualMachine.Type.SecondaryStorageVm, true);
> _serviceOffering.setUniqueName(ServiceOffering.ssvmDefaultOffUniqueName);
> _serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering);
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/server/src/com/cloud/test/DatabaseConfig.java
> ----------------------------------------------------------------------
> diff --git a/server/src/com/cloud/test/DatabaseConfig.java b/server/src/com/cloud/test/DatabaseConfig.java
> index 487e2c5..9ab2868 100755
> --- a/server/src/com/cloud/test/DatabaseConfig.java
> +++ b/server/src/com/cloud/test/DatabaseConfig.java
> @@ -330,6 +330,8 @@ public class DatabaseConfig {
> s_defaultConfigurationValues.put("system.vm.use.local.storage", "false");
> s_defaultConfigurationValues.put("use.local.storage", "false");
> s_defaultConfigurationValues.put("init", "false");
> + s_defaultConfigurationValues.put("cpu.overprovisioning.factor", "1");
> + s_defaultConfigurationValues.put("mem.overprovisioning.factor", "1");
> }
>
> protected DatabaseConfig() {
>
> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5f6387e1/setup/db/clouddev.sql
> ----------------------------------------------------------------------
> diff --git a/setup/db/clouddev.sql b/setup/db/clouddev.sql
> new file mode 100644
> index 0000000..10ad3b2
> --- /dev/null
> +++ b/setup/db/clouddev.sql
> @@ -0,0 +1,33 @@
> +-- Licensed to the Apache Software Foundation (ASF) under one
> +-- or more contributor license agreements. See the NOTICE file
> +-- distributed with this work for additional information
> +-- regarding copyright ownership. The ASF licenses this file
> +-- to you under the Apache License, Version 2.0 (the
> +-- "License"); you may not use this file except in compliance
> +-- with the License. You may obtain a copy of the License at
> +--
> +-- http://www.apache.org/licenses/LICENSE-2.0
> +--
> +-- Unless required by applicable law or agreed to in writing,
> +-- software distributed under the License is distributed on an
> +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> +-- KIND, either express or implied. See the License for the
> +-- specific language governing permissions and limitations
> +-- under the License.
> +
> +
> +UPDATE `cloud`.`configuration` SET value = 'true' where name = 'use.local.storage';
> +UPDATE `cloud`.`configuration` SET value = 'true' where name = 'system.vm.use.local.storage';
> +INSERT INTO `cloud`.`disk_offering` (id, name, uuid, display_text, created, use_local_storage, type) VALUES (17, 'tinyOffering', UUID(), 'tinyOffering', NOW(), 1, 'Service');
> +INSERT INTO `cloud`.`service_offering` (id, cpu, speed, ram_size) VALUES (17, 1, 100, 100);
> +INSERT INTO `cloud`.`disk_offering` (id, name, uuid, display_text, created, type, disk_size) VALUES (18, 'tinyDiskOffering', UUID(), 'tinyDiskOffering', NOW(), 'Disk', 1073741824);
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('router.ram.size', '100');
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('router.cpu.mhz','100');
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('console.ram.size','100');
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('console.cpu.mhz', '100');
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('ssvm.ram.size','100');
> +INSERT INTO `cloud`.`configuration` (name,value) VALUE('ssvm.cpu.mhz','100');
> +UPDATE `cloud`.`configuration` SET value='10' where name = 'storage.overprovisioning.factor';
> +UPDATE `cloud`.`configuration` SET value='10' where name = 'cpu.overprovisioning.factor';
> +UPDATE `cloud`.`configuration` SET value='10' where name = 'mem.overprovisioning.factor';
> +UPDATE `cloud`.`vm_template` SET unique_name="tiny Linux",name="tiny Linux",url="http://nfs1.lab.vmops.com/templates/ttylinux_pv.vhd",checksum="046e134e642e6d344b34648223ba4bc1",display_text="tiny Linux" where id=5;
>
RE: [2/3] add clouddev
Posted by Edison Su <Ed...@citrix.com>.
Fixed.
These files are copied from 3.0.x branch, that's why the licenses are missing...
> -----Original Message-----
> From: David Nalley [mailto:david@gnsa.us]
> Sent: Wednesday, June 27, 2012 11:54 PM
> To: cloudstack-dev@incubator.apache.org
> Subject: Re: [2/3] add clouddev
>
> Edison,
>
> When you instantiate a new file can you be sure to include the license
> header
>
> http://www.apache.org/legal/src-headers.html
>
> --David
>
> On Thu, Jun 28, 2012 at 2:29 AM, <ed...@apache.org> wrote:
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmops
> > ---------------------------------------------------------------------
> -
> > diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmops
> b/scripts/vm/hypervisor/xenserver/xcposs/vmops
> > new file mode 100644
> > index 0000000..e4a2fdc
> > --- /dev/null
> > +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmops
> > @@ -0,0 +1,1519 @@
> > +#!/usr/bin/python
> > +# Version 2.2.8.2012-02-28T23:50:03Z
> > +#
> > +# A plugin for executing script needed by vmops cloud
> > +
> > +import os, sys, time
> > +import XenAPIPlugin
> > +sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
> > +import base64
> > +import hostvmstats
> > +import socket
> > +import stat
> > +import tempfile
> > +import util
> > +import subprocess
> > +import zlib
> > +from util import CommandException
> > +
> > +def echo(fn):
> > + def wrapped(*v, **k):
> > + name = fn.__name__
> > + util.SMlog("#### VMOPS enter %s ####" % name )
> > + res = fn(*v, **k)
> > + util.SMlog("#### VMOPS exit %s ####" % name )
> > + return res
> > + return wrapped
> > +
> > +@echo
> > +def gethostvmstats(session, args):
> > + collect_host_stats = args['collectHostStats']
> > + consolidation_function = args['consolidationFunction']
> > + interval = args['interval']
> > + start_time = args['startTime']
> > + result = hostvmstats.get_stats(session, collect_host_stats,
> consolidation_function, interval, start_time)
> > + return result
> > +
> > +@echo
> > +def setup_iscsi(session, args):
> > + uuid=args['uuid']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/setup_iscsi.sh", uuid]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + return '> DONE <'
> > +
> > +@echo
> > +def getvncport(session, args):
> > + domid = args['domID']
> > + hvm = args['hvm']
> > + version = args['version']
> > + if hvm == 'true':
> > + path1 = "/local/domain/" + domid + "/qemu-pid"
> > + path2 = "/local/domain/" + domid + "/console/vnc-port"
> > + else:
> > + if version[:3] == '6.0':
> > + path1 = "/local/domain/" + domid + "/vncterm-pid"
> > + path2 = "/local/domain/" + domid + "/console/vnc-port"
> > + else:
> > + path1 = "/local/domain/" + domid + "/serial/0/vncterm-
> pid"
> > + path2 = "/local/domain/" + domid + "/serial/0/vnc-port"
> > + try:
> > + cmd = ["xenstore-read", path1]
> > + pid = util.pread2(cmd)
> > + pid = pid.strip()
> > + cmd = ["ps", pid]
> > + util.pread2(cmd)
> > + cmd = ["xenstore-read", path2]
> > + vncport = util.pread2(cmd)
> > + vncport = vncport.strip()
> > + return vncport
> > + except:
> > + return ''
> > +
> > +@echo
> > +def getgateway(session, args):
> > + mgmt_ip = args['mgmtIP']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/network_info.sh", "-g",
> mgmt_ip]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def preparemigration(session, args):
> > + uuid = args['uuid']
> > + try:
> > + cmd = ["/usr/lib/xcp/bin/make_migratable.sh", uuid]
> > + util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog("Catch prepare migration exception" )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def setIptables(session, args):
> > + try:
> > + '''cmd = ["/bin/bash", "/usr/lib/xcp/bin/setupxenserver.sh"]
> > + txt = util.pread2(cmd)'''
> > + txt = 'success'
> > + except:
> > + util.SMlog(" setIptables execution failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def pingdomr(session, args):
> > + host = args['host']
> > + port = args['port']
> > + socket.setdefaulttimeout(3)
> > + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > + try:
> > + s.connect((host,int(port)))
> > + txt = 'success'
> > + except:
> > + txt = ''
> > +
> > + s.close()
> > +
> > + return txt
> > +
> > +@echo
> > +def kill_copy_process(session, args):
> > + namelabel = args['namelabel']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/kill_copy_process.sh",
> namelabel]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = 'false'
> > + return txt
> > +
> > +@echo
> > +def pingxenserver(session, args):
> > + txt = 'success'
> > + return txt
> > +
> > +@echo
> > +def ipassoc(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/ipassoc.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" ip associate failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def vm_data(session, args):
> > + router_ip = args.pop('routerIP')
> > + vm_ip = args.pop('vmIP')
> > +
> > + util.SMlog(" adding vmdata for VM with IP: " + vm_ip + " to
> router with IP: " + router_ip)
> > +
> > + for pair in args:
> > + pairList = pair.split(',')
> > + vmDataFolder = pairList[0]
> > + vmDataFile = pairList[1]
> > + vmDataValue = args[pair]
> > + cmd = ["/bin/bash", "/usr/lib/xcp/bin/vm_data.sh", "-r",
> router_ip, "-v", vm_ip, "-F", vmDataFolder, "-f", vmDataFile]
> > +
> > + fd = None
> > + tmp_path = None
> > +
> > + try:
> > + fd,tmp_path = tempfile.mkstemp()
> > + tmpfile = open(tmp_path, 'w')
> > +
> > + if vmDataFolder == "userdata" and vmDataValue != "none":
> > + vmDataValue = base64.urlsafe_b64decode(vmDataValue)
> > +
> > + if vmDataValue != "none":
> > + tmpfile.write(vmDataValue)
> > +
> > + tmpfile.close()
> > + cmd.append("-d")
> > + cmd.append(tmp_path)
> > + except:
> > + util.SMlog(" vmdata failed to write tempfile " )
> > + os.close(fd)
> > + os.remove(tmp_path)
> > + return ''
> > +
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" vmdata failed with folder: " +
> vmDataFolder + " and file: " + vmDataFile)
> > + txt = ''
> > +
> > + if (fd != None):
> > + os.close(fd)
> > + os.remove(tmp_path)
> > +
> > + return txt
> > +
> > +def pingtest(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/pingtest.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" pingtest failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def savePassword(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/save_password_to_domr.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" save password to domr failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def saveDhcpEntry(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/dhcp_entry.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" save dhcp entry failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def lt2p_vpn(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/l2tp_vpn.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog("l2tp vpn failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def setLinkLocalIP(session, args):
> > + brName = args['brName']
> > + try:
> > + cmd = ["ip", "route", "del", "169.254.0.0/16"]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + try:
> > + cmd = ["ifconfig", brName, "169.254.0.1", "netmask",
> "255.255.0.0"]
> > + txt = util.pread2(cmd)
> > + except:
> > +
> > + try:
> > + cmd = ["brctl", "addbr", brName]
> > + txt = util.pread2(cmd)
> > + except:
> > + pass
> > +
> > + try:
> > + cmd = ["ifconfig", brName, "169.254.0.1", "netmask",
> "255.255.0.0"]
> > + txt = util.pread2(cmd)
> > + except:
> > + pass
> > + try:
> > + cmd = ["ip", "route", "add", "169.254.0.0/16", "dev", brName,
> "src", "169.254.0.1"]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + txt = 'success'
> > + return txt
> > +
> > +@echo
> > +def setFirewallRule(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/call_firewall.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" set firewall rule failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def setLoadBalancerRule(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/call_loadbalancer.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" set loadbalancer rule failed " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def createFile(session, args):
> > + file_path = args['filepath']
> > + file_contents = args['filecontents']
> > +
> > + try:
> > + f = open(file_path, "w")
> > + f.write(file_contents)
> > + f.close()
> > + txt = 'success'
> > + except:
> > + util.SMlog(" failed to create HA proxy cfg file ")
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def deleteFile(session, args):
> > + file_path = args["filepath"]
> > +
> > + try:
> > + if os.path.isfile(file_path):
> > + os.remove(file_path)
> > + txt = 'success'
> > + except:
> > + util.SMlog(" failed to remove HA proxy cfg file ")
> > + txt = ''
> > +
> > + return txt
> > +
> > +
> > +@echo
> > +def networkUsage(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/networkUsage.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + except:
> > + util.SMlog(" network usage error " )
> > + txt = ''
> > +
> > + return txt
> > +
> > +def get_private_nic(session, args):
> > + vms = session.xenapi.VM.get_all()
> > + host_uuid = args.get('host_uuid')
> > + host = session.xenapi.host.get_by_uuid(host_uuid)
> > + piflist = session.xenapi.host.get_PIFs(host)
> > + mgmtnic = 'eth0'
> > + for pif in piflist:
> > + pifrec = session.xenapi.PIF.get_record(pif)
> > + network = pifrec.get('network')
> > + nwrec = session.xenapi.network.get_record(network)
> > + if nwrec.get('name_label') == 'cloud-guest':
> > + return pifrec.get('device')
> > + if pifrec.get('management'):
> > + mgmtnic = pifrec.get('device')
> > +
> > + return mgmtnic
> > +
> > +def chain_name(vm_name):
> > + if vm_name.startswith('i-') or vm_name.startswith('r-'):
> > + if vm_name.endswith('untagged'):
> > + return '-'.join(vm_name.split('-')[:-1])
> > + return vm_name
> > +
> > +def chain_name_def(vm_name):
> > + if vm_name.startswith('i-'):
> > + if vm_name.endswith('untagged'):
> > + return '-'.join(vm_name.split('-')[:-2]) + "-def"
> > + return '-'.join(vm_name.split('-')[:-1]) + "-def"
> > + return vm_name
> > +
> > +def egress_chain_name(vm_name):
> > + return chain_name(vm_name) + "-eg"
> > +
> > +@echo
> > +def can_bridge_firewall(session, args):
> > + try:
> > + util.pread2(['ebtables', '-V'])
> > + util.pread2(['ipset', '-V'])
> > + except:
> > + return 'false'
> > +
> > + host_uuid = args.get('host_uuid')
> > + try:
> > + util.pread2(['iptables', '-N', 'BRIDGE-FIREWALL'])
> > + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '-m',
> 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
> > + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m',
> 'physdev', '--physdev-is-bridged', '-p', 'udp', '--dport', '67', '--
> sport', '68', '-j', 'ACCEPT'])
> > + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m',
> 'physdev', '--physdev-is-bridged', '-p', 'udp', '--dport', '68', '--
> sport', '67', '-j', 'ACCEPT'])
> > + util.pread2(['iptables', '-D', 'FORWARD', '-j', 'RH-
> Firewall-1-INPUT'])
> > + except:
> > + util.SMlog('Chain BRIDGE-FIREWALL already exists')
> > + privnic = get_private_nic(session, args)
> > + result = 'true'
> > + try:
> > + util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD |
> grep BRIDGE-FIREWALL'])
> > + except:
> > + try:
> > + util.pread2(['iptables', '-I', 'FORWARD', '-m',
> 'physdev', '--physdev-is-bridged', '-j', 'BRIDGE-FIREWALL'])
> > + util.pread2(['iptables', '-A', 'FORWARD', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', privnic, '-j',
> 'ACCEPT'])
> > + util.pread2(['iptables', '-A', 'FORWARD', '-j', 'DROP'])
> > + except:
> > + return 'false'
> > + default_ebtables_rules()
> > + allow_egress_traffic(session)
> > + if not os.path.exists('/var/run/cloud'):
> > + os.makedirs('/var/run/cloud')
> > + if not os.path.exists('/var/cache/cloud'):
> > + os.makedirs('/var/cache/cloud')
> > + #get_ipset_keyword()
> > +
> > + cleanup_rules_for_dead_vms(session)
> > + cleanup_rules(session, args)
> > +
> > + return result
> > +
> > +@echo
> > +def default_ebtables_rules():
> > + try:
> > + util.pread2(['ebtables', '-N', 'DEFAULT_EBTABLES'])
> > + util.pread2(['ebtables', '-A', 'FORWARD', '-
> j' 'DEFAULT_EBTABLES'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '--ip-dst', '255.255.255.255', '--ip-proto', 'udp', '--ip-
> dport', '67', '-j', 'ACCEPT'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'ARP', '--arp-op', 'Request', '-j', 'ACCEPT'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'ARP', '--arp-op', 'Reply', '-j', 'ACCEPT'])
> > + # deny mac broadcast and multicast
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '-d', 'Broadcast', '-j', 'DROP'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '-d', 'Multicast', '-j', 'DROP'])
> > + # deny ip broadcast and multicast
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'DROP'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '--ip-dst', '224.0.0.0/4', '-j', 'DROP'])
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv4', '-j', 'RETURN'])
> > + # deny ipv6
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> 'IPv6', '-j', 'DROP'])
> > + # deny vlan
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-p',
> '802_1Q', '-j', 'DROP'])
> > + # deny all others (e.g., 802.1d, CDP)
> > + util.pread2(['ebtables', '-A', 'DEFAULT_EBTABLES', '-j',
> 'DROP'])
> > + except:
> > + util.SMlog('Chain DEFAULT_EBTABLES already exists')
> > +
> > +
> > +@echo
> > +def allow_egress_traffic(session):
> > + devs = []
> > + for pif in session.xenapi.PIF.get_all():
> > + pif_rec = session.xenapi.PIF.get_record(pif)
> > + vlan = pif_rec.get('VLAN')
> > + dev = pif_rec.get('device')
> > + if vlan == '-1':
> > + devs.append(dev)
> > + else:
> > + devs.append(dev + "." + vlan)
> > + for d in devs:
> > + try:
> > + util.pread2(['/bin/bash', '-c', "iptables -n -L FORWARD
> | grep '%s '" % d])
> > + except:
> > + try:
> > + util.pread2(['iptables', '-I', 'FORWARD', '2', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', d, '-j', 'ACCEPT'])
> > + except:
> > + util.SMlog("Failed to add FORWARD rule through to
> %s" % d)
> > + return 'false'
> > + return 'true'
> > +
> > +
> > +def ipset(ipsetname, proto, start, end, ips):
> > + try:
> > + util.pread2(['ipset', '-N', ipsetname, 'iptreemap'])
> > + except:
> > + util.SMlog("ipset chain already exists" + ipsetname)
> > +
> > + result = True
> > + ipsettmp = ''.join(''.join(ipsetname.split('-')).split('_')) +
> str(int(time.time()) % 1000)
> > +
> > + try:
> > + util.pread2(['ipset', '-N', ipsettmp, 'iptreemap'])
> > + except:
> > + util.SMlog("Failed to create temp ipset, reusing old name= "
> + ipsettmp)
> > + try:
> > + util.pread2(['ipset', '-F', ipsettmp])
> > + except:
> > + util.SMlog("Failed to clear old temp ipset name=" +
> ipsettmp)
> > + return False
> > +
> > + try:
> > + for ip in ips:
> > + try:
> > + util.pread2(['ipset', '-A', ipsettmp, ip])
> > + except CommandException, cex:
> > + if cex.reason.rfind('already in set') == -1:
> > + raise
> > + except:
> > + util.SMlog("Failed to program ipset " + ipsetname)
> > + util.pread2(['ipset', '-F', ipsettmp])
> > + util.pread2(['ipset', '-X', ipsettmp])
> > + return False
> > +
> > + try:
> > + util.pread2(['ipset', '-W', ipsettmp, ipsetname])
> > + except:
> > + util.SMlog("Failed to swap ipset " + ipsetname)
> > + result = False
> > +
> > + try:
> > + util.pread2(['ipset', '-F', ipsettmp])
> > + util.pread2(['ipset', '-X', ipsettmp])
> > + except:
> > + # if the temporary name clashes next time we'll just reuse
> it
> > + util.SMlog("Failed to delete temp ipset " + ipsettmp)
> > +
> > + return result
> > +
> > +@echo
> > +def destroy_network_rules_for_vm(session, args):
> > + vm_name = args.pop('vmName')
> > + vmchain = chain_name(vm_name)
> > + vmchain_egress = egress_chain_name(vm_name)
> > + vmchain_default = chain_name_def(vm_name)
> > +
> > + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> > + if vm_name.startswith('i-') or vm_name.startswith('r-') or
> vm_name.startswith('l-'):
> > + try:
> > + util.pread2(['iptables', '-F', vmchain_default])
> > + util.pread2(['iptables', '-X', vmchain_default])
> > + except:
> > + util.SMlog("Ignoring failure to delete chain " +
> vmchain_default)
> > +
> > + destroy_ebtables_rules(vmchain)
> > +
> > + try:
> > + util.pread2(['iptables', '-F', vmchain])
> > + util.pread2(['iptables', '-X', vmchain])
> > + except:
> > + util.SMlog("Ignoring failure to delete ingress chain " +
> vmchain)
> > +
> > +
> > + try:
> > + util.pread2(['iptables', '-F', vmchain_egress])
> > + util.pread2(['iptables', '-X', vmchain_egress])
> > + except:
> > + util.SMlog("Ignoring failure to delete egress chain " +
> vmchain_egress)
> > +
> > + remove_rule_log_for_vm(vm_name)
> > +
> > + if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-
> '] ]:
> > + return 'true'
> > +
> > + try:
> > + setscmd = "ipset --save | grep " + vmchain + " | grep '^-N'
> | awk '{print $2}'"
> > + setsforvm = util.pread2(['/bin/bash', '-c',
> setscmd]).split('\n')
> > + for set in setsforvm:
> > + if set != '':
> > + util.pread2(['ipset', '-F', set])
> > + util.pread2(['ipset', '-X', set])
> > + except:
> > + util.SMlog("Failed to destroy ipsets for %" % vm_name)
> > +
> > +
> > + return 'true'
> > +
> > +@echo
> > +def destroy_ebtables_rules(vm_chain):
> > +
> > + delcmd = "ebtables-save | grep " + vm_chain + " | sed 's/-A/-
> D/'"
> > + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> > + delcmds.pop()
> > + for cmd in delcmds:
> > + try:
> > + dc = cmd.split(' ')
> > + dc.insert(0, 'ebtables')
> > + util.pread2(dc)
> > + except:
> > + util.SMlog("Ignoring failure to delete ebtables rules
> for vm " + vm_chain)
> > + try:
> > + util.pread2(['ebtables', '-F', vm_chain])
> > + util.pread2(['ebtables', '-X', vm_chain])
> > + except:
> > + util.SMlog("Ignoring failure to delete ebtables chain
> for vm " + vm_chain)
> > +
> > +@echo
> > +def destroy_arptables_rules(vm_chain):
> > + delcmd = "arptables -vL FORWARD | grep " + vm_chain + " | sed
> 's/-i any//' | sed 's/-o any//' | awk '{print $1,$2,$3,$4}' "
> > + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> > + delcmds.pop()
> > + for cmd in delcmds:
> > + try:
> > + dc = cmd.split(' ')
> > + dc.insert(0, 'arptables')
> > + dc.insert(1, '-D')
> > + dc.insert(2, 'FORWARD')
> > + util.pread2(dc)
> > + except:
> > + util.SMlog("Ignoring failure to delete arptables rules
> for vm " + vm_chain)
> > +
> > + try:
> > + util.pread2(['arptables', '-F', vm_chain])
> > + util.pread2(['arptables', '-X', vm_chain])
> > + except:
> > + util.SMlog("Ignoring failure to delete arptables chain for
> vm " + vm_chain)
> > +
> > +@echo
> > +def default_ebtables_antispoof_rules(vm_chain, vifs, vm_ip, vm_mac):
> > + if vm_mac == 'ff:ff:ff:ff:ff:ff':
> > + util.SMlog("Ignoring since mac address is not valid")
> > + return 'true'
> > +
> > + try:
> > + util.pread2(['ebtables', '-N', vm_chain])
> > + except:
> > + try:
> > + util.pread2(['ebtables', '-F', vm_chain])
> > + except:
> > + util.SMlog("Failed to create ebtables antispoof chain,
> skipping")
> > + return 'true'
> > +
> > + # note all rules for packets into the bridge (-i) precede all
> output rules (-o)
> > + # always start after the first rule in the FORWARD chain that
> jumps to DEFAULT_EBTABLES chain
> > + try:
> > + for vif in vifs:
> > + util.pread2(['ebtables', '-I', 'FORWARD', '2', '-
> i', vif, '-j', vm_chain])
> > + util.pread2(['ebtables', '-A', 'FORWARD', '-o', vif, '-
> j', vm_chain])
> > + except:
> > + util.SMlog("Failed to program default ebtables FORWARD rules
> for %s" % vm_chain)
> > + return 'false'
> > +
> > + try:
> > + for vif in vifs:
> > + # only allow source mac that belongs to the vm
> > + util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-s',
> '!', vm_mac, '-j', 'DROP'])
> > + # do not allow fake dhcp responses
> > + util.pread2(['ebtables', '-A', vm_chain, '-i', vif, '-p',
> 'IPv4', '--ip-proto', 'udp', '--ip-dport', '68', '-j', 'DROP'])
> > + # do not allow snooping of dhcp requests
> > + util.pread2(['ebtables', '-A', vm_chain, '-o', vif, '-p',
> 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67', '-j', 'DROP'])
> > + except:
> > + util.SMlog("Failed to program default ebtables antispoof
> rules for %s" % vm_chain)
> > + return 'false'
> > +
> > + return 'true'
> > +
> > +@echo
> > +def default_arp_antispoof(vm_chain, vifs, vm_ip, vm_mac):
> > + if vm_mac == 'ff:ff:ff:ff:ff:ff':
> > + util.SMlog("Ignoring since mac address is not valid")
> > + return 'true'
> > +
> > + try:
> > + util.pread2(['arptables', '-N', vm_chain])
> > + except:
> > + try:
> > + util.pread2(['arptables', '-F', vm_chain])
> > + except:
> > + util.SMlog("Failed to create arptables rule, skipping")
> > + return 'true'
> > +
> > + # note all rules for packets into the bridge (-i) precede all
> output rules (-o)
> > + try:
> > + for vif in vifs:
> > + util.pread2(['arptables', '-I', 'FORWARD', '-i', vif,
> '-j', vm_chain])
> > + util.pread2(['arptables', '-A', 'FORWARD', '-o', vif,
> '-j', vm_chain])
> > + except:
> > + util.SMlog("Failed to program default arptables rules in
> FORWARD chain vm=" + vm_chain)
> > + return 'false'
> > +
> > + try:
> > + for vif in vifs:
> > + #accept arp replies into the bridge as long as the
> source mac and ips match the vm
> > + util.pread2(['arptables', '-A', vm_chain, '-i', vif, '-
> -opcode', 'Reply', '--source-mac', vm_mac, '--source-ip', vm_ip, '-j',
> 'ACCEPT'])
> > + #accept any arp requests from this vm. In the future
> this can be restricted to deny attacks on hosts
> > + #also important to restrict source ip and src mac in
> these requests as they can be used to update arp tables on destination
> > + util.pread2(['arptables', '-A', vm_chain, '-i', vif, '-
> -opcode', 'Request', '--source-mac', vm_mac, '--source-ip', vm_ip,
> '-j', 'RETURN'])
> > + #accept any arp requests to this vm as long as the
> request is for this vm's ip
> > + util.pread2(['arptables', '-A', vm_chain, '-o', vif, '-
> -opcode', 'Request', '--destination-ip', vm_ip, '-j', 'ACCEPT'])
> > + #accept any arp replies to this vm as long as the mac
> and ip matches
> > + util.pread2(['arptables', '-A', vm_chain, '-o', vif, '-
> -opcode', 'Reply', '--destination-mac', vm_mac, '--destination-ip',
> vm_ip, '-j', 'ACCEPT'])
> > + util.pread2(['arptables', '-A', vm_chain, '-j', 'DROP'])
> > +
> > + except:
> > + util.SMlog("Failed to program default arptables rules")
> > + return 'false'
> > +
> > + return 'true'
> > +
> > +@echo
> > +def default_network_rules_systemvm(session, args):
> > + vm_name = args.pop('vmName')
> > + try:
> > + vm = session.xenapi.VM.get_by_name_label(vm_name)
> > + if len(vm) != 1:
> > + return 'false'
> > + vm_rec = session.xenapi.VM.get_record(vm[0])
> > + vm_vifs = vm_rec.get('VIFs')
> > + vifnums = [session.xenapi.VIF.get_record(vif).get('device')
> for vif in vm_vifs]
> > + domid = vm_rec.get('domid')
> > + except:
> > + util.SMlog("### Failed to get domid or vif list for vm ##"
> + vm_name)
> > + return 'false'
> > +
> > + if domid == '-1':
> > + util.SMlog("### Failed to get domid for vm (-1): " +
> vm_name)
> > + return 'false'
> > +
> > + vifs = ["vif" + domid + "." + v for v in vifnums]
> > + #vm_name = '-'.join(vm_name.split('-')[:-1])
> > + vmchain = chain_name(vm_name)
> > +
> > +
> > + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> > +
> > + try:
> > + util.pread2(['iptables', '-N', vmchain])
> > + except:
> > + util.pread2(['iptables', '-F', vmchain])
> > +
> > + allow_egress_traffic(session)
> > +
> > + for vif in vifs:
> > + try:
> > + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', vif, '-j', vmchain])
> > + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '4',
> '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', vif, '-j',
> vmchain])
> > + util.pread2(['iptables', '-I', vmchain, '-m', 'physdev',
> '--physdev-is-bridged', '--physdev-in', vif, '-j', 'RETURN'])
> > + except:
> > + util.SMlog("Failed to program default rules")
> > + return 'false'
> > +
> > +
> > + util.pread2(['iptables', '-A', vmchain, '-j', 'ACCEPT'])
> > +
> > + if write_rule_log_for_vm(vm_name, '-1', '_ignore_', domid,
> '_initial_', '-1') == False:
> > + util.SMlog("Failed to log default network rules for systemvm,
> ignoring")
> > + return 'true'
> > +
> > +
> > +@echo
> > +def default_network_rules(session, args):
> > + vm_name = args.pop('vmName')
> > + vm_ip = args.pop('vmIP')
> > + vm_id = args.pop('vmID')
> > + vm_mac = args.pop('vmMAC')
> > +
> > + try:
> > + vm = session.xenapi.VM.get_by_name_label(vm_name)
> > + if len(vm) != 1:
> > + util.SMlog("### Failed to get record for vm " +
> vm_name)
> > + return 'false'
> > + vm_rec = session.xenapi.VM.get_record(vm[0])
> > + domid = vm_rec.get('domid')
> > + except:
> > + util.SMlog("### Failed to get domid for vm " + vm_name)
> > + return 'false'
> > + if domid == '-1':
> > + util.SMlog("### Failed to get domid for vm (-1): " +
> vm_name)
> > + return 'false'
> > +
> > + vif = "vif" + domid + ".0"
> > + tap = "tap" + domid + ".0"
> > + vifs = [vif]
> > + try:
> > + util.pread2(['ifconfig', tap])
> > + vifs.append(tap)
> > + except:
> > + pass
> > +
> > + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> > +
> > +
> > + vmchain = chain_name(vm_name)
> > + vmchain_egress = egress_chain_name(vm_name)
> > + vmchain_default = chain_name_def(vm_name)
> > +
> > + destroy_ebtables_rules(vmchain)
> > +
> > +
> > + try:
> > + util.pread2(['iptables', '-N', vmchain])
> > + except:
> > + util.pread2(['iptables', '-F', vmchain])
> > +
> > + try:
> > + util.pread2(['iptables', '-N', vmchain_egress])
> > + except:
> > + util.pread2(['iptables', '-F', vmchain_egress])
> > +
> > + try:
> > + util.pread2(['iptables', '-N', vmchain_default])
> > + except:
> > + util.pread2(['iptables', '-F', vmchain_default])
> > +
> > + try:
> > + for v in vifs:
> > + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j',
> vmchain_default])
> > + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2',
> '-m', 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j',
> vmchain_default])
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'])
> > + #allow dhcp
> > + for v in vifs:
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-p', 'udp', '--
> dport', '67', '--sport', '68', '-j', 'ACCEPT'])
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-p', 'udp', '--
> dport', '68', '--sport', '67', '-j', 'ACCEPT'])
> > +
> > + #don't let vm spoof its ip address
> > + for v in vifs:
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source',
> vm_ip,'-p', 'udp', '--dport', '53', '-j', 'RETURN'])
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', '!',
> vm_ip, '-j', 'DROP'])
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', v, '--destination',
> '!', vm_ip, '-j', 'DROP'])
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-in', v, '--source', vm_ip,
> '-j', vmchain_egress])
> > +
> > + for v in vifs:
> > + util.pread2(['iptables', '-A', vmchain_default, '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j', vmchain])
> > + except:
> > + util.SMlog("Failed to program default rules for vm " +
> vm_name)
> > + return 'false'
> > +
> > + default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
> > + default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
> > +
> > + if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid,
> '_initial_', '-1', vm_mac) == False:
> > + util.SMlog("Failed to log default network rules, ignoring")
> > +
> > + util.SMlog("Programmed default rules for vm " + vm_name)
> > + return 'true'
> > +
> > +@echo
> > +def check_domid_changed(session, vmName):
> > + curr_domid = '-1'
> > + try:
> > + vm = session.xenapi.VM.get_by_name_label(vmName)
> > + if len(vm) != 1:
> > + util.SMlog("### Could not get record for vm ## " +
> vmName)
> > + else:
> > + vm_rec = session.xenapi.VM.get_record(vm[0])
> > + curr_domid = vm_rec.get('domid')
> > + except:
> > + util.SMlog("### Failed to get domid for vm ## " + vmName)
> > +
> > +
> > + logfilename = "/var/run/cloud/" + vmName +".log"
> > + if not os.path.exists(logfilename):
> > + return ['-1', curr_domid]
> > +
> > + lines = (line.rstrip() for line in open(logfilename))
> > +
> > + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno, _vmMac] = ['_',
> '-1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> > + for line in lines:
> > + try:
> > + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno,_vmMac]
> = line.split(',')
> > + except ValueError,v:
> > + [_vmName,_vmID,_vmIP,old_domid,_signature,_seqno] =
> line.split(',')
> > + break
> > +
> > + return [curr_domid, old_domid]
> > +
> > +@echo
> > +def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
> > + vm_name = vmName
> > + vmchain = chain_name_def(vm_name)
> > +
> > + delcmd = "iptables-save | grep '\-A BRIDGE-FIREWALL' | grep "
> + vmchain + " | sed 's/-A/-D/'"
> > + delcmds = util.pread2(['/bin/bash', '-c', delcmd]).split('\n')
> > + delcmds.pop()
> > + for cmd in delcmds:
> > + try:
> > + dc = cmd.split(' ')
> > + dc.insert(0, 'iptables')
> > + dc.pop()
> > + util.pread2(filter(None, dc))
> > + except:
> > + util.SMlog("Ignoring failure to delete rules for vm "
> + vmName)
> > +
> > +
> > +@echo
> > +def network_rules_for_rebooted_vm(session, vmName):
> > + vm_name = vmName
> > + [curr_domid, old_domid] = check_domid_changed(session, vm_name)
> > +
> > + if curr_domid == old_domid:
> > + return True
> > +
> > + if old_domid == '-1':
> > + return True
> > +
> > + if curr_domid == '-1':
> > + return True
> > +
> > + util.SMlog("Found a rebooted VM -- reprogramming rules for " +
> vm_name)
> > +
> > + delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
> > + if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-', 'l-
> '] ]:
> > + default_network_rules_systemvm(session, {"vmName":vm_name})
> > + return True
> > +
> > + vif = "vif" + curr_domid + ".0"
> > + tap = "tap" + curr_domid + ".0"
> > + vifs = [vif]
> > + try:
> > + util.pread2(['ifconfig', tap])
> > + vifs.append(tap)
> > + except:
> > + pass
> > + vmchain = chain_name(vm_name)
> > + vmchain_default = chain_name_def(vm_name)
> > +
> > + for v in vifs:
> > + util.pread2(['iptables', '-A', 'BRIDGE-FIREWALL', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-out', v, '-j',
> vmchain_default])
> > + util.pread2(['iptables', '-I', 'BRIDGE-FIREWALL', '2', '-m',
> 'physdev', '--physdev-is-bridged', '--physdev-in', v, '-j',
> vmchain_default])
> > +
> > + #change antispoof rule in vmchain
> > + try:
> > + delcmd = "iptables-save | grep '\-A " + vmchain_default +
> "' | grep physdev-in | sed 's/-A/-D/'"
> > + delcmd2 = "iptables-save | grep '\-A " + vmchain_default +
> "' | grep physdev-out | sed 's/-A/-D/'"
> > + inscmd = "iptables-save | grep '\-A " + vmchain_default +
> "' | grep physdev-in | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/'
> "
> > + inscmd2 = "iptables-save| grep '\-A " + vmchain_default +
> "' | grep physdev-in | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/'
> "
> > + inscmd3 = "iptables-save | grep '\-A " + vmchain_default +
> "' | grep physdev-out | grep vif | sed -r 's/vif[0-9]+.0/" + vif + "/'
> "
> > + inscmd4 = "iptables-save| grep '\-A " + vmchain_default +
> "' | grep physdev-out | grep tap | sed -r 's/tap[0-9]+.0/" + tap + "/'
> "
> > +
> > + ipts = []
> > + for cmd in [delcmd, delcmd2, inscmd, inscmd2, inscmd3,
> inscmd4]:
> > + cmds = util.pread2(['/bin/bash', '-c', cmd]).split('\n')
> > + cmds.pop()
> > + for c in cmds:
> > + ipt = c.split(' ')
> > + ipt.insert(0, 'iptables')
> > + ipt.pop()
> > + ipts.append(ipt)
> > +
> > + for ipt in ipts:
> > + try:
> > + util.pread2(filter(None,ipt))
> > + except:
> > + util.SMlog("Failed to rewrite antispoofing rules for
> vm " + vm_name)
> > +
> > + util.pread2(['/bin/bash', '-c', 'iptables -D ' +
> vmchain_default + " -j " + vmchain])
> > + util.pread2(['/bin/bash', '-c', 'iptables -A ' +
> vmchain_default + " -j " + vmchain])
> > + except:
> > + util.SMlog("No rules found for vm " + vm_name)
> > +
> > + destroy_ebtables_rules(vmchain)
> > + destroy_arptables_rules(vmchain)
> > + [vm_ip, vm_mac] = get_vm_mac_ip_from_log(vmchain)
> > + default_arp_antispoof(vmchain, vifs, vm_ip, vm_mac)
> > + default_ebtables_antispoof_rules(vmchain, vifs, vm_ip, vm_mac)
> > + rewrite_rule_log_for_vm(vm_name, curr_domid)
> > + return True
> > +
> > +def rewrite_rule_log_for_vm(vm_name, new_domid):
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > + if not os.path.exists(logfilename):
> > + return
> > + lines = (line.rstrip() for line in open(logfilename))
> > +
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-
> 1', '_', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
> > + for line in lines:
> > + try:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] =
> line.split(',')
> > + break
> > + except ValueError,v:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] =
> line.split(',')
> > +
> > + write_rule_log_for_vm(_vmName, _vmID, _vmIP, new_domid,
> _signature, '-1', _vmMac)
> > +
> > +def get_rule_log_for_vm(session, vmName):
> > + vm_name = vmName;
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > + if not os.path.exists(logfilename):
> > + return ''
> > +
> > + lines = (line.rstrip() for line in open(logfilename))
> > +
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-
> 1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> > + for line in lines:
> > + try:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] =
> line.split(',')
> > + break
> > + except ValueError,v:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] =
> line.split(',')
> > +
> > + return ','.join([_vmName, _vmID, _vmIP, _domID, _signature,
> _seqno])
> > +
> > +@echo
> > +def get_vm_mac_ip_from_log(vm_name):
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-
> 1', '0.0.0.0', '-1', '_', '-1','ff:ff:ff:ff:ff:ff']
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > + if not os.path.exists(logfilename):
> > + return ['_', '_']
> > +
> > + lines = (line.rstrip() for line in open(logfilename))
> > + for line in lines:
> > + try:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] =
> line.split(',')
> > + break
> > + except ValueError,v:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] =
> line.split(',')
> > +
> > + return [ _vmIP, _vmMac]
> > +
> > +@echo
> > +def get_rule_logs_for_vms(session, args):
> > + host_uuid = args.pop('host_uuid')
> > + try:
> > + thishost = session.xenapi.host.get_by_uuid(host_uuid)
> > + hostrec = session.xenapi.host.get_record(thishost)
> > + vms = hostrec.get('resident_VMs')
> > + except:
> > + util.SMlog("Failed to get host from uuid " + host_uuid)
> > + return ' '
> > +
> > + result = []
> > + try:
> > + for name in [session.xenapi.VM.get_name_label(x) for x in
> vms]:
> > + if 1 not in [ name.startswith(c) for c in ['r-', 's-',
> 'v-', 'i-', 'l-'] ]:
> > + continue
> > + network_rules_for_rebooted_vm(session, name)
> > + if name.startswith('i-'):
> > + log = get_rule_log_for_vm(session, name)
> > + result.append(log)
> > + except:
> > + util.SMlog("Failed to get rule logs, better luck next time!")
> > +
> > + return ";".join(result)
> > +
> > +@echo
> > +def cleanup_rules_for_dead_vms(session):
> > + try:
> > + vms = session.xenapi.VM.get_all()
> > + cleaned = 0
> > + for vm_name in [session.xenapi.VM.get_name_label(x) for x in
> vms]:
> > + if 1 in [ vm_name.startswith(c) for c in ['r-', 'i-', 's-',
> 'v-', 'l-'] ]:
> > + vm = session.xenapi.VM.get_by_name_label(vm_name)
> > + if len(vm) != 1:
> > + continue
> > + vm_rec = session.xenapi.VM.get_record(vm[0])
> > + state = vm_rec.get('power_state')
> > + if state != 'Running' and state != 'Paused':
> > + util.SMlog("vm " + vm_name + " is not running,
> cleaning up")
> > + destroy_network_rules_for_vm(session,
> {'vmName':vm_name})
> > + cleaned = cleaned+1
> > +
> > + util.SMlog("Cleaned up rules for " + str(cleaned) + " vms")
> > + except:
> > + util.SMlog("Failed to cleanup rules for dead vms!")
> > +
> > +
> > +@echo
> > +def cleanup_rules(session, args):
> > + instance = args.get('instance')
> > + if not instance:
> > + instance = 'VM'
> > + resident_vms = []
> > + try:
> > + hostname = util.pread2(['/bin/bash', '-c',
> 'hostname']).split('\n')
> > + if len(hostname) < 1:
> > + raise Exception('Could not find hostname of this host')
> > + thishost = session.xenapi.host.get_by_name_label(hostname[0])
> > + if len(thishost) < 1:
> > + raise Exception("Could not find host record from hostname %s
> of this host"%hostname[0])
> > + hostrec = session.xenapi.host.get_record(thishost[0])
> > + vms = hostrec.get('resident_VMs')
> > + resident_vms = [session.xenapi.VM.get_name_label(x) for x in vms]
> > + util.SMlog('cleanup_rules: found %s resident vms on this host
> %s' % (len(resident_vms)-1, hostname[0]))
> > +
> > + chainscmd = "iptables-save | grep '^:' | awk '{print $1}' | cut
> -d':' -f2 | sed 's/-def/-%s/'| sed 's/-eg//' | sort|uniq" % instance
> > + chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
> > + vmchains = [ch for ch in chains if 1 in [ ch.startswith(c) for
> c in ['r-', 'i-', 's-', 'v-', 'l-']]]
> > + util.SMlog('cleanup_rules: found %s iptables chains for vms on
> this host %s' % (len(vmchains), hostname[0]))
> > + cleaned = 0
> > + cleanup = []
> > + for chain in vmchains:
> > + vm = session.xenapi.VM.get_by_name_label(chain)
> > + if len(vm) != 1:
> > + vm = session.xenapi.VM.get_by_name_label(chain + "-
> untagged")
> > + if len(vm) != 1:
> > + util.SMlog("chain " + chain + " does not correspond
> to a vm, cleaning up")
> > + cleanup.append(chain)
> > + continue
> > + if chain not in resident_vms:
> > + util.SMlog("vm " + chain + " is not running, cleaning
> up")
> > + cleanup.append(chain)
> > +
> > + for vm_name in cleanup:
> > + destroy_network_rules_for_vm(session, {'vmName':vm_name})
> > +
> > + util.SMlog("Cleaned up rules for " + str(len(cleanup)) + "
> chains")
> > + return str(len(cleanup))
> > + except Exception, ex:
> > + util.SMlog("Failed to cleanup rules, reason= " + str(ex))
> > + return '-1';
> > +
> > +@echo
> > +def check_rule_log_for_vm(vmName, vmID, vmIP, domID, signature,
> seqno):
> > + vm_name = vmName;
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > + if not os.path.exists(logfilename):
> > + util.SMlog("Failed to find logfile %s" %logfilename)
> > + return [True, True, True]
> > +
> > + lines = (line.rstrip() for line in open(logfilename))
> > +
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,_vmMac] = ['_', '-
> 1', '_', '-1', '_', '-1', 'ff:ff:ff:ff:ff:ff']
> > + try:
> > + for line in lines:
> > + try:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno,
> _vmMac] = line.split(',')
> > + except ValueError,v:
> > + [_vmName,_vmID,_vmIP,_domID,_signature,_seqno] =
> line.split(',')
> > + break
> > + except:
> > + util.SMlog("Failed to parse log file for vm " + vmName)
> > + remove_rule_log_for_vm(vmName)
> > + return [True, True, True]
> > +
> > + reprogramDefault = False
> > + if (domID != _domID) or (vmID != _vmID) or (vmIP != _vmIP):
> > + util.SMlog("Change in default info set of vm %s" % vmName)
> > + return [True, True, True]
> > + else:
> > + util.SMlog("No change in default info set of vm %s" % vmName)
> > +
> > + reprogramChain = False
> > + rewriteLog = True
> > + if (int(seqno) > int(_seqno)):
> > + if (_signature != signature):
> > + reprogramChain = True
> > + util.SMlog("Seqno increased from %s to %s: reprogamming
> "\
> > + "ingress rules for vm %s" % (_seqno, seqno,
> vmName))
> > + else:
> > + util.SMlog("Seqno increased from %s to %s: but no change
> "\
> > + "in signature for vm: skip programming
> ingress "\
> > + "rules %s" % (_seqno, seqno, vmName))
> > + elif (int(seqno) < int(_seqno)):
> > + util.SMlog("Seqno decreased from %s to %s: ignoring these "\
> > + "ingress rules for vm %s" % (_seqno, seqno,
> vmName))
> > + rewriteLog = False
> > + elif (signature != _signature):
> > + util.SMlog("Seqno %s stayed the same but signature changed
> from "\
> > + "%s to %s for vm %s" % (seqno, _signature,
> signature, vmName))
> > + rewriteLog = True
> > + reprogramChain = True
> > + else:
> > + util.SMlog("Seqno and signature stayed the same: %s :
> ignoring these "\
> > + "ingress rules for vm %s" % (seqno, vmName))
> > + rewriteLog = False
> > +
> > + return [reprogramDefault, reprogramChain, rewriteLog]
> > +
> > +
> > +@echo
> > +def write_rule_log_for_vm(vmName, vmID, vmIP, domID, signature,
> seqno, vmMac='ff:ff:ff:ff:ff:ff'):
> > + vm_name = vmName
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > + util.SMlog("Writing log to " + logfilename)
> > + logf = open(logfilename, 'w')
> > + output = ','.join([vmName, vmID, vmIP, domID, signature, seqno,
> vmMac])
> > + result = True
> > + try:
> > + logf.write(output)
> > + logf.write('\n')
> > + except:
> > + util.SMlog("Failed to write to rule log file " + logfilename)
> > + result = False
> > +
> > + logf.close()
> > +
> > + return result
> > +
> > +@echo
> > +def remove_rule_log_for_vm(vmName):
> > + vm_name = vmName
> > + logfilename = "/var/run/cloud/" + vm_name +".log"
> > +
> > + result = True
> > + try:
> > + os.remove(logfilename)
> > + except:
> > + util.SMlog("Failed to delete rule log file " + logfilename)
> > + result = False
> > +
> > + return result
> > +
> > +@echo
> > +def inflate_rules (zipped):
> > + return zlib.decompress(base64.b64decode(zipped))
> > +
> > +@echo
> > +def cache_ipset_keyword():
> > + tmpname = 'ipsetqzvxtmp'
> > + try:
> > + util.pread2(['/bin/bash', '-c', 'ipset -N ' + tmpname + '
> iptreemap'])
> > + except:
> > + util.pread2(['/bin/bash', '-c', 'ipset -F ' + tmpname])
> > +
> > + try:
> > + util.pread2(['/bin/bash', '-c', 'iptables -A INPUT -m set --
> set ' + tmpname + ' src' + ' -j ACCEPT'])
> > + util.pread2(['/bin/bash', '-c', 'iptables -D INPUT -m set --
> set ' + tmpname + ' src' + ' -j ACCEPT'])
> > + keyword = 'set'
> > + except:
> > + keyword = 'match-set'
> > +
> > + try:
> > + util.pread2(['/bin/bash', '-c', 'ipset -X ' + tmpname])
> > + except:
> > + pass
> > +
> > + cachefile = "/var/cache/cloud/ipset.keyword"
> > + util.SMlog("Writing ipset keyword to " + cachefile)
> > + cachef = open(cachefile, 'w')
> > + try:
> > + cachef.write(keyword)
> > + cachef.write('\n')
> > + except:
> > + util.SMlog("Failed to write to cache file " + cachef)
> > +
> > + cachef.close()
> > + return keyword
> > +
> > +@echo
> > +def get_ipset_keyword():
> > + cachefile = "/var/cache/cloud/ipset.keyword"
> > + keyword = 'match-set'
> > +
> > + if not os.path.exists(cachefile):
> > + util.SMlog("Failed to find ipset keyword cachefile %s"
> %cachefile)
> > + keyword = cache_ipset_keyword()
> > + else:
> > + lines = (line.rstrip() for line in open(cachefile))
> > + for line in lines:
> > + keyword = line
> > + break
> > +
> > + return keyword
> > +
> > +@echo
> > +def network_rules(session, args):
> > + try:
> > + vm_name = args.get('vmName')
> > + vm_ip = args.get('vmIP')
> > + vm_id = args.get('vmID')
> > + vm_mac = args.get('vmMAC')
> > + signature = args.pop('signature')
> > + seqno = args.pop('seqno')
> > + deflated = 'false'
> > + if 'deflated' in args:
> > + deflated = args.pop('deflated')
> > +
> > + try:
> > + vm = session.xenapi.VM.get_by_name_label(vm_name)
> > + if len(vm) != 1:
> > + util.SMlog("### Could not get record for vm ## " +
> vm_name)
> > + return 'false'
> > + vm_rec = session.xenapi.VM.get_record(vm[0])
> > + domid = vm_rec.get('domid')
> > + except:
> > + util.SMlog("### Failed to get domid for vm ## " + vm_name)
> > + return 'false'
> > + if domid == '-1':
> > + util.SMlog("### Failed to get domid for vm (-1): " +
> vm_name)
> > + return 'false'
> > +
> > + vif = "vif" + domid + ".0"
> > + tap = "tap" + domid + ".0"
> > + vifs = [vif]
> > + try:
> > + util.pread2(['ifconfig', tap])
> > + vifs.append(tap)
> > + except:
> > + pass
> > +
> > +
> > + reason = 'seqno_change_or_sig_change'
> > + [reprogramDefault, reprogramChain, rewriteLog] = \
> > + check_rule_log_for_vm (vm_name, vm_id, vm_ip, domid,
> signature, seqno)
> > +
> > + if not reprogramDefault and not reprogramChain:
> > + util.SMlog("No changes detected between current state and
> received state")
> > + reason = 'seqno_same_sig_same'
> > + if rewriteLog:
> > + reason = 'seqno_increased_sig_same'
> > + write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid,
> signature, seqno, vm_mac)
> > + util.SMlog("Programming network rules for vm %s seqno=%s
> signature=%s guestIp=%s,"\
> > + " do nothing, reason=%s" % (vm_name, seqno, signature,
> vm_ip, reason))
> > + return 'true'
> > +
> > + if not reprogramChain:
> > + util.SMlog("###Not programming any ingress rules since no
> changes detected?")
> > + return 'true'
> > +
> > + if reprogramDefault:
> > + util.SMlog("Change detected in vmId or vmIp or domId,
> resetting default rules")
> > + default_network_rules(session, args)
> > + reason = 'domid_change'
> > +
> > + rules = args.pop('rules')
> > + if deflated.lower() == 'true':
> > + rules = inflate_rules (rules)
> > + keyword = '--' + get_ipset_keyword()
> > + lines = rules.split(' ')
> > +
> > + util.SMlog("Programming network rules for vm %s seqno=%s
> numrules=%s signature=%s guestIp=%s,"\
> > + " update iptables, reason=%s" % (vm_name, seqno,
> len(lines), signature, vm_ip, reason))
> > +
> > + cmds = []
> > + egressrules = 0
> > + for line in lines:
> > + tokens = line.split(':')
> > + if len(tokens) != 5:
> > + continue
> > + type = tokens[0]
> > + protocol = tokens[1]
> > + start = tokens[2]
> > + end = tokens[3]
> > + cidrs = tokens.pop();
> > + ips = cidrs.split(",")
> > + ips.pop()
> > + allow_any = False
> > +
> > + if type == 'E':
> > + vmchain = egress_chain_name(vm_name)
> > + action = "RETURN"
> > + direction = "dst"
> > + egressrules = egressrules + 1
> > + else:
> > + vmchain = chain_name(vm_name)
> > + action = "ACCEPT"
> > + direction = "src"
> > + if '0.0.0.0/0' in ips:
> > + i = ips.index('0.0.0.0/0')
> > + del ips[i]
> > + allow_any = True
> > + range = start + ":" + end
> > + if ips:
> > + ipsetname = vmchain + "_" + protocol + "_" + start + "_"
> + end
> > + if start == "-1":
> > + ipsetname = vmchain + "_" + protocol + "_any"
> > +
> > + if ipset(ipsetname, protocol, start, end, ips) == False:
> > + util.SMlog(" failed to create ipset for rule " +
> str(tokens))
> > +
> > + if protocol == 'all':
> > + iptables = ['iptables', '-I', vmchain, '-m', 'state',
> '--state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j',
> action]
> > + elif protocol != 'icmp':
> > + iptables = ['iptables', '-I', vmchain, '-
> p', protocol, '-m', protocol, '--dport', range, '-m', 'state', '--
> state', 'NEW', '-m', 'set', keyword, ipsetname, direction, '-j', action]
> > + else:
> > + range = start + "/" + end
> > + if start == "-1":
> > + range = "any"
> > + iptables = ['iptables', '-I', vmchain, '-p', 'icmp',
> '--icmp-type', range, '-m', 'set', keyword, ipsetname, direction, '-
> j', action]
> > +
> > + cmds.append(iptables)
> > + util.SMlog(iptables)
> > +
> > + if allow_any and protocol != 'all':
> > + if protocol != 'icmp':
> > + iptables = ['iptables', '-I', vmchain, '-
> p', protocol, '-m', protocol, '--dport', range, '-m', 'state', '--
> state', 'NEW', '-j', action]
> > + else:
> > + range = start + "/" + end
> > + if start == "-1":
> > + range = "any"
> > + iptables = ['iptables', '-I', vmchain, '-p', 'icmp',
> '--icmp-type', range, '-j', action]
> > + cmds.append(iptables)
> > + util.SMlog(iptables)
> > +
> > + vmchain = chain_name(vm_name)
> > + util.pread2(['iptables', '-F', vmchain])
> > + egress_vmchain = egress_chain_name(vm_name)
> > + util.pread2(['iptables', '-F', egress_vmchain])
> > +
> > + for cmd in cmds:
> > + util.pread2(cmd)
> > +
> > + if egressrules == 0 :
> > + util.pread2(['iptables', '-A', egress_vmchain, '-j',
> 'RETURN'])
> > + else:
> > + util.pread2(['iptables', '-A', egress_vmchain, '-j', 'DROP'])
> > +
> > + util.pread2(['iptables', '-A', vmchain, '-j', 'DROP'])
> > +
> > + if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domid, signature,
> seqno, vm_mac) == False:
> > + return 'false'
> > +
> > + return 'true'
> > + except:
> > + util.SMlog("Failed to network rule !")
> > +
> > +@echo
> > +def checkRouter(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/getRouterStatus.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + except:
> > + util.SMlog(" check router status fail! ")
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def bumpUpPriority(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/bumpUpPriority.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + txt = 'success'
> > + except:
> > + util.SMlog("bump up priority fail! ")
> > + txt = ''
> > +
> > + return txt
> > +
> > +@echo
> > +def createISOVHD(session, args):
> > + #hack for XCP on ubuntu 12.04, as can't attach iso to a vm
> > + vdis = session.xenapi.VDI.get_by_name_label("systemvm-vdi");
> > + util.SMlog(vdis)
> > + if len(vdis) > 0:
> > + vdi_record = session.xenapi.VDI.get_record(vdis[0])
> > + vdi_uuid = vdi_record['uuid']
> > + return vdi_uuid
> > + localsrUUid = args['uuid'];
> > + sr = session.xenapi.SR.get_by_uuid(localsrUUid)
> > + data = {'name_label': "systemvm-vdi",
> > + 'SR': sr,
> > + 'virtual_size': '50000000',
> > + 'type': 'user',
> > + 'sharable':False,
> > + 'read_only':False,
> > + 'other_config':{},
> > + }
> > + vdi = session.xenapi.VDI.create(data);
> > + vdi_record = session.xenapi.VDI.get_record(vdi)
> > +
> > + vdi_uuid = vdi_record['uuid']
> > +
> > + vms = session.xenapi.VM.get_all()
> > + ctrldom = None
> > + for vm in vms:
> > + dom0 = session.xenapi.VM.get_is_control_domain(vm)
> > + if dom0 is False:
> > + continue
> > + else:
> > + ctrldom = vm
> > +
> > + if ctrldom is None:
> > + return "Failed"
> > +
> > + vbds = session.xenapi.VM.get_VBDs(ctrldom)
> > + if len(vbds) == 0:
> > + vbd = session.xenapi.VBD.create({"VDI": vdi, "VM": ctrldom,
> "type":"Disk", "device": "xvda4", "bootable": False, "mode": "RW",
> "userdevice": "4", "empty":False,
> > + "other_config":{},
> "qos_algorithm_type":"", "qos_algorithm_params":{}})
> > + else:
> > + vbd = vbds[0]
> > +
> > + vbdr = session.xenapi.VBD.get_record(vbd)
> > + if session.xenapi.VBD.get_currently_attached(vbd) is False:
> > + session.xenapi.VBD.plug(vbd)
> > + vbdr = session.xenapi.VBD.get_record(vbd)
> > + util.pread2(["dd", "if=/usr/share/xcp/packages/iso/systemvm.iso",
> "of=" + "/dev/" + vbdr["device"]])
> > + session.xenapi.VBD.unplug(vbd)
> > + session.xenapi.VBD.destroy(vbd)
> > + return vdi_uuid
> > +
> > +@echo
> > +def getDomRVersion(session, args):
> > + sargs = args['args']
> > + cmd = sargs.split(' ')
> > + cmd.insert(0, "/usr/lib/xcp/bin/getDomRVersion.sh")
> > + cmd.insert(0, "/bin/bash")
> > + try:
> > + txt = util.pread2(cmd)
> > + except:
> > + util.SMlog(" get domR version fail! ")
> > + txt = ''
> > +
> > + return txt
> > +
> > +if __name__ == "__main__":
> > + XenAPIPlugin.dispatch({"pingtest": pingtest,
> "setup_iscsi":setup_iscsi, "gethostvmstats": gethostvmstats,
> > + "getvncport": getvncport, "getgateway":
> getgateway, "preparemigration": preparemigration,
> > + "setIptables": setIptables, "pingdomr":
> pingdomr, "pingxenserver": pingxenserver,
> > + "ipassoc": ipassoc, "vm_data": vm_data,
> "savePassword": savePassword,
> > + "saveDhcpEntry": saveDhcpEntry,
> "setFirewallRule": setFirewallRule,
> > + "setLoadBalancerRule":
> setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile,
> > + "networkUsage": networkUsage,
> "network_rules":network_rules,
> >
> + "can_bridge_firewall":can_bridge_firewall,
> "default_network_rules":default_network_rules,
> >
> + "destroy_network_rules_for_vm":destroy_net
> work_rules_for_vm,
> >
> + "default_network_rules_systemvm":default_n
> etwork_rules_systemvm,
> >
> + "get_rule_logs_for_vms":get_rule_logs_for_
> vms,
> > + "setLinkLocalIP":setLinkLocalIP,
> "lt2p_vpn":lt2p_vpn,
> > + "cleanup_rules":cleanup_rules,
> "checkRouter":checkRouter,
> > + "bumpUpPriority":bumpUpPriority,
> "getDomRVersion":getDomRVersion,
> > + "kill_copy_process":kill_copy_process,
> > + "createISOVHD":createISOVHD})
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmopsSn
> apshot
> > ---------------------------------------------------------------------
> -
> > diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> b/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> > new file mode 100644
> > index 0000000..15ea62f
> > --- /dev/null
> > +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmopsSnapshot
> > @@ -0,0 +1,552 @@
> > +#!/usr/bin/python
> > +# Version @VERSION@
> > +#
> > +# A plugin for executing script needed by vmops cloud
> > +
> > +import os, sys, time
> > +import XenAPIPlugin
> > +sys.path.append("/usr/lib/xcp/sm/")
> > +import SR, VDI, SRCommand, util, lvutil
> > +from util import CommandException
> > +import vhdutil
> > +import shutil
> > +import lvhdutil
> > +import errno
> > +import subprocess
> > +import xs_errors
> > +import cleanup
> > +import stat
> > +import random
> > +
> > +VHD_UTIL = 'vhd-util'
> > +VHD_PREFIX = 'VHD-'
> > +CLOUD_DIR = '/run/cloud_mount'
> > +
> > +def echo(fn):
> > + def wrapped(*v, **k):
> > + name = fn.__name__
> > + util.SMlog("#### VMOPS enter %s ####" % name )
> > + res = fn(*v, **k)
> > + util.SMlog("#### VMOPS exit %s ####" % name )
> > + return res
> > + return wrapped
> > +
> > +
> > +@echo
> > +def create_secondary_storage_folder(session, args):
> > + local_mount_path = None
> > +
> > + util.SMlog("create_secondary_storage_folder, args: " + str(args))
> > +
> > + try:
> > + try:
> > + # Mount the remote resource folder locally
> > + remote_mount_path = args["remoteMountPath"]
> > + local_mount_path = os.path.join(CLOUD_DIR,
> util.gen_uuid())
> > + mount(remote_mount_path, local_mount_path)
> > +
> > + # Create the new folder
> > + new_folder = local_mount_path + "/" + args["newFolder"]
> > + if not os.path.isdir(new_folder):
> > + current_umask = os.umask(0)
> > + os.makedirs(new_folder)
> > + os.umask(current_umask)
> > + except OSError, (errno, strerror):
> > + errMsg = "create_secondary_storage_folder failed: errno:
> " + str(errno) + ", strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + except:
> > + errMsg = "create_secondary_storage_folder failed."
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + finally:
> > + if local_mount_path != None:
> > + # Unmount the local folder
> > + umount(local_mount_path)
> > + # Remove the local folder
> > + os.system("rmdir " + local_mount_path)
> > +
> > + return "1"
> > +
> > +@echo
> > +def delete_secondary_storage_folder(session, args):
> > + local_mount_path = None
> > +
> > + util.SMlog("delete_secondary_storage_folder, args: " + str(args))
> > +
> > + try:
> > + try:
> > + # Mount the remote resource folder locally
> > + remote_mount_path = args["remoteMountPath"]
> > + local_mount_path = os.path.join(CLOUD_DIR,
> util.gen_uuid())
> > + mount(remote_mount_path, local_mount_path)
> > +
> > + # Delete the specified folder
> > + folder = local_mount_path + "/" + args["folder"]
> > + if os.path.isdir(folder):
> > + os.system("rm -f " + folder + "/*")
> > + os.system("rmdir " + folder)
> > + except OSError, (errno, strerror):
> > + errMsg = "delete_secondary_storage_folder failed: errno:
> " + str(errno) + ", strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + except:
> > + errMsg = "delete_secondary_storage_folder failed."
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + finally:
> > + if local_mount_path != None:
> > + # Unmount the local folder
> > + umount(local_mount_path)
> > + # Remove the local folder
> > + os.system("rmdir " + local_mount_path)
> > +
> > + return "1"
> > +
> > +@echo
> > +def post_create_private_template(session, args):
> > + local_mount_path = None
> > + try:
> > + try:
> > + # get local template folder
> > + templatePath = args["templatePath"]
> > + local_mount_path = os.path.join(CLOUD_DIR,
> util.gen_uuid())
> > + mount(templatePath, local_mount_path)
> > + # Retrieve args
> > + filename = args["templateFilename"]
> > + name = args["templateName"]
> > + description = args["templateDescription"]
> > + checksum = args["checksum"]
> > + file_size = args["size"]
> > + virtual_size = args["virtualSize"]
> > + template_id = args["templateId"]
> > +
> > + # Create the template.properties file
> > + template_properties_install_path = local_mount_path +
> "/template.properties"
> > + f = open(template_properties_install_path, "w")
> > + f.write("filename=" + filename + "\n")
> > + f.write("vhd=true\n")
> > + f.write("id=" + template_id + "\n")
> > + f.write("vhd.filename=" + filename + "\n")
> > + f.write("public=false\n")
> > + f.write("uniquename=" + name + "\n")
> > + f.write("vhd.virtualsize=" + virtual_size + "\n")
> > + f.write("virtualsize=" + virtual_size + "\n")
> > + f.write("checksum=" + checksum + "\n")
> > + f.write("hvm=true\n")
> > + f.write("description=" + description + "\n")
> > + f.write("vhd.size=" + str(file_size) + "\n")
> > + f.write("size=" + str(file_size) + "\n")
> > + f.close()
> > + util.SMlog("Created template.properties file")
> > +
> > + # Set permissions
> > + permissions = stat.S_IREAD | stat.S_IWRITE |
> stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH
> > + os.chmod(template_properties_install_path, permissions)
> > + util.SMlog("Set permissions on template and
> template.properties")
> > +
> > + except:
> > + errMsg = "post_create_private_template failed."
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + finally:
> > + if local_mount_path != None:
> > + # Unmount the local folder
> > + umount(local_mount_path)
> > + # Remove the local folder
> > + os.system("rmdir " + local_mount_path)
> > + return "1"
> > +
> > +def isfile(path, isISCSI):
> > + errMsg = ''
> > + exists = True
> > + if isISCSI:
> > + exists = checkVolumeAvailablility(path)
> > + else:
> > + exists = os.path.isfile(path)
> > +
> > + if not exists:
> > + errMsg = "File " + path + " does not exist."
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + return errMsg
> > +
> > +def copyfile(fromFile, toFile, isISCSI):
> > + util.SMlog("Starting to copy " + fromFile + " to " + toFile)
> > + errMsg = ''
> > + try:
> > + cmd = ['dd', 'if=' + fromFile, 'of=' + toFile, 'bs=4M']
> > + txt = util.pread2(cmd)
> > + except:
> > + try:
> > + os.system("rm -f " + toFile)
> > + except:
> > + txt = ''
> > + txt = ''
> > + errMsg = "Error while copying " + fromFile + " to " + toFile
> + " in secondary storage"
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + util.SMlog("Successfully copied " + fromFile + " to " + toFile)
> > + return errMsg
> > +
> > +def chdir(path):
> > + try:
> > + os.chdir(path)
> > + except OSError, (errno, strerror):
> > + errMsg = "Unable to chdir to " + path + " because of OSError
> with errno: " + str(errno) + " and strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + util.SMlog("Chdired to " + path)
> > + return
> > +
> > +def scanParent(path):
> > + # Do a scan for the parent for ISCSI volumes
> > + # Note that the parent need not be visible on the XenServer
> > + parentUUID = ''
> > + try:
> > + lvName = os.path.basename(path)
> > + dirname = os.path.dirname(path)
> > + vgName = os.path.basename(dirname)
> > + vhdInfo = vhdutil.getVHDInfoLVM(lvName, lvhdutil.extractUuid,
> vgName)
> > + parentUUID = vhdInfo.parentUuid
> > + except:
> > + errMsg = "Could not get vhd parent of " + path
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + return parentUUID
> > +
> > +def getParent(path, isISCSI):
> > + parentUUID = ''
> > + try :
> > + if isISCSI:
> > + parentUUID = vhdutil.getParent(path,
> lvhdutil.extractUuid)
> > + else:
> > + parentUUID = vhdutil.getParent(path,
> cleanup.FileVDI.extractUuid)
> > + except:
> > + errMsg = "Could not get vhd parent of " + path
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + return parentUUID
> > +
> > +def getParentOfSnapshot(snapshotUuid, primarySRPath, isISCSI):
> > + snapshotVHD = getVHD(snapshotUuid, isISCSI)
> > + snapshotPath = os.path.join(primarySRPath, snapshotVHD)
> > +
> > + baseCopyUuid = ''
> > + if isISCSI:
> > + checkVolumeAvailablility(snapshotPath)
> > + baseCopyUuid = scanParent(snapshotPath)
> > + else:
> > + baseCopyUuid = getParent(snapshotPath, isISCSI)
> > +
> > + util.SMlog("Base copy of snapshotUuid: " + snapshotUuid + " is "
> + baseCopyUuid)
> > + return baseCopyUuid
> > +
> > +def setParent(parent, child):
> > + try:
> > + cmd = [VHD_UTIL, "modify", "-p", parent, "-n", child]
> > + txt = util.pread2(cmd)
> > + except:
> > + errMsg = "Unexpected error while trying to set parent of " +
> child + " to " + parent
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + util.SMlog("Successfully set parent of " + child + " to " +
> parent)
> > + return
> > +
> > +def rename(originalVHD, newVHD):
> > + try:
> > + os.rename(originalVHD, newVHD)
> > + except OSError, (errno, strerror):
> > + errMsg = "OSError while renaming " + origiinalVHD + " to " +
> newVHD + "with errno: " + str(errno) + " and strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + return
> > +
> > +def makedirs(path):
> > + if not os.path.isdir(path):
> > + try:
> > + os.makedirs(path)
> > + except OSError, (errno, strerror):
> > + umount(path)
> > + if os.path.isdir(path):
> > + return
> > + errMsg = "OSError while creating " + path + " with errno:
> " + str(errno) + " and strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + return
> > +
> > +def mount(remoteDir, localDir):
> > + makedirs(localDir)
> > + options = "soft,tcp,timeo=133,retrans=1"
> > + try:
> > + cmd = ['mount', '-o', options, remoteDir, localDir]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + errMsg = "Unexpected error while trying to mount " +
> remoteDir + " to " + localDir
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > + util.SMlog("Successfully mounted " + remoteDir + " to " +
> localDir)
> > +
> > + return
> > +
> > +def umount(localDir):
> > + try:
> > + cmd = ['umount', localDir]
> > + util.pread2(cmd)
> > + except CommandException:
> > + errMsg = "CommandException raised while trying to umount " +
> localDir
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + util.SMlog("Successfully unmounted " + localDir)
> > + return
> > +
> > +def mountSnapshotsDir(secondaryStorageMountPath, relativeDir, dcId,
> accountId, instanceId):
> > + # The aim is to mount secondaryStorageMountPath on
> > + # And create <accountId>/<instanceId> dir on it, if it doesn't
> exist already.
> > + # Assuming that secondaryStorageMountPath exists remotely
> > +
> > + # Alex's suggestion and currently implemented:
> > + # Just mount secondaryStorageMountPath/<relativeDir> everytime
> > + # Never unmount.
> > + snapshotsDir = os.path.join(secondaryStorageMountPath,
> relativeDir)
> > +
> > + # Mkdir local mount point dir, if it doesn't exist.
> > + localMountPointPath = os.path.join(CLOUD_DIR, dcId)
> > + localMountPointPath = os.path.join(localMountPointPath,
> relativeDir)
> > +
> > + makedirs(localMountPointPath)
> > + # if something is not mounted already on localMountPointPath,
> > + # mount secondaryStorageMountPath on localMountPath
> > + if os.path.ismount(localMountPointPath):
> > + # There is only one secondary storage per zone.
> > + # And we are mounting each sec storage under a zone-specific
> directory
> > + # So two secondary storage snapshot dirs will never get
> mounted on the same point on the same XenServer.
> > + util.SMlog("The remote snapshots directory has already been
> mounted on " + localMountPointPath)
> > + else:
> > + mount(snapshotsDir, localMountPointPath)
> > +
> > + # Create accountId/instanceId dir on localMountPointPath, if it
> doesn't exist
> > + backupsDir = os.path.join(localMountPointPath, accountId)
> > + backupsDir = os.path.join(backupsDir, instanceId)
> > + makedirs(backupsDir)
> > + return backupsDir
> > +
> > +@echo
> > +def unmountSnapshotsDir(session, args):
> > + dcId = args['dcId']
> > + localMountPointPath = os.path.join(CLOUD_DIR, dcId)
> > + localMountPointPath = os.path.join(localMountPointPath,
> "snapshots")
> > + try:
> > + umount(localMountPointPath)
> > + except:
> > + util.SMlog("Ignoring the error while trying to unmount the
> snapshots dir.")
> > +
> > + return "1"
> > +
> > +def getPrimarySRPath(session, primaryStorageSRUuid, isISCSI):
> > + sr = session.xenapi.SR.get_by_uuid(primaryStorageSRUuid)
> > + srrec = session.xenapi.SR.get_record(sr)
> > + srtype = srrec["type"]
> > + if srtype == "file":
> > + pbd = session.xenapi.SR.get_PBDs(sr)[0]
> > + pbdrec = session.xenapi.PBD.get_record(pbd)
> > + primarySRPath = pbdrec["device_config"]["location"]
> > + return primarySRPath
> > + if isISCSI:
> > + primarySRDir = lvhdutil.VG_PREFIX + primaryStorageSRUuid
> > + return os.path.join(lvhdutil.VG_LOCATION, primarySRDir)
> > + else:
> > + return os.path.join(SR.MOUNT_BASE, primaryStorageSRUuid)
> > +
> > +def getBackupVHD(UUID):
> > + return UUID + '.' + SR.DEFAULT_TAP
> > +
> > +def getVHD(UUID, isISCSI):
> > + if isISCSI:
> > + return VHD_PREFIX + UUID
> > + else:
> > + return UUID + '.' + SR.DEFAULT_TAP
> > +
> > +def getIsTrueString(stringValue):
> > + booleanValue = False
> > + if (stringValue and stringValue == 'true'):
> > + booleanValue = True
> > + return booleanValue
> > +
> > +def makeUnavailable(uuid, primarySRPath, isISCSI):
> > + if not isISCSI:
> > + return
> > + VHD = getVHD(uuid, isISCSI)
> > + path = os.path.join(primarySRPath, VHD)
> > + manageAvailability(path, '-an')
> > + return
> > +
> > +def manageAvailability(path, value):
> > + if path.__contains__("/var/run/sr-mount"):
> > + return
> > + util.SMlog("Setting availability of " + path + " to " + value)
> > + try:
> > + cmd = ['/usr/sbin/lvchange', value, path]
> > + util.pread2(cmd)
> > + except: #CommandException, (rc, cmdListStr, stderr):
> > + #errMsg = "CommandException thrown while executing: " +
> cmdListStr + " with return code: " + str(rc) + " and stderr: " + stderr
> > + errMsg = "Unexpected exception thrown by lvchange"
> > + util.SMlog(errMsg)
> > + if value == "-ay":
> > + # Raise an error only if we are trying to make it
> available.
> > + # Just warn if we are trying to make it unavailable
> after the
> > + # snapshot operation is done.
> > + raise xs_errors.XenError(errMsg)
> > + return
> > +
> > +
> > +def checkVolumeAvailablility(path):
> > + try:
> > + if not isVolumeAvailable(path):
> > + # The VHD file is not available on XenSever. The volume
> is probably
> > + # inactive or detached.
> > + # Do lvchange -ay to make it available on XenServer
> > + manageAvailability(path, '-ay')
> > + except:
> > + errMsg = "Could not determine status of ISCSI path: " + path
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + success = False
> > + i = 0
> > + while i < 6:
> > + i = i + 1
> > + # Check if the vhd is actually visible by checking for the
> link
> > + # set isISCSI to true
> > + success = isVolumeAvailable(path)
> > + if success:
> > + util.SMlog("Made vhd: " + path + " available and
> confirmed that it is visible")
> > + break
> > +
> > + # Sleep for 10 seconds before checking again.
> > + time.sleep(10)
> > +
> > + # If not visible within 1 min fail
> > + if not success:
> > + util.SMlog("Could not make vhd: " + path + " available
> despite waiting for 1 minute. Does it exist?")
> > +
> > + return success
> > +
> > +def isVolumeAvailable(path):
> > + # Check if iscsi volume is available on this XenServer.
> > + status = "0"
> > + try:
> > + p = subprocess.Popen(["/bin/bash", "-c", "if [ -L " + path +
> " ]; then echo 1; else echo 0;fi"], stdout=subprocess.PIPE)
> > + status = p.communicate()[0].strip("\n")
> > + except:
> > + errMsg = "Could not determine status of ISCSI path: " + path
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + return (status == "1")
> > +
> > +def getVhdParent(session, args):
> > + util.SMlog("getParent with " + str(args))
> > + primaryStorageSRUuid = args['primaryStorageSRUuid']
> > + snapshotUuid = args['snapshotUuid']
> > + isISCSI = getIsTrueString(args['isISCSI'])
> > +
> > + primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid,
> isISCSI)
> > + util.SMlog("primarySRPath: " + primarySRPath)
> > +
> > + baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath,
> isISCSI)
> > +
> > + return baseCopyUuid
> > +
> > +
> > +def backupSnapshot(session, args):
> > + util.SMlog("Called backupSnapshot with " + str(args))
> > + primaryStorageSRUuid = args['primaryStorageSRUuid']
> > + dcId = args['dcId']
> > + accountId = args['accountId']
> > + volumeId = args['volumeId']
> > + secondaryStorageMountPath = args['secondaryStorageMountPath']
> > + snapshotUuid = args['snapshotUuid']
> > + prevBackupUuid = args['prevBackupUuid']
> > + backupUuid = args['backupUuid']
> > + isISCSI = getIsTrueString(args['isISCSI'])
> > +
> > + primarySRPath = getPrimarySRPath(session, primaryStorageSRUuid,
> isISCSI)
> > + util.SMlog("primarySRPath: " + primarySRPath)
> > +
> > + baseCopyUuid = getParentOfSnapshot(snapshotUuid, primarySRPath,
> isISCSI)
> > + baseCopyVHD = getVHD(baseCopyUuid, isISCSI)
> > + baseCopyPath = os.path.join(primarySRPath, baseCopyVHD)
> > + util.SMlog("Base copy path: " + baseCopyPath)
> > +
> > +
> > + # Mount secondary storage mount path on XenServer along the path
> > + # /var/run/sr-mount/<dcId>/snapshots/ and create
> <accountId>/<volumeId> dir
> > + # on it.
> > + backupsDir = mountSnapshotsDir(secondaryStorageMountPath,
> "snapshots", dcId, accountId, volumeId)
> > + util.SMlog("Backups dir " + backupsDir)
> > +
> > + # Check existence of snapshot on primary storage
> > + isfile(baseCopyPath, isISCSI)
> > + if prevBackupUuid:
> > + # Check existence of prevBackupFile
> > + prevBackupVHD = getBackupVHD(prevBackupUuid)
> > + prevBackupFile = os.path.join(backupsDir, prevBackupVHD)
> > + isfile(prevBackupFile, False)
> > +
> > + # copy baseCopyPath to backupsDir with new uuid
> > + backupVHD = getBackupVHD(backupUuid)
> > + backupFile = os.path.join(backupsDir, backupVHD)
> > + util.SMlog("Back up " + baseCopyUuid + " to Secondary Storage as
> " + backupUuid)
> > + copyfile(baseCopyPath, backupFile, isISCSI)
> > + vhdutil.setHidden(backupFile, False)
> > +
> > + # Because the primary storage is always scanned, the parent of
> this base copy is always the first base copy.
> > + # We don't want that, we want a chain of VHDs each of which is a
> delta from the previous.
> > + # So set the parent of the current baseCopyVHD to prevBackupVHD
> > + if prevBackupUuid:
> > + # If there was a previous snapshot
> > + setParent(prevBackupFile, backupFile)
> > +
> > + txt = "1#" + backupUuid
> > + return txt
> > +
> > +@echo
> > +def deleteSnapshotBackup(session, args):
> > + util.SMlog("Calling deleteSnapshotBackup with " + str(args))
> > + dcId = args['dcId']
> > + accountId = args['accountId']
> > + volumeId = args['volumeId']
> > + secondaryStorageMountPath = args['secondaryStorageMountPath']
> > + backupUUID = args['backupUUID']
> > +
> > + backupsDir = mountSnapshotsDir(secondaryStorageMountPath,
> "snapshots", dcId, accountId, volumeId)
> > + # chdir to the backupsDir for convenience
> > + chdir(backupsDir)
> > +
> > + backupVHD = getBackupVHD(backupUUID)
> > + util.SMlog("checking existence of " + backupVHD)
> > +
> > + # The backupVHD is on secondary which is NFS and not ISCSI.
> > + if not os.path.isfile(backupVHD):
> > + util.SMlog("backupVHD " + backupVHD + "does not exist. Not
> trying to delete it")
> > + return "1"
> > + util.SMlog("backupVHD " + backupVHD + " exists.")
> > +
> > + # Just delete the backupVHD
> > + try:
> > + os.remove(backupVHD)
> > + except OSError, (errno, strerror):
> > + errMsg = "OSError while removing " + backupVHD + " with
> errno: " + str(errno) + " and strerr: " + strerror
> > + util.SMlog(errMsg)
> > + raise xs_errors.XenError(errMsg)
> > +
> > + return "1"
> > +
> > +if __name__ == "__main__":
> >
> + XenAPIPlugin.dispatch({"getVhdParent":getVhdParent, "create_secon
> dary_storage_folder":create_secondary_storage_folder,
> "delete_secondary_storage_folder":delete_secondary_storage_folder,
> "post_create_private_template":post_create_private_template,
> "backupSnapshot": backupSnapshot, "deleteSnapshotBackup":
> deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir})
> > +
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/scripts/vm/hypervisor/xenserver/xcposs/vmopspr
> emium
> > ---------------------------------------------------------------------
> -
> > diff --git a/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> b/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> > new file mode 100644
> > index 0000000..ce6cdbc
> > --- /dev/null
> > +++ b/scripts/vm/hypervisor/xenserver/xcposs/vmopspremium
> > @@ -0,0 +1,129 @@
> > +#!/usr/bin/python
> > +# Version @VERSION@
> > +#
> > +# A plugin for executing script needed by vmops cloud
> > +
> > +import os, sys, time
> > +import XenAPIPlugin
> > +sys.path.append("/usr/lib/xcp/sm/")
> > +import util
> > +import socket
> > +
> > +def echo(fn):
> > + def wrapped(*v, **k):
> > + name = fn.__name__
> > + util.SMlog("#### VMOPS enter %s ####" % name )
> > + res = fn(*v, **k)
> > + util.SMlog("#### VMOPS exit %s ####" % name )
> > + return res
> > + return wrapped
> > +
> > +@echo
> > +def forceShutdownVM(session, args):
> > + domId = args['domId']
> > + try:
> > + cmd = ["/usr/lib/xcp/debug/xenops", "destroy_domain", "-
> domid", domId]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = '10#failed'
> > + return txt
> > +
> > +
> > +@echo
> > +def create_privatetemplate_from_snapshot(session, args):
> > + templatePath = args['templatePath']
> > + snapshotPath = args['snapshotPath']
> > + tmpltLocalDir = args['tmpltLocalDir']
> > + try:
> > + cmd = ["bash",
> "/usr/lib/xcp/bin/create_privatetemplate_from_snapshot.sh",snapshotPath,
> templatePath, tmpltLocalDir]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = '10#failed'
> > + return txt
> > +
> > +@echo
> > +def upgrade_snapshot(session, args):
> > + templatePath = args['templatePath']
> > + snapshotPath = args['snapshotPath']
> > + try:
> > + cmd = ["bash",
> "/usr/lib/xcp/bin/upgrate_snapshot.sh",snapshotPath, templatePath]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = '10#failed'
> > + return txt
> > +
> > +@echo
> > +def copy_vhd_to_secondarystorage(session, args):
> > + mountpoint = args['mountpoint']
> > + vdiuuid = args['vdiuuid']
> > + sruuid = args['sruuid']
> > + try:
> > + cmd = ["bash",
> "/usr/lib/xcp/bin/copy_vhd_to_secondarystorage.sh", mountpoint, vdiuuid,
> sruuid]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = '10#failed'
> > + return txt
> > +
> > +@echo
> > +def copy_vhd_from_secondarystorage(session, args):
> > + mountpoint = args['mountpoint']
> > + sruuid = args['sruuid']
> > + namelabel = args['namelabel']
> > + try:
> > + cmd = ["bash",
> "/usr/lib/xcp/bin/copy_vhd_from_secondarystorage.sh", mountpoint,
> sruuid, namelabel]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = '10#failed'
> > + return txt
> > +
> > +@echo
> > +def setup_heartbeat_sr(session, args):
> > + host = args['host']
> > + sr = args['sr']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_sr.sh",
> host, sr]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + return txt
> > +
> > +@echo
> > +def setup_heartbeat_file(session, args):
> > + host = args['host']
> > + sr = args['sr']
> > + add = args['add']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/setup_heartbeat_file.sh",
> host, sr, add]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt = ''
> > + return txt
> > +
> > +@echo
> > +def check_heartbeat(session, args):
> > + host = args['host']
> > + interval = args['interval']
> > + try:
> > + cmd = ["bash", "/usr/lib/xcp/bin/check_heartbeat.sh", host,
> interval]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt=''
> > + return txt
> > +
> > +
> > +@echo
> > +def heartbeat(session, args):
> > + '''
> > + host = args['host']
> > + interval = args['interval']
> > + try:
> > + cmd = ["/bin/bash", "/usr/lib/xcp/bin/launch_hb.sh", host,
> interval]
> > + txt = util.pread2(cmd)
> > + except:
> > + txt='fail'
> > + '''
> > + return '> DONE <'
> > +
> > +if __name__ == "__main__":
> > + XenAPIPlugin.dispatch({"forceShutdownVM":forceShutdownVM,
> "upgrade_snapshot":upgrade_snapshot,
> "create_privatetemplate_from_snapshot":create_privatetemplate_from_snap
> shot, "copy_vhd_to_secondarystorage":copy_vhd_to_secondarystorage,
> "copy_vhd_from_secondarystorage":copy_vhd_from_secondarystorage,
> "setup_heartbeat_sr":setup_heartbeat_sr,
> "setup_heartbeat_file":setup_heartbeat_file,
> "check_heartbeat":check_heartbeat, "heartbeat": heartbeat})
> > +
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/server/src/com/cloud/consoleproxy/ConsoleProxy
> ManagerImpl.java
> > ---------------------------------------------------------------------
> -
> > diff --git
> a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> > index b6c1057..f15e45a 100755
> > --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> > +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
> > @@ -1508,7 +1508,9 @@ public class ConsoleProxyManagerImpl implements
> ConsoleProxyManager, ConsoleProx
> > throw new ConfigurationException(msg);
> > }
> > } else {
> > - _serviceOffering = new ServiceOfferingVO("System
> Offering For Console Proxy", 1, DEFAULT_PROXY_VM_RAMSIZE,
> DEFAULT_PROXY_VM_CPUMHZ, 0, 0, false, null, useLocalStorage, true, null,
> true, VirtualMachine.Type.ConsoleProxy, true);
> > + int ramSize =
> NumbersUtil.parseInt(_configDao.getValue("console.ram.size"),
> DEFAULT_PROXY_VM_RAMSIZE);
> > + int cpuFreq =
> NumbersUtil.parseInt(_configDao.getValue("console.cpu.mhz"),
> DEFAULT_PROXY_VM_CPUMHZ);
> > + _serviceOffering = new ServiceOfferingVO("System
> Offering For Console Proxy", 1, ramSize, cpuFreq, 0, 0, false, null,
> useLocalStorage, true, null, true, VirtualMachine.Type.ConsoleProxy,
> true);
> > _serviceOffering.setUniqueName(ServiceOffering.consolePro
> xyDefaultOffUniqueName);
> > _serviceOffering =
> _offeringDao.persistSystemServiceOffering(_serviceOffering);
> >
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/server/src/com/cloud/network/router/VirtualNet
> workApplianceManagerImpl.java
> > ---------------------------------------------------------------------
> -
> > diff --git
> a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImp
> l.java
> b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImp
> l.java
> > index d044d4f..fc8c0f7 100755
> > ---
> a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImp
> l.java
> > +++
> b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImp
> l.java
> > @@ -603,8 +603,8 @@ public class VirtualNetworkApplianceManagerImpl
> implements VirtualNetworkApplian
> > final Map<String, String> configs =
> _configDao.getConfiguration("AgentManager", params);
> >
> > _mgmt_host = configs.get("host");
> > - _routerRamSize =
> NumbersUtil.parseInt(configs.get("router.ram.size"),
> DEFAULT_ROUTER_VM_RAMSIZE);
> > - _routerCpuMHz =
> NumbersUtil.parseInt(configs.get("router.cpu.mhz"),
> DEFAULT_ROUTER_CPU_MHZ);
> > + _routerRamSize =
> NumbersUtil.parseInt(_configDao.getValue("router.ram.size"),
> DEFAULT_ROUTER_VM_RAMSIZE);
> > + _routerCpuMHz =
> NumbersUtil.parseInt(_configDao.getValue("router.cpu.mhz"),
> DEFAULT_ROUTER_CPU_MHZ);
> >
> > _routerExtraPublicNics =
> NumbersUtil.parseInt(_configDao.getValue(Config.RouterExtraPublicNics.k
> ey()), 2);
> >
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/server/src/com/cloud/storage/secondary/Seconda
> ryStorageManagerImpl.java
> > ---------------------------------------------------------------------
> -
> > diff --git
> a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.ja
> va
> b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.ja
> va
> > index 29a9782..0573b65 100755
> > ---
> a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.ja
> va
> > +++
> b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.ja
> va
> > @@ -841,8 +841,10 @@ public class SecondaryStorageManagerImpl
> implements SecondaryStorageVmManager, V
> > throw new ConfigurationException(msg);
> > }
> > } else {
> > + int ramSize =
> NumbersUtil.parseInt(_configDao.getValue("ssvm.ram.size"),
> DEFAULT_SS_VM_RAMSIZE);
> > + int cpuFreq =
> NumbersUtil.parseInt(_configDao.getValue("ssvm.cpu.mhz"),
> DEFAULT_SS_VM_CPUMHZ);
> > _useLocalStorage =
> Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));
> > - _serviceOffering = new ServiceOfferingVO("System
> Offering For Secondary Storage VM", 1, DEFAULT_SS_VM_RAMSIZE,
> DEFAULT_SS_VM_CPUMHZ, null, null, false, null, _useLocalStorage, true,
> null, true, VirtualMachine.Type.SecondaryStorageVm, true);
> > + _serviceOffering = new ServiceOfferingVO("System
> Offering For Secondary Storage VM", 1, ramSize, cpuFreq, null, null,
> false, null, _useLocalStorage, true, null, true,
> VirtualMachine.Type.SecondaryStorageVm, true);
> > _serviceOffering.setUniqueName(ServiceOffering.ssvmDefaul
> tOffUniqueName);
> > _serviceOffering =
> _offeringDao.persistSystemServiceOffering(_serviceOffering);
> >
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/server/src/com/cloud/test/DatabaseConfig.java
> > ---------------------------------------------------------------------
> -
> > diff --git a/server/src/com/cloud/test/DatabaseConfig.java
> b/server/src/com/cloud/test/DatabaseConfig.java
> > index 487e2c5..9ab2868 100755
> > --- a/server/src/com/cloud/test/DatabaseConfig.java
> > +++ b/server/src/com/cloud/test/DatabaseConfig.java
> > @@ -330,6 +330,8 @@ public class DatabaseConfig {
> > s_defaultConfigurationValues.put("system.vm.use.local.storage
> ", "false");
> > s_defaultConfigurationValues.put("use.local.storage",
> "false");
> > s_defaultConfigurationValues.put("init", "false");
> >
> + s_defaultConfigurationValues.put("cpu.overprovisioning.factor",
> "1");
> >
> + s_defaultConfigurationValues.put("mem.overprovisioning.factor",
> "1");
> > }
> >
> > protected DatabaseConfig() {
> >
> > http://git-wip-us.apache.org/repos/asf/incubator-
> cloudstack/blob/5f6387e1/setup/db/clouddev.sql
> > ---------------------------------------------------------------------
> -
> > diff --git a/setup/db/clouddev.sql b/setup/db/clouddev.sql
> > new file mode 100644
> > index 0000000..10ad3b2
> > --- /dev/null
> > +++ b/setup/db/clouddev.sql
> > @@ -0,0 +1,33 @@
> > +-- Licensed to the Apache Software Foundation (ASF) under one
> > +-- or more contributor license agreements. See the NOTICE file
> > +-- distributed with this work for additional information
> > +-- regarding copyright ownership. The ASF licenses this file
> > +-- to you under the Apache License, Version 2.0 (the
> > +-- "License"); you may not use this file except in compliance
> > +-- with the License. You may obtain a copy of the License at
> > +--
> > +-- http://www.apache.org/licenses/LICENSE-2.0
> > +--
> > +-- Unless required by applicable law or agreed to in writing,
> > +-- software distributed under the License is distributed on an
> > +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> > +-- KIND, either express or implied. See the License for the
> > +-- specific language governing permissions and limitations
> > +-- under the License.
> > +
> > +
> > +UPDATE `cloud`.`configuration` SET value = 'true' where name =
> 'use.local.storage';
> > +UPDATE `cloud`.`configuration` SET value = 'true' where name =
> 'system.vm.use.local.storage';
> > +INSERT INTO `cloud`.`disk_offering` (id, name, uuid, display_text,
> created, use_local_storage, type) VALUES (17, 'tinyOffering', UUID(),
> 'tinyOffering', NOW(), 1, 'Service');
> > +INSERT INTO `cloud`.`service_offering` (id, cpu, speed, ram_size)
> VALUES (17, 1, 100, 100);
> > +INSERT INTO `cloud`.`disk_offering` (id, name, uuid, display_text,
> created, type, disk_size) VALUES (18, 'tinyDiskOffering', UUID(),
> 'tinyDiskOffering', NOW(), 'Disk', 1073741824);
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('router.ram.size', '100');
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('router.cpu.mhz','100');
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('console.ram.size','100');
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('console.cpu.mhz', '100');
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('ssvm.ram.size','100');
> > +INSERT INTO `cloud`.`configuration` (name,value)
> VALUE('ssvm.cpu.mhz','100');
> > +UPDATE `cloud`.`configuration` SET value='10' where name =
> 'storage.overprovisioning.factor';
> > +UPDATE `cloud`.`configuration` SET value='10' where name =
> 'cpu.overprovisioning.factor';
> > +UPDATE `cloud`.`configuration` SET value='10' where name =
> 'mem.overprovisioning.factor';
> > +UPDATE `cloud`.`vm_template` SET unique_name="tiny Linux",name="tiny
> Linux",url="http://nfs1.lab.vmops.com/templates/ttylinux_pv.vhd",checks
> um="046e134e642e6d344b34648223ba4bc1",display_text="tiny Linux" where
> id=5;
> >