Welcome to lvm2py’s documentation!

Contents:

Indices and tables

class lvm.LVM

The LVM class is used as a wrapper to the global lvm handle provided by the api.

LVM works by providing a “handle” that represents each object instance (volume groups, logical and physical volumes). This class provides the “global” lvm handle used to access each of the above mentioned objects:

from lvm2py import *

lvm = LVM()
close()

Closes the lvm handle. Usually you would never need to use this method unless you are trying to do operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
create_vg(name, devices)

Returns a new instance of VolumeGroup with the given name and added physycal volumes (devices):

from lvm2py import *

lvm = LVM()
vg = lvm.create_vg("myvg", ["/dev/sdb1", "/dev/sdb2"])

Args:

  • name (str): A volume group name.
  • devices (list): A list of device paths.

Raises:

  • HandleError, CommitError, ValueError
get_vg(name, mode='r')

Returns an instance of VolumeGroup. The name parameter should be an existing volume group. By default, all volume groups are open in “read” mode:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg")

To open a volume group with write permissions set the mode parameter to “w”:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")

Args:

  • name (str): An existing volume group name.
  • mode (str): “r” or “w” for read/write respectively. Default is “r”.

Raises:

  • HandleError
handle

Returns the lvm handle provided by the api represented by a ctypes opaque structure. After calling the close() method this will return None.

lvm_version

Returns the lvm library version.

open()

Obtains the lvm handle. Usually you would never need to use this method unless you are trying to do operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
remove_vg(vg)

Removes a volume group:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
lvm.remove_vg(vg)

Args:

  • vg (obj): A VolumeGroup instance.

Raises:

  • HandleError, CommitError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised.

classmethod set_system_dir(path)

This class method overrides the default system directory:

from lvm2py import *

lvm = LVM()
lvm.set_system_dir("/path/to/dir")

Note

LVM probes the environment variable LVM_SYSTEM_DIR, if this is not set it will use the default directory (usually /etc/lvm/) .

system_dir

Returns the lvm system directory. Returns None if you have not set it using the set_system_dir method.

vgscan()

Probes the system for volume groups and returns a list of VolumeGroup instances:

from lvm2py import *

lvm = LVM()
vgs = lvm.vgscan()

Raises:

  • HandleError
class vg.VolumeGroup(handle, name, mode='r')

The VolumeGroup class is used as a wrapper to the global vg_t handle provided by the LVM api.

LVM works by providing a “handle” that represents each object instance (volume groups, logical and physical volumes). This class provides the “global” vg_t handle used to do volume group operations. You need to provide an LVM instance to instantiate this class:

from lvm2py import *

lvm = LVM()
vg1 = lvm.get_vg("myexistingvg")

# or just provide the LVM instance
vg2 = VolumeGroup(lvm, "myexistingvg", mode="w")

Raises:

  • HandleError

Note

To create a new volume group use the LVM method create_vg.

add_pv(device)

Initializes a device as a physical volume and adds it to the volume group:

    from lvm2py import *

    lvm = LVM()
    vg = lvm.get_vg("myvg", "w")
    vg.add_pv("/dev/sdbX")

*Args:*

*       device (str):   An existing device.

*Raises:*

*       ValueError, CommitError, HandleError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised.

close()

Closes the lvm and vg_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
create_lv(name, length, units)

Creates a logical volume and returns the LogicalVolume instance associated with the lv_t handle:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
lv = vg.create_lv("mylv", 40, "MiB")

Args:

  • name (str): The desired logical volume name.
  • length (int): The desired size.
  • units (str): The size units.

Raises:

  • HandleError, CommitError, ValueError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised.

extent_count

Returns the volume group extent count.

extent_size(units='MiB')

Returns the volume group extent size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
free_extent_count

Returns the volume group free extent count.

free_size(units='MiB')

Returns the volume group free size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
get_lv(name)

Returns a LogicalVolume instance given an existin logical volume name:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
vg.get_lv("mylv")

Args:

  • name (str): An existing logical volume name.

Raises:

  • HandleError
get_pv(device)

Returns the physical volume associated with the given device:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
vg.get_pv("/dev/sdb1")

Args:

  • device (str): An existing device.

Raises:

  • ValueError, HandleError
handle

Returns the vg_t handle.

is_clustered

Returns True if the VG is clustered, False otherwise.

is_exported

Returns True if the VG is exported, False otherwise.

is_partial

Returns True if the VG is partial, False otherwise.

lvm

Returns the LVM instance holding the lvm handle.

lvscan()

Probes the volume group for logical volumes and returns a list of LogicalVolume instances:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg")
lvs = vg.lvscan()

Raises:

  • HandleError
max_lv_count

Returns the maximum allowed logical volume count.

max_pv_count

Returns the maximum allowed physical volume count.

mode

Returns the mode the instance is operating on (‘r’ or ‘w’).

name

returns the name of the volume group.

open()

Obtains the lvm and vg_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
pv_count

Returns the physical volume count.

pvscan()

Probes the volume group for physical volumes and returns a list of PhysicalVolume instances:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg")
pvs = vg.pvscan()

Raises:

  • HandleError
remove_all_lvs()

Removes all logical volumes from the volume group.

Raises:

  • HandleError, CommitError
remove_lv(lv)

Removes a logical volume from the volume group:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
lv = vg.lvscan()[0]
vg.remove_lv(lv)

Args:

  • lv (obj): A LogicalVolume instance.

Raises:

  • HandleError, CommitError, ValueError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised.

remove_pv(pv)

Removes a physical volume from the volume group:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
pv = vg.pvscan()[0]
vg.remove_pv(pv)

Args:

  • pv (obj): A PhysicalVolume instance.

Raises:

  • HandleError, CommitError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised. Also, when removing the last physical volume, the volume group is deleted in lvm, leaving the instance with a null handle.

sequence

Returns the volume group sequence number. This number increases everytime the volume group is modified.

set_extent_size(length, units)

Sets the volume group extent size in the given units:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myvg", "w")
vg.set_extent_size(2, "MiB")

Args:

  • length (int): The desired length size.
  • units (str): The desired units (“MiB”, “GiB”, etc...).

Raises:

  • HandleError, CommitError, KeyError

Note

The VolumeGroup instance must be in write mode, otherwise CommitError is raised.

set_mode(mode)

Sets the volume group in write or read mode.

Args:

  • mode (str): ‘r’ or ‘w’ for read/write respectively.
size(units='MiB')

Returns the volume group size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
uuid

Returns the volume group uuid.

class pv.PhysicalVolume(vg, pvh=None, name=None)

The PhysicalVolume class is used as a wrapper to the global pv_t handle provided by the LVM api.

LVM works by providing a “handle” that represents each object instance (volume groups, logical and physical volumes). This class provides the “global” pv_t handle used to do physical volume operations. You need to provide an VolumeGroup instance to instantiate this class:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myexistingvg")
# get pv from pvscan
pv1 = vg.pvscan()[0]
# get it by name
pv2 = vg.get_pv("/dev/sdb1")
# or just instantiate it
pv3 = PhysicalVolume(vg, name="/dev/sdb1")

Raises:

  • HandleError

Note

To add a new physical volume use the VolumeGroup method add_pv.

close()

Closes the lvm, vg_t and pv_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
dev_size(units='MiB')

Returns the device size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
free(units='MiB')

Returns the free size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
handle

Returns the pv_t handle.

mda_count

Returns the physical volume mda count.

name

Returns the physical volume device path.

open()

Obtains the lvm, vg_t and pv_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
size(units='MiB')

Returns the physical volume size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
uuid

Returns the physical volume uuid.

vg

Returns the VG instance holding the lvm and vg_t handle.

class lv.LogicalVolume(vg, lvh=None, name=None)

The LogicalVolume class is used as a wrapper to the global lv_t handle provided by the LVM api.

LVM works by providing a “handle” that represents each object instance (volume groups, logical and physical volumes). This class provides the “global” lv_t handle used to do logical volume operations. You need to provide an VolumeGroup instance to instantiate this class:

from lvm2py import *

lvm = LVM()
vg = lvm.get_vg("myexistingvg")
# get pv from pvscan
lv1 = vg.lvscan()[0]
# get it by name
lv2 = vg.get_lv("mylv")
# or just instantiate it
lv3 = LogicalVolume(vg, name="mylv")

Raises:

  • HandleError

Note

To create a new logical volume use the VolumeGroup method create_lv.

activate()

Activates the logical volume.

Raises:

  • HandleError
close()

Closes the lvm, vg_t and lv_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
deactivate()

Deactivates the logical volume.

Raises:

  • HandleError
handle

Returns the lv_t handle.

is_active

Returns True if the logical volume is active, False otherwise.

is_suspended

Returns True if the logical volume is suspended, False otherwise.

name

Returns the logical volume name.

open()

Obtains the lvm, vg_t and lv_t handle. Usually you would never need to use this method unless you are doing operations using the ctypes function wrappers in conversion.py

Raises:

  • HandleError
size(units='MiB')

Returns the logical volume size in the given units. Default units are MiB.

Args:

  • units (str): Unit label (‘MiB’, ‘GiB’, etc...). Default is MiB.
uuid

Returns the logical volume uuid.

vg

Returns the VG instance holding the lvm and vg_t handle.

exception exception.CommitError

Raised when there is an error in committing changes to lvm due to invalid actions such as initiating devices as physical volumes that are already associated to another volume group or trying to perform operations on a read-only volume group.

exception exception.HandleError

Raised when the lvm handle for LVM, VolumeGroup, PhysicalVolume or LogicalVolume is null or for some reason it can’t be obtained (bad parameters, duplicate namespaces, etc...)

Table Of Contents

Next topic

Quickstart

This Page