|
6 | 6 | # to you under the Apache License, Version 2.0 (the |
7 | 7 | # "License"); you may not use this file except in compliance |
8 | 8 | # with the License. You may obtain a copy of the License at |
9 | | -# |
| 9 | +# |
10 | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
11 | | -# |
| 11 | +# |
12 | 12 | # Unless required by applicable law or agreed to in writing, |
13 | 13 | # software distributed under the License is distributed on an |
14 | 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | 15 | # KIND, either express or implied. See the License for the |
16 | 16 | # specific language governing permissions and limitations |
17 | 17 | # under the License. |
18 | | -import sys |
| 18 | + |
| 19 | +import logging |
19 | 20 | import re |
| 21 | +import sys |
20 | 22 | from xml.dom.minidom import parse |
21 | 23 | from cloudutils.configFileOps import configFileOps |
22 | 24 | from cloudutils.networkConfig import networkConfig |
| 25 | + |
| 26 | +logging.basicConfig(filename='/var/log/libvirt/qemu-hook.log', |
| 27 | + filemode='a', |
| 28 | + format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', |
| 29 | + datefmt='%H:%M:%S', |
| 30 | + level=logging.INFO) |
| 31 | +logger = logging.getLogger('qemu-hook') |
| 32 | + |
23 | 33 | def isOldStyleBridge(brName): |
24 | 34 | if brName.find("cloudVirBr") == 0: |
25 | 35 | return True |
26 | 36 | else: |
27 | 37 | return False |
| 38 | + |
28 | 39 | def isNewStyleBridge(brName): |
29 | 40 | if brName.startswith('brvx-'): |
30 | 41 | return False |
31 | 42 | if re.match(r"br(\w+)-(\d+)", brName) == None: |
32 | 43 | return False |
33 | 44 | else: |
34 | 45 | return True |
| 46 | + |
35 | 47 | def getGuestNetworkDevice(): |
36 | | - netlib = networkConfig() |
| 48 | + netlib = networkConfig() |
37 | 49 | cfo = configFileOps("/etc/cloudstack/agent/agent.properties") |
38 | 50 | guestDev = cfo.getEntry("guest.network.device") |
39 | 51 | enslavedDev = netlib.getEnslavedDev(guestDev, 1) |
40 | 52 | return enslavedDev.split(".")[0] |
| 53 | + |
41 | 54 | def handleMigrateBegin(): |
42 | 55 | try: |
43 | 56 | domain = parse(sys.stdin) |
44 | 57 | for interface in domain.getElementsByTagName("interface"): |
45 | 58 | source = interface.getElementsByTagName("source")[0] |
46 | 59 | bridge = source.getAttribute("bridge") |
47 | 60 | if isOldStyleBridge(bridge): |
48 | | - vlanId = bridge.replace("cloudVirBr","") |
| 61 | + vlanId = bridge.replace("cloudVirBr", "") |
49 | 62 | elif isNewStyleBridge(bridge): |
50 | | - vlanId = re.sub(r"br(\w+)-","",bridge) |
| 63 | + vlanId = re.sub(r"br(\w+)-", "", bridge) |
51 | 64 | else: |
52 | 65 | continue |
53 | 66 | phyDev = getGuestNetworkDevice() |
54 | | - newBrName="br" + phyDev + "-" + vlanId |
| 67 | + newBrName = "br" + phyDev + "-" + vlanId |
55 | 68 | source.setAttribute("bridge", newBrName) |
56 | 69 | print(domain.toxml()) |
57 | 70 | except: |
58 | 71 | pass |
| 72 | + |
| 73 | + |
59 | 74 | if __name__ == '__main__': |
60 | 75 | if len(sys.argv) != 5: |
61 | 76 | sys.exit(0) |
62 | 77 |
|
63 | | - if sys.argv[2] == "migrate" and sys.argv[3] == "begin": |
64 | | - handleMigrateBegin() |
| 78 | + # For docs refer https://libvirt.org/hooks.html#qemu |
| 79 | + logger.debug("Executing qemu hook with args: %s" % sys.argv) |
| 80 | + action, status = sys.argv[2:4] |
| 81 | + |
| 82 | + if action == "migrate" and status == "begin": |
| 83 | + handleMigrateBegin() |
0 commit comments