Class: Yast::BootStorageClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
src/modules/BootStorage.rb

Instance Method Summary (collapse)

Instance Method Details

- (String) addMDSettingsToGlobals

FATE#305008: Failover boot configurations for md arrays with redundancy Function prapare disks for synchronizing of md array

Returns:

  • (String)

    includes disks separatet by “,”



916
917
918
919
920
921
# File 'src/modules/BootStorage.rb', line 916

def addMDSettingsToGlobals
  ret = ""

  ret = Builtins.mergestring(@md_physical_disks, ",") if checkMDSettings
  ret
end

- (Object) can_boot_from_partition



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'src/modules/BootStorage.rb', line 848

def can_boot_from_partition
  tm = Storage.GetTargetMap
  partition = @BootPartitionDevice || @RootPartitionDevice

  part = Storage.GetPartition(tm, partition)

  if !part
    Builtins.y2error("cannot find partition #{partition}")
    return false
  end

  fs = part["used_fs"]
  Builtins.y2milestone("FS for boot partition #{fs}")

  # cannot install stage one to xfs as it doesn't have reserved space (bnc#884255)
  return fs != :xfs
end

- (Object) changeOrderInDeviceMapping(device_mapping, bad_devices: [], priority_device: nil)

This function changes order of devices in device_mapping. All devices listed in bad_devices are maped to “hdN” are moved to the end (with changed number N). Priority device are always placed at first place # Example: device_mapping = $[ “/dev/sda” : “hd0”, “/dev/sdb” : “hd1”, “/dev/sdc” : “hd2”, “/dev/sdd” : “hd3”, “/dev/sde” : “hd4” ]; bad_devices = [ “/dev/sda”, “/dev/sdc” ];

 changeOrderInDeviceMapping(device_mapping, bad_devices: bad_devices);
 // returns:
 device_mapping -> $[ "/dev/sda" : "hd3",
                      "/dev/sdb" : "hd0",
                      "/dev/sdc" : "hd4",
                      "/dev/sdd" : "hd1",
                      "/dev/sde" : "hd2" ];


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

def changeOrderInDeviceMapping(device_mapping, bad_devices: [], priority_device: nil)
  log.info("Calling change of device map with #{device_mapping}, " +
    "bad_devices: #{bad_devices}, priority_device: #{priority_device}")
  device_mapping = device_mapping.dup
  first_available_id = 0
  keys = device_mapping.keys
  # sort keys by its order in device mapping
  keys.sort_by! {|k| device_mapping[k][/\d+$/] }

  if priority_device
    # change order of priority device if it is already in device map, otherwise ignore them
    if device_mapping[priority_device]
      first_available_id = 1
      old_first_device = device_mapping.key("hd0")
      old_device_id = device_mapping[priority_device]
      device_mapping[old_first_device] = old_device_id
      device_mapping[priority_device] = "hd0"
    else
      log.warn("Unknown priority device '#{priority_device}'. Skipping")
    end
  end

  # put bad_devices at bottom
  keys.each do |key|
    value = device_mapping[key]
    if !value # FIXME this should not happen, but openQA catch it, so be on safe side
      log.error("empty value in device map")
      next
    end
    # if device is mapped on hdX and this device is _not_ in bad_devices
    if value.start_with?("hd") &&
        !bad_devices.include?(key) &&
        key != priority_device
      # get device name of mapped on "hd"+cur_id
      tmp = device_mapping.key("hd#{first_available_id}")

      # swap tmp and key devices (swap their mapping)
      device_mapping[tmp] = value
      device_mapping[key] = "hd#{first_available_id}"

      first_available_id += 1
    end
  end

  device_mapping
end

- (Object) checkCallingDiskInfo

Check if function was called or storage change partitionig of disk. It is usefull fo using cached data about disk. Data is send to perl-Bootloader and it includes info about partitions, multi path and md-raid

Returns:

  • false if it is posible use cached data



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

def checkCallingDiskInfo
  ret = false

  # fix for problem with unintialized storage library in AutoYaST mode
  # bnc #464090
  if Mode.config && !@storage_initialized
    @storage_initialized = true
    Builtins.y2milestone("Init storage library in yast2-bootloader")
    Storage.InitLibstorage(true)
  end
  if @disk_change_time_checkCallingDiskInfo != Storage.GetTargetChangeTime ||
      Ops.less_than(Builtins.size(@partinfo), 1)
    # save last change time from storage
    @disk_change_time_checkCallingDiskInfo = Storage.GetTargetChangeTime
    Builtins.y2milestone(
      "disk was changed by storage or partinfo is empty: %1",
      Builtins.size(@partinfo)
    )
    Builtins.y2milestone(
      "generate partinfo, md_info, mountpoints and multipath_mapping"
    )
    ret = true
  else
    ret = false
    Builtins.y2milestone(
      "Skip genarating partinfo, md_info, mountpoints and multipath_mapping"
    )
  end

  ret
end

- (Boolean) checkDifferentDisks(devices)

FATE#305008: Failover boot configurations for md arrays with redundancy Check if devices has same partition number and if they are from different disks

Parameters:

  • list (string)

    list of devices

Returns:

  • (Boolean)

    true on success



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

def checkDifferentDisks(devices)
  devices = deep_copy(devices)
  ret = false
  disks = []
  no_partition = ""
  Builtins.foreach(devices) do |dev|
    p_dev = Storage.GetDiskPartition(dev)
    if !Builtins.contains(disks, Ops.get_string(p_dev, "disk", ""))
      disks = Builtins.add(disks, Ops.get_string(p_dev, "disk", ""))
    else
      Builtins.y2milestone(
        "Same disk for md array -> disable synchronize md arrays"
      )
      raise Break
    end
    # add disk from partition to md_physical_disks
    if !Builtins.contains(
        @md_physical_disks,
        Ops.get_string(p_dev, "disk", "")
      )
      @md_physical_disks = Builtins.add(
        @md_physical_disks,
        Ops.get_string(p_dev, "disk", "")
      )
    end
    no_p = Builtins.tostring(Ops.get(p_dev, "nr"))
    if no_p == ""
      Builtins.y2error(
        "Wrong number of partition: %1 from Storage::GetDiskPartition: %2",
        dev,
        p_dev
      )
      raise Break
    end
    if no_partition == ""
      no_partition = no_p
    elsif no_partition == no_p
      ret = true
    else
      Builtins.y2milestone(
        "Different number of partitions -> disable synchronize md arrays"
      )
    end
  end

  Builtins.y2milestone(
    "checkDifferentDisks for devices: %1 return: %2",
    devices,
    ret
  )

  ret
end

- (Object) checkMDDevices(tm, device)

FATE#305008: Failover boot configurations for md arrays with redundancy Check if device are build from 2 partitions with same number but from different disks

Parameters:

  • tm (Hash{String => map})

    taregte map from storage

  • device (String)

    (md device)

Returns:

  • true if device is from 2 partisions with same number and different disks



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

def checkMDDevices(tm, device)
  tm = deep_copy(tm)
  ret = false
  tm_dm = Convert.convert(
    Ops.get(tm, "/dev/md", {}),
    :from => "map",
    :to   => "map <string, any>"
  )

  @md_physical_disks = []
  # find partitions in target map
  Builtins.foreach(Ops.get_list(tm_dm, "partitions", [])) do |p|
    if Ops.get_string(p, "device", "") == device
      if Ops.get_string(p, "raid_type", "") == "raid1"
        p_devices = Ops.get_list(p, "devices", [])
        if Builtins.size(p_devices) == 2
          ret = checkDifferentDisks(p_devices)
        else
          Builtins.y2milestone(
            "Device: %1 doesn't contain 2 partitions: %2",
            device,
            p_devices
          )
        end
      else
        Builtins.y2milestone(
          "Device: %1 is not on raid1: %2",
          device,
          Ops.get_string(p, "raid_type", "")
        )
      end
    end
  end

  if Builtins.size(@md_physical_disks) != 2 ||
      Builtins.contains(@md_physical_disks, "")
    Builtins.y2milestone(
      "device: %1 is based on md_physical_disks: %2 is not valid for enable redundancy",
      device,
      @md_physical_disks
    )
  end

  if ret
    Builtins.y2milestone(
      "device: %1 is based on md_physical_disks: %2 is valid for enable redundancy",
      device,
      @md_physical_disks
    )
  end

  ret
end

- (Object) checkMDRaidDevices(devices, tm)

Check if MD raid is build on disks not on paritions

Parameters:

  • devices (Array<String>)
    • list of devices from MD raid

  • tm (Hash{String => map})
    • unfiltered target map

Returns:

    • true if MD RAID is build on disks (not on partitions)



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'src/modules/BootStorage.rb', line 344

def checkMDRaidDevices(devices, tm)
  devices = deep_copy(devices)
  tm = deep_copy(tm)
  ret = true
  Builtins.foreach(devices) do |key|
    if key != "" && ret
      if Ops.get(tm, key) != nil
        ret = true
      else
        ret = false
      end
    end
  end
  ret
end

- (Boolean) checkMDSettings

FATE#305008: Failover boot configurations for md arrays with redundancy Function check partitions and set redundancy available if partitioning of disk allows it. It means if md array is based on 2 partitions with same number but 2 different disks E.g. /dev/md0 is from /dev/sda1 and /dev/sb1 and /dev/md0 is “/” There is possible only boot from MBR (GRUB not generic boot code)

Returns:

  • (Boolean)

    true on success



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

def checkMDSettings
  ret = false
  tm = Storage.GetTargetMap

  if !Builtins.haskey(tm, "/dev/md")
    Builtins.y2milestone("Doesn't include md raid")
    return ret
  end
  boot_devices = []
  if @BootPartitionDevice != "" && @BootPartitionDevice != nil
    boot_devices = Builtins.add(boot_devices, @BootPartitionDevice)
  end
  if @BootPartitionDevice != @RootPartitionDevice &&
      @RootPartitionDevice != "" &&
      @BootPartitionDevice != nil
    boot_devices = Builtins.add(boot_devices, @RootPartitionDevice)
  end
  if @ExtendedPartitionDevice != "" && @ExtendedPartitionDevice != nil
    boot_devices = Builtins.add(boot_devices, @ExtendedPartitionDevice)
  end

  Builtins.y2milestone(
    "Devices for analyse of redundacy md array: %1",
    boot_devices
  )
  Builtins.foreach(boot_devices) do |dev|
    ret = checkMDDevices(tm, dev)
    if !ret
      Builtins.y2milestone("Skip enable redundancy of md arrays")
      raise Break
    end
  end

  ret
end

- (Object) DisksOrder

Get the order of disks according to BIOS mapping

Returns:

  • a list of all disks in the order BIOS sees them



551
552
553
554
555
556
557
558
559
560
561
# File 'src/modules/BootStorage.rb', line 551

def DisksOrder
  if @device_mapping == nil || Builtins.size(@device_mapping) == 0
    ProposeDeviceMap()
  end
  devmap_rev = Builtins.mapmap(@device_mapping) { |k, v| { v => k } }
  devmap_rev = Builtins.filter(devmap_rev) do |k, v|
    Builtins.substring(k, 0, 2) == "hd"
  end
  order = Builtins.maplist(devmap_rev) { |k, v| v }
  deep_copy(order)
end

- (Object) getKey(value, mapping)

Returns first key from mapping associated with value. Example: map = $[ “a” : “1”, “b” : “2”, “c” : “3”, “d” : “2”]; getDeviceFromMapping(“1”, map) -> “a” getDeviceFromMapping(“2”, map) -> “b”



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'src/modules/BootStorage.rb', line 260

def getKey(value, mapping)
  mapping = deep_copy(mapping)
  ret = ""

  Builtins.foreach(mapping) do |key, val|
    if value == val
      ret = key
      next
    end
  end

  ret
end

- (Object) getPartitionList(type, bl)

Returns list of partitions. Requests current partitioning from yast2-storage and creates list of partition for combobox, menu or other purpose.

Parameters:

  • type (Symbol)

    symbol boot - for bootloader installation root - for kernel root boot_other - for bootable partitions of other systems all - all partitions parts_old - all partitions, except those what will be created during isntallation deleted - all partitions deleted in current proposal kept - all partitions that won't be deleted, new created or formatted destroyed - all partition which are new, deleted or formatted

Returns:

  • a list of strings



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

def getPartitionList(type, bl)
  Builtins.y2milestone("getPartitionList: %1", type)
  devices = Storage.GetTargetMap
  partitions = []
  Builtins.foreach(devices) do |k, v|
    if type == :boot && bl == "grub"
      # check if device is in device map
      if Builtins.haskey(@device_mapping, k) ||
          Builtins.haskey(@device_mapping, ::Bootloader::DeviceMapping.to_mountby_device(k))
        partitions = Convert.convert(
          Builtins.merge(partitions, Ops.get_list(v, "partitions", [])),
          :from => "list",
          :to   => "list <map>"
        )
      end
    else
      partitions = Convert.convert(
        Builtins.merge(partitions, Ops.get_list(v, "partitions", [])),
        :from => "list",
        :to   => "list <map>"
      )
    end
  end

  devices = Builtins.filter(devices) do |k, v|
    Ops.get_symbol(v, "type", :CT_UNKNOWN) != :CT_LVM
  end

  devices = Builtins.filter(devices) do |k, v|
    if Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_DISK ||
        Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_DMRAID
      next true
    else
      next false
    end
  end if type == :boot ||
    type == :boot_other
  all_disks = Builtins.maplist(devices) { |k, v| k }



  if type == :deleted
    return Builtins.maplist(Builtins.filter(partitions) do |p|
      Ops.get_boolean(p, "delete", false)
    end) { |x| Ops.get_string(x, "device", "") }
  elsif type == :destroyed
    return Builtins.maplist(Builtins.filter(partitions) do |p|
      Ops.get_boolean(p, "delete", false) ||
        Ops.get_boolean(p, "format", false) ||
        Ops.get_boolean(p, "create", false)
    end) { |x| Ops.get_string(x, "device", "") }
  end
  partitions = Builtins.filter(partitions) do |p|
    !Ops.get_boolean(p, "delete", false)
  end
  # filter out disk which are not in device map
  all_disks = Builtins.filter(all_disks) do |k|
    if Builtins.haskey(@device_mapping, k) ||
        Builtins.haskey(@device_mapping, ::Bootloader::DeviceMapping.to_mountby_device(k))
      next true
    else
      next false
    end
  end if bl == "grub" &&
    type == :boot
  ret = deep_copy(all_disks)
  if type == :boot_other || type == :root || type == :parts_old ||
      type == :kept
    ret = []
  end

  if type == :boot
    partitions = Builtins.filter(partitions) do |p|
      Ops.get_symbol(p, "type", :primary) == :primary ||
        Ops.get_symbol(p, "type", :primary) == :extended ||
        Ops.get_symbol(p, "type", :primary) == :logical ||
        Ops.get_symbol(p, "type", :primary) == :sw_raid
    end
    # FIXME this checking is performed on 3 places, one function should
    # be developed for it
    partitions = Builtins.filter(partitions) do |p|
      fs = Ops.get_symbol(p, "used_fs", Ops.get(p, "detected_fs"))
      next false if fs == :xfs
      true
    end
  elsif type == :root
    partitions = Builtins.filter(partitions) do |p|
      Ops.get_symbol(p, "type", :primary) != :extended
    end
  elsif type == :parts_old
    partitions = Builtins.filter(partitions) do |p|
      !Ops.get_boolean(p, "create", false)
    end
  elsif type == :kept
    partitions = Builtins.filter(partitions) do |p|
      !(Ops.get_boolean(p, "create", false) ||
        Ops.get_boolean(p, "format", false))
    end
  end
  if type != :all && type != :parts_old && type != :kept
    partitions = Builtins.filter(partitions) do |p|
      Ops.get_string(p, "fstype", "") != "Linux swap"
    end
  end
  partitions = Builtins.filter(partitions) do |p|
    Ops.get_string(p, "fstype", "") == "Linux native" ||
      Ops.get_string(p, "fstype", "") == "Extended" ||
      Ops.get_string(p, "fstype", "") == "Linux RAID" ||
      Builtins.tolower(Ops.get_string(p, "fstype", "")) == "md raid" ||
      Ops.get_string(p, "fstype", "") == "DM RAID"
  end if type == :boot

  partition_names = Builtins.maplist(partitions) do |p|
    Ops.get_string(p, "device", "")
  end
  partition_names = Builtins.filter(partition_names) { |p| p != "" }
  ret = Convert.convert(
    Builtins.union(ret, partition_names),
    :from => "list",
    :to   => "list <string>"
  )
  ret = Builtins.toset(ret)
  deep_copy(ret)
end

- (Object) InitDiskInfo

Function init data for perl-Bootloader about disk It means fullfil md_info, multipath_mapping, partinfo and mountpoints



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

def InitDiskInfo
  if checkCallingDiskInfo
    # delete variables for perl-Bootloader
    @md_info = {}
    @multipath_mapping = {}
    @partinfo = []
    @mountpoints = {}

    tm = Storage.GetTargetMap

    @multipath_mapping = mapRealDevicesToMultipath
    @mountpoints = Builtins.mapmap(
      Convert.convert(
        Storage.GetMountPoints,
        :from => "map",
        :to   => "map <string, list>"
      )
    ) do |k, v|
      # detect all raid1 md devices and mark them in md_info
      device = Ops.get(v, 0)
      if Ops.get_string(v, 3, "") == "raid1"
        Ops.set(@md_info, Convert.to_string(device), [])
      end
      { k => device }
    end
    @mountpoints = Builtins.filter(@mountpoints) do |k, v|
      tmpdir = Convert.to_string(SCR.Read(path(".target.tmpdir")))
      tmp_sz = Builtins.size(tmpdir)
      Ops.is_string?(v) && Builtins.substring(k, 0, tmp_sz) != tmpdir
    end

    Builtins.y2milestone("Detected mountpoints: %1", @mountpoints)

    pi = Builtins.maplist(tm) do |disk, info|
      next [] if Ops.get_symbol(info, "type", :CT_UNKNOWN) == :CT_LVM
      next [] if Ops.get_symbol(info, "type", :CT_UNKNOWN) == :CT_EVMS
      partitions = Ops.get_list(info, "partitions", [])
      parts = Builtins.maplist(
        Convert.convert(partitions, :from => "list", :to => "list <map>")
      ) do |p|
        raid = ""
        if Ops.get_symbol(p, "used_by_type", :UB_NONE) == :UB_MD
          raid = Ops.get_string(p, "used_by_device", "")
        end
        device = Ops.get_string(p, "device", "")
        # We only pass along RAID1 devices as all other causes
        # severe breakage in the bootloader stack
        if raid != ""
          if Builtins.haskey(@md_info, raid)
            members = Ops.get(@md_info, raid, [])
            members = Builtins.add(members, device)
            Ops.set(@md_info, raid, members)
          end
        end
        nr = Ops.get(p, "nr")
        nr = 0 if nr == nil
        nr_str = Builtins.sformat("%1", nr)
        [
          device,
          disk,
          nr_str,
          Builtins.tostring(Ops.get_integer(p, "fsid", 0)),
          Ops.get_string(p, "fstype", "unknown"),
          Builtins.tostring(Ops.get(p, "type")),
          Builtins.tostring(Ops.get_integer(p, ["region", 0], 0)),
          Builtins.tostring(Ops.get_integer(p, ["region", 1], 0))
        ]
      end
      deep_copy(parts)
    end
    @partinfo = Builtins.flatten(pi)
    @partinfo = Builtins.filter(@partinfo) { |p| p != nil && p != [] }
    partinfo_mountby = []
    # adding moundby (by-id) via user preference
    Builtins.foreach(@partinfo) do |partition|
      tmp = []
      mount_by = ::Bootloader::DeviceMapping.to_mountby_device(
        Builtins.tostring(Ops.get_string(partition, 0, ""))
      )
      if mount_by != Builtins.tostring(Ops.get_string(partition, 0, ""))
        tmp = Builtins.add(partition, mount_by)
      else
        tmp = deep_copy(partition)
      end
      partinfo_mountby = Builtins.add(partinfo_mountby, tmp)
    end
    # y2milestone("added mountby: %1", partinfo_mountby);

    @partinfo = deep_copy(partinfo_mountby)
  end

  nil
end

- (Object) isDiskInList(disk, devices)

Function check if disk is in list of devices

Parameters:

  • disk (String)
  • list (string)

    list of devices

Returns:

  • true if success



365
366
367
368
369
370
371
372
373
374
375
# File 'src/modules/BootStorage.rb', line 365

def isDiskInList(disk, devices)
  devices = deep_copy(devices)
  ret = false
  Builtins.foreach(devices) do |dev|
    if dev == disk
      ret = true
      raise Break
    end
  end
  ret
end

- (Object) isDiskInMDRaid(disk, tm)

Check if disk is in MDRaid it means completed disk is used in RAID

Parameters:

  • disk (String)

    (/dev/sda)

  • tm (Hash{String => map})
    • target map

Returns:

    • true if disk (not only part of disk) is in MDRAID



381
382
383
384
385
386
387
388
389
390
391
# File 'src/modules/BootStorage.rb', line 381

def isDiskInMDRaid(disk, tm)
  tm = deep_copy(tm)
  ret = false
  Builtins.foreach(tm) do |dev, d_info|
    if Ops.get(d_info, "type") == :CT_MDPART
      ret = isDiskInList(disk, Ops.get_list(d_info, "devices", []))
    end
    raise Break if ret
  end
  ret
end

- (Object) isHd0(devices)

* helper functions for ProposeDeviceMap: * Returns true if any device from list devices is in device_mapping marked as hd0.



241
242
243
244
245
246
247
248
249
250
# File 'src/modules/BootStorage.rb', line 241

def isHd0(devices)
  devices = deep_copy(devices)
  ret = false

  Builtins.foreach(devices) do |value|
    ret = true if Ops.get(@device_mapping, value, "") == "hd0"
  end

  ret
end

- (Object) main



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

def main

  textdomain "bootloader"

  Yast.import "Storage"
  Yast.import "StorageDevices"
  Yast.import "Arch"
  Yast.import "Mode"


  # Saved change time from target map - only for checkCallingDiskInfo()
  @disk_change_time_checkCallingDiskInfo = nil

  # Storage locked
  @storage_initialized = false

  # device mapping between real devices and multipath
  @multipath_mapping = {}

  # mountpoints for perl-Bootloader
  @mountpoints = {}


  # list of all partitions for perl-Bootloader
  @partinfo = []

  # information about MD arrays for perl-Bootloader
  @md_info = {}

  # device mapping between Linux and firmware
  @device_mapping = {}

  # string sepresenting device name of /boot partition
  # same as RootPartitionDevice if no separate /boot partition
  @BootPartitionDevice = ""


  # string representing device name of / partition
  @RootPartitionDevice = ""

  # string representing device name of extended partition
  @ExtendedPartitionDevice = ""

  # FATE#305008: Failover boot configurations for md arrays with redundancy
  # list <string> includes physical disks used for md raid

  @md_physical_disks = []
end

- (Object) mapRealDevicesToMultipath

FIXME grub only



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'src/modules/BootStorage.rb', line 83

def mapRealDevicesToMultipath
  ret = {}
  tm = Storage.GetTargetMap
  Builtins.foreach(tm) do |disk, disk_info|
    if Ops.get(disk_info, "type") == :CT_DMMULTIPATH
      devices = Ops.get_list(disk_info, "devices", [])
      if Ops.greater_than(Builtins.size(devices), 0)
        Builtins.foreach(devices) { |d| Ops.set(ret, d, disk) }
      end
    end
  end
  deep_copy(ret)
end

- (Object) Md2Partitions(md_device)

Converts the md device to the list of devices building it

Parameters:

  • md_device (String)

    string md device

Returns:

  • a map of devices (from device name to BIOS ID or nil if not detected) building the md device



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'src/modules/BootStorage.rb', line 927

def Md2Partitions(md_device)
  ret = {}
  tm = Storage.GetTargetMap
  tm.each_pair do |disk, descr|
    bios_id = (descr["bios_id"] || 256).to_i # maximum + 1 (means: no bios_id found)
    partitions = descr["partitions"] || []
    partitions.each do |partition|
      if partition["used_by_device"] == md_device
        ret[partition["device"]] = bios_id
      end
    end
  end
  Builtins.y2milestone("Partitions building %1: %2", md_device, ret)

  ret
end

- (Object) propose_s390_device_map



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'src/modules/BootStorage.rb', line 393

def propose_s390_device_map
  # s390 have some special requirements for device map. Keep it short and simple (bnc#884798)
  # TODO device map is not needed at all for s390, so if we get rid of perl-Bootloader translations
  # we can keep it empty
    boot_part = Storage.GetEntryForMountpoint("/boot/zipl")
    boot_part = Storage.GetEntryForMountpoint("/boot") if boot_part.empty?
    boot_part = Storage.GetEntryForMountpoint("/") if boot_part.empty?

    raise "Cannot find boot partition" if boot_part.empty?

    disk = Storage.GetDiskPartition(boot_part["device"])["disk"]

    @device_mapping = { "hd0" => disk }

    Builtins.y2milestone("Detected device mapping: %1", @device_mapping)

    nil
end

- (Object) ProposeDeviceMap

Generate device map proposal, store it in internal variables.

FATE #302075: When user is installing from USB media or any non IDE disk or bios simply set any non IDE disk as first and user is not installing on this removable (non IDE) disk, the order of disks proposed by bios must be changed because of future remove of USB disk. This function must find right place for bootloader (which is most probably boot sector of boot partition (where /boot dir is located)) and change the order of disks in device map. This method is only heuristic because order of disks after remove of usb disk can't be determined by any method.

Algorithm for solving problem with usb disk propsed by bios as hd0: if usbDiskDevice == hd0 && BootDevice != usbDiskDevice: change order of disks in device_mappings to have BootDevice as hd0 FIXME: remove that function from here, as it is grub only



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

def ProposeDeviceMap
  @device_mapping = {}
  @multipath_mapping = {}

  if Mode.config
    Builtins.y2milestone("Skipping device map proposing in Config mode")
    return
  end

  if Arch.s390
    return propose_s390_device_map
  end

  usb_disks = [] # contains usb removable disks as it can affect BIOS order of disks

  targetMap = Storage.GetTargetMap

  # filter out non-disk devices
  targetMap = Builtins.filter(targetMap) do |k, v|
    Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_DMRAID ||
      Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_DISK ||
      Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_DMMULTIPATH ||
      Ops.get_symbol(v, "type", :CT_UNKNOWN) == :CT_MDPART &&
        checkMDRaidDevices(Ops.get_list(v, "devices", []), targetMap)
  end

  # filter out members of BIOS RAIDs and multipath devices
  targetMap = Builtins.filter(targetMap) do |k, v|
    Ops.get(v, "used_by_type") != :UB_DMRAID &&
      Ops.get(v, "used_by_type") != :UB_DMMULTIPATH &&
      (Ops.get(v, "used_by_type") == :UB_MDPART ?
        !isDiskInMDRaid(k, targetMap) :
        true)
  end

  Builtins.y2milestone("Target map: %1", targetMap)

  # add devices with known bios_id
  # collect BIOS IDs which are used
  ids = {}
  Builtins.foreach(targetMap) do |target_dev, target|
    bios_id = Ops.get_string(target, "bios_id", "")
    if bios_id != ""
      index = case Arch.architecture
      when /ppc/
        # on ppc it looks like "vdevice/v-scsi@71000002/@0"
        bios_id[/\d+\z/].to_i
      when "i386", "x86_64"
        Builtins.tointeger(bios_id) - 0x80
      else
        raise "no support for bios id '#{bios_id}' on #{Arch.architecture}"
      end
      grub_dev = Builtins.sformat("hd%1", index)
      # FATE #303548 - doesn't add disk with same bios_id with different name (multipath machine)
      if !Ops.get_boolean(ids, index, false)
        Ops.set(@device_mapping, target_dev, grub_dev)
        Ops.set(ids, index, true)
      end
    end
  end
  # and guess other devices
  # don't use already used BIOS IDs
  Builtins.foreach(targetMap) do |target_dev, target|
    bios_id = Ops.get_string(target, "bios_id", "")
    if bios_id == ""
      index = 0
      while Ops.get_boolean(ids, index, false)
        index = Ops.add(index, 1)
      end
      grub_dev = Builtins.sformat("hd%1", index)
      Ops.set(@device_mapping, target_dev, grub_dev)
      Ops.set(ids, index, true)
    end
  end

  # Fill usb_disks list with usb removable devices.
  #
  # It's not easy to determine how to identify removable usb devices. Now
  # it tests if driver of device is usb-storage. If you find better
  # algorithm how to find removable usb devices, put it here into foreach
  # to apply this algorithm on all devices.
  Builtins.foreach(targetMap) do |target_dev, target|
    driver = Ops.get_string(target, "driver", "")
    if driver == "usb-storage"
      usb_disks = Builtins.add(usb_disks, target_dev)
    end
  end
  Builtins.y2milestone("Found usb discs: %1", usb_disks)

  # change order in device_mapping if usb disk is hd0
  # (FATE #302075)
  if isHd0(usb_disks) &&
      @BootPartitionDevice != getKey("hd0", @device_mapping)
    Builtins.y2milestone("Detected device mapping: %1", @device_mapping)
    Builtins.y2milestone("Changing order in device mapping needed...")
    @device_mapping = changeOrderInDeviceMapping(@device_mapping, bad_devices: usb_disks)
  end

  # For us priority disk is device where /boot or / lives as we control this disk and
  # want to modify its MBR. So we get disk of such partition and change order to add it
  # to top of device map. For details see bnc#887808,bnc#880439
  priority_disks = real_disks_for_partition(@BootPartitionDevice)
  # if none of priority disk is hd0, then choose one and assign it
  if !isHd0(priority_disks)
    @device_mapping = changeOrderInDeviceMapping(@device_mapping,
        priority_device: priority_disks.first)
  end

  Builtins.y2milestone("Detected device mapping: %1", @device_mapping)

  @multipath_mapping = mapRealDevicesToMultipath

  Builtins.y2milestone("Detected multipath mapping: %1", @multipath_mapping)

  nil
end

- (Object) real_disks_for_partition(partition)

returns disk names where partition lives



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

def real_disks_for_partition(partition)
  # FIXME handle somehow if disk are in logical raid
  partitions = Md2Partitions(partition).keys
  partitions = [partition] if partitions.empty?
  res = partitions.map do |partition|
    Storage.GetDiskPartition(partition)["disk"]
  end
  res.uniq!
  # handle LVM disks
  tm = Storage.GetTargetMap
  res = res.reduce([]) do |ret, disk|
    disk_meta = tm[disk]
    if disk_meta
      if disk_meta["lvm2"]
        devices = (disk_meta["devices"] || []) + (disk_meta["devices_add"] || [])
        disks = devices.map { |d| real_disks_for_partition(d) }
        ret.concat(disks.flatten)
      else
        ret << disk
      end
    end
    ret
  end

  res.uniq
end

- (Hash{String => String}) remapDeviceMap(device_map)

Function remap device map to device name (/dev/sda) or to label (ufo_disk)

Parameters:

  • map (string, string)

    device map

Returns:

  • (Hash{String => String})

    new device map



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'src/modules/BootStorage.rb', line 569

def remapDeviceMap(device_map)
  device_map = deep_copy(device_map)
  by_mount = nil
  if Arch.ppc
    by_mount = :id
  else
    by_mount = Storage.GetDefaultMountBy
  end

  #by_mount = `id;
  return deep_copy(device_map) if by_mount == :label

  ret = {}
  # convert device names in device map to the device names by device or label
  ret = Builtins.mapmap(@device_mapping) do |k, v|
    { ::Bootloader::DeviceMapping.to_kernel_device(k) => v }
  end

  deep_copy(ret)
end