1
# Copyright (C) 2015 Canonical Ltd.
3
# Author: Ryan Harper <ryan.harper@canonical.com>
5
# Curtin is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU Affero General Public License as published by the
7
# Free Software Foundation, either version 3 of the License, or (at your
8
# option) any later version.
10
# Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
15
# You should have received a copy of the GNU Affero General Public License
16
# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
19
def compose_udev_equality(key, value):
20
"""Return a udev comparison clause, like `ACTION=="add"`."""
21
assert key == key.upper()
22
return '%s=="%s"' % (key, value)
25
def compose_udev_attr_equality(attribute, value):
26
"""Return a udev attribute comparison clause, like `ATTR{type}=="1"`."""
27
assert attribute == attribute.lower()
28
return 'ATTR{%s}=="%s"' % (attribute, value)
31
def compose_udev_setting(key, value):
32
"""Return a udev assignment clause, like `NAME="eth0"`."""
33
assert key == key.upper()
34
return '%s="%s"' % (key, value)
37
def generate_udev_rule(interface, mac):
38
"""Return a udev rule to set the name of network interface with `mac`.
40
The rule ends up as a single line looking something like:
42
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
43
ATTR{address}="ff:ee:dd:cc:bb:aa", NAME="eth0"
46
compose_udev_equality('SUBSYSTEM', 'net'),
47
compose_udev_equality('ACTION', 'add'),
48
compose_udev_equality('DRIVERS', '?*'),
49
compose_udev_attr_equality('address', mac),
50
compose_udev_setting('NAME', interface),
54
# vi: ts=4 expandtab syntax=python