Skip to content

Commit

Permalink
Fixed bugs in disk.py were methods failed when working with a fresh d…
Browse files Browse the repository at this point in the history
…isk, added custom exceptions, allow to enter sector_size manually on Size class.
  • Loading branch information
xzased committed May 1, 2012
1 parent 41c0009 commit 0248dbf
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 71 deletions.
41 changes: 20 additions & 21 deletions README
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
Ok, here is a quickie introduction -if u know what I mean-, so this is how it goes for now,
you can set a device and add partitions to it using the following code:
Reparted is my attempt at learning python ctypes module to create bindings for c api's so use it
at your own risk. I was a bit confused on the way parted and pyparted interface works, my aim was
to create a simple interface. It does not have the full set of features parted has to offer, only
enough to create and delete partitions and set the labels. Feel free to check the code and point
out enhancements. For any questions my email is rq.sysadmin@gmail.com, have fun!

############################
Quick Introduction to add partitions:

from reparted import *

dev = device.Device("/dev/sdb") # or whatever your preferred dev is. If you call it without any
# arguments it will probe all standard devices and default to the
# first one it finds
dev = device.Device("/dev/sdb") # or whatever your preferred dev is. If you call it without any
# arguments it will probe all standard devices and default to the
# first one it finds
mydisk = disk.Disk(dev)
sz = size.Size(dev, 200, "MB")
new = disk.Partition(disk, sz) # this defaults the filesystem to ext3 and partition type "NORMAL"
#new = Partition(disk, sz, fs="ext4") # set the filesystem blocks to ext4
#new = Partition(disk, sz, fs="ext4", name="hire_ruben") # set a name
#new = Partition(disk, sz, align='minimal') # sets the partition alignment to minimal.
new = disk.Partition(mydisk, sz) # this defaults the filesystem to ext3 and partition type "NORMAL"
#new = disk.Partition(mydisk, sz, fs="ext4") # set the filesystem blocks to ext4
#new = disk.Partition(mydisk, sz, fs="ext4", name="myname") # set a name
#new = disk.Partition(mydisk, sz, align='minimal') # sets the partition alignment to minimal.
mydisk.add_partition(new)
mydisk.commit()

############################

Partition Stuff
Partition

Now you can access partition properties and methods from the partition instance "new":

Expand All @@ -39,15 +41,13 @@ alignment -Returns either 'minimal' or 'optimal' depending on the partitio
new partition, this may return optimal depending if the partition start
sector turns to be aligned.

############################

Disk Stuff
Disk

You can create an instance of a disk by providing a device:


dev = Device("/dev/sdb") # or whatever your preferred dev is.
disk = Disk(dev)
dev = device.Device("/dev/sdb") # or whatever your preferred dev is.
mydisk = disk.Disk(dev)

These are the properties and methods for disk instances:

Expand All @@ -69,17 +69,16 @@ _ped_device -Returns the ctypes device structure.
get_partition -Returns a Partition instance given a partition number.
set_label -Takes a string (gpt or msdos) and sets the disk partition table.

############################

Device Stuff
Device

You can create an instance of a device by providing the path (ie. /dev/sda). If the path
is not provided, it will probe all standard devices until it finds one and default to it.
dev = Device("/dev/sdb") # or whatever your preferred dev is.
dev = device.Device("/dev/sdb") # or whatever your preferred dev is.

OR

dev = Device()
dev = device.Device()

These are the properties and methods for device instances:

Expand Down
22 changes: 21 additions & 1 deletion reparted/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
__all__ = ['conversion', 'size', 'device', 'disk']
#This file is part of reparted.

#reparted is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#reparted is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with reparted. If not, see <http://www.gnu.org/licenses/>.

__all__ = ['exception', 'conversion', 'size', 'device', 'disk']

from exception import *
from size import *
from device import *
from disk import *
25 changes: 15 additions & 10 deletions reparted/conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#This file is part of reparted.

#reparted is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#reparted is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with reparted. If not, see <http://www.gnu.org/licenses/>.

from ctypes.util import find_library
from ctypes import *

Expand Down Expand Up @@ -46,15 +61,13 @@ class PedDevice(Structure):
('arch_specific', c_void_p),
]

# Geometry stuff starts here
class PedGeometry(Structure):
_fields_ = [
('dev', POINTER(PedDevice)),
('start', PedSector),
('length', PedSector),
('end', PedSector),
]
# Geometry stuff ends here

class PedPartition(Structure):
pass
Expand Down Expand Up @@ -134,8 +147,6 @@ class PedDiskOps(Structure):
('disk_specific', c_void_p),
]

# Start of Alignment/Constraint stuff

class PedConstraint(Structure):
_fields_ = [
('start_align', POINTER(PedAlignment)),
Expand All @@ -145,7 +156,6 @@ class PedConstraint(Structure):
('min_size', PedSector),
('max_size', PedSector),
]
# End of Alignment/Constraint stuff

# Device Function conversions
device_get = parted.ped_device_get
Expand All @@ -167,7 +177,6 @@ class PedConstraint(Structure):
device_get__constraint = parted.ped_device_get_constraint
device_get_constraint.argtypes = [POINTER(PedDevice)]
device_get_constraint.restype = POINTER(PedConstraint)
#devices = POINTER(PedDevice)

# Disk Function conversions
disk_probe = parted.ped_disk_probe
Expand All @@ -191,17 +200,13 @@ class PedConstraint(Structure):
disk_delete_partition.argtypes = [POINTER(PedDisk), POINTER(PedPartition)]
disk_delete_all = parted.ped_disk_delete_all
disk_delete_all.argtypes = [POINTER(PedDisk)]
# I would rather call commit_to_dev and commit_to_os manually
#parted.ped_disk_commit.argtypes = [POINTER(PedDisk)]
disk_commit_to_os = parted.ped_disk_commit_to_os
disk_commit_to_os.argtypes = [POINTER(PedDisk)]
disk_commit_to_dev = parted.ped_disk_commit_to_dev
disk_commit_to_dev.argtypes = [POINTER(PedDisk)]
disk_destroy = parted.ped_disk_destroy
disk_destroy.argtypes = [POINTER(PedDisk)]
disk_destroy.restype = None
#disk_print = parted.ped_disk_print
#disk_print.argtypes = [POINTER(PedDisk)]
disk_get_type = parted.ped_disk_type_get
disk_get_type.restype = POINTER(PedDiskType)
disk_remove_partition = parted.ped_disk_remove_partition
Expand Down
22 changes: 20 additions & 2 deletions reparted/device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#This file is part of reparted.

#reparted is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#reparted is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with reparted. If not, see <http://www.gnu.org/licenses/>.

from conversion import *
from disk import *
from exception import DeviceError
import os

device_type = {
Expand Down Expand Up @@ -57,6 +73,8 @@ def __init__(self, path=None, dev=None):
self.__device = dev
else:
self.__device = self._probe_ped_device()
if not bool(self.__device):
raise DeviceError(500)

@property
def _ped_device(self):
Expand Down Expand Up @@ -131,9 +149,9 @@ def did(self):
def _probe_ped_device(self):
for path in standard_devices:
dev = device_probe(path)
if dev:
if bool(dev):
return dev
raise Exception("No devices found.")
raise DeviceError(500)

def probe_standard_devices():
devices = []
Expand Down
Loading

0 comments on commit 0248dbf

Please sign in to comment.