Commit Graph

31 Commits (0352d7733586347f6df5d23ab9bbb54f67e772a0)
 

Author SHA1 Message Date
Jose M. Guisado 0352d77335 context: wipe new partitions by default
Enable partition wipe to remove any old filesystem/RAID signature when
adding a new partition.

For example, when adding a new partition in the same start/end where a
previous filesystem was created.
2023-03-22 13:26:43 +01:00
Jose M. Guisado e4b2964ee0 label: check parttypes identity type
parttypes can be either:

- A code for MBR/DOS
- An UUID for GPT

User needs a label instance before using a parttype getter. If checking
for a parttype using a code, check if the current label support that
kind of parttype identification before retrieving it. Same applies for
UUID based parttypes.

If wrong identity type is used, raise an exception. Errors regarding
parttype are more informative this way.
2023-01-11 17:11:51 +01:00
Jose M. Guisado 34fd2cbe48 add 'string' field getter to parttype
'string' getter wraps fdisk_parttype_get_string to fetch the partition
type UUID in case of a GPT label device.

This is useful to confirm or check the partition type UUID of any device
with a GPT label.

>>> import fdisk
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> pa = cxt.partitions[0]
>>> pa.type.string
'C12A7328-F81F-11D2-BA4B-00A0C93EC93B'
>>> pa.type.name
'EFI System'
2023-01-11 17:11:41 +01:00
Jose M. Guisado dce0b0c1e3 context: fix assign_device
Fixes a bug where calling assign_device with readonly keyword parameter
raises exception stating it takes no keyword arguments.

Context_assign_device has positional and keyword arguments. Adds
flags METH_KEYWORDS in method declaration (see Context_methods).

Replaces format string in PyArg_ParseTupleAndKeywords from "s|p" to
"s|$p". Adds $ after |, meaning all later optional arguments are
also keyword only.
(See https://docs.python.org/3/c-api/arg.html#other-objects)

Empty names in the kwlist array correspond to positional arguments.

Replaces fname variable name with device for better readability.

Fixes 88c7374db2
("context: check self->cxt and rc in assign_device")
2023-01-10 10:15:24 +01:00
Jose M. Guisado 78cdaef9c6 release: bump to version 1.1 2022-12-20 16:38:40 +01:00
Jose M. Guisado 88c7374db2 context: check self->cxt and rc in assign_device
Assert self->cxt before calling libfdisk fdisk_assign_device.

Also check for its return code and raise exception if libfdisk report
any error assigning the device.
2022-12-20 16:33:50 +01:00
Jose M. Guisado 99a98f9236 context: add readonly optional param to assign_device
assign_device with readonly set to false by default to keep consistency
with Context object constructor.
2022-12-20 16:32:22 +01:00
Jose M. Guisado 7478453359 context: add label to repr function
Adds label information to context repr function.

%R is used to get the repr result of a PyObject, in this case this
object is the LabelObject representing the context's label information.

Helps showing more of the context information when printing out a
context object instance.

>>> import fdisk
>>> cxt = fdisk.Context('disk.bin')
>>> cxt
<libfdisk.Context object at 0x7fec52f39330, label=<libfdisk.Label object
at 0x7fec52f39310, name=gpt>, details=True, readonly=False>
2022-12-20 16:18:31 +01:00
Jose M. Guisado e23d9cd027 context: delete unused label field
It's not necessary to store the label in the context type, instead use
the corresponding library function to get the current in-memory label
container.

The device label information is stored in-memory by libfdisk library
when creating a context or assigning a device.

Avoids possible incosistencies between libfdisk in-memory label and
python-libfdisk context type label information.
2022-12-20 16:18:31 +01:00
Jose M. Guisado a732758f2b context: don't raise exception when no partitions found
Allows creation of a context object or device assign if no label
is present in the device.

This case is not frecuent but not critical, do not raise an exception.
For example, a brand new disk with no label or a raw virtual disk
image with no label.
2022-12-20 16:18:24 +01:00
Jose M. Guisado 9bf2d030c9 fdisk: declare kwlist array static 2022-12-15 17:51:17 +01:00
Jose M. Guisado f08c9618ea fdisk: remove unuseful comment
Some of python-libfdisk is inspired by the python bindings of libmount
(from util-linux project).

Remove a comment from pylibmount that slipped into python-libfdisk
2022-12-15 17:46:47 +01:00
Jose M. Guisado 5ec9ec73c8 fdisk: add set_PyErr_from_rc
python-libfdisk raises Python exceptions when the libfdisk reports an
error when executing some function. libfdisk returns negative
errno values when reporting some error.

Adds utility function to set PyErr string based on the strerror of
a given errno code. Useful when raising Python exceptions.
2022-12-15 17:46:42 +01:00
Jose M. Guisado 975acaf549 coding style: line breaks
Only two line breaks separate copyright notice from source.

For the rest of the source file any function declaration or similar
block is separated with a line break from any other block.
Except when a python function definition is previously followed by
a docstring #define block.
2022-12-15 17:37:07 +01:00
Jose M. Guisado eaf98290cf context: add_partition returns partno
libfdisk context add_partition function initializes a passed argument
with the new partition's partno.

If add_partition is successful, return its partno.
2022-12-15 17:37:07 +01:00
Jose M. Guisado bc6f5cbdff partition: add partition type getset
Partition type getset enables modifying and getting the type
of a give libfdisk partition.

The type of a partition is defined using PartType, which
can only be instanced via label specific functions
get_parttype_from_string or get_parttype_from_code.

For example, to set the type of a new partitions to 'EFI System':

>>> import fdisk
>>> pa = fdisk.Partition()
>>> pa.type
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> efitype = cxt.label.get_parttype_from_string("c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
>>> pa.type = efitype

Following the previous example, getting its current partition type:

>>> pa.type
<libfdisk.PartType object at 0x7f2f0a9a12d0, name=EFI System>
2022-12-15 17:37:07 +01:00
Jose M. Guisado 5eba6d4d65 parttype: add parttype class and functions
Parttype is a container for partition types in libfdisk.

In python-libfdisk, the only way to create parttype instances
is using the corresponding label-specific function:

	get_parttype_from_{code,string}

This function wraps libfdisk's label_get_parttype_from_code (lookup DOS
label parttype by hex code) and label_get_parttype_from_string (lookup
GPT parttype by type uuid)

For example, to get the parttype instance of 'EFI System'
partition type of a GPT label, with type uuid
'c12a7328-f81f-11d2-ba4b-00a0c93ec93b':

>>> import fdisk
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> efitype = cxt.label.get_parttype_from_string("c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
>>> efitype
<libfdisk.PartType object at 0x7f503e4a5270, name=EFI System>

See:

https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition-types.html
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Label.html#fdisk-label-get-parttype-from-code
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Label.html#fdisk-label-get-parttype-from-string
2022-12-15 17:37:07 +01:00
Jose M. Guisado 7271bc99db partition: add *_follow_default optional params
Add optional parameters inside init function of partition. Optional
parameters refer to:

- partno_follow_default
- start_follow_default
- end_follow_default

These options can be used in order to enable or disable default partno,
start and end value when adding partitions.

With those optional parameters enabled by default a user is able
to add a partition into the context label without specifying any
attribute.

>>> import fdisk
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> pa = fdisk.Partition()
>>> cxt.add_partition(pa)

This enables:

- "Filling" the rest of the disk with last partition
- No need to track start/end sector for any following partition
- No need to track next partno number for any following partition

See:

https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-partno-follow-default
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-start-follow-default
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-end-follow-default
2022-12-15 17:36:57 +01:00
Jose M. Guisado 8707d43111 context: add add_partition method
This method wraps fdisk_add_partition. Allows modifying in-memory
partition table of a given context.

Remember that changes need to be written to disk using the
relevant fdisk_write_disklabel function wrapper.
2022-12-15 12:27:35 +01:00
Jose M. Guisado 46ad17eaa7 partition: add partno setter
Allows changing in-memory partno field of a Partition instance:

  >>> import fdisk
  >>> pa = fdisk.Partition()
  >>> pa.partno = 3
  >>> pa
  <libfdisk.Partition object at 0x7f4603a38f30, partno=3>

If partno is unset repr shows 'None':

  >>> import fdisk
  >>> pa = fdisk.Partition()
  >>> pa
  <libfdisk.Partition object at 0x7f86c4338f30, partno=None>
2022-12-15 12:27:22 +01:00
Jose M. Guisado bd2703c54e partition: return None if partno is unset
Undefined values in libfdisk should map to None type in Python.

Py_BuildValue("%d", -1); is also incorrectly formatted and raises an
error when executed.
2022-12-15 12:16:52 +01:00
Jose M. Guisado 3d9db0b93b context: add disklabel creation and writing
Adds wrappers for following label related functions from libfdisk:

- fdisk_create_disklabel
- fdisk_write_disklabel

These functions are declared as methods of a Context python object.
2022-12-15 12:16:49 +01:00
Jose M. Guisado e58c21bdc8 context: rename set_size_unit variables
Renames 'cval' to 'szunit' for better readability. This variable is used
to store the size_unit constant that is going to be set using
fdisk_set_size_unit.
2022-12-15 10:52:47 +01:00
Jose M. Guisado 06e59706fc context: add readonly parameter
fdisk_assign_device() contains 'readonly' parameter to indicate how to
open the device.

Assigned device 'readonly' must be false (0) in order to write in-memory
changes to it.
2022-12-15 10:51:04 +01:00
Jose M. Guisado Gomez 0747a84d1c context: add size_unit getset
Size unit can be get or set using 'size_unit' context member.

>>> for pa in cxt.partitions:
...     cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE)
...
'114.6G'
>>> cxt.size_unit
0
>>> cxt.size_unit == fdisk.FDISK_SIZEUNIT_HUMAN
True
>>> cxt.size_unit = fdisk.FDISK_SIZEUNIT_BYTES
>>> for pa in cxt.partitions:
...     cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE)
...
'123010531328'

Use fdisk_get_size_unit to get size unit value.
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Context.html#fdisk-get-size-unit

Use fdisk_set_size_unit to set size unit value.
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Context.html#fdisk-set-size-unit
2022-06-07 16:21:51 +02:00
Jose M. Guisado Gomez ba67cc7a7b Use c99 struct initialization
Declutters PyTypeObject struct initialization when declaring new types.
2022-04-28 15:47:55 +02:00
Jose M. Guisado Gomez c5ae6a3967 context: rename parts to partitions 2022-04-28 15:37:28 +02:00
Jose M. Guisado Gomez b905c1996f fdisk.c: add partition module object
Call Partition_AddModuleObject when initializing the python module.

Fixes bug when using the Partition class but the class has not
been added to the module via Py_TypeReady.

A common error was the type not being ready (missing attributes):

>>> for pa in cxt.parts:
...     print(pa.partno)
...
Traceback (most recent call last):
   File "<stdin>", line 2, in <module>
AttributeError: 'libfdisk.Partition' object has no attribute 'partno'
2022-04-26 17:08:49 +02:00
Jose M. Guisado Gomez ef613790e9 Add COPYING and license headers
LGPL2.1 or later.
2022-04-07 17:03:34 +02:00
Jose M. Guisado Gomez 02e75b5564 Add MANIFEST.in
Add MANIFEST.in to include .h files in source distributions.
2022-04-06 15:40:31 +02:00
Jose M. Guisado Gomez ca92f15e2a Initial commit
Add sources, setup.py and .gitignore

Build/Install:

	python setup.py build
	python setup.py install
2022-04-06 12:56:11 +02:00