Skip to content

Commit

Permalink
Merge 6980e86 into 7a1f350
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Sep 27, 2018
2 parents 7a1f350 + 6980e86 commit 721726e
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 3 deletions.
76 changes: 76 additions & 0 deletions src/lib/y2partitioner/actions/add_bcache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "y2partitioner/dialogs/bcache"
require "y2partitioner/device_graphs"

Yast.import "Popup"

module Y2Partitioner
module Actions
# Action following click on Add Bcache button.
class AddBcache
def run
dialog = Dialogs::Bcache.new(device_graph, usable_blk_devices,
usable_blk_devices + existing_caches)

create_device(dialog) if dialog.run == :next
:finish
end

private

def create_device(dialog)
backing = dialog.backing_device
raise "Invalid result #{dialog.inspect}. Backing not found." unless backing

caching = dialog.caching_device
raise "Invalid result #{dialog.inspect}. Caching not found." unless caching

bcache = backing.create_bcache(Y2Storage::Bcache.find_free_name(device_graph))

if !caching.is?(:bcache_cset)
caching.remove_descendants
caching = caching.create_bcache_cset
end

bcache.attach_bcache_cset(caching)
end

def device_graph
DeviceGraphs.instance.current
end

def usable_blk_devices
device_graph.blk_devices.select do |dev|
dev.component_of.empty? &&
(dev.filesystem.nil? || dev.filesystem.mount_point.nil?) &&
(!dev.respond_to?(:partitions) || dev.partitions.empty?)
end
end

def existing_caches
device_graph.bcache_csets
end
end
end
end
177 changes: 177 additions & 0 deletions src/lib/y2partitioner/dialogs/bcache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# encoding: utf-8

# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "yast2/popup"
require "cwm/common_widgets"
require "y2partitioner/dialogs/base"

module Y2Partitioner
module Dialogs
# Form to set the backing and caching device for bcache.
# Part of {Actions::AddBcache}.
class Bcache < Base
# @param device_graph [Y2Storage::DeviceGraph] graph on which it operates
# @param suitable_backing [Array<Y2Storage::BlkDevice>] devices that can be used for backing
# @param suitable_caching [Array<Y2Storage::BlkDevice,Y2Storage::BcacheCset>]
# devices that can be used for caching
# @param device [Y2Storage::Bcache] device if already exists or nil for newly created one.
def initialize(device_graph, suitable_backing, suitable_caching, device = nil)
# TODO: handle if device is passed
textdomain "storage"
@caching = CachingDevice.new(device_graph, device, suitable_caching)
@backing = BackingDevice.new(device_graph, device, suitable_backing, @caching)
@device_graph = device_graph
end

# @macro seeDialog
def title
_("Bcache Device")
end

# @macro seeDialog
def contents
VBox(
HBox(
@backing,
HSpacing(1),
@caching
),
VSpacing(1)
)
end

# Selected caching device. Undefined if result of dialog is not `:next`
# @return [Y2Storage::BlkDevice, Y2Storage::BcacheCset]
def caching_device
@caching.result
end

# Selected backing device. Undefined if result of dialog is not `:next`
# @return [Y2Storage::BlkDevice]
def backing_device
@backing.result
end

# Widget to select the backing device
class BackingDevice < CWM::ComboBox
def initialize(device_graph, device, devices, caching)
textdomain "storage"
@device = device
@devices = devices
@device_graph = device_graph
@caching = caching
end

def label
_("Backing Device")
end

def items
@devices.map do |dev|
[dev.sid, dev.name]
end
end

def help
# TODO: write it
""
end

def init
return unless @device

self.value = @device.sid
end

def store
val = value
@result = @device_graph.find_device(val)
end

def validate
log.info "selected value #{value.inspect}"
# value can be empty string if there is no items
if value.nil? || value == ""
Yast2::Popup.show(
_("Empty backing device is not yet supported"),
headline: _("Cannot Create Bcache")
)
return false
elsif value == @caching.value
Yast2::Popup.show(
_("Backing and Caching device cannot be identical."),
headline: _("Cannot Create Bcache")
)
return false
else
true
end
end

attr_reader :result
end

# Widget to select the caching device
class CachingDevice < CWM::ComboBox
def initialize(device_graph, device, devices)
textdomain "storage"
@device_graph = device_graph
@device = device
@devices = devices
end

def label
_("Caching Device")
end

def items
@devices.map do |dev|
case dev
when Y2Storage::BcacheCset
[dev.sid, dev.display_name]
else
[dev.sid, dev.name]
end
end
end

def help
# TODO: write it
""
end

def init
return unless @device

self.value = @device.sid
end

def store
val = value
@result = @device_graph.find_device(val)
end

attr_reader :result
end
end
end
end
49 changes: 49 additions & 0 deletions src/lib/y2partitioner/widgets/bcache_add_button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "cwm"
require "y2partitioner/actions/add_bcache"

module Y2Partitioner
module Widgets
# Button for opening a wizard to add a new Bcache array
class BcacheAddButton < CWM::PushButton
# Constructor
def initialize
textdomain "storage"
end

# @macro seeAbstractWidget
def label
# TRANSLATORS: button label to add a new Bcache device
_("Add Bcache...")
end

# @macro seeAbstractWidget
# @see Actions::AddMd
def handle
res = Actions::AddBcache.new.run
res == :finish ? :redraw : nil
end
end
end
end
4 changes: 3 additions & 1 deletion src/lib/y2partitioner/widgets/pages/bcaches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "cwm/tree_pager"
require "y2partitioner/icons"
require "y2partitioner/widgets/configurable_blk_devices_table"
require "y2partitioner/widgets/bcache_add_button"
require "y2partitioner/device_graphs"

module Y2Partitioner
Expand Down Expand Up @@ -64,7 +65,8 @@ def contents
table,
Left(
HBox(
# TODO: buttons
BcacheAddButton.new
# TODO: other buttons
)
)
)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/y2storage/bcache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Bcache < Partitionable
# @return [BcacheCset, nil] returns associated bcache cset
storage_forward :bcache_cset, as: "BcacheCset", check_with: :has_bcache_cset

# @!method attach_bcache_cset(set)
# @param set [BcacheCset] set to attach
# @raise if attaching failed
storage_forward :attach_bcache_cset

# @!method blk_device
# @return [BlkDevice] returns a backing device for cache
storage_forward :blk_device, as: "BlkDevice"
Expand All @@ -55,6 +60,12 @@ class Bcache < Partitionable
# @return [Bcache] nil if there is no such device
storage_class_forward :find_by_name, as: "Bcache"

# @!method self.find_free_name(devicegraph)
# Returns available free name for bcache device.
# @param devicegraph [Devicegraph] in which search for free name
# @return [String] full path to new bcache device like "/dev/bcache3"
storage_class_forward :find_free_name

def inspect
"<Bcache #{name} #{bcache_cset.inspect} -> #{blk_device}>"
end
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/bcache_cset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BcacheCset < Device
storage_class_forward :all, as: "BcacheCset"

def inspect
"<BcacheCset #{uuid} #{blk_devices.inspect}>"
"<BcacheCset uuid:#{uuid} #{blk_devices.inspect}>"
end

# Gets user friendly name for caching set. It is translated and ready to show to user.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/blk_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def udev_full_ids
#
# @raise [Storage::WrongNumberOfChildren] if there is any children
# @return [BcacheCset]
storage_forward :create_bcache_cset, as: "Bcache", raise_errors: true
storage_forward :create_bcache_cset, as: "BcacheCset", raise_errors: true

# @!method create_encryption(dm_name)
# Creates a new encryption object on top of the device.
Expand Down
1 change: 1 addition & 0 deletions src/lib/y2storage/devicegraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Devicegraph
# @!method find_device(device)
# Find a device by its {Device#sid sid}
#
# @param device [Integer] sid of device
# @return [Device]
storage_forward :find_device, as: "Device"

Expand Down

0 comments on commit 721726e

Please sign in to comment.