Skip to content

Commit

Permalink
Merge pull request #1373 from joseivanlopez/extract-device-description
Browse files Browse the repository at this point in the history
Extract device description and filesystem label
  • Loading branch information
joseivanlopez authored Mar 8, 2024
2 parents 5707135 + b67ea7d commit b3e7d0d
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 503 deletions.
8 changes: 8 additions & 0 deletions package/yast2-storage-ng.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
-------------------------------------------------------------------
Thu Mar 7 16:26:04 UTC 2024 - José Iván López González <jlopez@suse.com>

- Share the logic for generating the description of a device and
the label of a file system.
- Needed for (gh#openSUSE/agama#1079).
- 5.0.8

-------------------------------------------------------------------
Mon Mar 4 14:36:58 UTC 2024 - Ancor Gonzalez Sosa <ancor@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-storage-ng.spec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

Name: yast2-storage-ng
Version: 5.0.7
Version: 5.0.8
Release: 0
Summary: YaST2 - Storage Configuration
License: GPL-2.0-only OR GPL-3.0-only
Expand Down
37 changes: 3 additions & 34 deletions src/lib/y2partitioner/widgets/columns/filesystem_label.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) [2020] SUSE LLC
# Copyright (c) [2020-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand All @@ -19,6 +19,7 @@

require "yast"
require "y2partitioner/widgets/columns/base"
require "y2storage/filesystem_label"

module Y2Partitioner
module Widgets
Expand All @@ -39,45 +40,13 @@ def title

# @see Columns::Base#value_for
def value_for(device)
return fstab_filesystem_label(device) if fstab_entry?(device)

filesystem_label(device)
Y2Storage::FilesystemLabel.new(device, system_graph: system_graph).to_s
end

# @see Columns::Base#id
def id
:label
end

private

# Returns the label for the given device, when possible
#
# @param device [Y2Storage::Device, nil]
# @return [String] the label if possible; empty string otherwise
def filesystem_label(device)
return "" unless device
return "" if device.is?(:btrfs_subvolume)

filesystem = filesystem_for(device)

return "" unless filesystem
return "" if part_of_multidevice?(device, filesystem)
# fs may not support labels, like NFS
return "" unless filesystem.respond_to?(:label)

filesystem.label
end

# Returns the label for the given fstab entry, when possible
#
# @see #filesystem_label
# @param fstab_entry [Y2Storage::SimpleEtcFstabEntry]
def fstab_filesystem_label(fstab_entry)
device = fstab_entry.device(system_graph)

filesystem_label(device)
end
end
end
end
Expand Down
231 changes: 4 additions & 227 deletions src/lib/y2partitioner/widgets/columns/type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) [2020] SUSE LLC
# Copyright (c) [2020-2024] SUSE LLC
#
# All Rights Reserved.
#
Expand All @@ -20,6 +20,7 @@
require "yast"
require "y2partitioner/icons"
require "y2partitioner/widgets/columns/base"
require "y2storage/device_description"

module Y2Partitioner
module Widgets
Expand All @@ -45,34 +46,6 @@ class Type < Base
}
private_constant :DEVICE_ICONS

# Default labels based on the device type
#
# @see #default_label
DEVICE_LABELS = {
bcache: N_("Bcache"),
disk: N_("Disk"),
dasd: N_("Disk"),
multipath: N_("Multipath"),
nfs: N_("NFS"),
bios_raid: N_("BIOS RAID"),
software_raid: N_("RAID"),
lvm_pv: N_("PV"),
lvm_vg: N_("LVM"),
lvm_thin: N_("Thin LV"),
lvm_thin_pool: N_("Thin Pool"),
lvm_raid: N_("RAID LV"),
lvm_cache: N_("Cache LV"),
lvm_cache_pool: N_("Cache Pool"),
lvm_writecache: N_("Writecache LV"),
lvm_snapshot: N_("Snapshot LV"),
lvm_mirror: N_("Mirror LV"),
lvm_lv: N_("LV"),
stray: N_("Xen"),
partition: N_("Partition"),
btrfs_subvolume: N_("Btrfs Subvolume")
}
private_constant :DEVICE_LABELS

# Constructor
def initialize
super
Expand Down Expand Up @@ -128,208 +101,12 @@ def fstab_device_icon(fstab_entry)

# A text describing the given device
#
# @see DEVICE_ICONS
# @see DeviceDescription
#
# @param device [Y2Storage::Device, Y2Storage::LvmPv, Y2Storage::SimpleEtcFstabEntry]
# @return [String]
def device_label(device)
return fstab_device_label(device) if fstab_entry?(device)
return device.type.to_human_string if device.is?(:filesystem)
return default_label(device) if device.is?(:lvm_vg, :btrfs_subvolume)
return snapshot_type_label(device) if device.is?(:lvm_snapshot)

formatted_device_type_label(device) || unformatted_device_type_label(device)
end

# Label for the device in the given fstab entry
#
# @param fstab_entry [Y2Storage::SimpleEtcFstabEntry]
# @return [String] the #device_label if device is found in the system; empty string otherwise
def fstab_device_label(fstab_entry)
device = fstab_entry.device(system_graph)
device ? device_label(device) : ""
end

# Label for a formatted device (e.g., Ext4 LVM, XFS RAID, Swap Partition, etc)
#
# @return [String, nil] label if the device has a filesystem; nil otherwise
def formatted_device_type_label(device)
fs = filesystem_for(device)

return nil unless fs

if device.journal?
journal_type_label(fs)
elsif show_multidevice_type_label?(fs)
multidevice_type_label(fs)
else
# TRANSLATORS: %{fs_type} is the filesystem type. I.e., FAT, Ext4, etc
# %{device_label} is the device label. I.e., Partition, Disk, etc
format(
_("%{fs_type} %{device_label}"),
fs_type: fs_type(device, fs),
device_label: default_label(device)
)
end
end

# The filesystem type representation (FAT, Ext4, etc)
#
# @return [String]
def fs_type(device, filesystem)
if device.is?(:partition) && device.efi_system?
device.id.to_human_string
else
filesystem.type.to_human_string
end
end

# Label for unformatted device (e.g., LVM, RAID, Partition, etc)
#
# @return [String]
def unformatted_device_type_label(device)
if device.lvm_pv
lvm_pv_type_label(device.lvm_pv)
elsif device.md
part_of_label(device.md)
elsif device.bcache
bcache_backing_label(device.bcache)
elsif device.in_bcache_cset
bcache_cset_label
else
default_unformatted_label(device)
end
end

# Label when the device is a LVM physical volume
#
# @param lvm_pv [Y2Storage::LvmPv]
# @return [String]
def lvm_pv_type_label(lvm_pv)
vg = lvm_pv.lvm_vg

return _("Unused LVM PV") if vg.nil?
return _("PV of LVM") if vg.basename.empty?

# TRANSLATORS: %s is the volume group name. E.g., "vg0"
format(_("PV of %s"), vg.basename)
end

# Label for an LVM snapshot device
#
# @param lvm_snapshot [Y2Storage::LvmLv]
# @return [String]
def snapshot_type_label(lvm_snapshot)
label =
if lvm_snapshot.is?(:lvm_thin_snapshot)
# TRANSLATORS: %{origin} is replaced by an LVM logical volumme name
# (e.g., /dev/vg0/user-data)
_("Thin Snapshot of %{origin}")
else
# TRANSLATORS: %{origin} is replaced by an LVM logical volumme name
# (e.g., /dev/vg0/user-data)
_("Snapshot of %{origin}")
end

format(label, origin: lvm_snapshot.origin.lv_name)
end

# Label when the device holds a journal
#
# @param filesystem [Y2Storage::BlkFilesystem]
# @return [String]
def journal_type_label(filesystem)
data_device = filesystem.blk_devices.find { |d| !d.journal? }

# TRANSLATORS: %{fs_type} is the filesystem type. E.g., Btrfs, Ext4, etc.
# %{data_device_name} is the data device name. E.g., sda1
format(
_("%{fs_type} Journal (%{data_device_name})"),
fs_type: filesystem.type.to_human_string,
data_device_name: data_device.basename
)
end

# Label when the device belongs to a multi-device filesystem
#
# @param filesystem [Y2Storage::BlkFilesystem]
# @return [String]
def multidevice_type_label(filesystem)
# TRANSLATORS: %{fs_name} is the filesystem name. E.g., Btrfs, Ext4, etc.
# %{blk_device_name} is a device base name. E.g., sda1...
format(
_("Part of %{fs_name} %{blk_device_name}"),
fs_name: filesystem.type,
blk_device_name: filesystem.blk_device_basename
)
end

# Label when the device is used as backing device of a Bcache
#
# @param [Y2Storage::Device] device
def bcache_backing_label(device)
# TRANSLATORS: %{bcache} is replaced by a device name (e.g., bcache0).
format(_("Backing of %{bcache}"), bcache: device.basename)
end

# Label when the device is used as caching device in a Bcache
#
# @return [String]
def bcache_cset_label
# TRANSLATORS: an special type of device
_("Bcache caching")
end

# Label when the device is part of another one, like Bcache or RAID
#
# @param ancestor_device [Y2Storage::BlkDevice]
# @return [String]
def part_of_label(ancestor_device)
format(_("Part of %s"), ancestor_device.basename)
end

# Default label when device is unformatted
#
# @return [String]
def default_unformatted_label(device)
# The "model" field from hwinfo is a combination of vendor + device with quite some added
# heuristics to make the result nice looking. See comment#66 at bsc#1200975.
model = device.model || ""

return model unless model.empty?
return device.id.to_human_string if device.respond_to?(:id)

default_label(device)
end

# Default label for the device
#
# @see DEVICE_LABELS
#
# @param device [Y2Storage::Device]
# @return [String]
def default_label(device)
type = DEVICE_LABELS.keys.find { |k| device.is?(k) }

return "" if type.nil?

_(DEVICE_LABELS[type])
end

# Whether the "Part of *fs.type*" label should be displayed
#
# The Ext3/4 filesystem could be detected as a multi-device filesystem
# when its journal is placed in an external device. However, we do not
# want to display "Part of ..." for them because we know that data
# partition is over a single device.
#
# @see #formatted_device_type_label
# @return [Boolean] true if the filesystem is multi-device BUT not an Ext3/4 one
def show_multidevice_type_label?(filesystem)
return false unless filesystem
return false if filesystem.type.is?(:ext3, :ext4)

filesystem.multidevice?
Y2Storage::DeviceDescription.new(device, system_graph: system_graph).to_s
end
end
end
Expand Down
Loading

0 comments on commit b3e7d0d

Please sign in to comment.