-
Notifications
You must be signed in to change notification settings - Fork 34
/
LanItems.rb
2695 lines (2359 loc) · 86.2 KB
/
LanItems.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# ***************************************************************************
#
# Copyright (c) 2012 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# **************************************************************************
require "yast"
require "yaml"
require "network/install_inf_convertor"
require "network/wicked"
module Yast
# Does way too many things.
#
# 1. Aggregates data about network interfaces, both configured
# and unconfigured, in {#Items}, which see.
#
# 2. Provides direct access to individual items of ifcfg files.
# For example BOOTPROTO and STARTMODE are accessible in
# {#bootproto} and {#startmode} (set via {#SetDeviceVars}
# via {#Select} or {#SetItem}). The reverse direction (putting
# the individual values back to an item) is {#Commit}.
#
# 3. ...
#
# FIXME: well this class really is not nice
class LanItemsClass < Module
attr_reader :ipoib_modes
attr_accessor :ipoib_mode
include Logger
include Wicked
def main
Yast.import "UI"
textdomain "network"
Yast.import "NetworkInterfaces"
Yast.import "ProductFeatures"
Yast.import "NetworkConfig"
Yast.import "NetworkStorage"
Yast.import "Host"
Yast.import "Storage"
Yast.import "Directory"
Yast.import "Stage"
Yast.include self, "network/complex.rb"
Yast.include self, "network/routines.rb"
Yast.include self, "network/lan/s390.rb"
Yast.include self, "network/lan/udev.rb"
Yast.include self, "network/lan/bridge.rb"
reset_cache
# Hardware information
# @see #ReadHardware
@Hardware = []
@udev_net_rules = {}
@driver_options = {}
# used at autoinstallation time
@autoinstall_settings = {}
# Data was modified?
# current selected HW
@hw = {}
# Which operation is pending?
@operation = nil
# in special cases when rcnetwork reload is not enought
@force_restart = false
@description = ""
@type = ""
# ifcfg name for the @current device
@device = ""
# FIXME: always empty string - remove all occuriences
@alias = ""
@current = -1
@hotplug = ""
@Requires = []
# address options
# boot protocol: BOOTPROTO
@bootproto = "static"
@ipaddr = ""
@remoteip = ""
@netmask = ""
@prefix = ""
@startmode = "auto"
@ifplugd_priority = "0"
@mtu = ""
@ethtool_options = ""
# wireless options
@wl_mode = ""
@wl_essid = ""
@wl_nwid = ""
@wl_auth_mode = ""
# when adding another key, don't forget the chmod 600 in NetworkInterfaces
@wl_wpa_psk = ""
@wl_key_length = ""
@wl_key = []
@wl_default_key = 0
@wl_nick = ""
# bond options
@bond_slaves = []
@bond_option = ""
# VLAN option
@vlan_etherdevice = ""
@vlan_id = ""
# interfaces attached to bridge (list delimited by ' ')
@bridge_ports = ""
# wl_wpa_eap aggregates the settings in a map for easier CWM access.
#
# **Structure:**
#
# wpa_eap
# WPA_EAP_MODE: string ("TTLS" "PEAP" or "TLS")
# WPA_EAP_IDENTITY: string
# WPA_EAP_PASSWORD: string (for TTLS and PEAP)
# WPA_EAP_ANONID: string (for TTLS and PEAP)
# WPA_EAP_CLIENT_CERT: string (for TLS, file name)
# WPA_EAP_CLIENT_KEY: string (for TLS, file name)
# WPA_EAP_CLIENT_KEY_PASSWORD: string (for TLS)
# WPA_EAP_CA_CERT: string (file name)
# WPA_EAP_AUTH: string ("", "MD5", "GTC", "CHAP"*, "PAP"*, "MSCHAP"*, "MSCHAPV2") (*: TTLS only)
# WPA_EAP_PEAP_VERSION: string ("", "0", "1")
@wl_wpa_eap = {}
@wl_channel = ""
@wl_frequency = ""
@wl_bitrate = ""
@wl_accesspoint = ""
@wl_power = true
@wl_ap_scanmode = ""
# Card Features from hwinfo
# if not provided, we use the default full list
@wl_auth_modes = nil
@wl_enc_modes = nil
@wl_channels = nil
@wl_bitrates = nil
# s390 options
@qeth_portname = ""
@qeth_portnumber = ""
# * ctc as PROTOCOL (or ctc mode, number in { 0, 1, .., 4 }, default: 0)
@chan_mode = "0"
@qeth_options = ""
@ipa_takeover = false
# * iucv as ROUTER (or iucv user, a zVM guest, string of 1 to 8 chars )
@iucv_user = ""
# #84148
# 26bdd00.pdf
# Ch 7: qeth device driver for OSA-Express (QDIO) and HiperSockets
# MAC address handling for IPv4 with the layer2 option
@qeth_layer2 = false
@qeth_macaddress = "00:00:00:00:00:00"
@qeth_chanids = ""
# Timeout for LCS LANCMD
@lcs_timeout = "5"
# aliases
@aliases = {}
# for TUN / TAP devices
@tunnel_set_owner = ""
@tunnel_set_group = ""
# infiniband options
@ipoib_mode = ""
@ipoib_modes = {
# translators: a possible value for: IPoIB device mode
"connected" => _("connected"),
"datagram" => _("datagram")
}
Yast.include self, "network/hardware.rb"
# Default values used when creating an emulated NIC for physical s390 hardware.
@s390_defaults = YAML.load_file(Directory.find_data_file("network/s390_defaults.yml")) if Arch.s390
# the defaults here are what sysconfig defaults to
# (as opposed to what a new interface gets, in {#Select)}
@SysconfigDefaults = YAML.load_file(Directory.find_data_file("network/sysconfig_defaults.yml"))
# this is the map of kernel modules vs. requested firmware
# non-empty keys are firmware packages shipped by SUSE
@request_firmware = YAML.load_file(Directory.find_data_file("network/firmwares.yml"))
end
# Returns configuration of item (see LanItems::Items) with given id.
#
# @param itemId [Integer] a key for {#Items}
def GetLanItem(itemId)
Ops.get_map(@Items, itemId, {})
end
# Returns configuration for currently modified item.
def getCurrentItem
GetLanItem(@current)
end
# Returns true if the item (see LanItems::Items) has
# netconfig configuration.
#
# @param itemId [Integer] a key for {#Items}
def IsItemConfigured(itemId)
ret = !GetLanItem(itemId)["ifcfg"].to_s.empty?
log.info("IsItemConfigured: item=#{itemId} configured=#{ret}")
ret
end
# Returns true if current (see LanItems::current) has
# configuration
def IsCurrentConfigured
IsItemConfigured(@current)
end
# Returns device name for given lan item.
#
# First it looks into the item's netconfig and if it doesn't exist
# it uses device name from hwinfo if available.
#
# @param item_id [Integer] a key for {#Items}
def GetDeviceName(item_id)
lan_item = GetLanItem(item_id)
return lan_item["ifcfg"] if lan_item["ifcfg"]
return lan_item["hwinfo"]["dev_name"] || "" if lan_item["hwinfo"]
log.error("Item #{item_id} has no dev_name nor configuration associated")
"" # this should never happen
end
# Returns name which is going to be used in the udev rule
def current_udev_name
if LanItems.current_renamed?
LanItems.current_renamed_to
else
LanItems.GetItemUdev("NAME")
end
end
# transforms given list of item ids onto device names
#
# item id is index into internal @Items structure
def GetDeviceNames(items)
return [] unless items
items.map { |itemId| GetDeviceName(itemId) }.reject(&:empty?)
end
# Returns device name for current lan item (see LanItems::current)
def GetCurrentName
GetDeviceName(@current)
end
# Returns device type for particular lan item
#
# @param itemId [Integer] a key for {#Items}
def GetDeviceType(itemId)
NetworkInterfaces.GetType(GetDeviceName(itemId))
end
# Returns device type for current lan item (see LanItems::current)
def GetCurrentType
GetDeviceType(@current)
end
# Returns ifcfg configuration for particular item
#
# @param itemId [Integer] a key for {#Items}
def GetDeviceMap(itemId)
return nil if !IsItemConfigured(itemId)
devname = GetDeviceName(itemId)
devtype = NetworkInterfaces.GetType(devname)
Convert.convert(
Ops.get(NetworkInterfaces.FilterDevices("netcard"), [devtype, devname]),
from: "any",
to: "map <string, any>"
)
end
def GetCurrentMap
GetDeviceMap(@current)
end
# Sets item's sysconfig device map to given one
#
# It updates NetworkInterfaces according given map. Map is expected
# to be a hash where both key even value are strings
#
# @param item_id [Integer] a key for {#Items}
def SetDeviceMap(item_id, devmap)
devname = GetDeviceName(item_id)
return false if devname.nil? || devname.empty?
NetworkInterfaces.Change2(devname, devmap, false)
end
# Sets one option in items sysconfig device map
#
# Currently no checks on sysconfig option validity are performed
#
# @param item_id [Integer] a key for {#Items}
def SetItemSysconfigOpt(item_id, opt, value)
devmap = GetDeviceMap(item_id)
return false if devmap.nil?
devmap[opt] = value
SetDeviceMap(item_id, devmap)
end
# Returns udev rule known for particular item
#
# @param itemId [Integer] a key for {#Items}
def GetItemUdevRule(itemId)
Ops.get_list(GetLanItem(itemId), ["udev", "net"], [])
end
def ReadUdevDriverRules
Builtins.y2milestone("Reading udev rules ...")
@udev_net_rules = Convert.convert(
SCR.Read(path(".udev_persistent.net")),
from: "any",
to: "map <string, any>"
)
Builtins.y2milestone("Reading driver options ...")
Builtins.foreach(SCR.Dir(path(".modules.options"))) do |driver|
pth = Builtins.sformat(".modules.options.%1", driver)
Builtins.foreach(
Convert.convert(
SCR.Read(Builtins.topath(pth)),
from: "any",
to: "map <string, string>"
)
) do |key, value|
Ops.set(
@driver_options,
driver,
Builtins.sformat(
"%1%2%3=%4",
Ops.get_string(@driver_options, driver, ""),
if Ops.greater_than(
Builtins.size(Ops.get_string(@driver_options, driver, "")),
0
)
" "
else
""
end,
key,
value
)
)
end
end
true
end
def getUdevFallback
udev_rules = Ops.get_list(getCurrentItem, ["udev", "net"], [])
if IsEmpty(udev_rules)
udev_rules = GetDefaultUdevRule(
GetCurrentName(),
Ops.get_string(getCurrentItem, ["hwinfo", "mac"], "")
)
Builtins.y2milestone(
"No Udev rules found, creating default: %1",
udev_rules
)
end
deep_copy(udev_rules)
end
# It returns a value for the particular key of udev rule belonging to the current item.
def GetItemUdev(key)
udev_key_value(getUdevFallback, key)
end
# It replaces a tuple identified by replace_key in current item's udev rule
#
# Note that the tuple is identified by key only. However modification flag is
# set only if value was changed (in case when replace_key == new_key)
#
# It also contain a logic on tuple operators. When the new_key is "NAME"
# then assignment operator (=) is used. Otherwise equality operator (==) is used.
# Thats bcs this function is currently used for touching "NAME", "KERNELS" and
# "ATTR{address}" keys only
#
# @param replace_key [string] udev key which identifies tuple to be replaced
# @param new_key [string] new key to by used
# @param new_val [string] value for new key
# @return updated rule when replace_key is found, current rule otherwise
def ReplaceItemUdev(replace_key, new_key, new_val)
# = for assignment
# == for equality checks
operator = new_key == "NAME" ? "=" : "=="
current_rule = getUdevFallback
rule = RemoveKeyFromUdevRule(getUdevFallback, replace_key)
new_rule = AddToUdevRule(rule, "#{new_key}#{operator}\"#{new_val}\"")
log.info("ReplaceItemUdev: new udev rule = #{new_rule}")
if current_rule.sort != new_rule.sort
SetModified()
Items()[@current]["udev"] = { "net" => {} } if !Items()[@current]["udev"]
Items()[@current]["udev"]["net"] = new_rule
end
deep_copy(new_rule)
end
# Updates device name.
#
# It updates device's udev rules and config name.
# Updating config name means that old configuration is deleted from
# the system.
#
# @param itemId [Integer] a key for {#Items}
#
# Returns new name
def SetItemName(itemId, name)
lan_items = LanItems.Items
if name && !name.empty?
if lan_items[itemId]["udev"]
updated_rule = update_udev_rule_key(GetItemUdevRule(itemId), "NAME", name)
lan_items[itemId]["udev"]["net"] = updated_rule
end
else
# rewrite rule for empty name is meaningless
lan_items[itemId].delete("udev")
end
if lan_items[itemId].key?("ifcfg")
NetworkInterfaces.Delete2(lan_items[itemId]["ifcfg"])
lan_items[itemId]["ifcfg"] = name.to_s
end
name
end
# Updates current device name.
#
# It updates device's udev rules and config name.
# Updating config name means that old configuration is deleted from
# the system.
#
# Returns new name
def SetCurrentName(name)
SetItemName(@current, name)
end
# Sets new device name for current item
def rename(name)
if GetCurrentName() != name
@Items[@current]["renamed_to"] = name
SetModified()
else
@Items[@current].delete("renamed_to")
end
end
# Returns new name for current item
#
# @param item_id [Integer] a key for {#Items}
def renamed_to(item_id)
@Items[item_id]["renamed_to"]
end
def current_renamed_to
renamed_to(@current)
end
# Tells if current item was renamed
#
# @param item_id [Integer] a key for {#Items}
def renamed?(item_id)
return false if !LanItems.Items[item_id].key?("renamed_to")
renamed_to(item_id) != GetDeviceName(item_id)
end
def current_renamed?
renamed?(@current)
end
# Writes udev rules for all items.
#
# Currently only interesting change is renaming interface.
def WriteUdevItemsRules
# loop over all items and checks if device name has changed
net_rules = []
Builtins.foreach(
Convert.convert(
Map.Keys(@Items),
from: "list",
to: "list <integer>"
)
) do |key|
item_udev_net = GetItemUdevRule(key)
next if IsEmpty(item_udev_net)
dev_name = Ops.get_string(@Items, [key, "hwinfo", "dev_name"], "")
@current = key
if dev_name != GetItemUdev("NAME")
# when changing device name you have a choice
# - change kernel "match rule", or
# - remove it completely
# removing is less error prone when tracking name changes, so it was chosen.
item_udev_net = RemoveKeyFromUdevRule(item_udev_net, "KERNEL")
# setting links down during AY is forbidden bcs it can freeze ssh installation
SetLinkDown(dev_name) if !Mode.autoinst
@force_restart = true
end
net_rules = Builtins.add(
net_rules,
Builtins.mergestring(item_udev_net, ", ")
)
end
Builtins.y2milestone("write net udev rules: %1", net_rules)
write_update_udevd(net_rules)
true
end
def WriteUdevDriverRules
udev_drivers_rules = {}
Builtins.foreach(
Convert.convert(
Map.Keys(@Items),
from: "list",
to: "list <integer>"
)
) do |key|
driver = Ops.get_string(@Items, [key, "udev", "driver"], "")
if IsNotEmpty(driver)
modalias = Ops.get_string(@Items, [key, "hwinfo", "modalias"], "")
driver_rule = []
driver_rule = AddToUdevRule(
driver_rule,
Builtins.sformat("ENV{MODALIAS}==\"%1\"", modalias)
)
driver_rule = AddToUdevRule(
driver_rule,
Builtins.sformat("ENV{MODALIAS}=\"%1\"", driver)
)
Ops.set(udev_drivers_rules, driver, driver_rule)
end
end
Builtins.y2milestone("write drivers udev rules: %1", udev_drivers_rules)
SCR.Write(path(".udev_persistent.drivers"), udev_drivers_rules)
# write rules from driver
Builtins.foreach(
Convert.convert(
@driver_options,
from: "map <string, any>",
to: "map <string, string>"
)
) do |key, value|
val = {}
Builtins.foreach(Builtins.splitstring(value, " ")) do |k|
l = Builtins.splitstring(k, "=")
Ops.set(val, Ops.get(l, 0, ""), Ops.get(l, 1, ""))
end
val = nil if IsEmpty(value)
SCR.Write(Builtins.add(path(".modules.options"), key), val)
end
SCR.Write(path(".modules"), nil)
nil
end
def WriteUdevRules
WriteUdevItemsRules()
WriteUdevDriverRules()
# wait so that ifcfgs written in NetworkInterfaces are newer
# (1-second-wise) than netcontrol status files,
# and rcnetwork reload actually works (bnc#749365)
SCR.Execute(path(".target.bash"), "udevadm settle")
sleep(1)
nil
end
def write
renamed_items = @Items.keys.select { |item_id| renamed?(item_id) }
renamed_items.each do |item_id|
devmap = GetDeviceMap(item_id)
# change configuration name if device is configured
NetworkInterfaces.Change2(renamed_to(item_id), devmap, false) if devmap
SetItemName(item_id, renamed_to(item_id))
end
LanItems.WriteUdevRules if !Stage.cont && InstallInfConvertor.instance.AllowUdevModify
# FIXME: hack: no "netcard" filter as biosdevname names it diferently (bnc#712232)
NetworkInterfaces.Write("")
end
# Exports configuration for use in AY profile
#
# TODO: it currently exports only udevs (as a consequence of dropping LanUdevAuto)
# so once it is extended, all references has to be checked
def export(devices)
export_udevs(devices)
end
# Function which returns if the settings were modified
# @return [Boolean] settings were modified
def GetModified
@modified
end
# Function sets internal variable, which indicates, that any
# settings were modified, to "true"
def SetModified
@modified = true
nil
end
def AddNew
@current = @Items.to_h.size
@Items[@current] = { "commited" => false }
@operation = :add
nil
end
# return list of available modules for current device
# with default default_module (on first possition)
def GetItemModules(default_module)
mods = []
mods = Builtins.add(mods, default_module) if IsNotEmpty(default_module)
Builtins.foreach(
Ops.get_list(@Items, [@current, "hwinfo", "drivers"], [])
) do |row|
tmp_mod = Ops.get_string(row, ["modules", 0, 0], "")
mods = Builtins.add(mods, tmp_mod) if !Builtins.contains(mods, tmp_mod)
end
deep_copy(mods)
end
# Searches map of known devices and decides if referenced lan item
# can be enslaved in a bond device
#
# @param [String] bondMaster name of master device
# @param [Fixnum] itemId index into LanItems::Items
# TODO: Check for valid configurations. E.g. bond device over vlan
# is nonsense and is not supported by netconfig.
# Also devices enslaved in a bridge should be excluded too.
def IsBondable(bondMaster, itemId)
ret = true
devname = GetDeviceName(itemId)
bonded = BuildBondIndex()
# check if the device is L2 capable
if Arch.s390
s390_config = s390_ReadQethConfig(devname)
# only devices with L2 support can be enslaved in bond. See bnc#719881
ret &&= s390_config["QETH_LAYER2"] == "yes"
end
ifcfg = GetDeviceMap(itemId)
itemBondMaster = bonded[devname] || ""
if !itemBondMaster.empty? && bondMaster != itemBondMaster
log.debug("IsBondable: excluding lan item (#{itemId}: #{devname}) for #{GetCurrentName()} - is already bonded")
return false
end
return ret if ifcfg.nil?
# filter the eth devices (BOOTPROTO=none)
# don't care about STARTMODE (see bnc#652987c6)
ret &&= ifcfg["BOOTPROTO"] == "none"
ret
end
# Decides if given lan item can be enslaved in a bridge.
#
# @param [String] bridgeMaster name of master device
# @param [Fixnum] itemId index into LanItems::Items
# TODO: bridgeMaster is not used yet bcs detection of bridge master
# for checked device is missing.
def IsBridgeable(_bridgeMaster, itemId)
ifcfg = GetDeviceMap(itemId)
# no netconfig configuration has been found so nothing
# blocks using the device as bridge slave
return true if ifcfg.nil?
devname = GetDeviceName(itemId)
bonded = BuildBondIndex()
if bonded[devname]
log.debug("Excluding lan item (#{itemId}: #{devname}) - is bonded")
return false
end
devtype = GetDeviceType(itemId)
# exclude forbidden configurations
case devtype
when "br"
log.debug("Excluding lan item (#{itemId}: #{devname}) - is bridge")
return false
when "tun"
log.debug("Excluding lan item (#{itemId}: #{devname}) - is tun")
return false
end
case ifcfg["STARTMODE"]
when "nfsroot"
log.debug("Excluding lan item (#{itemId}: #{devname}) - is nfsroot")
return false
when "ifplugd"
log.debug("Excluding lan item (#{itemId}: #{devname}) - ifplugd")
return false
else
return true
end
end
# Iterates over all items and lists those for which given validator returns
# true.
#
# @param [boolean (string, integer)] validator a reference to function which checks if an interface
# can be enslaved. Validator takes one argument - itemId.
# @return [Array] of lan item ids (see LanItems::Items)
def GetSlaveCandidates(master, validator)
validator = deep_copy(validator)
if validator.nil?
Builtins.y2error("GetSlaveCandidates: needs a validator.")
return []
end
if IsEmpty(master)
Builtins.y2error("GetSlaveCandidates: master device name is required.")
return []
end
result = []
LanItems.Items.each do |itemId, _attribs|
if @current != itemId && validator.call(master, itemId)
result = Builtins.add(result, itemId)
else
Builtins.y2debug(
"GetSlaveCandidates: validation failed for item (%1), current (%2)",
itemId,
@current
)
end
end
Builtins.y2milestone(
"GetSlaveCandidates: candidates for enslaving: %1",
result
)
deep_copy(result)
end
# Creates list of items (interfaces) which can be used as
# a bond slave.
#
# @param [String] bondMaster bond device name
def GetBondableInterfaces(bondMaster)
GetSlaveCandidates(
bondMaster,
fun_ref(method(:IsBondable), "boolean (string, integer)")
)
end
# Creates list of items (interfaces) which can be used as
# a bridge slave.
#
# @param [String] bridgeMaster bridge device name
def GetBridgeableInterfaces(bridgeMaster)
GetSlaveCandidates(
bridgeMaster,
fun_ref(method(:IsBridgeable), "boolean (string, integer)")
)
end
# Creates list of all known netcard items
#
# It means list of item ids of all netcards which are detected and/or
# configured in the system
def GetNetcardInterfaces
@Items.keys
end
# Creates list of names of all known netcards configured even unconfigured
def GetNetcardNames
GetDeviceNames(GetNetcardInterfaces())
end
# get list of all configurations for "netcard" macro in NetworkInterfaces module
def getNetworkInterfaces
configurations = NetworkInterfaces.FilterDevices("netcard")
devtypes = NetworkInterfaces.CardRegex["netcard"].to_s.split("|")
devtypes.inject([]) do |acc, type|
conf = configurations[type].to_h
acc.concat(conf.keys)
end
end
# Finds item_id by device name
#
# If an item is associated with config file of given name (ifcfg-<device>)
# then its id is returned
#
# @param [String] device name (e.g. eth0)
# @return index in Items or nil
def find_configured(device)
@Items.select { |_k, v| v["ifcfg"] == device }.keys.first
end
def FindAndSelect(device)
item_id = find_configured(device)
@current = item_id if item_id
!item_id.nil?
end
# search all known devices to find it's index in Items array
#
# @param [String] device matched with item[ "hwinfo", "dev_name"]
# @return index in Items or -1 if not found
def FindDeviceIndex(device)
ret = -1
Builtins.foreach(
Convert.convert(
@Items,
from: "map <integer, any>",
to: "map <integer, map <string, any>>"
)
) do |i, a|
if Ops.get_string(a, ["hwinfo", "dev_name"], "") == device
ret = i
raise Break
end
end
ret
end
# It finds a new style device name for device name in old fashioned format
#
# It goes through currently present devices and tries to mach it to given
# old fashioned name
#
# @returns [String] new style name in case of success. Given name otherwise.
def getDeviceName(oldname)
newname = oldname
hardware = ReadHardware("netcard")
hardware.each do |hw|
hw_dev_name = hw["dev_name"] || ""
hw_dev_mac = hw["mac"] || ""
hw_dev_busid = hw["busid"] || ""
case oldname
when /.*-id-#{hw_dev_mac}/i
log.info("device by ID found: #{oldname}")
newname = hw_dev_name
when /.*-bus-#{hw_dev_busid}/i
log.info("device by BUS found #{oldname}")
newname = hw_dev_name
end
end
log.info("nothing changed, #{newname} is old style dev_name") if oldname == newname
newname
end
# preinitializates @Items according info on physically detected network cards
def ReadHw
@Items = {}
@Hardware = ReadHardware("netcard")
# Hardware = [$["active":true, "bus":"pci", "busid":"0000:02:00.0", "dev_name":"wlan0", "drivers":[$["active":true, "modprobe":true, "modules":[["ath5k" , ""]]]], "link":true, "mac":"00:22:43:37:55:c3", "modalias":"pci:v0000168Cd0000001Csv00001A3Bsd00001026bc02s c00i00", "module":"ath5k", "name":"AR242x 802.11abg Wireless PCI Express Adapter", "num":0, "options":"", "re quires":[], "sysfs_id":"/devices/pci0000:00/0000:00:1c.1/0000:02:00.0", "type":"wlan", "udi":"/org/freedeskto p/Hal/devices/pci_168c_1c", "wl_auth_modes":["open", "sharedkey", "wpa-psk", "wpa-eap"], "wl_bitrates":nil, " wl_channels":["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], "wl_enc_modes":["WEP40", "WEP104", "T KIP", "CCMP"]], $["active":true, "bus":"pci", "busid":"0000:01:00.0", "dev_name":"eth0", "drivers":[$["active ":true, "modprobe":true, "modules":[["atl1e", ""]]]], "link":false, "mac":"00:23:54:3f:7c:c3", "modalias":"pc i:v00001969d00001026sv00001043sd00008324bc02sc00i00", "module":"atl1e", "name":"L1 Gigabit Ethernet Adapter", "num":1, "options":"", "requires":[], "sysfs_id":"/devices/pci0000:00/0000:00:1c.3/0000:01:00.0", "type":"et h", "udi":"/org/freedesktop/Hal/devices/pci_1969_1026", "wl_auth_modes":nil, "wl_bitrates":nil, "wl_channels" :nil, "wl_enc_modes":nil]];
ReadUdevDriverRules()
udev_drivers_rules = Convert.convert(
SCR.Read(path(".udev_persistent.drivers")),
from: "any",
to: "map <string, any>"
)
Builtins.foreach(@Hardware) do |hwitem|
udev_net = if Ops.get_string(hwitem, "dev_name", "") != ""
Ops.get_list(
@udev_net_rules,
Ops.get_string(hwitem, "dev_name", ""),
[]
)
else
[]
end
mod = Builtins.deletechars(
Ops.get(
Builtins.splitstring(
Ops.get(
Ops.get_list(
udev_drivers_rules,
Ops.get_string(hwitem, "modalias", ""),
[]
),
1,
""
),
"="
),
1,
""
),
"\""
)
Ops.set(
@Items,
Builtins.size(@Items),
"hwinfo" => hwitem,
"udev" => { "net" => udev_net, "driver" => mod }
)
end
nil
end
# initializates @Items
#
# It does:
# (1) read hardware present on the system
# (2) read known configurations (e.g. ifcfg-eth0)
# (3) joins together. Join is done via device name (e.g. eth0) as key.
# It is full outer join in -> you can have hwinfo part with no coresponding
# netconfig part (or vice versa) in @Items when the method is done.
def Read
reset_cache
ReadHw()
NetworkInterfaces.Read
NetworkInterfaces.CleanHotplugSymlink