/
add_partition.rb
206 lines (180 loc) · 6.29 KB
/
add_partition.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
138
139
140
141
142
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
# 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 "y2partitioner/actions/transaction_wizard"
require "y2partitioner/actions/new_blk_device"
require "y2partitioner/actions/controllers"
require "y2partitioner/dialogs"
module Y2Partitioner
module Actions
# formerly EpCreatePartition, DlgCreatePartition
class AddPartition < TransactionWizard
include NewBlkDevice
# @param disk [Y2Storage::BlkDevice]
def initialize(disk)
textdomain "storage"
super()
@device_sid = disk.sid
end
# Removes the filesystem when the device is directly formatted
def delete_filesystem
part_controller.delete_filesystem if part_controller.disk_formatted?
:next
end
def type
case available_types.size
when 0
raise "No partition type possible"
when 1
part_controller.type = available_types.first
:next
else
# Only run the dialog if more than one partition type is available
# (bsc#1075443)
Dialogs::PartitionType.run(part_controller)
end
end
def size
part_controller.delete_partition
result = Dialogs::PartitionSize.run(part_controller)
part_controller.create_partition if [:next, :finish].include?(result)
if result == :next
part = part_controller.partition
title = part_controller.wizard_title
self.fs_controller = Controllers::Filesystem.new(part, title)
end
result
end
protected
# @return [Controllers::Partition]
attr_reader :part_controller
# @see TransactionWizard
def init_transaction
# The controller object must be created within the transaction
@part_controller = Controllers::Partition.new(device.name)
# Once the controller is created we know which steps can be skipped
# when going back
skip_steps
end
# @see TransactionWizard
def sequence_hash
{
"ws_start" => "delete_filesystem",
"delete_filesystem" => { next: "type" },
"type" => { next: "size" },
"size" => { next: new_blk_device_step1, finish: :finish }
}.merge(new_blk_device_steps)
end
def disk_name
part_controller.disk_name
end
# @see TransactionWizard
# @note In case the device is formatted, the wizard is started
# only if the user confirms to delete the current filesystem.
#
# @return [Boolean]
def run?
partitionable_validation && not_used_validation &&
not_formatted_validation && available_space_validation
end
# Checks whether the device can contain partitions, which is not true
# for StrayBlkDevice objects (they are listed as disks but they are not).
#
# @return [Boolean] true if device can be partitioned, false otherwise
def partitionable_validation
if part_controller.disk.respond_to?(:partitions)
true
else
impossible_partition_popup
false
end
end
# Checks whether the device is not used
#
# @see Controllers::Partition#disk_used?
#
# @return [Boolean] true if device is not used; false otherwise.
def not_used_validation
return true unless part_controller.disk_used?
Yast::Popup.Error(
_("The disk is in use and cannot be modified.")
)
false
end
# Checks whether the device is not formatted
#
# @see Controllers::Partition#disk_formatted?
#
# @note A confirm popup to delete the filesystem is shown when the device
# is directly formatted.
#
# @return [Boolean] true if device is not formatted or confirm popup is
# accepted; false otherwise.
def not_formatted_validation
return true unless part_controller.disk_formatted?
Yast::Popup.YesNo(
# TRANSLATORS: %{name} is a device name (e.g. "/dev/sda")
format(
_("The device %{name} is directly formatted.\n"\
"Remove the filesystem on %{name}?"),
name: disk_name
)
)
end
# Checks whether it is possible to create a new partition.
#
# @see Controllers::Partition#new_partition_possible?
#
# @note When the device is formatted, it is consisered that there is enough
# space for a new partition due to the filesystem could be deleted.
#
# @return [Boolean]
def available_space_validation
return true if part_controller.disk_formatted?
return true if part_controller.new_partition_possible?
impossible_partition_popup
false
end
# Displays a popup telling the user it's not possible to create a
# partition
def impossible_partition_popup
Yast::Popup.Error(
format(
# TRANSLATORS: %{name} is a device name (e.g. "/dev/sda")
_("It is not possible to create a partition on %{name}."),
name: disk_name
)
)
end
# Convenience method for returning the device available partition types
#
# @return [Array<Y2Storage::PartitionType>]
def available_types
part_controller.available_partition_types
end
# Convenience method for setting the steps that have to be skipped when
# going back
def skip_steps
self.class.skip_stack :delete_filesystem
self.class.skip_stack :type if available_types.size < 2
end
end
end
end