summaryrefslogtreecommitdiffstats
path: root/iw/raid_dialog_gui.py
blob: a8d8507f70b346f6859e712759c5b7dd10df4c56 (plain)
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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#
# raid_dialog_gui.py: dialog for editting a raid request
#
# Michael Fulbright <msf@redhat.com>
#
# Copyright 2001-2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import copy

import gobject
import gtk

from rhpl.translate import _, N_

import gui
from fsset import *
from partRequests import *
from partition_ui_helpers_gui import *
from constants import *

class RaidEditor:

    def createAllowedRaidPartitionsList(self, allraidparts, reqraidpart,
                                        preexist):

	store = gtk.TreeStore(gobject.TYPE_BOOLEAN,
			      gobject.TYPE_STRING,
			      gobject.TYPE_STRING)
	partlist = WideCheckList(2, store)

	sw = gtk.ScrolledWindow()
	sw.add(partlist)
	sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
	sw.set_shadow_type(gtk.SHADOW_IN)

	partrow = 0
	for part, size, used in allraidparts:
	    partname = "%s" % part
	    partsize = "%8.0f MB" % size
	    if used or not reqraidpart:
		selected = 1
	    else:
		selected = 0

            if preexist == 0 or selected == 1:
                partlist.append_row((partname, partsize), selected)

	return (partlist, sw)

    def createRaidLevelMenu(self, levels, reqlevel):
	leveloption = gtk.OptionMenu()
	leveloptionmenu = gtk.Menu()
	defindex = None
	i = 0
	for lev in levels:
	    item = gtk.MenuItem(lev)
	    item.set_data("level", lev)
	    # XXX gtk bug, if you don't show then the menu will be larger
	    # than the largest menu item
	    item.show()        
	    leveloptionmenu.add(item)
	    if reqlevel and lev == reqlevel:
		defindex = i
	    if self.sparesb:
		item.connect("activate", self.raidlevelchangeCB, self.sparesb)
	    i = i + 1

	leveloption.set_menu(leveloptionmenu)

	if defindex:
	    leveloption.set_history(defindex)

	if reqlevel and reqlevel == "RAID0":
	    self.sparesb.set_sensitive(0)

	return (leveloption, leveloptionmenu)

    def createRaidMinorMenu(self, minors, reqminor):
	minoroption = gtk.OptionMenu()
	minoroptionmenu = gtk.Menu()
	defindex = None
	i = 0
	for minor in minors:
	    item = gtk.MenuItem("md%d" % (minor,))
	    item.set_data("minor", minor)
	    # XXX gtk bug, if you don't show then the menu will be larger
	    # than the largest menu item
	    item.show()
	    minoroptionmenu.add(item)
	    if reqminor and minor == reqminor:
		defindex = i
	    i = i + 1

	minoroption.set_menu(minoroptionmenu)

	if defindex:
	    minoroption.set_history(defindex)

	return (minoroption, minoroptionmenu)


    def raidlevelchangeCB(self, widget, sparesb):
	raidlevel = widget.get_data("level")
	numparts = sparesb.get_data("numparts")
	maxspares = raid.get_raid_max_spares(raidlevel, numparts)

	if maxspares > 0 and raidlevel != "RAID0":
	    adj = sparesb.get_adjustment() 
	    value = adj.value 
	    if adj.value > maxspares: 
		value = maxspares 

	    sparesb.set_sensitive(1)
	    spareAdj = gtk.Adjustment(value = value, lower = 0,
				      upper = maxspares, step_incr = 1)
	    spareAdj.clamp_page(0, maxspares)
	    sparesb.set_adjustment(spareAdj)
	    sparesb.set_value(value)
	else:
	    sparesb.set_value(0)
	    sparesb.set_sensitive(0)

    def run(self):
	if self.dialog is None:
	    return None
	
	while 1:
	    rc = self.dialog.run()

	    # user hit cancel, do nothing
	    if rc == 2:
		self.destroy()
		return None

	    # read out UI into a partition specification
	    request = copy.copy(self.origrequest)

	    # doesn't make sense for RAID device
	    request.badblocks = None
            if not self.origrequest.getPreExisting():
                filesystem = self.fstypeoptionMenu.get_active().get_data("type")
                request.fstype = filesystem

		if request.fstype.isMountable():
		    request.mountpoint = self.mountCombo.entry.get_text()
		else:
		    request.mountpoint = None

	    raidmembers = []
	    model = self.raidlist.get_model()
	    iter = model.get_iter_first()
	    next = 1
	    while next:
		val   = model.get_value(iter, 0)
		part = model.get_value(iter, 1)

		if val:
		    req = self.partitions.getRequestByDeviceName(part)
		    raidmembers.append(req.uniqueID)

		next = model.iter_next(iter)

            if not self.origrequest.getPreExisting():
                request.raidminor = self.minorOptionMenu.get_active().get_data("minor")

                request.raidmembers = raidmembers
                request.raidlevel = self.leveloptionmenu.get_active().get_data("level")
                if request.raidlevel != "RAID0":
                    request.raidspares = self.sparesb.get_value_as_int()
                else:
                    request.raidspares = 0

		if self.formatButton:
		    request.format = self.formatButton.get_active()
		else:
		    request.format = 0
	    else:
		if self.fsoptionsDict.has_key("formatrb"):
		    formatrb = self.fsoptionsDict["formatrb"]
		else:
		    formatrb = None

		if formatrb:
                    request.format = formatrb.get_active()
                    if request.format:
                        request.fstype = self.fsoptionsDict["fstypeMenu"].get_active().get_data("type")
                else:
                    request.format = 0

		if self.fsoptionsDict.has_key("migraterb"):
		    migraterb = self.fsoptionsDict["migraterb"]
		else:
		    migraterb = None
		    
		if migraterb:
                    request.migrate = migraterb.get_active()
                    if request.migrate:
                        request.fstype =self.fsoptionsDict["migfstypeMenu"].get_active().get_data("type")
                else:
                    request.migrate = 0

                # set back if we are not formatting or migrating
		origfstype = self.origrequest.origfstype
                if not request.format and not request.migrate:
                    request.fstype = origfstype

                if request.fstype.isMountable():
                    request.mountpoint =  self.mountCombo.entry.get_text()
                else:
                    request.mountpoint = None

	    err = request.sanityCheckRequest(self.partitions)
	    if err:
		self.intf.messageWindow(_("Error With Request"),
					"%s" % (err))
		continue

	    if (not request.format and
		request.mountpoint and request.formatByDefault()):
		if not queryNoFormatPreExisting(self.intf):
		    continue

	    # everything ok, break out
	    break


	return request

    def destroy(self):
	if self.dialog:
	    self.dialog.destroy()

	self.dialog = None
	
    def __init__(self, partitions, diskset, intf, parent, origrequest, isNew = 0):
	self.partitions = partitions
	self.diskset = diskset
	self.origrequest = origrequest
	self.isNew = isNew
	self.intf = intf
	self.parent = parent

	self.dialog = None

	#
	# start of editRaidRequest
	#
	availraidparts = self.partitions.getAvailRaidPartitions(origrequest,
								self.diskset)
	# if no raid partitions exist, raise an error message and return
	if len(availraidparts) < 2:
	    dlg = gtk.MessageDialog(self.parent, 0, gtk.MESSAGE_ERROR,
				    gtk.BUTTONS_OK,
				    _("At least two unused software RAID "
				      "partitions are needed to create "
				      "a RAID device.\n\n"
				      "First create at least two partitions "
				      "of type \"software RAID\", and then "
				      "select the \"RAID\" option again."))
	    gui.addFrame(dlg)
	    dlg.show_all()
	    dlg.set_position(gtk.WIN_POS_CENTER)
	    dlg.run()
	    dlg.destroy()
	    return

	dialog = gtk.Dialog(_("Make RAID Device"), self.parent)
	gui.addFrame(dialog)
	dialog.add_button('gtk-cancel', 2)
	dialog.add_button('gtk-ok', 1)
	dialog.set_position(gtk.WIN_POS_CENTER)

	maintable = gtk.Table()
	maintable.set_row_spacings(5)
	maintable.set_col_spacings(5)
	row = 0

	# Mount Point entry
	maintable.attach(createAlignedLabel(_("Mount Point:")),
					    0, 1, row, row + 1)
	self.mountCombo = createMountPointCombo(origrequest)
	maintable.attach(self.mountCombo, 1, 2, row, row + 1)
	row = row + 1

	# Filesystem Type
	maintable.attach(createAlignedLabel(_("Filesystem type:")),
					    0, 1, row, row + 1)

        if not origrequest.getPreExisting():
            (self.fstypeoption, self.fstypeoptionMenu) = createFSTypeMenu(origrequest.fstype,
                                                                          fstypechangeCB,
                                                                          self.mountCombo,
                                                                          ignorefs = ["software RAID"])
        else:
            if origrequest.fstype.getName():
                self.fstypeoption = gtk.Label(origrequest.fstype.getName())
            else:
                self.fstypeoption = gtk.Label(_("Unknown"))
            
	maintable.attach(self.fstypeoption, 1, 2, row, row + 1)
	row = row + 1

	# raid minors
	maintable.attach(createAlignedLabel(_("RAID Device:")),
                         0, 1, row, row + 1)

        if not origrequest.getPreExisting():
            availminors = self.partitions.getAvailableRaidMinors()[:16]
            reqminor = origrequest.raidminor
            if reqminor is not None:
                availminors.append(reqminor)

            availminors.sort()
            (self.minorOption, self.minorOptionMenu) = self.createRaidMinorMenu(availminors, reqminor)
        else:
            self.minorOption = gtk.Label("md%s" %(origrequest.raidminor,))
	maintable.attach(self.minorOption, 1, 2, row, row + 1)
	row = row + 1

	# raid level
	maintable.attach(createAlignedLabel(_("RAID Level:")),
					    0, 1, row, row + 1)

        if not origrequest.getPreExisting():
            # Create here, pack below
            numparts =  len(availraidparts)
            if origrequest.raidspares:
                nspares = origrequest.raidspares
            else:
                nspares = 0

            if origrequest.raidlevel:
                maxspares = raid.get_raid_max_spares(origrequest.raidlevel, numparts)
            else:
                maxspares = 0

            spareAdj = gtk.Adjustment(value = nspares, lower = 0,
                                      upper = maxspares, step_incr = 1)
            self.sparesb = gtk.SpinButton(spareAdj, digits = 0)
            self.sparesb.set_data("numparts", numparts)

            if maxspares > 0:
                self.sparesb.set_sensitive(1)
            else:
                self.sparesb.set_value(0)
                self.sparesb.set_sensitive(0)
        else:
            self.sparesb = gtk.Label(str(origrequest.raidspares))


        if not origrequest.getPreExisting():
            (self.leveloption, self.leveloptionmenu) = \
                               self.createRaidLevelMenu(availRaidLevels,
                                                        origrequest.raidlevel)
        else:
            self.leveloption = gtk.Label(origrequest.raidlevel)

	maintable.attach(self.leveloption, 1, 2, row, row + 1)
	row = row + 1

	# raid members
	maintable.attach(createAlignedLabel(_("RAID Members:")),
			 0, 1, row, row + 1)

	# XXX need to pass in currently used partitions for this device
	(self.raidlist, sw) = self.createAllowedRaidPartitionsList(availraidparts,
                                                                   origrequest.raidmembers,
                                                                   origrequest.getPreExisting())

	self.raidlist.set_size_request(275, 80)
	maintable.attach(sw, 1, 2, row, row + 1)
	row = row + 1

        if origrequest.getPreExisting():
            self.raidlist.set_sensitive(gtk.FALSE)

	# number of spares - created widget above
	maintable.attach(createAlignedLabel(_("Number of spares:")),
			 0, 1, row, row + 1)
	maintable.attach(self.sparesb, 1, 2, row, row + 1)
	row = row + 1

	# format or not?
	self.formatButton = None
	self.fsoptionsDict = {}
	if (origrequest.fstype and origrequest.fstype.isFormattable()) and not origrequest.getPreExisting():
	    self.formatButton = gtk.CheckButton(_("Format partition?"))
	    if origrequest.format == None or origrequest.format != 0:
		self.formatButton.set_active(1)
	    else:
		self.formatButton.set_active(0)
            # it only makes sense to show this for preexisting RAID
            if origrequest.getPreExisting():
                maintable.attach(self.formatButton, 0, 2, row, row + 1)
                row = row + 1
	else:
	    (row, self.fsoptionsDict) = createPreExistFSOptionSection(self.origrequest, maintable, row, self.mountCombo, showbadblocks=0)

	# put main table into dialog
	dialog.vbox.pack_start(maintable)

	dialog.show_all()
	self.dialog = dialog
	return



class RaidCloneDialog:
    def createDriveList(self, diskset):

	store = gtk.ListStore(gobject.TYPE_STRING)
        view = gtk.TreeView(store)

	sw = gtk.ScrolledWindow()
	sw.add(view)
	sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
	sw.set_shadow_type(gtk.SHADOW_IN)

        drives = diskset.disks.keys()
        drives.sort()

        for drive in drives:
            iter = store.append()
            store.set_value(iter, 0, drive)
            
        view.set_property("headers-visible", gtk.FALSE)

        col = gtk.TreeViewColumn("", gtk.CellRendererText(), text=0)
        view.append_column(col)
        
        return (sw, view)

    def getInterestingRequestsForDrive(self, drive):
        allrequests = self.partitions.getRequestsByDevice(self.diskset, drive)

        # remove extended partitions
        requests = []
        for req in allrequests:
            try:
                part = partedUtils.get_partition_by_name(self.diskset.disks,
                                                         req.device)
            except:
                part = None
                
            if part and part.type & parted.PARTITION_EXTENDED:
                continue

            requests.append(req)

        return requests

    def sanityCheckSourceDrive(self):
        # first see if it has any non-software RAID partitions
        requests = self.getInterestingRequestsForDrive(self.sourceDrive)
        
        errmsg1 = _("The source drive has no partitions to be cloned.  "
                    "You must first defined partitions of type "
                    "'software RAID' on this drive before it can be cloned.")
        if not requests or len(requests) == 0:
            self.intf.messageWindow(_("Source Drive Error"), errmsg1)
            return 0

        for req in requests:
            if not req.fstype or req.fstype.getName() != "software RAID":
                self.intf.messageWindow(_("Source Drive Error"),
                                        _("The source drive selected has "
                                          "partitions on it which are not of "
                                          "type 'software RAID'.\n\n"
                                          "These "
                                          "partitions will have to removed "
                                          "before this drive can be cloned. "))
                return 0

        for req in requests:
            if not req.drive or req.drive[0] != self.sourceDrive or len(req.drive) > 1:
                self.intf.messageWindow(_("Source Drive Error"),
                                        _("The source drive selected has "
                                          "partitions which are not "
                                          "constrained to the drive /dev/%s.\n\n"
                                          "These partitions will have to "
                                          "removed or restricted to this "
                                          "drive "
                                          "before this drive can be cloned. ")
                                        %(self.sourceDrive,))
                return 0

        for req in requests:
            if self.partitions.isRaidMember(req):
                self.intf.messageWindow(_("Source Drive Error"),
                                        _("The source drive selected has "
                                          "software RAID partition(s) which "
                                          "are members of an active "
                                          "software RAID device.\n\n"
                                          "These partitions will have to "
                                          "removed before this drive "
                                          "can be cloned."))
                return 0

        return 1

    def sanityCheckTargetDrives(self):
        if self.targetDrives is None or len(self.targetDrives) < 1:
                self.intf.messageWindow(_("Target Drive Error"),
                                        _("Please select the target drives "
                                          "for the clone operation."))
                return 0

        if self.sourceDrive in self.targetDrives:
                self.intf.messageWindow(_("Target Drive Error"),
                                        _("The source drive /dev/%s cannot be "
                                          "selected as a target drive as well.") % (self.sourceDrive,))
                return 0

        for drive in self.targetDrives:
            requests = self.getInterestingRequestsForDrive(drive)
            for req in requests:
                rc = partIntfHelpers.isNotChangable(req, self.partitions)
                if rc:
                    self.intf.messageWindow(_("Target Drive Error"),
                                            _("The target drive /dev/%s "
                                              "has a partition which cannot "
                                              "be removed for the following "
                                              "reason:\n\n\"%s\"\n\n"
                                              "This partition must be removed "
                                              "(if possible) before "
                                              "this drive can be a target.") %
                                            (drive, rc % (_("delete"),)))
                    return 0

        return 1

    def targetSelectFunc(self, model, path, iter):
        self.targetDrives.append(model.get_value(iter,0))
        
    def run(self):
	if self.dialog is None:
	    return None
	
	while 1:
	    rc = self.dialog.run()

	    # user hit cancel, do nothing
	    if rc == 2:
		self.destroy()
		return None

            # see what drive they selected as the source
            selection = self.sourceView.get_selection()
            (model, iter) = selection.get_selected()
            if iter is None:
                self.intf.messageWindow(_("Error"),
                                        _("Please select a source drive."))
                continue

            self.sourceDrive = model.get_value(iter, 0)

            # sanity check it
            if not self.sanityCheckSourceDrive():
                continue

            
            # now get target drive(s)
            self.targetDrives = []
            selection = self.targetView.get_selection()
            selection.selected_foreach(self.targetSelectFunc)

            # sanity check it
            if not self.sanityCheckTargetDrives():
                continue
            

	    # everything ok, break out
	    break

	return None

    def destroy(self):
	if self.dialog:
	    self.dialog.destroy()

	self.dialog = None
	
    def __init__(self, partitions, diskset, intf, parent):
	self.partitions = partitions
	self.diskset = diskset
	self.intf = intf
	self.parent = parent

	self.dialog = None
	self.dialog = gtk.Dialog(_("Make RAID Device"), self.parent)
        self.dialog.set_size_request(500, 400)
	gui.addFrame(self.dialog)
	self.dialog.add_button('gtk-cancel', 2)
	self.dialog.add_button('gtk-ok', 1)
	self.dialog.set_position(gtk.WIN_POS_CENTER)

        # present list of drives as source
        vbox = gtk.VBox()

        lbl = gui.WrappingLabel(_("Clone Drive Tool\n\n"
                                  "This tool allows you to significantly "
                                  "reduce the amount of effort required "
                                  "to setup RAID arrays.  The idea is to "
                                  "take a source drive which has been "
                                  "prepared with the desired partitioning "
                                  "layout, and clone this layout onto other "
                                  "similar sized drives.  Then a RAID device "
                                  "can be created.\n\n"
                                  "NOTE: The source drive must have "
                                  "partitions which are restricted to be on "
                                  "that drive only, and can only contain "
                                  "unused software RAID partitions.  Other "
                                  "partition types are not allowed.\n\n"
                                  "EVERYTHING on the target drive(s) will be "
                                  "destroyed by this process."))
        vbox.pack_start(lbl)
                                  
        box = gtk.HBox()

        lbl = gtk.Label(_("Source Drive:"))
        lbl.set_alignment(0.0, 0.0)
        box.pack_start(lbl, padding=5)
        (sw, self.sourceView) = self.createDriveList(diskset)
        selection = self.sourceView.get_selection()
        selection.set_mode(gtk.SELECTION_SINGLE)
        box.pack_start(sw)

        lbl = gtk.Label(_("Target Drive(s):"))
        lbl.set_alignment(0.0, 0.0)
        box.pack_start(lbl, padding=5)
        (sw, self.targetView) = self.createDriveList(diskset)
        selection = self.targetView.get_selection()
        selection.set_mode(gtk.SELECTION_MULTIPLE)
        box.pack_start(sw)

        frame = gtk.Frame(_("Drives"))
        frame.add(box)
        vbox.pack_start(frame)

	# put contents into dialog
	self.dialog.vbox.pack_start(vbox)

	self.dialog.show_all()

	return