2008-12-07 22:37:07 +03:00
/*
2009-07-24 03:40:50 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2009-02-24 16:03:45 +03:00
* Copyright ( C ) 2004 - 2009 Red Hat , Inc . All rights reserved .
2008-12-07 22:37:07 +03:00
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <readline/readline.h>
2009-07-29 18:06:31 +04:00
# include "lvm2app.h"
2008-12-07 22:37:07 +03:00
# define MAX_ARGS 64
static int lvm_split ( char * str , int * argc , char * * argv , int max )
{
char * b = str , * e ;
* argc = 0 ;
while ( * b ) {
while ( * b & & isspace ( * b ) )
b + + ;
2009-07-24 03:40:50 +04:00
if ( ( ! * b ) | | ( ( * argc = = 0 ) & & ( * b = = ' # ' ) ) )
2008-12-07 22:37:07 +03:00
break ;
e = b ;
while ( * e & & ! isspace ( * e ) )
e + + ;
argv [ ( * argc ) + + ] = b ;
if ( ! * e )
break ;
* e + + = ' \0 ' ;
b = e ;
if ( * argc = = max )
break ;
}
return * argc ;
}
2009-07-24 03:40:50 +04:00
static void _show_help ( void )
{
2009-07-27 00:58:38 +04:00
printf ( " 'lv_activate vgname lvname: "
" Activate an LV \n " ) ;
printf ( " 'lv_deactivate vgname lvname: "
" Deactivate an LV \n " ) ;
2009-07-26 18:37:15 +04:00
printf ( " 'vg_remove_lv vgname lvname': "
" Remove a LV \n " ) ;
2009-07-26 06:35:47 +04:00
printf ( " 'vg_create_lv_linear vgname lvname size_in_bytes': "
" Create a linear LV \n " ) ;
2009-07-24 16:51:32 +04:00
printf ( " 'scan_vgs': "
" Scan the system for LVM metadata \n " ) ;
2009-07-26 17:08:00 +04:00
printf ( " 'list_vg_names': "
2009-07-24 16:51:32 +04:00
" List the names of the VGs that exist in the system \n " ) ;
2009-07-26 17:08:00 +04:00
printf ( " 'list_vg_ids': "
2009-07-24 16:51:32 +04:00
" List the uuids of the VGs that exist in the system \n " ) ;
2009-07-24 03:40:50 +04:00
printf ( " 'vg_list_pvs vgname': "
" List the PVs that exist in VG vgname \n " ) ;
printf ( " 'vg_list_lvs vgname': "
" List the LVs that exist in VG vgname \n " ) ;
printf ( " 'vgs_open': "
" List the VGs that are currently open \n " ) ;
printf ( " 'vgs': "
" List all VGs known to the system \n " ) ;
2009-07-27 21:45:21 +04:00
printf ( " 'vg_extend vgname device: "
" Issue a lvm_vg_extend() API call on VG 'vgname' \n " ) ;
printf ( " 'vg_reduce vgname device: "
" Issue a lvm_vg_reduce() API call on VG 'vgname' \n " ) ;
2009-07-24 03:40:50 +04:00
printf ( " 'vg_open vgname ['r' | 'w']': "
" Issue a lvm_vg_open() API call on VG 'vgname' \n " ) ;
printf ( " 'vg_close vgname': "
" Issue a lvm_vg_close() API call on VG 'vgname' \n " ) ;
2009-08-08 01:22:37 +04:00
printf ( " 'vg_create vgname: "
" Issue a lvm_vg_create() to create VG 'vgname' \n " ) ;
printf ( " 'vg_remove vgname: "
" Issue a lvm_vg_remove() to remove VG 'vgname' \n " ) ;
2009-07-28 01:02:51 +04:00
printf ( " 'config_reload': "
" Issue a lvm_config_reload() API to reload LVM config \n " ) ;
printf ( " 'config_override' device: "
" Issue a lvm_config_override() with accept device filter \n " ) ;
2009-07-24 03:40:50 +04:00
printf ( " 'quit': exit the program \n " ) ;
}
static struct dm_hash_table * _vgid_hash = NULL ;
static struct dm_hash_table * _vgname_hash = NULL ;
static struct dm_hash_table * _pvname_hash = NULL ;
static struct dm_hash_table * _lvname_hash = NULL ;
static void _hash_destroy_single ( struct dm_hash_table * * htable )
{
if ( htable & & * htable ) {
dm_hash_destroy ( * htable ) ;
* htable = NULL ;
}
}
static void _hash_destroy ( void )
{
_hash_destroy_single ( & _vgname_hash ) ;
_hash_destroy_single ( & _vgid_hash ) ;
_hash_destroy_single ( & _pvname_hash ) ;
_hash_destroy_single ( & _lvname_hash ) ;
}
static int _hash_create ( void )
{
if ( ! ( _vgname_hash = dm_hash_create ( 128 ) ) )
return 0 ;
if ( ! ( _pvname_hash = dm_hash_create ( 128 ) ) ) {
_hash_destroy_single ( & _vgname_hash ) ;
return 0 ;
}
if ( ! ( _lvname_hash = dm_hash_create ( 128 ) ) ) {
_hash_destroy_single ( & _vgname_hash ) ;
_hash_destroy_single ( & _pvname_hash ) ;
return 0 ;
}
if ( ! ( _vgid_hash = dm_hash_create ( 128 ) ) ) {
_hash_destroy_single ( & _vgname_hash ) ;
_hash_destroy_single ( & _pvname_hash ) ;
_hash_destroy_single ( & _lvname_hash ) ;
return 0 ;
}
return 1 ;
}
2009-07-26 18:37:15 +04:00
/* FIXME: this should be per vg */
2009-08-13 16:17:32 +04:00
static lv_t _lookup_lv_by_name ( const char * name )
2009-07-26 18:37:15 +04:00
{
2009-08-13 16:17:32 +04:00
lv_t lv ;
2009-07-26 18:37:15 +04:00
if ( ! name ) {
printf ( " Invalid LV name \n " ) ;
return NULL ;
}
if ( ! ( lv = dm_hash_lookup ( _lvname_hash , name ) ) ) {
printf ( " Can't find %s in LVs - run vg_create_lv first \n " ,
name ) ;
return NULL ;
}
return lv ;
}
2009-08-13 16:16:45 +04:00
static vg_t _lookup_vg_by_name ( char * * argv , int argc )
2009-07-24 03:40:50 +04:00
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-24 03:40:50 +04:00
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return NULL ;
}
if ( ! ( vg = dm_hash_lookup ( _vgid_hash , argv [ 1 ] ) ) & &
! ( vg = dm_hash_lookup ( _vgname_hash , argv [ 1 ] ) ) ) {
printf ( " Can't find %s in open VGs - run vg_open first \n " ,
argv [ 1 ] ) ;
return NULL ;
}
return vg ;
}
2009-07-24 08:15:30 +04:00
static void _add_lvs_to_lvname_hash ( struct dm_list * lvs )
{
struct lvm_lv_list * lvl ;
dm_list_iterate_items ( lvl , lvs ) {
/* Concatenate VG name with LV name */
dm_hash_insert ( _lvname_hash , lvm_lv_get_name ( lvl - > lv ) , lvl - > lv ) ;
}
}
static void _add_pvs_to_pvname_hash ( struct dm_list * pvs )
{
struct lvm_pv_list * pvl ;
dm_list_iterate_items ( pvl , pvs ) {
dm_hash_insert ( _pvname_hash , lvm_pv_get_name ( pvl - > pv ) , pvl - > pv ) ;
}
}
2009-07-24 03:40:50 +04:00
2009-07-27 21:45:21 +04:00
static void _remove_device_from_pvname_hash ( struct dm_list * pvs , const char * name )
{
struct lvm_pv_list * pvl ;
dm_list_iterate_items ( pvl , pvs ) {
if ( ! strncmp ( lvm_pv_get_name ( pvl - > pv ) , name , strlen ( name ) ) )
dm_hash_remove ( _pvname_hash , name ) ;
}
}
static void _add_device_to_pvname_hash ( struct dm_list * pvs , const char * name )
{
struct lvm_pv_list * pvl ;
dm_list_iterate_items ( pvl , pvs ) {
if ( ! strncmp ( lvm_pv_get_name ( pvl - > pv ) , name , strlen ( name ) ) )
dm_hash_insert ( _pvname_hash , name , pvl - > pv ) ;
}
}
static void _vg_reduce ( char * * argv , int argc , lvm_t libh )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-27 21:45:21 +04:00
struct dm_list * pvs ;
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
if ( ! ( vg = dm_hash_lookup ( _vgid_hash , argv [ 1 ] ) ) & &
! ( vg = dm_hash_lookup ( _vgname_hash , argv [ 1 ] ) ) ) {
printf ( " VG not open \n " ) ;
return ;
}
if ( lvm_vg_reduce ( vg , argv [ 2 ] ) ) {
printf ( " Error reducing %s by %s \n " , argv [ 1 ] , argv [ 2 ] ) ;
return ;
}
printf ( " Success reducing vg %s by %s \n " , argv [ 1 ] , argv [ 2 ] ) ;
/*
* Add the device into the hashes for lookups
*/
pvs = lvm_vg_list_pvs ( vg ) ;
if ( pvs & & ! dm_list_empty ( pvs ) )
_remove_device_from_pvname_hash ( pvs , argv [ 2 ] ) ;
}
2009-07-28 17:49:28 +04:00
/* Print "Error" or "Success" depending on lvm status */
static int _lvm_status_to_pass_fail ( int rc )
{
if ( rc )
printf ( " Error " ) ;
else
printf ( " Success " ) ;
return rc ;
}
2009-07-28 01:02:51 +04:00
static void _config_override ( char * * argv , int argc , lvm_t libh )
{
int rc ;
char tmp [ 64 ] ;
if ( argc < 2 ) {
printf ( " Please enter device \n " ) ;
return ;
}
snprintf ( tmp , 63 , " devices{filter=[ \" a|%s| \" , \" r|.*| \" ]} " , argv [ 1 ] ) ;
rc = lvm_config_override ( libh , tmp ) ;
2009-07-28 17:49:28 +04:00
_lvm_status_to_pass_fail ( rc ) ;
2009-07-28 01:02:51 +04:00
printf ( " overriding LVM configuration \n " ) ;
}
static void _config_reload ( char * * argv , int argc , lvm_t libh )
{
int rc ;
rc = lvm_config_reload ( libh ) ;
2009-07-28 17:49:28 +04:00
_lvm_status_to_pass_fail ( rc ) ;
2009-07-28 01:02:51 +04:00
printf ( " reloading LVM configuration \n " ) ;
}
2009-07-27 21:45:21 +04:00
static void _vg_extend ( char * * argv , int argc , lvm_t libh )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-27 21:45:21 +04:00
struct dm_list * pvs ;
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
if ( ! ( vg = dm_hash_lookup ( _vgid_hash , argv [ 1 ] ) ) & &
! ( vg = dm_hash_lookup ( _vgname_hash , argv [ 1 ] ) ) ) {
printf ( " VG not open \n " ) ;
return ;
}
if ( lvm_vg_extend ( vg , argv [ 2 ] ) ) {
printf ( " Error extending %s with %s \n " , argv [ 1 ] , argv [ 2 ] ) ;
return ;
}
printf ( " Success extending vg %s with %s \n " , argv [ 1 ] , argv [ 2 ] ) ;
/*
* Add the device into the hashes for lookups
*/
pvs = lvm_vg_list_pvs ( vg ) ;
if ( pvs & & ! dm_list_empty ( pvs ) )
_add_device_to_pvname_hash ( pvs , argv [ 2 ] ) ;
}
2009-07-24 03:40:50 +04:00
static void _vg_open ( char * * argv , int argc , lvm_t libh )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-24 03:40:50 +04:00
struct dm_list * lvs ;
struct dm_list * pvs ;
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
if ( ( vg = dm_hash_lookup ( _vgid_hash , argv [ 1 ] ) ) | |
( vg = dm_hash_lookup ( _vgname_hash , argv [ 1 ] ) ) ) {
printf ( " VG already open \n " ) ;
return ;
}
2009-07-26 06:35:19 +04:00
if ( argc < 3 )
vg = lvm_vg_open ( libh , argv [ 1 ] , " r " , 0 ) ;
else
vg = lvm_vg_open ( libh , argv [ 1 ] , argv [ 2 ] , 0 ) ;
2009-07-24 03:40:50 +04:00
if ( ! vg | | ! lvm_vg_get_name ( vg ) ) {
printf ( " Error opening %s \n " , argv [ 1 ] ) ;
return ;
}
printf ( " Success opening vg %s \n " , argv [ 1 ] ) ;
dm_hash_insert ( _vgname_hash , lvm_vg_get_name ( vg ) , vg ) ;
dm_hash_insert ( _vgid_hash , lvm_vg_get_uuid ( vg ) , vg ) ;
/*
* Add the LVs and PVs into the hashes for lookups
*/
lvs = lvm_vg_list_lvs ( vg ) ;
2009-07-24 08:15:30 +04:00
if ( lvs & & ! dm_list_empty ( lvs ) )
_add_lvs_to_lvname_hash ( lvs ) ;
2009-07-24 03:40:50 +04:00
pvs = lvm_vg_list_pvs ( vg ) ;
2009-07-24 08:15:30 +04:00
if ( pvs & & ! dm_list_empty ( pvs ) )
_add_pvs_to_pvname_hash ( pvs ) ;
2009-07-24 03:40:50 +04:00
}
2009-07-28 17:49:28 +04:00
/* Lookup the vg and remove it from the vgname and vgid hashes */
2009-08-13 16:16:45 +04:00
static vg_t _lookup_and_remove_vg ( const char * vgname )
2009-07-28 17:49:28 +04:00
{
2009-08-13 16:16:45 +04:00
vg_t vg = NULL ;
2009-07-24 03:40:50 +04:00
2009-07-28 18:12:29 +04:00
if ( ( vg = dm_hash_lookup ( _vgname_hash , vgname ) ) ) {
2009-07-28 17:49:28 +04:00
dm_hash_remove ( _vgid_hash , lvm_vg_get_uuid ( vg ) ) ;
dm_hash_remove ( _vgname_hash , lvm_vg_get_name ( vg ) ) ;
}
2009-07-28 18:12:29 +04:00
if ( ! vg & & ( vg = dm_hash_lookup ( _vgid_hash , vgname ) ) ) {
2009-07-28 17:49:28 +04:00
dm_hash_remove ( _vgid_hash , lvm_vg_get_uuid ( vg ) ) ;
dm_hash_remove ( _vgname_hash , lvm_vg_get_name ( vg ) ) ;
}
return vg ;
}
static void _vg_write ( char * * argv , int argc )
2009-07-24 03:40:50 +04:00
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-28 17:49:28 +04:00
int rc = 0 ;
2009-07-24 03:40:50 +04:00
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
2009-07-28 17:49:28 +04:00
vg = _lookup_vg_by_name ( argv , argc ) ;
2009-08-08 01:22:37 +04:00
if ( ! vg ) {
printf ( " Can't find vg_name %s \n " , argv [ 1 ] ) ;
return ;
}
rc = lvm_vg_write ( vg ) ;
2009-07-28 17:49:28 +04:00
_lvm_status_to_pass_fail ( rc ) ;
printf ( " writing VG %s \n " , lvm_vg_get_name ( vg ) ) ;
}
2009-08-08 01:22:37 +04:00
static void _vg_create ( char * * argv , int argc , lvm_t libh )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-08-08 01:22:37 +04:00
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
vg = lvm_vg_create ( libh , argv [ 1 ] ) ;
if ( ! vg | | ! lvm_vg_get_name ( vg ) ) {
printf ( " Error creating %s \n " , argv [ 1 ] ) ;
return ;
}
printf ( " Success creating vg %s \n " , argv [ 1 ] ) ;
dm_hash_insert ( _vgname_hash , lvm_vg_get_name ( vg ) , vg ) ;
dm_hash_insert ( _vgid_hash , lvm_vg_get_uuid ( vg ) , vg ) ;
}
static void _vg_remove ( char * * argv , int argc )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-08-08 01:22:37 +04:00
int rc = 0 ;
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
}
vg = _lookup_vg_by_name ( argv , argc ) ;
if ( ! vg ) {
printf ( " Can't find vg_name %s \n " , argv [ 1 ] ) ;
return ;
}
rc = lvm_vg_remove ( vg ) ;
_lvm_status_to_pass_fail ( rc ) ;
printf ( " removing VG \n " ) ;
}
2009-07-28 17:49:28 +04:00
static void _vg_close ( char * * argv , int argc )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-08-08 01:22:37 +04:00
int rc = 0 ;
2009-07-28 17:49:28 +04:00
if ( argc < 2 ) {
printf ( " Please enter vg_name \n " ) ;
return ;
2009-07-24 03:40:50 +04:00
}
2009-07-28 17:49:28 +04:00
vg = _lookup_and_remove_vg ( argv [ 1 ] ) ;
2009-08-08 01:22:37 +04:00
if ( ! vg ) {
printf ( " Can't find vg_name %s \n " , argv [ 1 ] ) ;
return ;
}
rc = lvm_vg_close ( vg ) ;
_lvm_status_to_pass_fail ( rc ) ;
printf ( " closing VG \n " ) ;
2009-07-24 03:40:50 +04:00
}
2009-08-13 16:16:45 +04:00
static void _show_one_vg ( vg_t vg )
2009-07-24 03:40:50 +04:00
{
2009-07-28 17:17:04 +04:00
printf ( " %s (%s): sz=% " PRIu64 " , free=% " PRIu64 " , #pv=% " PRIu64
" , seq#=% " PRIu64 " \n " ,
2009-07-26 17:07:41 +04:00
lvm_vg_get_name ( vg ) , lvm_vg_get_uuid ( vg ) ,
2009-07-26 20:06:46 +04:00
lvm_vg_get_size ( vg ) , lvm_vg_get_free_size ( vg ) ,
2009-07-28 17:17:04 +04:00
lvm_vg_get_pv_count ( vg ) , lvm_vg_get_seqno ( vg ) ) ;
2009-07-24 03:40:50 +04:00
}
static void _list_open_vgs ( void )
{
dm_hash_iter ( _vgid_hash , ( dm_hash_iterate_fn ) _show_one_vg ) ;
}
static void _pvs_in_vg ( char * * argv , int argc )
{
struct dm_list * pvs ;
struct lvm_pv_list * pvl ;
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-24 03:40:50 +04:00
if ( ! ( vg = _lookup_vg_by_name ( argv , argc ) ) )
return ;
pvs = lvm_vg_list_pvs ( vg ) ;
if ( ! pvs | | dm_list_empty ( pvs ) ) {
printf ( " No PVs in VG %s \n " , lvm_vg_get_name ( vg ) ) ;
return ;
}
printf ( " PVs in VG %s: \n " , lvm_vg_get_name ( vg ) ) ;
dm_list_iterate_items ( pvl , pvs ) {
2009-07-26 17:07:41 +04:00
printf ( " %s (%s): mda_count=% " PRIu64 " \n " ,
lvm_pv_get_name ( pvl - > pv ) , lvm_pv_get_uuid ( pvl - > pv ) ,
lvm_pv_get_mda_count ( pvl - > pv ) ) ;
2009-07-24 03:40:50 +04:00
}
}
2009-07-24 16:51:32 +04:00
static void _scan_vgs ( lvm_t libh )
{
2009-07-26 20:44:05 +04:00
lvm_scan ( libh ) ;
2009-07-24 16:51:32 +04:00
}
2009-07-26 17:08:00 +04:00
static void _list_vg_names ( lvm_t libh )
2009-07-24 16:51:32 +04:00
{
struct dm_list * list ;
struct lvm_str_list * strl ;
const char * tmp ;
list = lvm_list_vg_names ( libh ) ;
printf ( " VG names: \n " ) ;
dm_list_iterate_items ( strl , list ) {
tmp = strl - > str ;
printf ( " %s \n " , tmp ) ;
}
}
2009-07-26 17:08:00 +04:00
static void _list_vg_ids ( lvm_t libh )
2009-07-24 16:51:32 +04:00
{
struct dm_list * list ;
struct lvm_str_list * strl ;
const char * tmp ;
2009-07-26 20:06:21 +04:00
list = lvm_list_vg_uuids ( libh ) ;
2009-07-24 16:51:32 +04:00
printf ( " VG uuids: \n " ) ;
dm_list_iterate_items ( strl , list ) {
tmp = strl - > str ;
printf ( " %s \n " , tmp ) ;
}
}
2009-07-24 03:40:50 +04:00
static void _lvs_in_vg ( char * * argv , int argc )
{
struct dm_list * lvs ;
struct lvm_lv_list * lvl ;
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-07-24 03:40:50 +04:00
if ( ! ( vg = _lookup_vg_by_name ( argv , argc ) ) )
return ;
lvs = lvm_vg_list_lvs ( vg ) ;
if ( ! lvs | | dm_list_empty ( lvs ) ) {
printf ( " No LVs in VG %s \n " , lvm_vg_get_name ( vg ) ) ;
return ;
}
printf ( " LVs in VG %s: \n " , lvm_vg_get_name ( vg ) ) ;
dm_list_iterate_items ( lvl , lvs ) {
2009-07-27 00:59:02 +04:00
printf ( " %s/%s (%s): size=% " PRIu64 " , %sACTIVE / %sSUSPENDED \n " ,
lvm_vg_get_name ( vg ) ,
2009-07-26 17:07:41 +04:00
lvm_lv_get_name ( lvl - > lv ) , lvm_lv_get_uuid ( lvl - > lv ) ,
2009-07-27 00:59:02 +04:00
lvm_lv_get_size ( lvl - > lv ) ,
lvm_lv_is_active ( lvl - > lv ) ? " " : " IN " ,
lvm_lv_is_suspended ( lvl - > lv ) ? " " : " NOT " ) ;
2009-07-24 03:40:50 +04:00
}
}
2009-07-27 00:58:38 +04:00
static void _lv_deactivate ( char * * argv , int argc )
{
2009-08-13 16:17:32 +04:00
lv_t lv ;
2009-07-28 17:49:28 +04:00
int rc = 0 ;
2009-07-27 00:58:38 +04:00
if ( argc < 3 ) {
printf ( " Please enter vgname, lvname \n " ) ;
return ;
}
if ( ! ( lv = _lookup_lv_by_name ( argv [ 2 ] ) ) )
return ;
2009-07-28 17:49:28 +04:00
rc = lvm_lv_deactivate ( lv ) ;
_lvm_status_to_pass_fail ( rc ) ;
2009-07-27 00:58:38 +04:00
printf ( " De-activating LV %s in VG %s \n " ,
argv [ 2 ] , argv [ 1 ] ) ;
}
static void _lv_activate ( char * * argv , int argc )
{
2009-08-13 16:17:32 +04:00
lv_t lv ;
2009-07-28 17:49:28 +04:00
int rc = 0 ;
2009-07-27 00:58:38 +04:00
if ( argc < 3 ) {
printf ( " Please enter vgname, lvname \n " ) ;
return ;
}
if ( ! ( lv = _lookup_lv_by_name ( argv [ 2 ] ) ) )
return ;
2009-07-28 17:49:28 +04:00
rc = lvm_lv_activate ( lv ) ;
_lvm_status_to_pass_fail ( rc ) ;
2009-07-27 00:58:38 +04:00
printf ( " activating LV %s in VG %s \n " ,
argv [ 2 ] , argv [ 1 ] ) ;
}
2009-07-26 18:37:15 +04:00
static void _vg_remove_lv ( char * * argv , int argc )
{
2009-08-13 16:17:32 +04:00
lv_t lv ;
2009-07-26 18:37:15 +04:00
if ( argc < 3 ) {
printf ( " Please enter vgname, lvname \n " ) ;
return ;
}
if ( ! ( lv = _lookup_lv_by_name ( argv [ 2 ] ) ) )
return ;
2009-07-27 00:29:56 +04:00
if ( lvm_vg_remove_lv ( lv ) )
2009-07-26 18:37:15 +04:00
printf ( " Error " ) ;
else {
printf ( " Success " ) ;
dm_hash_remove ( _lvname_hash , argv [ 2 ] ) ;
}
printf ( " removing LV %s in VG %s \n " ,
argv [ 2 ] , argv [ 1 ] ) ;
}
2009-07-26 06:35:47 +04:00
static void _vg_create_lv_linear ( char * * argv , int argc )
{
2009-08-13 16:16:45 +04:00
vg_t vg ;
2009-08-13 16:17:32 +04:00
lv_t lv ;
2009-07-26 06:35:47 +04:00
if ( argc < 4 ) {
printf ( " Please enter vgname, lvname, and size \n " ) ;
return ;
}
if ( ! ( vg = _lookup_vg_by_name ( argv , argc ) ) )
return ;
lv = lvm_vg_create_lv_linear ( vg , argv [ 2 ] , atol ( argv [ 3 ] ) ) ;
if ( ! lv )
printf ( " Error " ) ;
2009-07-26 18:37:15 +04:00
else {
2009-07-26 06:35:47 +04:00
printf ( " Success " ) ;
2009-07-26 18:37:15 +04:00
dm_hash_insert ( _lvname_hash , argv [ 2 ] , lv ) ;
}
2009-07-26 06:35:47 +04:00
printf ( " creating LV %s in VG %s \n " ,
2009-07-26 18:37:15 +04:00
argv [ 2 ] , argv [ 1 ] ) ;
2009-07-26 06:35:47 +04:00
}
2009-02-24 16:03:45 +03:00
static int lvmapi_test_shell ( lvm_t libh )
2008-12-07 22:37:07 +03:00
{
2009-07-24 03:40:50 +04:00
int argc ;
2008-12-07 22:37:07 +03:00
char * input = NULL , * args [ MAX_ARGS ] , * * argv ;
2009-07-24 03:40:50 +04:00
_hash_create ( ) ;
argc = 0 ;
2008-12-07 22:37:07 +03:00
while ( 1 ) {
free ( input ) ;
2009-07-28 01:02:51 +04:00
input = readline ( " liblvm> " ) ;
2008-12-07 22:37:07 +03:00
/* EOF */
if ( ! input ) {
printf ( " \n " ) ;
break ;
}
/* empty line */
if ( ! * input )
continue ;
argv = args ;
if ( lvm_split ( input , & argc , argv , MAX_ARGS ) = = MAX_ARGS ) {
printf ( " Too many arguments, sorry. " ) ;
continue ;
}
if ( ! strcmp ( argv [ 0 ] , " lvm " ) ) {
argv + + ;
argc - - ;
}
if ( ! argc )
continue ;
if ( ! strcmp ( argv [ 0 ] , " quit " ) | | ! strcmp ( argv [ 0 ] , " exit " ) ) {
printf ( " Exiting. \n " ) ;
break ;
} else if ( ! strcmp ( argv [ 0 ] , " ? " ) | | ! strcmp ( argv [ 0 ] , " help " ) ) {
2009-07-24 03:40:50 +04:00
_show_help ( ) ;
2009-07-28 01:02:51 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " config_reload " ) ) {
_config_reload ( argv , argc , libh ) ;
} else if ( ! strcmp ( argv [ 0 ] , " config_override " ) ) {
_config_override ( argv , argc , libh ) ;
2009-07-27 21:45:21 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_extend " ) ) {
_vg_extend ( argv , argc , libh ) ;
} else if ( ! strcmp ( argv [ 0 ] , " vg_reduce " ) ) {
_vg_reduce ( argv , argc , libh ) ;
2009-07-28 17:49:28 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_write " ) ) {
_vg_write ( argv , argc ) ;
2009-07-24 03:40:50 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_open " ) ) {
_vg_open ( argv , argc , libh ) ;
} else if ( ! strcmp ( argv [ 0 ] , " vg_close " ) ) {
_vg_close ( argv , argc ) ;
2009-08-08 01:22:37 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_create " ) ) {
_vg_create ( argv , argc , libh ) ;
} else if ( ! strcmp ( argv [ 0 ] , " vg_remove " ) ) {
_vg_remove ( argv , argc ) ;
2009-07-27 00:58:38 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " lv_activate " ) ) {
_lv_activate ( argv , argc ) ;
} else if ( ! strcmp ( argv [ 0 ] , " lv_deactivate " ) ) {
_lv_deactivate ( argv , argc ) ;
2009-07-26 18:37:15 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_remove_lv " ) ) {
_vg_remove_lv ( argv , argc ) ;
2009-07-24 03:40:50 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vgs_open " ) ) {
_list_open_vgs ( ) ;
} else if ( ! strcmp ( argv [ 0 ] , " vg_list_pvs " ) ) {
_pvs_in_vg ( argv , argc ) ;
} else if ( ! strcmp ( argv [ 0 ] , " vg_list_lvs " ) ) {
_lvs_in_vg ( argv , argc ) ;
2009-07-26 17:08:00 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " list_vg_names " ) ) {
_list_vg_names ( libh ) ;
} else if ( ! strcmp ( argv [ 0 ] , " list_vg_ids " ) ) {
_list_vg_ids ( libh ) ;
2009-07-24 16:51:32 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " scan_vgs " ) ) {
_scan_vgs ( libh ) ;
2009-07-26 06:35:47 +04:00
} else if ( ! strcmp ( argv [ 0 ] , " vg_create_lv_linear " ) ) {
_vg_create_lv_linear ( argv , argc ) ;
2009-07-24 03:40:50 +04:00
} else {
printf ( " Unrecognized command %s \n " , argv [ 0 ] ) ;
2008-12-07 22:37:07 +03:00
}
}
2009-07-24 03:40:50 +04:00
dm_hash_iter ( _vgname_hash , ( dm_hash_iterate_fn ) lvm_vg_close ) ;
_hash_destroy ( ) ;
2008-12-07 22:37:07 +03:00
free ( input ) ;
return 0 ;
}
2009-07-24 03:40:50 +04:00
2008-12-07 22:37:07 +03:00
int main ( int argc , char * argv [ ] )
{
2009-02-24 16:03:45 +03:00
lvm_t libh ;
2008-12-07 22:37:07 +03:00
2009-07-28 13:16:18 +04:00
libh = lvm_init ( NULL ) ;
2009-02-24 16:03:45 +03:00
if ( ! libh ) {
2008-12-07 22:37:07 +03:00
printf ( " Unable to open lvm library instance \n " ) ;
return 1 ;
}
2009-07-28 15:03:28 +04:00
printf ( " Library version: %s \n " , lvm_library_get_version ( ) ) ;
2009-02-24 16:03:45 +03:00
lvmapi_test_shell ( libh ) ;
2008-12-07 22:37:07 +03:00
2009-07-28 13:16:18 +04:00
lvm_quit ( libh ) ;
2008-12-07 22:37:07 +03:00
return 0 ;
}