From Fedora Project Wiki

< LVM‎ | liblvm

Line 159: Line 159:
  * usage example:  
  * usage example:  
  * lvm_pv_obj_t *pv = lvm_pv_obj_create("/dev/sda1");
  * lvm_pv_obj_t *pv = lvm_pv_obj_create("/dev/sda1");
* lvm_pv_obj_set_zero(pv, 1);
  * lvm_pv_obj_set_uuid(pv "WDhaBn-bSGn-EW98-DkI7-M8zB-R4S9-BPkFqS");
  * lvm_pv_obj_set_uuid(pv "WDhaBn-bSGn-EW98-DkI7-M8zB-R4S9-BPkFqS");
  * status = lvm_pv_create(handle, pv);
  * status = lvm_pv_create(handle, pv, 1);
  */
  */



Revision as of 15:16, 2 December 2008

High Level API Proposal

liblvm/liblvm.h

#include "error.h"
#include "handle.h"
#include "units.h"
#include "physical_volume.h"

/*
 * lvm_init
 *   Initialize LVM
 */
lvm_handle lvm_init(void);

/*
 * lvm_load_config_file
 *   Load a lvm configuration file
 */
lvm_error_status lvm_load_config_file(lvm_handle h, const char *filename);

/*
 * lvm_set_config_option
 *   Load an lvm config option into the existing configuration.
 *
 *   The formation of the option parameter is similar to the names
 *   in /etc/lvm/lvm.conf.
 *   An option within a section is specified with a '/' between
 *   the section name and option.  For example, the 'filter' option
 *   in the devices section is specified by 'devices/filter'
 */
lvm_error_status lvm_set_config_option(lvm_handle h, const char *option,
                                       const char *value);
/*
 * lvm_clear_config_option
 */
lvm_error_status lvm_remove_config_option(lvm_handle h, const char *option);

liblvm/handle.h

struct _lvm_handle {
  char* config_file; /* default: NULL */
  char** config_options; /* default: NULL */
  char** config_values; /* default: NULL */

  char* last_error_message; /* default: NULL */
};

typedef struct _lvm_handle lvm_handle;

liblvm/error.h

#include "handle.h"

enum _lvm_error_status {
        NO_ERROR = 0,
        INVALID_PV_NAME,
        INVALID_PV_SIZE,
        INVALID_DEV_SIZE,
        INVALID_PE_SIZE,
        INVALID_UUID,
        INVALID_LABEL_SECTOR,
        INVALID_METADATA_COPIES,
        INVALID_METADATA_SIZE,
        INVALID_PE_START,
        INVALID_EXTENT_COUNT,
        INVALID_EXTENT_SIZE,
        
        /* more to come */
};

typedef enum _lvm_error_status lvm_error_status;

/*
 * lvm_strerror
 *   Returns error string stored in the handle
 */
const char* lvm_strerror(lvm_handle h);

liblvm/physical_volume.h

#incldue "error.h"
#include "handle.h"

/*
 * lvm physical volume information
 */
struct lvm_physical_volume {
  char* pv_name;
  char* vg_name;
  uint64_t pv_size; /* units: use functions for conversions */
  uint64_t dev_size; /* units */
  uint64_t pe_size; /* units */
  uint64_t total_pe;
  uint64_t free_pe;
  char* uuid;
};

/*
 * private lvm pv object for physical volume creation and modification
 */
typedef struct lvm_pv_obj {
  char* phys_vol;
  uint64_t size;
  int64_t labelsector;
  int metadata_copies;
  uint64_t metadata_size;
  uint64_t pe_start;
  uint32_t extent_count;
  uint32_t extent_size;
  const char* uuid;  
} lvm_pv_obj_t;

/*
 * lvm_pv_scan
 *   scans all supported LVM block devices in the system for physical volumes
 *   and gets attibutes of all physicalvolume
 *   returns: status
 */
lvm_error_status lvm_pv_scan(lvm_handle h,
                             struct LVM_PYSICAL_VOLUME** pv_return,
                             uint32_t* pv_count);

/*
 * lvm_pv_get
 *   gets attibutes of one physicalvolume by volume path
 *   returns: status
 */
lvm_error_status lvm_pv_get(lvm_handle h,
                            struct lvm_physical_volume* pv_return,
                            const char* phys_vol_path);


/*
 * lvm_pv_list
 *   gets attibutes of all physicalvolume by volume paths
 *   returns: status
 */
lvm_error_status lvm_pv_list(lvm_handle h,
                             struct lvm_physical_volume** pv_return,
                             const char** phys_vol_paths, uint32_t* pv_count);

/*
 * lvm_pv_remove
 *   removes a physicalvolume by volume path
 *   returns: status
 */
lvm_error_status lvm_pv_remove(lvm_handle h, const char* phys_vol_path);

/*
 * lvm_pv_create
 *   creates a physicalvolume object model with accessors and mutators
 */

/*
 * usage example: 
 * lvm_pv_obj_t *pv = lvm_pv_obj_create("/dev/sda1");
 * lvm_pv_obj_set_uuid(pv "WDhaBn-bSGn-EW98-DkI7-M8zB-R4S9-BPkFqS");
 * status = lvm_pv_create(handle, pv, 1);
 */

lvm_pv_obj_t* lvm_pv_obj_create(const char* phys_vol);
/* accessors */
lvm_error_status lvm_pv_obj_set_volume(lvm_pv_obj_t* pv_obj,
                                       const char* phys_vol);
lvm_error_status lvm_pv_obj_set_size(lvm_pv_obj_t* pv_obj, uint64t size);
lvm_error_status lvm_pv_obj_set_labelsector(lvm_pv_obj_t* pv_obj,
                                            int64_t labelsector);
lvm_error_status lvm_pv_obj_set_metadata_copies(lvm_pv_obj_t* pv_obj,
                                                int metadata_copies);
lvm_error_status lvm_pv_obj_set_metadata_size(lvm_pv_obj_t* pv_obj,
                                              uint64_t metadata_size);
lvm_error_status lvm_pv_obj_set_pe_start(lvm_pv_obj_t* pv_obj,
                                         uint64_t pe_start);
lvm_error_status lvm_pv_obj_set_extent_count(lvm_pv_obj_t* pv_obj,
                                             uint32_t extent_count);
lvm_error_status lvm_pv_obj_set_extent_size(lvm_pv_obj_t* pv_obj,
                                            uint32_t extent_size);
lvm_error_status lvm_pv_obj_set_uuid(lvm_pv_obj_t* pv_obj, const char* uuid);
lvm_error_status lvm_pv_obj_set_volume(lvm_pv_obj_t* pv_obj,
                                       const char* phys_vol);
/* mutators */
uint64_t lvm_pv_obj_get_size(lvm_pv_obj_t* pv_obj);
int64_t lvm_pv_obj_get_labelsector(lvm_pv_obj_t* pv_obj);
int lvm_pv_obj_get_metadata_copies(lvm_pv_obj_t* pv_obj);
uint64_t lvm_pv_obj_get_metadata_size(lvm_pv_obj_t* pv_obj);
uint64_t lvm_pv_obj_get_pe_start(lvm_pv_obj_t* pv_obj);
uint32_t lvm_pv_obj_get_extent_count(lvm_pv_obj_t* pv_obj);
uint32_t lvm_pv_obj_get_extent_size(lvm_pv_obj_t* pv_obj);
const char* lvm_pv_obj_get_uuid(lvm_pv_obj_t* pv_obj);
const char* lvm_error_status lvm_pv_obj_get_volume(lvm_pv_obj_t* pv_obj);

lvm_error_status lvm_pv_create(lvm_handle h, lvm_pv_obj_t* pv_obj, int zero);

/*
 * lvm_pv_change
 *   changes physicalvolume parameters
 */
lvm_error_status lvm_pv_obj_get(const char* phys_vol,
                                lvm_pv_obj_t* pv_obj_return);
lvm_error_status lvm_pv_change(lvm_handle h, lvm_pv_obj_t* pv_obj);

/*
 * lvm_pv_restore_from
 *  restore physicalvolume parameters, use lvm_pv_change afterwards
 */
lvm_error_status lvm_pv_obj_restore_from(const char* uuid,
                                         lvm_pv_obj_t* pv_obj_return,
                                         const char* restore_file);
/*
 * lvm_pv_fsck
 */
lvm_error_status lvm_pv_fsck(const char* phys_vol, int64_t labelsector);

liblvm/units.h

enum lvm_unit_type {
  TYPE_UNITS = 0,
  TYPE_BYTES,
  TYPE_KILO_BYTE,
  TYPE_KIBI_BYTE,
  TYPE_GIGA_BYTE,
  TYPE_GIBI_BYTE,
  TYPE_TERA_BYTE,
  TYPE_TEBI_BYTE,
  TYPE_PETA_BYTE,
  TYPE_PEBI_BYTE,
  TYPE_EXA_BYTE,
  TYPE_EXBI_BYTE
};

uint64_t lvm_unit_convert(uint64_t value,
                          enum lvm_unit_type from_type,
                          enum lvm_unit_type to_type);

uint64_t lvm_units_to_bytes(uint64_t units) {
  return lvm_unit_convert(units, TYPE_UNITS, TYPE_BYTES);
}
uint64_t lvm_bytes_to_units(uint64_t bytes) {
  return lvm_unit_convert(units, TYPE_BYTES, TYPE_UNITS);
}

static const char* lvm_unit_string(uint64_t units, enum lvm_unit_type type);