Skip to content

Instantly share code, notes, and snippets.

@ancorgs
Created November 23, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ancorgs/1d99477acc55c475a150f038d247cf96 to your computer and use it in GitHub Desktop.
Save ancorgs/1d99477acc55c475a150f038d247cf96 to your computer and use it in GitHub Desktop.
#
# Example one:
#
def something
- efi_partition = Yast::Storage.GetEntryForMountpoint("/boot/efi")["device"]
- efi_partition ||= Yast::Storage.GetEntryForMountpoint("/boot")["device"]
- efi_partition ||= Yast::Storage.GetEntryForMountpoint("/")["device"]
- efi_disk = Yast::Storage.GetDiskPartition(efi_partition)["disk"]
+ efi_partition = filesystems.with_mountpoint("/boot/efi").partitions.first
+ efi_partition ||= filesystems.with_mountpoint("/boot").partitions.first
+ efi_partition ||= filesystems.with_mountpoint("/").partitions.first
+ efi_disk = efi_partition.partitionable
efi_disk
end
+ private
+
+ # Filesystems in the staging (planned) devicegraph
+ #
+ # @return [Y2Storage::FilesystemsList]
+ def filesystems
+ staging = Y2Storage::StorageManager.instance.staging
+ staging.filesystems
+ end
#
# Example two:
#
def mbr_is_gpt?
- mbr_storage_object = target_map[mbr_disk]
+ mbr_storage_object = devicegraph.disks.with(name: mbr_disk).first
raise "Cannot find in storage mbr disk #{mbr_disk}" unless mbr_storage_object
- mbr_type = mbr_storage_object["label"]
- mbr_type == "gpt"
+ mbr_storage_object.gpt?
end
private
- def target_map
- @target_map ||= Yast::Storage.GetTargetMap
- end
+ def devicegraph
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example three
#
def activatable_partitions(disk)
- partitions = target_map.fetch(disk, {}).fetch("partitions", [])
+ return [] unless disk
+
# do not select swap and do not select BIOS grub partition
# as it clear its special flags (bnc#894040)
- partitions.select do |p|
- p["used_fs"] != :swap && p["fsid"] != Yast::Partitions.fsid_bios_grub
+ devicegraph.disks.with(name: disk.name).partitions.reject do |part|
+ [Storage::ID_SWAP, Storage::ID_GPT_BIOS].include?(part.id)
end
end
private
- def target_map
- @target_map ||= Yast::Storage.GetTargetMap
- end
+ def devicegraph
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example four:
#
def underlaying_devices_one_level(dev)
- tm = Yast::Storage.GetTargetMap
- disk_data = Yast::Storage.GetDiskPartition(dev)
- if disk?(disk_data)
- disk = Yast::Storage.GetDisk(tm, dev)
- if disk["type"] == :CT_LVM
- res = lvm_underlaying_devices(disk)
- return res.map { |r| Yast::Storage.GetDiskPartition(r)["disk"] }
- end
- # given device is partition
- else
- part = Yast::Storage.GetPartition(tm, dev)
- if part["type"] == :lvm
- lvm_group = Yast::Storage.GetDisk(tm, disk_data["disk"])
- return lvm_underlaying_devices(lvm_group)
- end
- end
+ vgs = devicegraph.volume_groups.with(vg_name: dev)
+ return usable_pvs(vgs).disks.to_a unless vgs.empty?
+
+ lvs = devicegraph.logical_volumes.with(lv_name: dev)
+ return usable_pvs(lvs.vgs).map { |pv| pv.blk_device.name } unless lvs.empty?
+
[]
end
- # returns underlaying devices that creating lvm group
- def lvm_underlaying_devices(lvm_group)
- tm = Yast::Storage.GetTargetMap
- res = devices_on(lvm_group)
- # skip lvm on partiotionless disks as it cannot be used, see bnc#980529
- res.reject { |d| tm[d] }
- end
-
- # returns if given result of GetDiskPartition indicate that it is disk
- def disk?(disk_data)
- disk_data["nr"].to_s.empty?
- end
+ # Physical volumes from a given set of volume groups that are useful for the
+ # bootloader.
+ #
+ # Ignores physical volumes on a whole disk. See bnc#980529
+ #
+ # @param volumes_group_list [Y2Storage::LvmVgsList]
+ # @return [Y2Storage::LvmPvsList]
+ def usable_pvs(volume_groups_list)
+ pvs = volume_groups_list.physical_volumes
+ pvs.with { |pv| !Storage.disk?(pv.blk_device) }
+ end
+
+ def devicegraph
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example five:
#
def boot_partition_on_mbr_disk?
underlaying_boot_partition_devices.any? do |dev|
- pdp = Yast::Storage.GetDiskPartition(dev)
- p_disk = pdp["disk"] || ""
- p_disk == Yast::BootStorage.mbr_disk
+ disk = devicegraph.disks.with(name: dev).first
+ disk ||= devicegraph.partitions.with(name: dev).disks.first
+
+ disk && disk.name == Yast::BootStorage.mbr_disk
end
end
private
+
+ def devicegraph
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example six:
#
def whatever?(current_bl)
targets = current_bl.stage1.devices
- target_map = Yast::Storage.GetTargetMap
- boot_discs = targets.map { |d| Yast::Storage.GetDisk(target_map, d) }
- boot_discs.any? { |d| d["label"] == "gpt" }
+ boot_disks = staging.disks.with(name: targets)
+ boot_disks += staging.partitions.with(name: targets).disks
+
+ boot_disks.any? { |disk| disk.gpt? }
end
+
+ def staging
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example seven:
#
def extended_partition_for(device)
- disk_partition = Yast::Storage.GetDiskPartition(device)
- return nil unless disk_partition["disk"]
-
- target_map = Yast::Storage.GetTargetMap
- disk_map = target_map[disk_partition["disk"]] || {}
- partitions = disk_map["partitions"] || []
- ext_part = partitions.find { |p| p["type"] == :extended }
- return nil unless ext_part
-
- ext_part["device"]
+ disk_list = staging.disks.with(name: device)
+ disk_list ||= staging.partitions.with(name: device).disks
+ return nil if disk_list.empty?
+
+ part = disk_list.partitions.with(type: Storage::PartitionType_EXTENDED).first
+ part ? part.name : nil
end
private
+
+ def staging
+ Y2Storage::StorageManager.instance.staging
+ end
#
# Example eight
#
def init_boot_info
return if @boot_initialized
-
@boot_initialized = true
- boot_disk_map = Yast::Storage.GetTargetMap[Yast::BootStorage.disk_with_boot_partition] || {}
- boot_part = Yast::Storage.GetPartition(Yast::Storage.GetTargetMap,
- Yast::BootStorage.BootPartitionDevice)
- @logical_boot = boot_part["type"] == :logical
- @boot_with_btrfs = boot_part["used_fs"] == :btrfs
+
+ boot_part = devicegraph.partitions.with(name: Yast::BootStorage.BootPartitionDevice).first
+ @logical_boot = boot_part.type == Storage::PartitionType_LOGICAL
+ @boot_with_btrfs = with_btrfs?(boot_part)
# check for sure also underlaying partitions
- (boot_disk_map["partitions"] || []).each do |p|
- @extended = p["device"] if p["type"] == :extended
- next unless underlaying_boot_partition_devices.include?(p["device"])
+ disk_name = Yast::BootStorage.disk_with_boot_partition
+ devicegraph.disks.with(name: disk_name).partitions.each do |p|
+ @extended = p.name if p.type == Storage::PartitionType_EXTENDED
+ next unless underlaying_boot_partition_devices.include?(p.name)
- @boot_with_btrfs = true if p["used_fs"] == :btrfs
- @logical_boot = true if p["type"] == :logical
+ @boot_with_btrfs = with_btrfs?(p)
+ @logical_boot = true if p.type == Storage::PartitionType_LOGICAL
end
end
+
+ def with_btrfs?(partition)
+ partition.filesystem.type == ::Storage::FsType_BTRFS
+ rescue Storage::WrongNumberOfChildren
+ # No filesystem in the partition
+ false
+ end
+
+ def devicegraph
+ Y2Storage::StorageManager.instance.staging
+ end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment