]>
Commit | Line | Data |
---|---|---|
15ef32ea RP |
1 | #!/usr/bin/python |
2 | ||
3 | # This should be pretty simple and might not really even need to exist. | |
4 | # The key is that we need to call link_create with a type of "dummy" | |
5 | # since that will translate to 'ip link add loopbackX type dummy' | |
6 | # The config file should probably just indicate that the type is | |
7 | # loopback or dummy. | |
8 | ||
9 | from ifupdown.iface import * | |
10 | from ifupdownaddons.modulebase import moduleBase | |
11 | from ifupdownaddons.iproute2 import iproute2 | |
12 | import logging | |
13 | ||
14 | class loopback(moduleBase): | |
15 | _modinfo = {'mhelp' : 'configure extra loopback module based on ' + | |
16 | 'dummy device' } | |
17 | ||
18 | def __init__(self, *args, **kargs): | |
19 | moduleBase.__init__(self, *args, **kargs) | |
20 | self.ipcmd = None | |
21 | ||
22 | def _is_loopback_by_name(self, ifacename): | |
23 | return 'loop' in ifacename | |
24 | ||
25 | def _up(self, ifaceobj): | |
26 | if self._is_loopback_by_name(ifaceobj.name): | |
27 | self.ipcmd.link_create(ifaceobj.name, 'dummy') | |
28 | ||
29 | def _down(self, ifaceobj): | |
30 | if not self.PERFMODE and not self.ipcmd.link_exists(ifaceobj.name): | |
31 | return | |
32 | try: | |
33 | self.ipcmd.link_delete(ifaceobj.name) | |
34 | except Exception, e: | |
35 | self.log_warn(str(e)) | |
36 | ||
37 | def _query_check(self, ifaceobj, ifaceobjcurr): | |
38 | if not self.ipcmd.link_exists(ifaceobj.name): | |
15ef32ea RP |
39 | return |
40 | ||
41 | _run_ops = {'pre-up' : _up, | |
42 | 'post-down' : _down, | |
43 | 'query-checkcurr' : _query_check} | |
44 | ||
45 | def get_ops(self): | |
46 | return self._run_ops.keys() | |
47 | ||
48 | def _init_command_handlers(self): | |
49 | if not self.ipcmd: | |
50 | self.ipcmd = iproute2(**self.get_flags()) | |
51 | ||
84ca006f | 52 | def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args): |
15ef32ea RP |
53 | op_handler = self._run_ops.get(operation) |
54 | if not op_handler: | |
55 | return | |
56 | if (operation != 'query-running' and | |
57 | not self._is_loopback_by_name(ifaceobj.name)): | |
58 | return | |
59 | self._init_command_handlers() | |
60 | if operation == 'query-checkcurr': | |
61 | op_handler(self, ifaceobj, query_ifaceobj) | |
62 | else: | |
63 | op_handler(self, ifaceobj) |