Class: Yast::NewIDClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) Activate

Activate value stored in the internal list

Returns:

  • (Boolean)

    True if all settings were successfuly set



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

def Activate
  ret = true

  Builtins.foreach(@new_ids) do |newid|
    modulename = Ops.get_string(newid, "driver", "")
    sysdir = Ops.get_string(newid, "sysdir", "")
    # load kernel module if it isn't already loaded
    if modulename != nil && modulename != ""
      ModuleLoading.Load(
        modulename,
        "", # TODO allow setting of module args?
        # vendor is empty, device name is unknown
        "",
        _("Unknown device"),
        Linuxrc.manual,
        true
      )
    end
    sysdir = modulename if sysdir == nil || sysdir == ""
    targetfile = Builtins.sformat("/sys/bus/pci/drivers/%1/new_id", sysdir)
    # create ID string passed to the driver
    idstring = FormatActivationString(newid)
    # check whether target file exists
    filesize = Convert.to_integer(
      SCR.Read(path(".target.size"), targetfile)
    )
    if Ops.greater_or_equal(filesize, 0)
      # set the new value
      set = Convert.to_integer(
        SCR.Execute(
          path(".target.bash"),
          Builtins.sformat(
            "echo '%1' > '%2'",
            String.Quote(idstring),
            String.Quote(targetfile)
          )
        )
      ) == 0

      if !set
        Builtins.y2error(
          "Setting the new id failed: driver: %1, value: %2",
          targetfile,
          idstring
        )
        ret = false
      else
        Builtins.y2milestone(
          "File %1 - new PCI ID '%2' was succesfully set",
          targetfile,
          idstring
        )
      end
    else
      # Error message
      Report.Error(
        Builtins.sformat(
          _("File '%1' does not exist. Cannot set new PCI ID."),
          targetfile
        )
      )
      ret = false
    end
  end if @new_ids != nil

  ret
end

- (Object) AddID(new_id)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File '../../src/modules/NewID.rb', line 54

def AddID(new_id)
  new_id = deep_copy(new_id)
  # initialize list if needed
  @new_ids = [] if @new_ids == nil

  if new_id != nil && new_id != {}
    if @new_ids == nil
      @new_ids = [new_id]
      @refresh_proposal = true

      if Builtins.contains(@removed_ids, new_id)
        # remove added id from removed list
        @removed_ids = Builtins.filter(@removed_ids) { |i| i != new_id }
      end
    elsif !Builtins.contains(@new_ids, new_id)
      @new_ids = Builtins.add(@new_ids, new_id)
      @refresh_proposal = true
    end
  end

  nil
end

- (Object) AddIDs(id)



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

def AddIDs(id)
  id = deep_copy(id)
  newid = deep_copy(id)

  if Builtins.haskey(newid, "uniq")
    # add device/vendor values from PCI scan for selected PCI device
    Builtins.foreach(GetPCIdevices()) do |pcidev|
      if Ops.get_string(pcidev, "unique_key", "") ==
          Ops.get_string(newid, "uniq", "")
        Builtins.y2debug("Found PCI device: %1", pcidev)
        # libhd uses 0x10000 offset for PCI devices
        if Builtins.haskey(pcidev, "device_id")
          Ops.set(
            newid,
            "device",
            Builtins.tohexstring(
              Ops.subtract(Ops.get_integer(pcidev, "device_id", 0), 65536)
            )
          )
        end
        if Builtins.haskey(pcidev, "sub_device_id")
          Ops.set(
            newid,
            "subdevice",
            Builtins.tohexstring(
              Ops.subtract(
                Ops.get_integer(pcidev, "sub_device_id", 0),
                65536
              )
            )
          )
        end
        if Builtins.haskey(pcidev, "vendor_id")
          Ops.set(
            newid,
            "vendor",
            Builtins.tohexstring(
              Ops.subtract(Ops.get_integer(pcidev, "vendor_id", 0), 65536)
            )
          )
        end
        if Builtins.haskey(pcidev, "sub_vendor_id")
          Ops.set(
            newid,
            "subvendor",
            Builtins.tohexstring(
              Ops.subtract(
                Ops.get_integer(pcidev, "sub_vendor_id", 0),
                65536
              )
            )
          )
        end
      end
    end
  end

  deep_copy(newid)
end

- (Object) FormatActivationString(newid)



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

def FormatActivationString(newid)
  newid = deep_copy(newid)
  # create ID string which is passed to the driver
  ret = ""

  pci_any_id = "ffffffff"
  default_class = "0"
  default_mask = "0"

  newid = AddIDs(newid) if Builtins.haskey(newid, "uniq")

  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "class_mask", "")),
    ret,
    default_mask
  )
  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "class", "")),
    ret,
    default_class
  )
  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "subdevice", "")),
    ret,
    pci_any_id
  )
  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "subvendor", "")),
    ret,
    pci_any_id
  )
  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "device", "")),
    ret,
    pci_any_id
  )
  ret = prepend_option(
    remove_hex_prefix(Ops.get_string(newid, "vendor", "")),
    ret,
    pci_any_id
  )

  ret
end

- (Object) GetModelString(uniq)



659
660
661
662
663
664
665
666
667
668
669
670
# File '../../src/modules/NewID.rb', line 659

def GetModelString(uniq)
  ret = ""

  Builtins.foreach(GetPCIdevices()) do |d|
    if Ops.get_string(d, "unique_key", "") == uniq
      ret = Ops.get_string(d, "model", "")
    end
  end 


  ret
end

- (Object) GetNewID(index)



101
102
103
# File '../../src/modules/NewID.rb', line 101

def GetNewID(index)
  Ops.get(@new_ids, index, {})
end

- (Object) GetNewIDs



97
98
99
# File '../../src/modules/NewID.rb', line 97

def GetNewIDs
  deep_copy(@new_ids)
end

- (Object) GetPCIdevices



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File '../../src/modules/NewID.rb', line 37

def GetPCIdevices
  if @pcidevices == nil
    # initialize list
    @pcidevices = Convert.convert(
      SCR.Read(path(".probe.pci")),
      :from => "any",
      :to   => "list <map>"
    )

    # still nil, set to empty - avoid reprobing next time
    @pcidevices = [] if @pcidevices == nil
  end

  deep_copy(@pcidevices)
end

- (Object) HwcfgFileName(newid)



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

def HwcfgFileName(newid)
  newid = deep_copy(newid)
  ret = ""

  newid = AddIDs(newid) if Builtins.haskey(newid, "uniq")

  vendor = remove_hex_prefix(Ops.get_string(newid, "vendor", ""))
  device = remove_hex_prefix(Ops.get_string(newid, "device", ""))

  if Ops.greater_than(Builtins.size(vendor), 0) &&
      Ops.greater_than(Builtins.size(device), 0)
    ret = Builtins.sformat("vpid-%1-%2", vendor, device)

    subvendor = remove_hex_prefix(Ops.get_string(newid, "subvendor", ""))
    subdevice = remove_hex_prefix(Ops.get_string(newid, "subdevice", ""))

    if Ops.greater_than(Builtins.size(subvendor), 0) &&
        Ops.greater_than(Builtins.size(subdevice), 0)
      ret = Builtins.sformat("%1-%2-%3", ret, subvendor, subdevice)
    end
  end

  Builtins.y2debug("activation string: %1", ret)
  ret
end

- (Object) main



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File '../../src/modules/NewID.rb', line 15

def main
  Yast.import "UI"
  Yast.import "String"
  Yast.import "Report"
  Yast.import "ModuleLoading"
  Yast.import "Linuxrc"
  Yast.import "FileUtils"

  Yast.include self, "hwinfo/routines.rb"
  textdomain "tune"

  # list of configured PCI IDs
  @new_ids = nil
  @removed_ids = []

  # cache .probe.pci values
  @pcidevices = nil

  @refresh_proposal = false
  @configfile = "/etc/sysconfig/hardware/newids"
end

- (Array) MakeProposal

Return new ID description

Returns:

  • (Array)

    (string) list of hardware desciptions



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

def MakeProposal
  ret = []

  Builtins.foreach(@new_ids) do |newid|
    modulename = Ops.get_string(newid, "driver", "")
    sysdir = Ops.get_string(newid, "sysdir", "")
    idstring = FormatActivationString(newid)
    targetfile = sysdir != "" ? sysdir : modulename
    # test for installation proposal
    # %1 - name of kernel driver (e.g. e100)
    # %2 - PCI ID (hexnumbers)
    info = Builtins.sformat(
      _("Driver: %1, New PCI ID: %2"),
      targetfile,
      idstring
    )
    if Builtins.haskey(newid, "uniq")
      model = GetModelString(Ops.get_string(newid, "uniq", ""))

      if model != nil && model != ""
        info = Ops.add(info, Builtins.sformat(" (%1)", model))
      end
    end
    ret = Builtins.add(ret, info)
  end if Ops.greater_than(
    Builtins.size(@new_ids),
    0
  )

  Builtins.y2milestone("NewID proposal: %1", ret)

  # proposal is valid
  @refresh_proposal = false

  deep_copy(ret)
end

- (Object) prepend_option(newopt, opts, defval)

Prepend option to PCI ID string, use default value if required

Parameters:

  • newopt (String)

    Prepend this option

  • opts (String)

    Already existing option string

  • defval (String)

    Default value, used when newopt is empty



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File '../../src/modules/NewID.rb', line 282

def prepend_option(newopt, opts, defval)
  return "" if opts == "" && newopt == ""

  if Ops.greater_than(Builtins.size(opts), 0)
    return Ops.add(
      Ops.add(
        Ops.greater_than(Builtins.size(newopt), 0) ? newopt : defval,
        " "
      ),
      opts
    )
  else
    return newopt
  end
end

- (Object) Read(filename)



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

def Read(filename)
  if filename != nil && filename != ""
    @new_ids = []

    # read file
    file = nil
    if FileUtils.Exists(filename)
      file = Convert.to_string(SCR.Read(path(".target.string"), filename))
    else
      Builtins.y2milestone("File %1 does not exist yet", filename)
    end

    return false if file == nil

    lines = Builtins.splitstring(file, "\n")

    Builtins.y2debug("lines: %1", lines)
    comment = []

    # parse lines
    Builtins.foreach(lines) do |line|
      line = String.CutBlanks(line)
      if Builtins.regexpmatch(line, "^#.*")
        # line is a comment
        comment = Builtins.add(comment, line)
      else
        parts = Builtins.splitstring(line, ",")

        driver = Ops.get(parts, 1)
        sysdir = Ops.get(parts, 2)

        # parse newid line
        # replace tabs by spaces
        line = Builtins.mergestring(
          Builtins.splitstring(Ops.get(parts, 0, ""), "\t"),
          " "
        )

        idparts = Builtins.splitstring(line, " ")

        idparts = Builtins.filter(idparts) do |part|
          part != nil && part != ""
        end

        vendor = Ops.get(idparts, 0)
        device = Ops.get(idparts, 1)
        subvendor = Ops.get(idparts, 2)
        subdevice = Ops.get(idparts, 3)
        _class = Ops.get(idparts, 4)
        class_mask = Ops.get(idparts, 5)
        driver_data = Ops.get(idparts, 6)

        newid = {}

        # search for existing PCI card if class is not specified
        if class_mask == nil && _class == nil && vendor != nil &&
            device != nil
          vid = nil
          did = nil
          svid = nil
          sdid = nil

          if vendor != nil
            vid = Builtins.tointeger(
              !has_hex_prefix(vendor) ? Ops.add("0x", vendor) : vendor
            )
          end
          if device != nil
            did = Builtins.tointeger(
              !has_hex_prefix(device) ? Ops.add("0x", device) : device
            )
          end
          if subvendor != nil
            svid = Builtins.tointeger(
              !has_hex_prefix(subvendor) ?
                Ops.add("0x", subvendor) :
                subvendor
            )
          end
          if subdevice != nil
            sdid = Builtins.tointeger(
              !has_hex_prefix(subdevice) ?
                Ops.add("0x", subdevice) :
                subdevice
            )
          end

          Builtins.y2debug("vid: %1", vid)
          Builtins.y2debug("did: %1", did)
          Builtins.y2debug("svid: %1", svid)
          Builtins.y2debug("sdid: %1", sdid)

          Builtins.foreach(GetPCIdevices()) do |dev|
            # check ID
            if vid ==
                Ops.subtract(Ops.get_integer(dev, "vendor_id", 0), 65536) &&
                did ==
                  Ops.subtract(Ops.get_integer(dev, "device_id", 0), 65536)
              # some devices don't have subdevice, subvendor
              if Builtins.haskey(dev, "sub_vendor_id") &&
                  Builtins.haskey(dev, "sub_device_id")
                if svid ==
                    Ops.subtract(
                      Ops.get_integer(dev, "sub_vendor_id", 0),
                      65536
                    ) &&
                    sdid ==
                      Ops.subtract(
                        Ops.get_integer(dev, "sub_device_id", 0),
                        65536
                      )
                  Ops.set(
                    newid,
                    "uniq",
                    Ops.get_string(dev, "unique_key", "")
                  )
                end
              else
                Ops.set(
                  newid,
                  "uniq",
                  Ops.get_string(dev, "unique_key", "")
                )
              end
            end
          end
        end

        if !Builtins.haskey(newid, "uniq")
          Ops.set(newid, "vendor", vendor) if vendor != nil
          Ops.set(newid, "device", device) if device != nil
          Ops.set(newid, "subvendor", subvendor) if subvendor != nil
          Ops.set(newid, "subdevice", subdevice) if subdevice != nil
          Ops.set(newid, "class", _class) if _class != nil
          Ops.set(newid, "class_mask", class_mask) if class_mask != nil
        end

        Ops.set(newid, "driver_data", driver_data) if driver_data != nil

        Ops.set(newid, "driver", driver) if driver != nil
        Ops.set(newid, "sysdir", sysdir) if sysdir != nil
        if Ops.greater_than(Builtins.size(comment), 0)
          Ops.set(newid, "comment", comment)
        end

        Builtins.y2milestone("read newid: %1", newid)

        @new_ids = Builtins.add(@new_ids, newid) if newid != {}

        comment = []
      end
    end 


    Builtins.y2milestone("Read settings: %1", @new_ids)

    return true
  end
  false
end

- (Object) RefreshProposal



113
114
115
# File '../../src/modules/NewID.rb', line 113

def RefreshProposal
  @refresh_proposal
end

- (Object) RemoveExistingFile(fname)



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File '../../src/modules/NewID.rb', line 528

def RemoveExistingFile(fname)
  ret = true

  if fname != nil && fname != ""
    # remove old config file if it exists
    if Ops.greater_than(SCR.Read(path(".target.size"), fname), 0)
      res = Convert.to_integer(
        SCR.Execute(path(".target.bash"), Ops.add("/bin/rm ", fname))
      )

      if res != 0
        Builtins.y2warning(
          "Removing of file %1 has failed, exit: %2",
          fname,
          res
        )
      else
        Builtins.y2milestone("Removed file: %1", fname)
      end
    end
  end

  ret
end

- (Object) RemoveID(index)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File '../../src/modules/NewID.rb', line 77

def RemoveID(index)
  removed_id = Ops.get(@new_ids, index, {})

  @new_ids = Builtins.remove(@new_ids, index)
  @refresh_proposal = true

  # add to removed
  if removed_id != nil && removed_id != {} &&
      !Builtins.contains(@removed_ids, removed_id)
    @removed_ids = Builtins.add(@removed_ids, removed_id)

    if Builtins.contains(@new_ids, removed_id)
      # remove deleted id from list of new
      @new_ids = Builtins.filter(@new_ids) { |i| i != removed_id }
    end
  end

  nil
end

- (Object) SetNewID(nid, index)



105
106
107
108
109
110
111
# File '../../src/modules/NewID.rb', line 105

def SetNewID(nid, index)
  nid = deep_copy(nid)
  Ops.set(@new_ids, index, nid)
  @refresh_proposal = true

  nil
end

- (Object) Write



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

def Write
  Builtins.y2milestone("Writing PCI ID cofiguration...")

  ret = true

  # content of /etc/sysconfig/hardware/newids
  sysconfig = ""

  # map ID commands to driver
  settings = {}

  # handle removed configurations - remove all modprobe entries
  if Ops.greater_than(Builtins.size(@removed_ids), 0)
    drvs = SCR.Dir(path(".modprobe_newid.install"))

    if drvs != nil && Ops.greater_than(Builtins.size(drvs), 0)
      Builtins.foreach(drvs) do |d|
        SCR.Write(Builtins.add(path(".modprobe_newid.install"), d), nil)
      end
    end
  end

  Builtins.foreach(@new_ids) do |newid|
    modulename = Ops.get_string(newid, "driver", "")
    sysdir = Ops.get_string(newid, "sysdir", "")
    idstring = FormatActivationString(newid)
    # write settings to /etc/modprobe.d/50-newid.conf if the module is known
    # (the module is not compiled into the kernel)
    if modulename != ""
      targetfile = sysdir != "" ? sysdir : modulename
      install_string = Builtins.sformat(
        "echo '%1' > '/sys/bus/pci/drivers/%2/new_id'",
        String.Quote(idstring),
        String.Quote(targetfile)
      )

      current = Ops.get(settings, modulename, [])
      current = Builtins.add(current, install_string)
      Ops.set(settings, modulename, current)
    end
    # write hwcfg file to load the driver
    WriteHwcfg(newid)
    # add to /etc/sysconfig/hardware/newids
    if Builtins.haskey(newid, "comment")
      # add the comment
      sysconfig = Ops.add(
        Ops.add(
          sysconfig,
          Builtins.mergestring(Ops.get_list(newid, "comment", []), "\n")
        ),
        "\n"
      )
    end
    sysconfig = Ops.add(
      Ops.add(Ops.add(sysconfig, idstring), ","),
      modulename
    )
    sysconfig = Ops.add(Ops.add(sysconfig, ","), sysdir) if sysdir != ""
    # add trailing newline
    sysconfig = Ops.add(sysconfig, "\n")
  end if @new_ids != nil

  # write sysconfig settings
  if Ops.greater_than(Builtins.size(sysconfig), 0)
    # write sysconfig file
    ret = ret && SCR.Write(path(".target.string"), @configfile, sysconfig)
  else
    # remove old config file if it exists
    RemoveExistingFile(@configfile)
  end

  # write modprobe settings
  if Ops.greater_than(Builtins.size(settings), 0)
    Builtins.foreach(settings) do |modulename, values|
      install_string = Builtins.sformat(
        "/sbin/modprobe --ignore-install %1; %2",
        modulename,
        Builtins.mergestring(values, "; ")
      )
      ret = ret &&
        SCR.Write(
          Builtins.add(path(".modprobe_newid.install"), modulename),
          install_string
        )
    end 


    # flush changes
    SCR.Write(path(".modprobe_newid"), nil)
  end

  # handle removed configurations - remove hwcfg files
  if Ops.greater_than(Builtins.size(@removed_ids), 0)
    Builtins.foreach(@removed_ids) do |rem|
      fname = HwcfgFileName(rem)
      if fname != ""
        # remove the file
        fname = Ops.add("/etc/sysconfig/hardware/hwcfg-", fname)
        RemoveExistingFile(fname)
      end
    end
  end

  ret
end

- (Object) WriteHwcfg(newid)



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

def WriteHwcfg(newid)
  newid = deep_copy(newid)
  ret = false
  cfgname = HwcfgFileName(newid)
  driver = Ops.get_string(newid, "driver", "")

  Builtins.y2debug("newid: %1", newid)
  Builtins.y2debug("cfgname: %1", cfgname)
  Builtins.y2debug("driver: %1", driver)

  if cfgname != "" && driver != ""
    # prepare hwcfg values
    startmode = "auto"
    module_options = ""

    p = Ops.add(path(".sysconfig.hardware.value"), Builtins.topath(cfgname))

    # write the values
    SCR.Write(Ops.add(p, path(".MODULE")), driver)
    SCR.Write(Ops.add(p, path(".STARTMODE")), startmode)
    SCR.Write(Ops.add(p, path(".MODULE_OPTIONS")), module_options)

    # flush the changes
    SCR.Write(path(".sysconfig.hardware"), nil)
  end

  ret
end