Class: Yast::NetHwDetectionClass

Inherits:
Module
  • Object
show all
Defined in:
src/modules/NetHwDetection.rb

Instance Method Summary collapse

Instance Method Details

#DuplicateIP(ip) ⇒ Object

Duplicate IP detection

See Also:

  • ip(8)

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
# File 'src/modules/NetHwDetection.rb', line 196

def DuplicateIP(ip)
  # missing param for arping. Arping does nothing in such case only
  # floods logs.
  return false if ip.nil? || ip.empty?

  command = "LC_ALL=C /usr/sbin/ip link show | /usr/bin/grep BROADCAST | /usr/bin/grep -v NOARP | /usr/bin/cut -d: -f2"
  exe = SCR.Execute(path(".target.bash_output"), command)
  ifs = Ops.get_string(exe, "stdout", "")
  ifsl = Builtins.filter(Builtins.splitstring(ifs, " \t\n")) { |i| i != "" }

  # #45169: must only probe the interface being set up
  # but I don't know which one it is :-(
  # so pretend there are no dups if we have more interfaces
  return false if Ops.greater_than(Builtins.size(ifsl), 1)

  # find the interface that detects the dup
  ifc = Builtins.find(ifsl) do |ifname|
    # no need to be quiet, diagnostics is good
    command = "/usr/sbin/arping  -D -c2 -w3 -I#{ifname.shellescape} #{ip.shellescape}"
    # 0 no dup, 1 dup, 2 other error (eg. ifc not up, #182473)
    SCR.Execute(path(".target.bash"), command) == 1
  end

  !ifc.nil?
end

#LoadNetModulesObject

Set up the first eth interface, if not already running WATCH OUT, this is the place where modules are loaded


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
# File 'src/modules/NetHwDetection.rb', line 99

def LoadNetModules
  Builtins.y2milestone("Network detection prepare")

  hardware = ReadHardware("netcard")

  Builtins.y2debug("Hardware=%1", hardware)
  return false if Ops.less_than(Builtins.size(hardware), 1)

  needed_modules = Builtins.listmap(hardware) do |h|
    # Formerly we simply modprobed the first module of the first
    # driver, if it was not already loaded.  But if the user
    # configured the card to use the second driver and unloads it
    # and wants to load the first, it will not work because the
    # first driver is already loaded but not bound to the device
    # (the second one took it). N#59794#c31
    # We will only load a driver if there's no driver for the card active.
    active_driver = Builtins.find(Ops.get_list(h, "drivers", [])) do |d|
      Ops.get_boolean(d, "active", false)
    end
    { Ops.get_string(h, "module", "") => active_driver.nil? }
    # TODO: list of todos
    # 1: choose which driver to load
    # 2: load all its modules: no cards use multiple modules
    # 3: either modprobe or insmod: ISA history
  end
  needed_modules = Builtins.filter(needed_modules) do |m, load|
    load && !m.nil? && m != "" &&
      SCR.Execute(
        path(".target.bash"),
        Builtins.sformat("/usr/bin/grep ^%1 /proc/modules", m.shellescape)
      ) != 0
  end
  @detection_modules = Builtins.maplist(needed_modules) { |m, _a| m }
  Package.InstallKernel(Builtins.maplist(@detection_modules) do |m|
    Ops.add(m, ".ko")
  end)
  Builtins.foreach(@detection_modules) do |mod|
    Builtins.y2milestone("Loading module: %1", mod)
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat("/usr/sbin/modprobe --use-blacklist %1 2>&1", mod.shellescape)
    )
  end

  Builtins.y2milestone("Network detection prepare (end)")

  true
end

#mainObject


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
# File 'src/modules/NetHwDetection.rb', line 64

def main
  Yast.import "UI"
  textdomain "network"

  Yast.import "Directory"
  Yast.import "Package"
  Yast.import "String"

  # yuck, FIXME
  # this is here just because network/hardware.ycp references it
  # because of detection and module loading (StartEthInterface)
  # general stuff
  @description = ""
  @type = ""
  @unique = ""
  @hotplug = ""
  @Requires = []

  Yast.include self, "network/hardware.rb"

  # Detection result
  # (in dhcpcd-<i>interface</i>.info format)
  @result = {}

  # True, if detection is running
  @running = false

  @tmpdir = Directory.tmpdir

  @detection_modules = []
end

#ResolveIP(ip) ⇒ Object

this function is moved here just to kee it out of the tangle of includes which will be torn apart in the next release. dependency hell. Resolve IP to hostname


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'src/modules/NetHwDetection.rb', line 228

def ResolveIP(ip)
  # quick check to avoid timeout
  if Builtins.size(ip) == 0 ||
      Convert.to_integer(
        SCR.Execute(
          path(".target.bash"),
          Builtins.sformat("/usr/bin/grep -q %1 /etc/hosts", ip.shellescape)
        )
      ) != 0
    return ""
  end

  command = "/usr/bin/getent hosts #{ip.shellescape} | /usr/bin/sed \"s/^[0-9.: \t]\\+//g\""
  getent = SCR.Execute(path(".target.bash_output"), command)
  hnent = Ops.get_string(getent, "stdout", "")
  Builtins.y2debug("%1", hnent)
  hnent = String.FirstChunk(hnent, " \t\n")
  hnent = "" if hnent.nil?
  Builtins.y2debug("'%1'", hnent)
  String.CutBlanks(hnent)
end

#StartObject

Start the detection


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'src/modules/NetHwDetection.rb', line 150

def Start
  if @running == true
    Builtins.y2error("Detection already running")
    return false
  end

  Builtins.y2milestone(
    "IFCONFIG1: %1",
    SCR.Execute(path(".target.bash_output"), "/usr/sbin/ip addr show")
  )
  ret = false
  if LoadNetModules()
    @running = true
    ret = true
  end

  Builtins.y2milestone(
    "IFCONFIG2: %1",
    SCR.Execute(path(".target.bash_output"), "/usr/sbin/ip addr show")
  )
  Builtins.y2milestone("Detection start result: %1", ret)
  ret
end

#StopObject

Stop the detection


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'src/modules/NetHwDetection.rb', line 176

def Stop
  if @running != true
    Builtins.y2error("Detection not running")
    return false
  end
  @running = false

  Builtins.y2milestone(
    "IFCONFIG3: %1",
    SCR.Execute(path(".target.bash_output"), "/usr/sbin/ip addr show")
  )

  Builtins.y2milestone("Detection stop ")
  true
end