summaryrefslogtreecommitdiffstats
path: root/isys/isys.py
blob: 0c1943bc045006e1df205158a3d847695ec69c04 (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
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
#
# isys.py - installer utility functions and glue for C module
#
# Matt Wilson <msw@redhat.com>
# Erik Troan <ewt@redhat.com>
# Jeremy Katz <katzj@redhat.com>
#
# Copyright 2001 - 2004 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 _isys
import string
import os
import os.path
import socket
import stat
import posix
import sys
import kudzu
import iutil
import warnings
import resource
import re
import rhpl
import struct
import block

import logging
log = logging.getLogger("anaconda")
import warnings

mountCount = {}
raidCount = {}

MIN_RAM = _isys.MIN_RAM
MIN_GUI_RAM = _isys.MIN_GUI_RAM
EARLY_SWAP_RAM = _isys.EARLY_SWAP_RAM

def pathSpaceAvailable(path, fsystem = "ext2"):
    return _isys.devSpaceFree(path)

def spaceAvailable(device, fsystem = "ext2"):
    mount(device, "/mnt/space", fstype = fsystem)
    space = _isys.devSpaceFree("/mnt/space/.")
    umount("/mnt/space")
    return space

def fsSpaceAvailable(fsystem):
    return _isys.devSpaceFree(fsystem)

mdadmOutput = "/tmp/mdadmout"

class MdadmError(Exception):
    """An error occurred when running mdadm."""

    def __init__(self, args, name=None):
        self.args = args
        self.name = name
        self.log = self.getCmdOutput()

    def getCmdOutput(self):
        f = open(mdadmOutput, "r")
        lines = reduce(lambda x,y: x + [string.strip(y),], f.readlines(), [])
        lines = string.join(reduce(lambda x,y: x + ["   %s" % (y,)], \
                                    lines, []), "\n")
        return lines

    def __str__(self):
        s = ""
        if not self.name is None:
            s = " for device %s" % (self.name,)
        command = "mdadm " + string.join(self.args, " ")
        return "'%s' failed%s\nLog:\n%s" % (command, s, self.log)

def _mdadm(*args):
    try:
        lines = iutil.execWithCapture("mdadm", args, stderr = mdadmOutput)
        lines = string.split(lines, '\n')
        lines = reduce(lambda x,y: x + [y.strip(),], lines, [])
        return lines
    except:
        raise MdadmError, args

def _getRaidInfo(drive):
    log.info("mdadm -E %s" % (drive,))
    try:
        lines = _mdadm("-E", drive)
    except MdadmError:
        ei = sys.exc_info()
        ei[1].name = drive
        raise ei[0], ei[1], ei[2]

    info = {
            'major': "-1",
            'minor': "-1",
            'uuid' : "",
            'level': -1,
            'nrDisks': -1,
            'totalDisks': -1,
            'mdMinor': -1,
        }

    for line in lines:
        vals = string.split(string.strip(line), ' : ')
        if len(vals) != 2:
            continue
        if vals[0] == "Version":
            vals = string.split(vals[1], ".")
            info['major'] = vals[0]
            info['minor'] = vals[1]
        elif vals[0] == "UUID":
            info['uuid'] = vals[1]
        elif vals[0] == "Raid Level":
            info['level'] = int(vals[1][4:])
        elif vals[0] == "Raid Devices":
            info['nrDisks'] = int(vals[1])
        elif vals[0] == "Total Devices":
            info['totalDisks'] = int(vals[1])
        elif vals[0] == "Preferred Minor":
            info['mdMinor'] = int(vals[1])
        else:
            continue

    if info['uuid'] == "":
        raise ValueError, info

    return info

def _stopRaid(mdDevice):
    log.info("mdadm --stop %s" % (mdDevice,))
    try:
        _mdadm("--stop", mdDevice)
    except MdadmError:
        ei = sys.exc_info()
        ei[1].name = mdDevice
        raise ei[0], ei[1], ei[2]

def raidstop(mdDevice):
    log.info("stopping raid device %s" %(mdDevice,))
    if raidCount.has_key (mdDevice):
        if raidCount[mdDevice] > 1:
            raidCount[mdDevice] = raidCount[mdDevice] - 1
            return
        del raidCount[mdDevice]

    devInode = "/dev/%s" % mdDevice

    makeDevInode(mdDevice, devInode)
    try:
        _stopRaid(devInode)
    except:
        pass

def _startRaid(mdDevice, mdMinor, uuid):
    log.info("mdadm -A --uuid=%s --super-minor=%s %s" % (uuid, mdMinor, mdDevice))
    try:
        _mdadm("-A", "--uuid=%s" % (uuid,), "--super-minor=%s" % (mdMinor,), \
                mdDevice)
    except MdadmError:
        ei = sys.exc_info()
        ei[1].name = mdDevice
        raise ei[0], ei[1], ei[2]

def raidstart(mdDevice, aMember):
    log.info("starting raid device %s" %(mdDevice,))    
    if raidCount.has_key(mdDevice) and raidCount[mdDevice]:
	raidCount[mdDevice] = raidCount[mdDevice] + 1
	return

    raidCount[mdDevice] = 1

    mdInode = "/dev/%s" % mdDevice
    mbrInode = "/dev/%s" % aMember

    makeDevInode(mdDevice, mdInode)
    makeDevInode(aMember, mbrInode)

    minor = os.minor(os.stat(mdInode).st_rdev)
    try:
        info = _getRaidInfo(mbrInode)
        if info.has_key('mdMinor'):
            minor = info['mdMinor']
        _startRaid(mdInode, minor, info['uuid'])
    except:
        pass

def wipeRaidSB(device):
    try:
        fd = os.open(device, os.O_WRONLY)
    except OSError, e:
        log.warning("error wiping raid device superblock for %s: %s", device, e)
        return

    try:
        _isys.wiperaidsb(fd)
    finally:
        os.close(fd)
    return

def raidsb(mdDevice):
    makeDevInode(mdDevice, "/dev/%s" % mdDevice)
    return raidsbFromDevice("/dev/%s" % mdDevice)

def raidsbFromDevice(device):
    try:
        info = _getRaidInfo(device)
        return (info['major'], info['minor'], info['uuid'], info['level'],
                info['nrDisks'], info['totalDisks'], info['mdMinor'])
    except:
        raise ValueError

def getRaidChunkFromDevice(device):
    fd = os.open(device, os.O_RDONLY)
    rc = 64
    try:
        rc = _isys.getraidchunk(fd)
    finally:
        os.close(fd)
    return rc

def losetup(device, file, readOnly = 0):
    if readOnly:
	mode = os.O_RDONLY
    else:
	mode = os.O_RDWR
    targ = os.open(file, mode)
    loop = os.open(device, mode)
    try:
        _isys.losetup(loop, targ, file)
    finally:
        os.close(loop)
        os.close(targ)

def lochangefd(device, file):
    loop = os.open(device, os.O_RDONLY)
    targ = os.open(file, os.O_RDONLY)
    try:
        _isys.lochangefd(loop, targ)
    finally:
        os.close(loop)
        os.close(targ)

def unlosetup(device):
    loop = os.open(device, os.O_RDONLY)
    try:
        _isys.unlosetup(loop)
    finally:
        os.close(loop)

def ddfile(file, megs, pw = None):
    buf = '\x00' * (1024 * 256)

    fd = os.open(file, os.O_RDWR | os.O_CREAT)

    total = megs * 4	    # we write out 1/4 of a meg each time through

    if pw:
	(fn, title, text) = pw
	win = fn(title, text, total - 1)

    for n in range(total):
	os.write(fd, buf)
	if pw:
	    win.set(n)

    if pw:
	win.pop()

    os.close(fd)

def mount(device, location, fstype = "ext2", readOnly = 0, bindMount = 0, remount = 0):
    location = os.path.normpath(location)

    # We don't need to create device nodes for devices that start with '/'
    # (like '/usbdevfs') and also some special fake devices like 'proc'.
    # First try to make a device node and if that fails, assume we can
    # mount without making a device node.  If that still fails, the caller
    # will have to deal with the exception.
    # We note whether or not we created a node so we can clean up later.
    createdNode = 0
    if device and device != "none" and device[0] != "/":
	devName = "/tmp/%s" % device
	
	try:
	    makeDevInode (device, devName)
	    device = devName
	    createdNode = 1
	except SystemError:
	    pass

    if mountCount.has_key(location) and mountCount[location] > 0:
	mountCount[location] = mountCount[location] + 1
	return

    log.debug("isys.py:mount()- going to mount %s on %s" %(device, location))
    rc = _isys.mount(fstype, device, location, readOnly, bindMount, remount)

    if not rc:
	mountCount[location] = 1

    # did we create a node, if so remove
    if createdNode:
	os.unlink(device)

    return rc

def umount(what, removeDir = 1):
    what = os.path.normpath(what)

    if not os.path.isdir(what):
	raise ValueError, "isys.umount() can only umount by mount point"

    if mountCount.has_key(what) and mountCount[what] > 1:
	mountCount[what] = mountCount[what] - 1
	return

    rc = _isys.umount(what)

    if removeDir and os.path.isdir(what):
	os.rmdir(what)

    if not rc and mountCount.has_key(what):
	del mountCount[what]

    return rc

def smpAvailable():
    return _isys.smpavailable()

htavailable = _isys.htavailable

def chroot (path):
    warnings.warn("isys.chroot is deprecated.  Use os.chroot instead.",
                  DeprecationWarning, stacklevel=2)
    return os.chroot (path)

def checkBoot (path):
    return _isys.checkBoot (path)

def swapoff (path):
    return _isys.swapoff (path)

def swapon (path):
    return _isys.swapon (path)

def loadFont():
    return _isys.loadFont ()

def loadKeymap(keymap):
    return _isys.loadKeymap (keymap)

classMap = { "disk": kudzu.CLASS_HD,
             "cdrom": kudzu.CLASS_CDROM,
             "floppy": kudzu.CLASS_FLOPPY,
             "tape": kudzu.CLASS_TAPE }

cachedDrives = None

def flushDriveDict():
    global cachedDrives
    cachedDrives = None

def driveDict(klassArg):
    import parted
    global cachedDrives
    if cachedDrives is None:
        # FIXME: need to add dasd probing to kudzu
        devs = kudzu.probe(kudzu.CLASS_HD | kudzu.CLASS_CDROM | \
                           kudzu.CLASS_FLOPPY | kudzu.CLASS_TAPE,
                           kudzu.BUS_UNSPEC, kudzu.PROBE_SAFE)
        new = {}
        for dev in devs:
            device = dev.device
            if device is None: # none devices make no sense
                continue
            if dev.deviceclass != classMap["disk"]:
                new[device] = dev
                continue
            try:
                devName = "/dev/%s" % (device,)
                makeDevInode(device, devName)

                if not mediaPresent (device):
                    new[device] = dev
                    continue

                # blacklist the device which the live image is running from
                # installing over that is almost certainly the wrong
                # thing to do.
                if os.path.exists("/dev/live") and \
                       stat.S_ISBLK(os.stat("/dev/live")[stat.ST_MODE]):
                    livetarget = os.path.realpath("/dev/live")
                    if livetarget.startswith(devName):
                        log.info("%s looks to be the live device; ignoring" % (device,))
                        continue
                    

                if device.startswith("sd"):
                    peddev = parted.PedDevice.get(devName)
                    model = peddev.model

                    # blacklist *STMF on power5 iSeries boxes
                    if rhpl.getArch() == "ppc" and \
                            model.find("IBM *STMF KERNEL") != -1:
                        log.info("%s looks like STMF, ignoring" % (device,))
                        del peddev
                        continue

                    # blacklist PS3 flash 
                    if rhpl.getArch() == "ppc" and \
                            model.find("SCEI Flash-5") != -1:
                        log.info("%s looks like PS3 flash, ignoring" % \
                            (device,))
                        del peddev
                        continue

                    # blacklist DGC/EMC LUNs for which we have no ACL.
                    # We should be ignoring LUN_Z for all vendors, but I
                    # don't know how (if) other vendors encode this into
                    # the model info.
                    #
                    # XXX I need to work some SCC2 LUN mode page detection
                    # into libbdevid, and then this should use that instead.
                    # -- pjones
                    if str(peddev.model) == "DGC LUNZ":
                        log.info("%s looks like a LUN_Z device, ignoring" % \
                            (device,))
                        del peddev
                        continue

                    del peddev
                new[device] = dev
            except Exception, e:
                log.debug("exception checking disk blacklist on %s: %s" % \
                    (device, e))
        cachedDrives = new

    ret = {}
    for key,dev in cachedDrives.items():
        # XXX these devices should have deviceclass attributes.  Or they
        # should all be subclasses in a device tree and we should be able
        # to use isinstance on all of them.  Not both.
        if isinstance(dev, block.MultiPath) or isinstance(dev, block.RaidSet):
            if klassArg == "disk":
                ret[key] = dev
        elif dev.deviceclass == classMap[klassArg]:
            ret[key] = dev.desc
    return ret

def hardDriveDict():
    return driveDict("disk")

def floppyDriveDict():
    return driveDict("floppy")

def cdromList():
    list = driveDict("cdrom").keys()
    list.sort()
    return list

def tapeDriveList():
    list = driveDict("tape").keys()
    list.sort()
    return list

def getDasdPorts():
    return _isys.getDasdPorts()

def isUsableDasd(device):
    return _isys.isUsableDasd(device)

def isLdlDasd(device):
    return _isys.isLdlDasd(device)

# read /proc/dasd/devices and get a mapping between devs and the dasdnum
def getDasdDevPort():
    ret = {}
    f = open("/proc/dasd/devices", "r")
    lines = f.readlines()
    f.close()

    for line in lines:
        index = line.index("(")
        dasdnum = line[:index]
        
        start = line[index:].find("dasd")
        end = line[index + start:].find(":")
        dev = line[index + start:end + start + index].strip()
        
        ret[dev] = dasdnum

    return ret

# get active/ready state of a dasd device
# returns 0 if we're fine, 1 if not
def getDasdState(dev):
    devs = getDasdDevPort()
    if not devs.has_key(dev):
        log.warning("don't have %s in /dev/dasd/devices!" %(dev,))
        return 0
    
    f = open("/proc/dasd/devices", "r")
    lines = f.readlines()
    f.close()
    
    for line in lines:
        if not line.startswith(devs[dev]):
            continue
        # 2.6 seems to return basic
        if line.find(" basic") != -1:
            return 1
        # ... and newer 2.6 returns unformatted.  consistency!
        if line.find(" unformatted") != -1:
            return 1
        
    return 0


def makeDevInode(name, fn=None):
    if fn:
        if fn.startswith("/tmp"):
            warnings.warn("device node created in /tmp", stacklevel=2)
        if os.path.exists(fn):
            return fn
        _isys.mkdevinode(name, fn)
        return fn
    path = '/dev/%s' % (name,)
    try:
        os.stat(path)
    except OSError:
        path = '/tmp/%s' % (name,)
        _isys.mkdevinode(name, path)
    return path

def makedev(major, minor):
    warnings.warn("isys.makedev is deprecated.  Use os.makedev instead.",
                  DeprecationWarning, stacklevel=2)
    return os.makedev(major, minor)

def mknod(pathname, mode, dev):
    warnings.warn("isys.mknod is deprecated.  Use os.mknod instead.",
                  DeprecationWarning, stacklevel=2)
    return os.mknod(pathname, mode, dev)

def inet_calcNetBroad (ip, nm):
    (ipaddr,) = struct.unpack('I', socket.inet_pton(socket.AF_INET, ip))
    ipaddr = socket.ntohl(ipaddr)

    (nmaddr,) = struct.unpack('I', socket.inet_pton(socket.AF_INET, nm))
    nmaddr = socket.ntohl(nmaddr)

    netaddr = ipaddr & nmaddr
    bcaddr = netaddr | (~nmaddr)

    nw = socket.inet_ntop(socket.AF_INET, struct.pack('!I', netaddr))
    bc = socket.inet_ntop(socket.AF_INET, struct.pack('!I', bcaddr))

    return (nw, bc)

def getopt(*args):
    warnings.warn("isys.getopt is deprecated.  Use optparse instead.",
                  DeprecationWarning, stacklevel=2)
    return apply(_isys.getopt, args)

def doProbeBiosDisks():
    if rhpl.getArch() not in ("i386", "x86_64"):
        return None
    return _isys.biosDiskProbe()

def doGetBiosDisk(mbrSig):
    return _isys.getbiosdisk(mbrSig)

handleSegv = _isys.handleSegv

biosdisks = {}
for d in range(80, 80 + 15):
    disk = doGetBiosDisk("%d" %(d,))
    #print "biosdisk of %s is %s" %(d, disk)
    if disk is not None:
        biosdisks[disk] = d

def compareDrives(first, second):
    if biosdisks.has_key(first) and biosdisks.has_key(second):
        one = biosdisks[first]
        two = biosdisks[second]
        if (one < two):
            return -1
        elif (one > two):
            return 1

    if first.startswith("hd"):
        type1 = 0
    elif first.startswith("sd"):
        type1 = 1
    elif first.startswith("xvd"):
        type1 = -1
    else:
        type1 = 2

    if second.startswith("hd"):
        type2 = 0
    elif second.startswith("sd"):
	type2 = 1
    elif second.startswith("xvd"):
        type2 = -1
    else:
	type2 = 2

    if (type1 < type2):
	return -1
    elif (type1 > type2):
	return 1
    else:
	len1 = len(first)
	len2 = len(second)

	if (len1 < len2):
	    return -1
	elif (len1 > len2):
	    return 1
	else:
	    if (first < second):
		return -1
	    elif (first > second):
		return 1

    return 0

def compareNetDevices(first, second):
    try:
        trimmed_first = int(first.lstrip(string.letters))
        trimmed_second = int(second.lstrip(string.letters))
    except:
        return 0

    if trimmed_first < trimmed_second:
        return -1
    elif trimmed_first > trimmed_second:
        return 1
    else:
        return 0

# called from anaconda (basically rescue mode only) to configure an interface
# when we have collected all of the configuration information from the user
def configNetDevice(device, gateway):
    devname = device.get('device')
    ipv4 = device.get('ipaddr')
    netmask = device.get('netmask')
    ipv6 = device.get('ipv6addr')
    prefix = device.get('ipv6prefix')

    return _isys.confignetdevice(devname, ipv4, netmask, ipv6, prefix, gateway)

def resetResolv():
    return _isys.resetresolv()

def setResolvRetry(count):
    return _isys.setresretry(count)

# called from anaconda to run DHCP (that's DHCP, DHCPv6, or auto neighbor
# discovery) on a particular interface
def dhcpNetDevice(device):
    # returns None on failure, "" if no nameserver is found, nameserver IP
    # otherwise
    devname = device.get('device')
    v4 = 0
    v6 = 0
    v4method = ''
    v6method = ''
    klass = device.get('dhcpclass')

    if device.get('useipv4'):
        v4 = 1
        if device.get('bootproto') == 'dhcp':
            v4method = 'dhcp'
        else:
            v4method = 'manual'

    if device.get('useipv6'):
        v6 = 1
        if device.get('ipv6_autoconf') == 'yes':
            v6method = 'auto'
        elif device.get('ipv6_autoconf') == 'no' and device.get('bootproto') == 'dhcp':
            v6method = 'dhcp'
        else:
            v6method = 'manual'

    if klass is None:
        klass = ''

    return _isys.dhcpnetdevice(devname, v4, v4method, v6, v6method, klass)

def readXFSLabel_int(device):
    try:
        fd = os.open(device, os.O_RDONLY)
    except:
        return None

    try:
        buf = os.read(fd, 128)
        os.close(fd)
    except OSError, e:
        log.debug("error reading xfs label on %s: %s" %(device, e))
        try:
            os.close(fd)
        except:
            pass
        return None

    xfslabel = None
    if len(buf) == 128 and buf[0:4] == "XFSB":
        xfslabel = string.rstrip(buf[108:120],"\0x00")

    return xfslabel
    
def readXFSLabel(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
	label = readXFSLabel_int("/tmp/disk")
	os.unlink("/tmp/disk")
    else:
        label = readXFSLabel_int(device)
    return label

def readJFSLabel_int(device):
    jfslabel = None
    try:
        fd = os.open(device, os.O_RDONLY)
    except:
        return jfslabel

    try:
        os.lseek(fd, 32768, 0)
        buf = os.read(fd, 180)
        os.close(fd)
    except OSError, e:
        log.debug("error reading jfs label on %s: %s" %(device, e))
        try:
            os.close(fd)
        except:
            pass
        return jfslabel

    if (len(buf) == 180 and buf[0:4] == "JFS1"):
        jfslabel = string.rstrip(buf[152:168],"\0x00")

    return jfslabel
    
def readJFSLabel(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
	label = readJFSLabel_int("/tmp/disk")
	os.unlink("/tmp/disk")
    else:
        label = readJFSLabel_int(device)
    return label

def readSwapLabel_int(device):
    label = None
    try:
        fd = os.open(device, os.O_RDONLY)
    except:
        return label

    pagesize = resource.getpagesize()
    try:
        buf = os.read(fd, pagesize)
        os.close(fd)
    except OSError, e:
        log.debug("error reading swap label on %s: %s" %(device, e))
        try:
            os.close(fd)
        except:
            pass
        return label

    if ((len(buf) == pagesize) and (buf[pagesize - 10:] == "SWAPSPACE2")):
        label = string.rstrip(buf[1052:1068], "\0x00")
    return label

def readSwapLabel(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
        label = readSwapLabel_int("/tmp/disk")
        os.unlink("/tmp/disk")
    else:
        label = readSwapLabel_int(device)
    return label

def readExt2Label(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
        label = _isys.e2fslabel("/tmp/disk");
        os.unlink("/tmp/disk")
    else:
        label = _isys.e2fslabel(device)
    return label

def readReiserFSLabel_int(device):
    label = None

    try:
        fd = os.open(device, os.O_RDONLY)
    except OSError, e:
        log.debug("error opening device %s: %s" % (device, e))
        return label

    # valid block sizes in reiserfs are 512 - 8192, powers of 2
    # we put 4096 first, since it's the default
    # reiserfs superblock occupies either the 2nd or 16th block
    for blksize in (4096, 512, 1024, 2048, 8192):
        for start in (blksize, (blksize*16)):
            try:
                os.lseek(fd, start, 0)
                # read 120 bytes to get s_magic and s_label
                buf = os.read(fd, 120)

                # see if this block is the superblock
                # this reads reiserfs_super_block_v1.s_magic as defined
                # in include/reiserfs_fs.h in the reiserfsprogs source
                m = string.rstrip(buf[52:61], "\0x00")
                if m == "ReIsErFs" or m == "ReIsEr2Fs" or m == "ReIsEr3Fs":
                    # this reads reiserfs_super_block.s_label as
                    # defined in include/reiserfs_fs.h
                    label = string.rstrip(buf[100:116], "\0x00")
                    os.close(fd)
                    return label
            except OSError, e:
                # [Error 22] probably means we're trying to read an
                # extended partition. 
                log.debug("error reading reiserfs label on %s: %s" %(device, e))
    
                try:
                    os.close(fd)
                except:
                    pass
    
                return label

    os.close(fd)
    return label

def readReiserFSLabel(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
	label = readReiserFSLabel_int("/tmp/disk")
        os.unlink("/tmp/disk")
    else:
        label = readReiserFSLabel_int(device)
    return label

def readFSLabel(device, makeDevNode = 1):
    label = readExt2Label(device, makeDevNode)
    if label is None:
        label = readSwapLabel(device, makeDevNode)
    if label is None:
        label = readXFSLabel(device, makeDevNode)
    if label is None:
        label = readJFSLabel(device, makeDevNode)
    if label is None:
        label = readReiserFSLabel(device, makeDevNode)
    return label

def ext2Clobber(device, makeDevNode = 1):
    _isys.e2fsclobber(device)

def ext2IsDirty(device):
    makeDevInode(device, "/tmp/disk")
    label = _isys.e2dirty("/tmp/disk");
    os.unlink("/tmp/disk")
    return label

def ext2HasJournal(device, makeDevNode = 1):
    if makeDevNode:
        makeDevInode(device, "/tmp/disk")
        hasjournal = _isys.e2hasjournal("/tmp/disk");
        os.unlink("/tmp/disk")
    else:
        hasjournal = _isys.e2hasjournal(device);
    return hasjournal

def ejectCdrom(device, makeDevice = 1):
    if makeDevice:
        makeDevInode(device, "/tmp/cdrom")
        fd = os.open("/tmp/cdrom", os.O_RDONLY|os.O_NONBLOCK)
    else:
        fd = os.open(device, os.O_RDONLY|os.O_NONBLOCK)

    # this is a best effort
    try:
	_isys.ejectcdrom(fd)
    except SystemError, e:
        log.warning("error ejecting cdrom (%s): %s" %(device, e))
	pass

    os.close(fd)

    if makeDevice:
        os.unlink("/tmp/cdrom")

def driveUsesModule(device, modules):
    """Returns true if a drive is using a prticular module.  Only works
       for SCSI devices right now."""

    if not isinstance(modules, ().__class__) and not \
            isinstance(modules, [].__class__):
        modules = [modules]
        
    if device[:2] == "hd":
        return False
    rc = False
    if os.access("/tmp/scsidisks", os.R_OK):
        sdlist=open("/tmp/scsidisks", "r")
        sdlines = sdlist.readlines()
        sdlist.close()
        for l in sdlines:
            try:
                # each line has format of:  <device>  <module>
                (sddev, sdmod) = string.split(l)

                if sddev == device:
                    if sdmod in modules:
                        rc = True
                        break
            except:
                    pass
    return rc

def mediaPresent(device):
    try:
        fd = os.open("/dev/%s" % device, os.O_RDONLY)
    except OSError, (errno, strerror):
        # error 123 = No medium found
        if errno == 123:
            return False
        else:
            return True
    else:
        os.close(fd)
        return True

def driveIsIscsi(device):
    # ewww.  just ewww.
    if not os.path.islink("/sys/block/%s/device" %(device,)):
        return False
    target = os.readlink("/sys/block/%s/device" %(device,))
    if re.search("/platform/host[0-9]*/session[0-9]*/target[0-9]*:[0-9]*:[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*", target) is not None:
        return True
    return False

def vtActivate (num):
    _isys.vtActivate (num)

def isPsudoTTY (fd):
    return _isys.isPsudoTTY (fd)

def sync ():
    return _isys.sync ()

def isIsoImage(file):
    return _isys.isisoimage(file)

def fbinfo():
    return _isys.fbinfo()

def cdRwList():
    if not os.access("/proc/sys/dev/cdrom/info", os.R_OK): return []

    f = open("/proc/sys/dev/cdrom/info", "r")
    lines = f.readlines()
    f.close()

    driveList = []
    finalDict = {}

    for line in lines:
	line = string.split(line, ':', 1)

	if (line and line[0] == "drive name"):
	    line = string.split(line[1])
	    # no CDROM drives
	    if not line:  return []

	    for device in line:
		if device[0:2] == 'sr':
		    device = "scd" + device[2:]
		driveList.append(device)
	elif ((line and line[0] == "Can write CD-R") or
	      (line and line[0] == "Can write CD-RW")):
	    line = string.split(line[1])
	    field = 0
	    for ability in line:
		if ability == "1":
		    finalDict[driveList[field]] = 1
		field = field + 1

    l = finalDict.keys()
    l.sort()
    return l

def ideCdRwList():
    newList = []
    for dev in cdRwList():
	if dev[0:2] == 'hd': newList.append(dev)

    return newList

def getLinkStatus(dev):
    if dev == '' or dev is None:
        return False

    # getLinkStatus returns 1 for link, 0 for no link, -1 for unknown state
    if _isys.getLinkStatus(dev) == 1:
        return True
    else:
        return False

def getMacAddress(dev):
    return _isys.getMacAddress(dev)

def isWireless(dev):
    return _isys.isWireless(dev)

def getIPAddress(dev):
    return _isys.getIPAddress(dev)

def resetFileContext(fn, instroot = '/'):
    return _isys.resetFileContext(fn, instroot)

def prefix2netmask(prefix):
    return _isys.prefix2netmask(prefix)

def netmask2prefix (netmask):
    prefix = 0

    while prefix < 33:
        if (prefix2netmask(prefix) == netmask):
            return prefix

        prefix += 1

    return prefix

isPAE = None
def isPaeAvailable():
    global isPAE
    if isPAE is not None:
        return isPAE

    isPAE = False
    if rhpl.getArch() not in ("i386", "x86_64"):
        return isPAE

    try:
        f = open("/proc/iomem", "r")
        lines = f.readlines()
        for line in lines:
            if line[0].isspace():
                continue
            start = line.split(' ')[0].split('-')[0]
            start = long(start, 16)

            if start > 0x100000000L:
                isPAE = True
                break

        f.close()
    except:
        pass

    return isPAE

auditDaemon = _isys.auditdaemon

handleSegv = _isys.handleSegv

printObject = _isys.printObject
bind_textdomain_codeset = _isys.bind_textdomain_codeset
isVioConsole = _isys.isVioConsole