/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
*
* 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>
#include "lvm2app.h"
#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++;
if ((!*b) || ((*argc == 0)&&(*b == '#')))
break;
e = b;
while (*e && !isspace(*e))
e++;
argv[(*argc)++] = b;
if (!*e)
break;
*e++ = '\0';
b = e;
if (*argc == max)
break;
}
return *argc;
}
static void _show_help(void)
{
printf("'lv_activate vgname lvname: "
"Activate an LV\n");
printf("'lv_deactivate vgname lvname: "
"Deactivate an LV\n");
printf("'vg_remove_lv vgname lvname': "
"Remove a LV\n");
printf("'vg_create_lv_linear vgname lvname size_in_bytes': "
"Create a linear LV\n");
printf("'scan_vgs': "
"Scan the system for LVM metadata\n");
printf("'list_vg_names': "
"List the names of the VGs that exist in the system\n");
printf("'list_vg_ids': "
"List the uuids of the VGs that exist in the system\n");
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");
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");
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");
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");
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");
printf("'quit': exit the program\n");
}
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]);
}
/* 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;
}
static void _config_override(char **argv, int argc, lvm_t libh)
{
int rc;
char tmp[64];
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]);
}
/*
* Add the LVs and PVs into the hashes for lookups
*/
lvs = lvm_vg_list_lvs(vg);
if (lvs && !dm_list_empty(lvs))
_add_lvs_to_lvname_hash(lvs);
pvs = lvm_vg_list_pvs(vg);
if (pvs && !dm_list_empty(pvs))
_add_pvs_to_pvname_hash(pvs);
}
/* Lookup the vg and remove it from the vgname and vgid hashes */
static vg_t _lookup_and_remove_vg(const char *vgname)
{
vg_t vg=NULL;