summaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
0 files changed, 0 insertions, 0 deletions
id='n41' href='#n41'>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 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
#
# partitions.py: partition object containing partitioning info
#
# Matt Wilson <msw@redhat.com>
# Jeremy Katz <katzj@redhat.com>
# Mike Fulbright <msf@redhat.com>
# Harald Hoyer <harald@redhat.de>
#
# Copyright 2002-2005 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.
#
"""Overarching partition object."""

import parted
import iutil
import isys
import string
import os, sys

from constants import *

import fsset
import raid
import lvm
import partedUtils
import partRequests

from rhpl.translate import _
from rhpl.log import log

class Partitions:
    """Defines all of the partition requests and delete requests."""
    def __init__ (self, diskset = None):
        """Initializes a Partitions object.

        Can pass in the diskset if it already exists.
        """
        self.requests = []
        """A list of RequestSpec objects for all partitions."""

        self.deletes = []
        """A list of DeleteSpec objects for partitions to be deleted."""

        self.autoPartitionRequests = []
        """A list of RequestSpec objects for autopartitioning.
        These are setup by the installclass and folded into self.requests
        by auto partitioning."""

        self.autoClearPartType = CLEARPART_TYPE_NONE
        """What type of partitions should be cleared?"""

        self.autoClearPartDrives = None
        """Drives to clear partitions on (note that None is equiv to all)."""

        self.nextUniqueID = 1
        """Internal counter.  Don't touch unless you're smarter than me."""

        self.reinitializeDisks = 0
        """Should the disk label be reset on all disks?"""

        self.zeroMbr = 0
        """Should the mbr be zero'd?"""

        # partition method to be used.  not to be touched externally
        self.useAutopartitioning = 1
        self.useFdisk = 0

        # autopartitioning info becomes kickstart partition requests
        # and its useful to be able to differentiate between the two
        self.isKickstart = 0

        if diskset:
            self.setFromDisk(diskset)


    def setFromDisk(self, diskset):
        """Clear the delete list and set self.requests to reflect disk."""
        self.deletes = []
        self.requests = []
        diskset.refreshDevices()
        labels = diskset.getLabels()
        drives = diskset.disks.keys()
        drives.sort()
        for drive in drives:
            disk = diskset.disks[drive]
            part = disk.next_partition()
            while part:
                if part.type & parted.PARTITION_METADATA:
                    part = disk.next_partition(part)
                    continue

                format = None
                if part.type & parted.PARTITION_FREESPACE:
                    ptype = None
                elif part.type & parted.PARTITION_EXTENDED:
                    ptype = None
                elif part.get_flag(parted.PARTITION_RAID) == 1:
                    ptype = fsset.fileSystemTypeGet("software RAID")
                elif part.get_flag(parted.PARTITION_LVM) == 1:
                    ptype = fsset.fileSystemTypeGet("physical volume (LVM)")
                else:
                    ptype = partedUtils.get_partition_file_system_type(part)

                    # FIXME: we don't handle ptype being None very well, so
                    # just say it's foreign.  Should probably fix None
                    # handling instead some day.
                    if ptype is None:
                        ptype = fsset.fileSystemTypeGet("foreign")
                    
                start = part.geom.start
                end = part.geom.end
                size = partedUtils.getPartSizeMB(part)
                drive = partedUtils.get_partition_drive(part)

                spec = partRequests.PreexistingPartitionSpec(ptype,
                                                             size = size,
                                                             start = start,
                                                             end = end,
                                                             drive = drive,
                                                             format = format)
                spec.device = fsset.PartedPartitionDevice(part).getDevice()

                # set label if makes sense
                if ptype and ptype.isMountable() and \
                   (ptype.getName() == "ext2" or ptype.getName() == "ext3"):
                    if spec.device in labels.keys():
                        if labels[spec.device] and len(labels[spec.device])>0:
                            spec.fslabel = labels[spec.device]

                self.addRequest(spec)
                part = disk.next_partition(part)

        # now we need to read in all pre-existing RAID stuff
        diskset.startAllRaid()
        mdList = diskset.mdList
        for raidDev in mdList:
            (theDev, devices, level, numActive) = raidDev
            level = "RAID%s" %(level,)

            if level not in raid.availRaidLevels:
                log("raid level %s not supported, skipping %s" %(level,
                                                                  theDev))
                continue

            try:
                chunk = isys.getRaidChunkFromDevice("/dev/%s" %(theDev,))
            except Exception, e:
                log("couldn't get chunksize of %s: %s" %(theDev, e))
                chunk = None
            
            # is minor always mdN ?
            minor = int(theDev[2:])
            raidvols = []
            for dev in devices:
                req = self.getRequestByDeviceName(dev)
                if not req:
                    log("RAID device %s using non-existent partition %s"
                        %(theDev, dev))
                    continue
                raidvols.append(req.uniqueID)
                

            fs = partedUtils.sniffFilesystemType(theDev)
            if fs is None:
                fsystem = fsset.fileSystemTypeGet("foreign")
            else:
                fsystem = fsset.fileSystemTypeGet(fs)

            mnt = None
            format = 0
                    
            spares = len(devices) - numActive
            spec = partRequests.RaidRequestSpec(fsystem, format = format,
                                                raidlevel = level,
                                                raidmembers = raidvols,
                                                raidminor = minor,
                                                raidspares = spares,
                                                mountpoint = mnt,
                                                preexist = 1,
                                                chunksize = chunk)
            spec.size = spec.getActualSize(self, diskset)
            self.addRequest(spec)

        # now to read in pre-existing LVM stuff
        lvm.vgscan()
        lvm.vgactivate()

        pvs = lvm.pvlist()
        for (vg, size, pesize) in lvm.vglist():
            try:
                preexist_size = float(size)
            except:
                log("preexisting size for %s not a valid integer, ignoring" %(vg,))
                preexist_size = None

            pvids = []
            for (dev, pvvg, size) in pvs:
                if vg != pvvg:
                    continue
                req = self.getRequestByDeviceName(dev[5:])
                if not req:
                    log("Volume group %s using non-existent partition %s"
                        %(vg, dev))
                    continue
                pvids.append(req.uniqueID)
            spec = partRequests.VolumeGroupRequestSpec(format = 0,
                                                       vgname = vg,
                                                       physvols = pvids,
                                                       pesize = pesize,
                                                       preexist = 1,
                                                       preexist_size = preexist_size)
            vgid = self.addRequest(spec)

            for (lvvg, lv, size, lvorigin) in lvm.lvlist():
                if lvorigin:
                    continue
                if lvvg != vg:
                    continue
                
                # size is number of bytes, we want size in megs
                lvsize = float(size)

                theDev = "/dev/%s/%s" %(vg, lv)
                fs = partedUtils.sniffFilesystemType(theDev)
                if fs is None:
                    fsystem = fsset.fileSystemTypeGet("foreign")
                else:
                    fsystem = fsset.fileSystemTypeGet(fs)

                mnt = None
                format = 0

                spec = partRequests.LogicalVolumeRequestSpec(fsystem,
                    format = format, size = lvsize, volgroup = vgid,
                    lvname = lv, mountpoint = mnt, preexist = 1)
                self.addRequest(spec)

        for vg in lvm.partialvgs():
            spec = partRequests.PartialVolumeGroupSpec(vgname = vg)
            self.addDelete(spec)
            
        lvm.vgdeactivate()

        diskset.stopAllRaid()
            
        

    def addRequest (self, request):
        """Add a new request to the list."""
        if not request.uniqueID:
            request.uniqueID = self.nextUniqueID
            self.nextUniqueID = self.nextUniqueID + 1
        self.requests.append(request)
        self.requests.sort()

        return request.uniqueID

    def addDelete (self, delete):
        """Add a new DeleteSpec to the list."""
        self.deletes.append(delete)
        self.deletes.sort()

    def removeRequest (self, request):
        """Remove a request from the list."""
        self.requests.remove(request)

    def getRequestByMountPoint(self, mount):
        """Find and return the request with the given mountpoint."""
        for request in self.requests:
            if request.mountpoint == mount:
                return request
	    
	for request in self.requests:
	    if request.type == REQUEST_LV and request.mountpoint == mount:
		return request
        return None

    def getRequestByDeviceName(self, device):
        """Find and return the request with the given device name."""
	if device is None:
	    return None
	
        for request in self.requests:
	    if request.type == REQUEST_RAID and request.raidminor is not None:
		tmp = "md%d" % (request.raidminor,)
		if tmp == device:
		    return request
	    elif request.device == device:
                return request
        return None


    def getRequestsByDevice(self, diskset, device):
        """Find and return the requests on a given device (like 'hda')."""
        if device is None:
            return None

        drives = diskset.disks.keys()
        if device not in drives:
            return None

        rc = []
        disk = diskset.disks[device]
        part = disk.next_partition()
        while part:
            dev = partedUtils.get_partition_name(part)
            request = self.getRequestByDeviceName(dev)

            if request:
                rc.append(request)
            part = disk.next_partition(part)

        if len(rc) > 0:
            return rc
        else:
            return None

    def getRequestByVolumeGroupName(self, volname):
        """Find and return the request with the given volume group name."""
	if volname is None:
	    return None
	
	for request in self.requests:
	    if (request.type == REQUEST_VG and
                request.volumeGroupName == volname):
		return request
        return None

    def getRequestByLogicalVolumeName(self, lvname):
        """Find and return the request with the given logical volume name."""
	if lvname is None:
	    return None
	for request in self.requests:
	    if (request.type == REQUEST_LV and
                request.logicalVolumeName == lvname):
		return request
        return None

    def getRequestByID(self, id):
        """Find and return the request with the given unique ID.

        Note that if id is a string, it will be converted to an int for you.
        """
	if type(id) == type("a string"):
	    id = int(id)
        for request in self.requests:
            if request.uniqueID == id:
                return request
        return None

    def getRaidRequests(self):
        """Find and return a list of all of the RAID requests."""
        retval = []
        for request in self.requests:
            if request.type == REQUEST_RAID:
                retval.append(request)

        return retval

    def getRaidDevices(self):
        """Find and return a list of all of the requests for use in RAID."""
        raidRequests = []
        for request in self.requests:
            if isinstance(request, partRequests.RaidRequestSpec):
                raidRequests.append(request)
                
        return raidRequests

    def getAvailableRaidMinors(self):
        """Find and return a list of all of the unused minors for use in RAID."""
        raidMinors = range(0,32)
        for request in self.requests:
            if isinstance(request, partRequests.RaidRequestSpec) and request.raidminor in raidMinors:
                raidMinors.remove(request.raidminor)
                
        return raidMinors
	

    def getAvailRaidPartitions(self, request, diskset):
        """Return a list of tuples of RAID partitions which can be used.

        Return value is (part, size, used) where used is 0 if not,
        1 if so, 2 if used for *this* request.
        """
        rc = []
        drives = diskset.disks.keys()
        raiddevs = self.getRaidDevices()
        drives.sort()
        for drive in drives:
            disk = diskset.disks[drive]
            for part in partedUtils.get_raid_partitions(disk):
                partname = partedUtils.get_partition_name(part)
                used = 0
                for raid in raiddevs:
                    if raid.raidmembers:
                        for raidmem in raid.raidmembers:
                            tmpreq = self.getRequestByID(raidmem)
                            if (partname == tmpreq.device):
                                if raid.uniqueID == request.uniqueID:
                                    used = 2
                                else:
                                    used = 1
                                break
                    if used:
                        break
                size = partedUtils.getPartSizeMB(part)

                if not used:
                    rc.append((partname, size, 0))
                elif used == 2:
                    rc.append((partname, size, 1))
		    
        return rc

    def getRaidMemberParent(self, request):
        """Return RAID device request containing this request."""
        raiddev = self.getRaidRequests()
        if not raiddev or not request.device:
            return None
        for dev in raiddev:
            if not dev.raidmembers:
                continue
            for member in dev.raidmembers:
                if request.device == self.getRequestByID(member).device:
                    return dev
        return None

    def isRaidMember(self, request):
        """Return whether or not the request is being used in a RAID device."""
	if self.getRaidMemberParent(request) is not None:
	    return 1
	else:
	    return 0

    def getLVMLVForVGID(self, vgid):        
        """Find and return a list of all the LVs associated with a VG id."""
        retval = []
        for request in self.requests:
	    if request.type == REQUEST_LV:
		if request.volumeGroup == vgid:
                    retval.append(request)
        return retval

    def getLVMLVForVG(self, vgrequest):
        """Find and return a list of all of the LVs in the VG."""
        vgid = vgrequest.uniqueID
        return self.getLVMLVForVGID(vgid)
		
    def getLVMRequests(self):
        """Return a dictionary of all of the LVM bits.

        The dictionary returned is of the form vgname: [ lvrequests ]
        """
        retval = {}
        for request in self.requests:
            if request.type == REQUEST_VG:
                retval[request.volumeGroupName] = self.getLVMLVForVG(request)
	    
        return retval

    def getPartialLVMRequests(self):
        """Return a list of all of the partial volume groups names."""
        retval = []
        for request in self.deletes:
            if isinstance(request, partRequests.PartialVolumeGroupSpec):
                retval.append(request.volumeGroupName)
	    
        return retval

    def getLVMVGRequests(self):
        """Find and return a list of all of the volume groups."""
        retval = []
        for request in self.requests:
            if request.type == REQUEST_VG:
                retval.append(request)

        return retval

    def getLVMLVRequests(self):
        """Find and return a list of all of the logical volumes."""
        retval = []
        for request in self.requests:
            if request.type == REQUEST_LV:
                retval.append(request)

        return retval

    def getAvailLVMPartitions(self, request, diskset):
        """Return a list of tuples of PV partitions which can be used.

        Return value is (part, size, used) where used is 0 if not,
        1 if so, 2 if used for *this* request.
        """
        rc = []
        drives = diskset.disks.keys()
        drives.sort()
        volgroups = self.getLVMVGRequests()
        for drive in drives:
            disk = diskset.disks[drive]
            for part in partedUtils.get_lvm_partitions(disk):
                partname = partedUtils.get_partition_name(part)
                partrequest = self.getRequestByDeviceName(partname)
                used = 0
                for volgroup in volgroups:
                    if volgroup.physicalVolumes:
                        if partrequest.uniqueID in volgroup.physicalVolumes:
                            if (request and request.uniqueID and
                                volgroup.uniqueID == request.uniqueID):
                                used = 2
                            else:
                                used = 1

                    if used:
                        break
                size = None
                for pvpart, pvvg, pvsize in lvm.pvlist():
                    if pvpart == "/dev/%s" % (partname,):
                        size = pvsize
                if size is None:
                    # if we get here, there's no PV data in the partition,
                    # so clamp the partition's size to 64M
                    size = partedUtils.getPartSizeMB(part)
                    size = lvm.clampPVSize(size, 65536)

                if used == 0:
                    rc.append((partrequest.uniqueID, size, 0))
                elif used == 2:
                    rc.append((partrequest.uniqueID, size, 1))

	# now find available RAID devices
	raiddev = self.getRaidRequests()
	if raiddev:
	    raidcounter = 0
	    for dev in raiddev:
                used = 0

		if dev.fstype is None:
		    continue
		if dev.fstype.getName() != "physical volume (LVM)":
		    continue
		
                for volgroup in volgroups:
                    if volgroup.physicalVolumes:
                        if dev.uniqueID in volgroup.physicalVolumes:
                            if (request and request.uniqueID and
                                volgroup.uniqueID == request.uniqueID):
                                used = 2
                            else:
                                used = 1

                    if used:
                        break
		    
                size = dev.getActualSize(self, diskset)

                if used == 0:
                    rc.append((dev.uniqueID, size, 0))
                elif used == 2:
                    rc.append((dev.uniqueID, size, 1))

		raidcounter = raidcounter + 1
        return rc

    def getLVMVolumeGroupMemberParent(self, request):
        """Return parent volume group of a physical volume"""
	volgroups = self.getLVMVGRequests()
	if not volgroups:
	    return None

	for volgroup in volgroups:
	    if volgroup.physicalVolumes:
		if request.uniqueID in volgroup.physicalVolumes:
		    return volgroup

	return None

    def isLVMVolumeGroupMember(self, request):
        """Return whether or not the request is being used in an LVM device."""
	if self.getLVMVolumeGroupMemberParent(request) is None:
	    return 0
	else:
	    return 1
    
    def isVolumeGroupNameInUse(self, vgname):
        """Return whether or not the requested volume group name is in use."""
        if not vgname:
            return None

        lvmrequests = self.getLVMRequests()
        if lvmrequests:
            if vgname in lvmrequests.keys():
                return 1

        lvmrequests = self.getPartialLVMRequests()
        if lvmrequests:
            if vgname in lvmrequests:
                return 1

        return 0

    def getBootableRequest(self):
        """Return the name of the current 'boot' mount point."""
        bootreq = None

        if iutil.getArch() == "ia64":
            bootreq = self.getRequestByMountPoint("/boot/efi")
            if bootreq:
                return [ bootreq ]
            else:
                return None
        elif iutil.getPPCMachine() == "iSeries":
            for req in self.requests:
                if req.fstype == fsset.fileSystemTypeGet("PPC PReP Boot"):
                    return [ req ]
            return None
        elif (iutil.getPPCMachine() == "pSeries"):
            # pSeries and Mac bootable requests are odd.
            # have to consider both the PReP or Bootstrap partition (with
            # potentially > 1 existing) as well as /boot,/

            if iutil.getPPCMachine() == "pSeries":
                boottype = "PPC PReP Boot"
            else:
                boottype = "Apple Bootstrap"

            ret = []
            for req in self.requests:
                if req.fstype == fsset.fileSystemTypeGet("PPC PReP Boot"):
                    ret.append(req)

            # now add the /boot
            bootreq = self.getRequestByMountPoint("/boot")
            if not bootreq:
                bootreq = self.getRequestByMountPoint("/")
            if bootreq:
                ret.append(bootreq)

            if len(ret) >= 1:
                return ret
            return None
        elif (iutil.getPPCMachine() == "PMac"):
            # for the bootstrap partition, we want either the first or the
            # first non-preexisting one
            bestprep = None
            for req in self.requests:
                if req.fstype == fsset.fileSystemTypeGet("Apple Bootstrap"):
                    if ((bestprep is None) or
                        (bestprep.getPreExisting() and
                         not req.getPreExisting())):
                        bestprep = req

            if bestprep:
                ret = [ bestprep ]
            else:
                ret = []

            # now add the /boot
            bootreq = self.getRequestByMountPoint("/boot")
            if not bootreq:
                bootreq = self.getRequestByMountPoint("/")
            if bootreq:
                ret.append(bootreq)

            if len(ret) >= 1:
                return ret
            return None
        
        if not bootreq:
            bootreq = self.getRequestByMountPoint("/boot")
        if not bootreq:
            bootreq = self.getRequestByMountPoint("/")

        if bootreq:
            return [ bootreq ]
        return None

    def getBootableMountpoints(self):
        """Return a list of bootable valid mountpoints for this arch."""
        # FIXME: should be somewhere else, preferably some sort of arch object

        if iutil.getArch() == "ia64":
            return [ "/boot/efi" ]
        else:
            return [ "/boot", "/" ]

    def isBootable(self, request):
        """Returns if the request should be considered a 'bootable' request.

        This basically means that it should be sorted to the beginning of
        the drive to avoid cylinder problems in most cases.
        """
        bootreqs = self.getBootableRequest()
        if not bootreqs:
            return 0

        for bootreq in bootreqs:
            if bootreq == request:
                return 1

            if bootreq.type == REQUEST_RAID and \
                   request.uniqueID in bootreq.raidmembers:
                return 1

        return 0

    def sortRequests(self):
        """Resort the requests into allocation order."""
        n = 0
        while n < len(self.requests):
	    # Ignore LVM Volume Group and Logical Volume requests,
	    # since these are not related to allocating disk partitions
	    if (self.requests[n].type == REQUEST_VG or self.requests[n].type == REQUEST_LV):
		n = n + 1
		continue
	    
            for request in self.requests:
		# Ignore LVM Volume Group and Logical Volume requests,
		# since these are not related to allocating disk partitions
		if (request.type == REQUEST_VG or request.type == REQUEST_LV):
		    continue
                # for raid requests, the only thing that matters for sorting
                # is the raid device since ordering by size is mostly
                # irrelevant.  this also keeps things more consistent
                elif (request.type == REQUEST_RAID or
                    self.requests[n].type == REQUEST_RAID):
                    if (request.type == self.requests[n].type and
                        (self.requests[n].raidminor != None) and
                        ((request.raidminor is None) or
                         request.raidminor > self.requests[n].raidminor)):
                        tmp = self.requests[n]
                        index = self.requests.index(request)
                        self.requests[n] = request
                        self.requests[index] = tmp
                # for sized requests, we want the larger ones first
                elif (request.size and self.requests[n].size and
                    (request.size < self.requests[n].size)):
                    tmp = self.requests[n]
                    index = self.requests.index(request)
                    self.requests[n] = request
                    self.requests[index] = tmp
                # for cylinder-based, sort by order on the drive
                elif (request.start and self.requests[n].start and
                      (request.drive == self.requests[n].drive) and
                      (request.type == self.requests[n].type) and 
                      (request.start > self.requests[n].start)):
                    tmp = self.requests[n]
                    index = self.requests.index(request)
                    self.requests[n] = request
                    self.requests[index] = tmp
                # finally just use when they defined the partition so
                # there's no randomness thrown in
                elif (request.size and self.requests[n].size and
                      (request.size == self.requests[n].size) and
                      (request.uniqueID < self.requests[n].uniqueID)):
                    tmp = self.requests[n]
                    index = self.requests.index(request)
                    self.requests[n] = request
                    self.requests[index] = tmp                
            n = n + 1

        tmp = self.getBootableRequest()

        boot = []
        if tmp:
            for req in tmp:
                # if raid, we want all of the contents of the bootable raid
                if req.type == REQUEST_RAID:
                    for member in req.raidmembers:
                        boot.append(self.getRequestByID(member))
                else:
                    boot.append(req)

        # remove the bootables from the request
        for bootable in boot:
            self.requests.pop(self.requests.index(bootable))

        # move to the front of the list
        boot.extend(self.requests)
        self.requests = boot

    def sanityCheckAllRequests(self, diskset, baseChecks = 0):
        """Do a sanity check of all of the requests.

        This function is called at the end of partitioning so that we
        can make sure you don't have anything silly (like no /, a really
        small /, etc).  Returns (errors, warnings) where each is a list
        of strings or None if there are none.
        If baseChecks is set, the basic sanity tests which the UI runs prior to
        accepting a partition will be run on the requests as well.
        """
        checkSizes = [('/usr', 250), ('/tmp', 50), ('/var', 384),
                      ('/home', 100), ('/boot', 75)]
        warnings = []
        errors = []

        slash = self.getRequestByMountPoint('/')
        if not slash:
            errors.append(_("You have not defined a root partition (/), "
                            "which is required for installation of %s "
                            "to continue.") % (productName,))

        if slash and slash.getActualSize(self, diskset) < 250:
            warnings.append(_("Your root partition is less than 250 "
                              "megabytes which is usually too small to "
                              "install %s.") % (productName,))

        if iutil.getArch() == "ia64":
            bootreq = self.getRequestByMountPoint("/boot/efi")
            if not bootreq or bootreq.getActualSize(self, diskset) < 50:
                errors.append(_("You must create a /boot/efi partition of "
                                "type FAT and a size of 50 megabytes."))

        if iutil.getPPCMacGen() == "NewWorld":
            reqs = self.getBootableRequest()
            found = 0

            bestreq = None
            if reqs:
                for req in reqs:
                    if req.fstype == fsset.fileSystemTypeGet("Apple Bootstrap"):
                        found = 1
                        # the best one is either the first or the first
                        # newly formatted one
                        if ((bestreq is None) or ((bestreq.format == 0) and
                                                  (req.format == 1))):
                            bestreq = req
                        break
                
            if not found:
                errors.append(_("You must create an Apple Bootstrap partition."))

        if (iutil.getPPCMachine() == "pSeries" or
            iutil.getPPCMachine() == "iSeries"):
            reqs = self.getBootableRequest()
            found = 0

            bestreq = None
            if reqs:
                for req in reqs:
                    if req.fstype == fsset.fileSystemTypeGet("PPC PReP Boot"):
                        found = 1
                        # the best one is either the first or the first
                        # newly formatted one
                        if ((bestreq is None) or ((bestreq.format == 0) and
                                                  (req.format == 1))):
                            bestreq = req
                        break
            if iutil.getPPCMachine() == "iSeries" and iutil.hasiSeriesNativeStorage():
                found = 1
                
            if not found:
                errors.append(_("You must create a PPC PReP Boot partition."))

            if bestreq is not None:
                if (iutil.getPPCMachine() == "pSeries"):
                    minsize = 2
                else:
                    minsize = 12
                if bestreq.getActualSize(self, diskset) < minsize:
                    warnings.append(_("Your %s partition is less than %s "
                                      "megabytes which is lower than "
                                      "recommended for a normal %s install.")
                                    %(_("PPC PReP Boot"), minsize, productName))
                    

        for (mount, size) in checkSizes:
            req = self.getRequestByMountPoint(mount)
            if not req:
                continue
            if req.getActualSize(self, diskset) < size:
                warnings.append(_("Your %s partition is less than %s "
                                  "megabytes which is lower than recommended "
                                  "for a normal %s install.")
                                %(mount, size, productName))

        foundSwap = 0
        swapSize = 0
        for request in self.requests:
            if request.fstype and request.fstype.getName() == "swap":
                foundSwap = foundSwap + 1
                swapSize = swapSize + request.getActualSize(self, diskset)
            if baseChecks:
                rc = request.doSizeSanityCheck()
                if rc:
                    warnings.append(rc)
                rc = request.doMountPointLinuxFSChecks()
                if rc:
                    errors.append(rc)
                if isinstance(request, partRequests.RaidRequestSpec):
                    rc = request.sanityCheckRaid(self)
                    if rc:
                        errors.append(rc)

        bootreqs = self.getBootableRequest()
        if bootreqs:
            for bootreq in bootreqs:
                if (bootreq and
                    (isinstance(bootreq, partRequests.RaidRequestSpec)) and
                    (not raid.isRaid1(bootreq.raidlevel))):
                    errors.append(_("Bootable partitions can only be on RAID1 "
                                    "devices."))

                # can't have bootable partition on LV
                if (bootreq and
                    (isinstance(bootreq,
                                partRequests.LogicalVolumeRequestSpec))):
                    errors.append(_("Bootable partitions cannot be on a "
                                    "logical volume."))

                # most arches can't have boot on RAID
                if (bootreq and
                    (isinstance(bootreq, partRequests.RaidRequestSpec)) and
                    (iutil.getArch() not in raid.raidBootArches)):
                    errors.append("Bootable partitions cannot be on a RAID "
                                  "device.")

        if foundSwap == 0:
            warnings.append(_("You have not specified a swap partition.  "
                              "Although not strictly required in all cases, "
                              "it will significantly improve performance for "
                              "most installations."))

        # XXX number of swaps not exported from kernel and could change
        if foundSwap >= 32:
            warnings.append(_("You have specified more than 32 swap devices.  "
                              "The kernel for %s only supports 32 "
                              "swap devices.") % (productName,))

        mem = iutil.memInstalled()
        rem = mem % 16384
        if rem:
            mem = mem + (16384 - rem)
        mem = mem / 1024

        if foundSwap and (swapSize < (mem - 8)) and (mem < 1024):
            warnings.append(_("You have allocated less swap space (%dM) than "
                              "available RAM (%dM) on your system.  This "
                              "could negatively impact performance.")
                            %(swapSize, mem))

        if warnings == []:
            warnings = None
        if errors == []:
            errors = None

        return (errors, warnings)

    def setProtected(self, dispatch):
        """Set any partitions which should be protected to be so."""
        protected = dispatch.method.protectedPartitions()
        if protected:
            for device in protected:
                log("%s is a protected partition" % (device))
                request = self.getRequestByDeviceName(device)
                if request is not None:
                    request.setProtected(1)
                else:
                    log("no request, probably a removable drive")

    def copy (self):
        """Deep copy the object."""
        new = Partitions()
        for request in self.requests:
            new.addRequest(request)
        for delete in self.deletes:
            new.addDelete(delete)
        new.autoPartitionRequests = self.autoPartitionRequests
        new.autoClearPartType = self.autoClearPartType
        new.autoClearPartDrives = self.autoClearPartDrives
        new.nextUniqueID = self.nextUniqueID
        new.useAutopartitioning = self.useAutopartitioning
        new.useFdisk = self.useFdisk
        new.reinitializeDisks = self.reinitializeDisks
        return new

    def getClearPart(self):
        """Get the kickstart directive related to the clearpart being used."""
        clearpartargs = []
        if self.autoClearPartType == CLEARPART_TYPE_LINUX:
            clearpartargs.append('--linux')
        elif self.autoClearPartType == CLEARPART_TYPE_ALL:
            clearpartargs.append('--all')
        else:
            return None

        if self.reinitializeDisks:
            clearpartargs.append('--initlabel')

        if self.autoClearPartDrives:
            drives = string.join(self.autoClearPartDrives, ',')
            clearpartargs.append('--drives=%s' % (drives))

        return "#clearpart %s\n" %(string.join(clearpartargs))
    
    def writeKS(self, f):
        """Write out the partitioning information in kickstart format."""
        f.write("# The following is the partition information you requested\n")
        f.write("# Note that any partitions you deleted are not expressed\n")
        f.write("# here so unless you clear all partitions first, this is\n")
        f.write("# not guaranteed to work\n")
        clearpart = self.getClearPart()
        if clearpart:
            f.write(clearpart)

        # lots of passes here -- parts, raid, volgroup, logvol
        # XXX what do we do with deleted partitions?
        for request in self.requests:
            args = []
            if request.type == REQUEST_RAID:
                continue

            # no fstype, no deal (same with foreigns)
            if not request.fstype or request.fstype.getName() == "foreign":
                continue

            # first argument is mountpoint, which can also be swap or
            # the unique RAID identifier.  I hate kickstart partitioning
            # syntax.  a lot.  too many special cases 
            if request.fstype.getName() == "swap":
                args.append("swap")
            elif request.fstype.getName() == "software RAID":
                # since we guarantee that uniqueIDs are ints now...
                args.append("raid.%s" % (request.uniqueID))
            elif request.fstype.getName() == "physical volume (LVM)":
                # see above about uniqueIDs being ints
                args.append("pv.%s" % (request.uniqueID))
            elif request.fstype.getName() == "PPC PReP Boot":
                args.extend(["prepboot", "--fstype", "\"PPC PReP Boot\""])
            elif request.fstype.getName() == "Apple Bootstrap":
                args.extend(["appleboot", "--fstype", "\"Apple Bootstrap\""])
            elif request.mountpoint:
                fstype = request.fstype.getName()
                if fstype.find(" ") > 0:
                    fstype = "\"%s\"" %(fstype,)
                args.append(request.mountpoint)
                args.append("--fstype")
                args.append(fstype)
            else:
                continue

            # generic options
            if not request.format:
                args.append("--noformat")
            if request.badblocks:
                args.append("--badblocks")

            # preexisting only
            if request.type == REQUEST_PREEXIST and request.device:
                args.append("--onpart")
                args.append(request.device)
            # we have a billion ways to specify new partitions
            elif request.type == REQUEST_NEW:
                if request.size:
                    args.append("--size=%s" % (int(request.size),))
                if request.size == 0:
                    args.append("--size=0")
                if request.grow:
                    args.append("--grow")
                if request.start:
                    args.append("--start=%s" % (request.start))
                if request.end:
                    args.append("--end=%s" % (request.end))
                if request.maxSizeMB:
                    args.append("--maxsize=%s" % (request.maxSizeMB))
                if request.drive:
                    args.append("--ondisk=%s" % (request.drive[0]))
                if request.primary:
                    args.append("--asprimary")
            else: # how the hell did we get this?
                continue

            f.write("#part %s\n" % (string.join(args)))
                
                
        for request in self.requests:
            args = []
            if request.type != REQUEST_RAID:
                continue

            # no fstype, no deal (same with foreigns)
            if not request.fstype or request.fstype.getName() == "foreign":
                continue

            # also require a raidlevel and raidmembers for raid
            if (request.raidlevel == None) or not request.raidmembers:
                continue

            # first argument is mountpoint, which can also be swap
            if request.fstype.getName() == "swap":
                args.append("swap")
            elif request.fstype.getName() == "physical volume (LVM)":
                # see above about uniqueIDs being ints
                args.append("pv.%s" % (request.uniqueID))
            elif request.mountpoint:
                args.append(request.mountpoint)
            else:
                continue

            # generic options
            if not request.format:
                args.append("--noformat")
            if request.fstype:
                fstype = request.fstype.getName()
                if fstype.find(" ") > 0:
                    fstype = "\"%s\"" %(fstype,)
                args.append("--fstype")
                args.append(fstype)
            if request.badblocks:
                args.append("--badblocks")

            args.append("--level=%s" % (request.raidlevel))

            if request.raidspares:
                args.append("--spares=%s" % (request.raidspares))

            # silly raid member syntax
            raidmems = []
            for member in request.raidmembers:
                if (type(member) != type("")) or (member[0:5] != "raid."):
                    raidmems.append("raid.%s" % (member))
                else:
                    raidmems.append(member)
            args.append("%s" % (string.join(raidmems)))

            f.write("#raid %s\n" % (string.join(args)))

        for request in self.requests:
            args = []
            if request.type != REQUEST_VG:
                continue

            args.append(request.volumeGroupName)

            args.append("--pesize=%s" %(request.pesize,))

            # silly pv syntax
            pvs = []
            for member in request.physicalVolumes:
                if (type(member) != type("")) or not member.startswith("pv."):
                    pvs.append("pv.%s" % (member))
                else:
                    pvs.append(member)
            args.append("%s" % (string.join(pvs)))

            f.write("#volgroup %s\n" % (string.join(args)))

        for request in self.requests:
            args = []
            if request.type != REQUEST_LV:
                continue

            # no fstype, no deal (same with foreigns)
            if not request.fstype or request.fstype.getName() == "foreign":
                continue

            # require a vg name and an lv name
            if (request.logicalVolumeName is None or
                request.volumeGroup is None):
                continue

            # first argument is mountpoint, which can also be swap
            if request.fstype.getName() == "swap":
                args.append("swap")
            elif request.mountpoint:
                args.append(request.mountpoint)
            else:
                continue

            # generic options
            if not request.format:
                args.append("--noformat")
            if request.fstype:
                args.append("--fstype")
                args.append(request.fstype.getName())

            vg = self.getRequestByID(request.volumeGroup)
            if vg is None:
                continue

            args.extend(["--name=%s" %(request.logicalVolumeName,),
                         "--vgname=%s" %(vg.volumeGroupName,)])

	    if request.grow:
		if request.startSize is not None:
		    args.append("--size=%s" % (int(request.startSize),))
		else:
		    # shouldnt happen
		    continue
		
		args.append("--grow")
		if request.maxSizeMB is not None and int(request.maxSizeMB) > 0:
		    args.append("--maxsize=%s" % (request.maxSizeMB,))
	    else:
		if request.percent is not None:
		    args.append("--percent=%s" %(request.percent,))
		elif request.size is not None:
		    args.append("--size=%s" %(int(request.size),))
		else:
		    continue
            
            f.write("#logvol %s\n" % (string.join(args)))            

    def deleteAllLogicalPartitions(self, part):
        """Add delete specs for all logical partitions in part."""
        for partition in partedUtils.get_logical_partitions(part.disk):
            partName = partedUtils.get_partition_name(partition)
            request = self.getRequestByDeviceName(partName)
            self.removeRequest(request)
            if request.preexist:
                drive = partedUtils.get_partition_drive(partition)
                delete = partRequests.DeleteSpec(drive, partition.geom.start,
                                                 partition.geom.end)
                self.addDelete(delete)

    def containsImmutablePart(self, part):
        """Returns whether the partition contains parts we can't delete."""
        if not part or (type(part) == type("RAID")) or (type(part) == type(1)):
            return None

        if not part.type & parted.PARTITION_EXTENDED:
            return None

        disk = part.disk
        while part:
            if not part.is_active():
                part = disk.next_partition(part)
                continue

            device = partedUtils.get_partition_name(part)
            request = self.getRequestByDeviceName(device)

            if request:
                if request.getProtected():
                    return _("the partition in use by the installer.")

                if self.isRaidMember(request):
                    return _("a partition which is a member of a RAID array.")

                if self.isLVMVolumeGroupMember(request):
                    return _("a partition which is a member of a LVM Volume Group.")
                    
            part = disk.next_partition(part)
        return None


    def doMetaDeletes(self, diskset):
        """Does the removal of all of the non-physical volumes in the delete list."""

        # have to have lvm on, which requires raid to be started
        diskset.startAllRaid()
        lvm.vgactivate()

        snapshots = {}
        for (lvvg, lv, size, lvorigin) in lvm.lvlist():
            snapshots.setdefault(lv, [])
            if lvorigin:
                snapshots.setdefault(lvorigin, [])
                snapshots[lvorigin].append((lv, lvvg))

        lvm_parent_deletes = []
        tmp = {}
        def addSnap(name, vg):
            snaps = snapshots[name]
            for snap, snapvg in snaps:
                addSnap(snap, snapvg)
            if not tmp.has_key((name, vg)):
                tmp[(name, vg)] = 1
                lvm_parent_deletes.append((name,vg))

        # now, go through and delete logical volumes
        for delete in self.deletes:
            if isinstance(delete, partRequests.DeleteLogicalVolumeSpec):
                if not delete.beenDeleted():
                    addSnap(delete.name, delete.vg)
                    delete.setDeleted(1)

        for name,vg in lvm_parent_deletes:
            log("removing lv %s" % (name,))
            lvm.lvremove(name, vg)

        # now, go through and delete volume groups
        for delete in self.deletes:
            if isinstance(delete, partRequests.DeleteVolumeGroupSpec):
                if not delete.beenDeleted():
                    lvm.vgremove(delete.name)
                    delete.setDeleted(1)

        lvm.vgdeactivate()
        diskset.stopAllRaid()


    def deleteDependentRequests(self, request):
        """Handle deletion of this request and all requests which depend on it.

        eg, delete all logical volumes from a volume group, all volume groups
        which depend on the raid device.

        Side effects: removes all dependent requests from self.requests
                      adds needed dependent deletes to self.deletes
        """