Skip to content

Commit 3ee70bc

Browse files
committed
First version of devicegraph_conversions
1 parent 07fb25d commit 3ee70bc

File tree

15 files changed

+1157
-0
lines changed

15 files changed

+1157
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) [2025] SUSE LLC
4+
#
5+
# All Rights Reserved.
6+
#
7+
# This program is free software; you can redistribute it and/or modify it
8+
# under the terms of version 2 of the GNU General Public License as published
9+
# by the Free Software Foundation.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
# more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, contact SUSE LLC.
18+
#
19+
# To contact SUSE LLC about this file by physical or electronic mail, you may
20+
# find current contact information at www.suse.com.
21+
22+
require "agama/storage/devicegraph_conversions/to_json"
23+
24+
module Agama
25+
module Storage
26+
# Conversions for the Y2Storage devicegraph
27+
module DevicegraphConversion
28+
end
29+
end
30+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) [2025] SUSE LLC
4+
#
5+
# All Rights Reserved.
6+
#
7+
# This program is free software; you can redistribute it and/or modify it
8+
# under the terms of version 2 of the GNU General Public License as published
9+
# by the Free Software Foundation.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
# more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, contact SUSE LLC.
18+
#
19+
# To contact SUSE LLC about this file by physical or electronic mail, you may
20+
# find current contact information at www.suse.com.
21+
22+
require "agama/storage/devicegraph_conversions/to_json_conversions/device"
23+
24+
module Agama
25+
module Storage
26+
module DevicegraphConversions
27+
# Devicegraph conversion to JSON array according to schema.
28+
class ToJSON
29+
# @param devicegraph [Y2Storage::Devicegraph]
30+
def initialize(devicegraph)
31+
@devicegraph = devicegraph
32+
end
33+
34+
# Performs the conversion to array according to the JSON schema.
35+
#
36+
# @return [Hash]
37+
def convert
38+
original_devices.map { |d| ToJSONConversions::Device.new(d).convert }
39+
end
40+
41+
private
42+
43+
# @return [Y2Storage::Devicegraph]
44+
attr_reader :devicegraph
45+
46+
# First-level devices to be included in the JSON representation.
47+
#
48+
# @return [Array<Y2Storage::Device>]
49+
def original_devices
50+
devicegraph.disk_devices +
51+
devicegraph.stray_blk_devices +
52+
devicegraph.software_raids +
53+
devicegraph.lvm_vgs
54+
end
55+
end
56+
end
57+
end
58+
end
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) [2025] SUSE LLC
4+
#
5+
# All Rights Reserved.
6+
#
7+
# This program is free software; you can redistribute it and/or modify it
8+
# under the terms of version 2 of the GNU General Public License as published
9+
# by the Free Software Foundation.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
# more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, contact SUSE LLC.
18+
#
19+
# To contact SUSE LLC about this file by physical or electronic mail, you may
20+
# find current contact information at www.suse.com.
21+
22+
require "agama/storage/devicegraph_conversions/to_json_conversions/section"
23+
require "agama/storage/device_shrinking"
24+
25+
module Agama
26+
module Storage
27+
module DevicegraphConversions
28+
module ToJSONConversions
29+
# Section with properties for block devices.
30+
class Block < Section
31+
# @see Section.apply?
32+
def self.apply?(storage_device)
33+
storage_device.is?(:blk_device)
34+
end
35+
36+
private
37+
38+
# @see Section#conversions
39+
def conversions
40+
{
41+
start: block_start,
42+
active: block_active,
43+
encrypted: block_encrypted,
44+
udevIds: block_udev_ids,
45+
udevPaths: block_udev_paths,
46+
size: block_size,
47+
shrinking: block_shrinking,
48+
systems: block_systems
49+
}
50+
end
51+
52+
# Position of the first block of the region.
53+
#
54+
# @return [Integer]
55+
def block_start
56+
storage_device.start
57+
end
58+
59+
# Whether the block device is currently active
60+
#
61+
# @return [Boolean]
62+
def block_active
63+
storage_device.active?
64+
end
65+
66+
# Whether the block device is encrypted.
67+
#
68+
# @return [Boolean]
69+
def block_encrypted
70+
storage_device.encrypted?
71+
end
72+
73+
# Name of the udev by-id links
74+
#
75+
# @return [Array<String>]
76+
def block_udev_ids
77+
storage_device.udev_ids
78+
end
79+
80+
# Name of the udev by-path links
81+
#
82+
# @return [Array<String>]
83+
def block_udev_paths
84+
storage_device.udev_paths
85+
end
86+
87+
# Size of the block device in bytes
88+
#
89+
# @return [Integer]
90+
def block_size
91+
storage_device.size.to_i
92+
end
93+
94+
# Shrinking information.
95+
#
96+
# @return [Hash]
97+
def block_shrinking
98+
shrinking = Agama::Storage::DeviceShrinking.new(storage_device)
99+
100+
if shrinking.supported?
101+
{ supported: shrinking.min_size.to_i }
102+
else
103+
{ unsupported: shrinking.unsupported_reasons }
104+
end
105+
end
106+
107+
# Name of the currently installed systems
108+
#
109+
# @return [Array<String>]
110+
def block_systems
111+
return @systems if @systems
112+
113+
filesystems = storage_device.descendants.select { |d| d.is?(:filesystem) }
114+
@systems = filesystems.map(&:system_name).compact
115+
end
116+
end
117+
end
118+
end
119+
end
120+
end
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) [2025] SUSE LLC
4+
#
5+
# All Rights Reserved.
6+
#
7+
# This program is free software; you can redistribute it and/or modify it
8+
# under the terms of version 2 of the GNU General Public License as published
9+
# by the Free Software Foundation.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
# more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, contact SUSE LLC.
18+
#
19+
# To contact SUSE LLC about this file by physical or electronic mail, you may
20+
# find current contact information at www.suse.com.
21+
22+
require "agama/storage/devicegraph_conversions/to_json_conversions/sections"
23+
require "y2storage/device_description"
24+
25+
module Agama
26+
module Storage
27+
module DevicegraphConversions
28+
module ToJSONConversions
29+
# Device conversion to JSON hash according to schema.
30+
class Device
31+
# @param storage_device [Y2Storage::Device]
32+
def initialize(storage_device)
33+
@storage_device = storage_device
34+
end
35+
36+
# Hash representing the Y2Storage device
37+
#
38+
# @return [Hash]
39+
def convert
40+
result = {
41+
sid: device_sid,
42+
name: device_name,
43+
description: device_description
44+
}
45+
add_sections(result)
46+
add_nested_devices(result)
47+
result
48+
end
49+
50+
private
51+
52+
# Device to convert
53+
# @return [Y2Storage::Device]
54+
attr_reader :storage_device
55+
56+
# sid of the device.
57+
#
58+
# @return [Integer]
59+
def device_sid
60+
storage_device.sid
61+
end
62+
63+
# Name to represent the device.
64+
#
65+
# @return [String] e.g., "/dev/sda".
66+
def device_name
67+
storage_device.display_name || ""
68+
end
69+
70+
# Description of the device.
71+
#
72+
# @return [String] e.g., "EXT4 Partition".
73+
def device_description
74+
Y2Storage::DeviceDescription.new(storage_device, include_encryption: true).to_s
75+
end
76+
77+
# Adds the required sub-sections according to the storage object.
78+
#
79+
# @param hash [Hash] the argument gets modified
80+
def add_sections(hash)
81+
conversions = Section.subclasses.select { |c| c.apply?(storage_device) }
82+
83+
conversions.each do |conversion|
84+
hash.merge!(conversion.new(storage_device).convert)
85+
end
86+
end
87+
88+
# Add nested devices like partitions or LVM logical volumes
89+
#
90+
# @param hash [Hash] the argument gets modified
91+
def add_nested_devices(hash)
92+
add_partitions(hash)
93+
add_logical_volumes(hash)
94+
end
95+
96+
# @see #add_nested_devices
97+
def add_partitions(hash)
98+
return unless PartitionTable.apply?(storage_device)
99+
100+
hash[:partitions] = storage_device.partition_table.partitions.map do |part|
101+
self.class.new(part).convert
102+
end
103+
end
104+
105+
# @see #add_nested_devices
106+
def add_logical_volumes(hash)
107+
return unless VolumeGroup.apply?(storage_device)
108+
109+
hash[:logicalVolumes] = storage_device.lvm_lvs.map do |lv|
110+
self.class.new(lv).convert
111+
end
112+
end
113+
end
114+
end
115+
end
116+
end
117+
end

0 commit comments

Comments
 (0)