Skip to content

Commit

Permalink
Fix and improve renaming udev rules
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Aug 27, 2019
1 parent 7153318 commit 4a863d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/lib/y2network/udev_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ def find_for(device)
# @param mac [String] MAC address
def new_mac_based_rename(name, mac)
new_network_rule(
[UdevRulePart.new("ATTR{address}", "=", mac), UdevRulePart.new("NAME", "=", name)]
[
# Guard to not try to rename everything with the same MAC address (e.g. vlan devices
# inherit the MAC address from the underlying device).
UdevRulePart.new("KERNEL", "==", "eth*"),
# The port number of a NIC where the ports share the same hardware device.
UdevRulePart.new("ATTR{dev_id}", "==", "0x0"),
UdevRulePart.new("ATTR{address}", "==", mac),
UdevRulePart.new("NAME", "=", name)
]
)
end

Expand All @@ -63,8 +71,8 @@ def new_mac_based_rename(name, mac)
# @param bus_id [String] BUS ID (e.g., "0000:08:00.0")
# @param dev_port [String] Device port
def new_bus_id_based_rename(name, bus_id, dev_port = nil)
parts = [UdevRulePart.new("KERNELS", "=", bus_id)]
parts << UdevRulePart.new("ATTR{dev_port}", "=", dev_port) if dev_port
parts = [UdevRulePart.new("KERNELS", "==", bus_id)]
parts << UdevRulePart.new("ATTR{dev_port}", "==", dev_port) if dev_port
parts << UdevRulePart.new("NAME", "=", name)
new_network_rule(parts)
end
Expand Down
7 changes: 4 additions & 3 deletions test/y2network/udev_rule_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
rule = described_class.new_mac_based_rename("eth0", "01:23:45:67:89:ab")
expect(rule.to_s).to eq(
"SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{type}==\"1\", " \
"ATTR{address}=\"01:23:45:67:89:ab\", NAME=\"eth0\""
"KERNEL==\"eth*\", ATTR{dev_id}==\"0x0\", ATTR{address}==\"01:23:45:67:89:ab\", " \
"NAME=\"eth0\""
)
end
end
Expand All @@ -68,7 +69,7 @@
rule = described_class.new_bus_id_based_rename("eth0", "0000:08:00.0", "1")
expect(rule.to_s).to eq(
"SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{type}==\"1\", " \
"KERNELS=\"0000:08:00.0\", ATTR{dev_port}=\"1\", NAME=\"eth0\""
"KERNELS==\"0000:08:00.0\", ATTR{dev_port}==\"1\", NAME=\"eth0\""
)
end

Expand All @@ -77,7 +78,7 @@
rule = described_class.new_bus_id_based_rename("eth0", "0000:08:00.0")
expect(rule.to_s).to eq(
"SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{type}==\"1\", " \
"KERNELS=\"0000:08:00.0\", NAME=\"eth0\""
"KERNELS==\"0000:08:00.0\", NAME=\"eth0\""
)
end
end
Expand Down

0 comments on commit 4a863d1

Please sign in to comment.