Module: Yast::PartitioningAutoPartPrepareInclude

Defined in:
../../src/include/partitioning/auto_part_prepare.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) initialize_partitioning_auto_part_prepare(include_target)



35
36
37
38
39
# File '../../src/include/partitioning/auto_part_prepare.rb', line 35

def initialize_partitioning_auto_part_prepare(include_target)
  textdomain "storage"

  Yast.import "Partitions"
end

- (Object) prepare_partitions(target, partitions)



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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File '../../src/include/partitioning/auto_part_prepare.rb', line 41

def prepare_partitions(target, partitions)
  target = deep_copy(target)
  partitions = deep_copy(partitions)
  # --------------------------------------------------------------
  # The size of a unit (eg. one cylinder)
  bytes_per_unit = Ops.get_integer(target, "cyl_size", 1)
  # The size of the disk in units
  disk_size = Ops.get_integer(target, "cyl_count", 1)
  Builtins.y2milestone(
    "prepare_partitions bytes_per_unit: %1 disk_size:%2",
    bytes_per_unit,
    disk_size
  )

  size_of_boot = Partitions.ProposedBootsize
  size_of_swap = Ops.multiply(
    1024 * 1024,
    Partitions.SwapSizeMb(0, StorageProposal.GetProposalSuspend)
  )

  # The minimum size needed to install a default system
  required_size = Ops.add(
    Ops.add(1500 * 1024 * 1024, size_of_boot),
    size_of_swap
  )

  # filter out all "create" paritions, they will be re-created at exit
  #   (this ensures a 'clean' partition list if this dialogue is re-entered

  partitions = Builtins.filter(partitions) do |pentry|
    !Ops.get_boolean(pentry, "create", false)
  end

  # reset all "delete" paritions, they will be re-created at exit
  #   (this ensures a 'clean' partition list if this dialogue is re-entered

  partitions = Builtins.maplist(partitions) do |pentry|
    Builtins.add(pentry, "delete", false)
  end

  # The region that describes the full disk
  full_region = [0, disk_size]

  #-------------------------------------------------------------------------
  # The action
  #-------------------------------------------------------------------------

  # First sort the partitions on the starting cylinder
  partitions = Builtins.sort(partitions) do |p1, p2|
    Ops.less_than(
      start_of_region(Ops.get_list(p1, "region", [])),
      start_of_region(Ops.get_list(p2, "region", []))
    )
  end

  # now check if automatic partitioning if feasible

  # unpartitioned disk -> yes

  @can_do_auto = false

  if Builtins.size(partitions) == 0
    # No partitions -> use the entire disk
    @can_do_auto = true
    @unused_region = deep_copy(full_region)
  end

  # extended region with enough free space -> yes

  if !@can_do_auto && contains_extended(partitions)
    # Extended partition already exists -> look for free space at
    # the end of it
    @unused_region = unused_extended_region(partitions)

    # check if this is enough
    if Ops.greater_than(
        size_of_region(@unused_region, bytes_per_unit),
        required_size
      ) &&
        can_create_logical(
          partitions,
          5,
          Ops.get_integer(target, "max_logical", 15)
        )
      @can_do_auto = true
    end
  end

  # no extended region, but primaries left
  #   if there is enough space after the last defined primary -> yes

  if !@can_do_auto && !contains_extended(partitions) &&
      num_primary(partitions) != @max_primary
    last_partition = Ops.get(
      partitions,
      Ops.subtract(Builtins.size(partitions), 1),
      {}
    )
    if ignored_partition(target, last_partition)
      last_partition = Ops.get(
        partitions,
        Ops.subtract(Builtins.size(partitions), 2),
        {}
      )
    end
    last_region = Ops.get_list(last_partition, "region", [])
    last_used = end_of_region(last_region)

    if Ops.less_than(last_used, disk_size)
      @unused_region = [last_used, Ops.subtract(disk_size, last_used)]
      if Ops.greater_than(
          size_of_region(@unused_region, bytes_per_unit),
          required_size
        )
        @can_do_auto = true
      end
    end
  end


  #-------------------------------------------------------------------------
  # Augment the partition list with a description for holes

  last_end = 0
  free_nr = 0

  last_end = 1 if Ops.get_string(target, "label", "") == "sun"

  # first the mid-disk holes

  partitions = Builtins.flatten(Builtins.maplist(partitions) do |pentry|
    ret = []
    region = Ops.get_list(pentry, "region", [])
    if !ignored_partition(target, pentry) &&
        Ops.greater_than(start_of_region(region), last_end)
      free_nr = Ops.add(free_nr, 1)
      ret = Builtins.add(
        ret,
        {
          "type"   => :free,
          "region" => [
            last_end,
            Ops.subtract(start_of_region(region), last_end)
          ]
        }
      )
      # if free space is directly located before extended partition
      last_end = start_of_region(region)
    end
    # if this partition is not the extended partition or a ignored
    #    use its end_of_region as last_end
    # on BSD-like partitions, partition # 3 is handled similary
    if Ops.get_symbol(pentry, "type", :unknown) != :extended &&
        !ignored_partition(target, pentry)
      last_end = end_of_region(Ops.get_list(pentry, "region", []))
    end
    Builtins.add(ret, pentry)
  end)

  # then the end-disk hole

  if Ops.less_than(last_end, disk_size)
    free_nr = Ops.add(free_nr, 1)
    partitions = Builtins.add(
      partitions,
      {
        "type"   => :free,
        "region" => [last_end, Ops.subtract(disk_size, last_end)]
      }
    )
  end

  # now the partitions list spans the whole disk

  #-------------------------------------------------------------------------
  # Create a checkbox for every real (primary or logical) partition
  # and any free space

  # give each partition a unique id

  ui_id = 0
  partitions = Builtins.maplist(partitions) do |p|
    ui_id = Ops.add(ui_id, 1)
    Ops.set(p, "ui_id", ui_id)
    if Ops.get_symbol(p, "type", :unknown) == :free
      Ops.set(
        p,
        "size_k",
        Ops.divide(
          size_of_region(
            Convert.convert(
              Ops.get(p, "region") { [0, 0] },
              :from => "any",
              :to   => "list <const integer>"
            ),
            bytes_per_unit
          ),
          1024
        )
      )
    end
    if Builtins.haskey(p, "mount") &&
        Ops.get_string(p, "mount", "") != "swap" &&
        !Ops.get_boolean(p, "inactive", false)
      p = Builtins.remove(p, "mount")
    end
    deep_copy(p)
  end

  # now the partitions list spans the whole disk
  Builtins.y2milestone("prepare_partitions partitions: %1", partitions)

  deep_copy(partitions)
end