English 中文(简体)
Why can t I in python call HDIO_GETGEO?
原标题:
  • 时间:2010-06-27 07:31:25
  •  标签:
  • python
  • ioctl
#!/usr/bin/env python
# -*- coding: utf-8 -*-
########## THIS NOW WORKS! ##########

UNSUITABLE_ENVIRONMENT_ERROR =  
    "This program requires at least Python 2.6 and Linux"

import sys 
import struct
import os
from array import array

# +++ Check environment
try:
    import platform # Introduced in Python 2.3
except ImportError:
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR
if platform.system() != "Linux":
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR
if platform.python_version_tuple()[:2] < (2, 6): 
    print >>sys.stderr, UNSUITABLE_ENVIRONMENT_ERROR

# --- Check environment

HDIO_GETGEO = 0x301 # Linux
import fcntl

def get_disk_geometry(fd):
    geometry = array( c ,"XXXXXXXX")
    fcntl.ioctl(fd, HDIO_GETGEO, geometry, True)
    heads, sectors, cylinders, start =  
        struct.unpack("BBHL",geometry.tostring())
    return {  heads  : heads,  cylinders : cylinders,  sectors : sectors, "start": start }

from pprint import pprint
fd=os.open("/dev/sdb", os.O_RDWR)
pprint(get_disk_geometry(fd))
问题回答

Nobody seems to be able to tell me why you can t do this, but you can do it with ctypes so it doesn t really matter.

#!/usr/bin/env python
from ctypes import *
import os
from pprint import pprint

libc = CDLL("libc.so.6")
HDIO_GETGEO = 0x301 # Linux

class HDGeometry(Structure):
    _fields_ = (("heads", c_ubyte),
                ("sectors", c_ubyte),
                ("cylinders", c_ushort),
                ("start", c_ulong))

    def __repr__(self):
        return """Heads: %s, Sectors %s, Cylinders %s, Start %s""" % (
                self.heads, self.sectors, self.cylinders, self.start)

def get_disk_geometry(fd):
    """ Returns the heads, sectors, cylinders and start of disk as rerpoted by
    BIOS. These us usually bogus, but we still need them"""

    buffer = create_string_buffer(sizeof(HDGeometry))
    g = cast(buffer, POINTER(HDGeometry))
    result = libc.ioctl(fd, HDIO_GETGEO, byref(buffer))
    assert result == 0
    return g.contents

if __name__ == "__main__":
    fd = os.open("/dev/sdb", os.O_RDWR)
    print repr(get_disk_geometry(fd))




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签