249
246
#define VSH_BYNAME (1 << 3)
251
248
static virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
252
const char *optname, char **name, int flag);
249
char **name, int flag);
254
251
/* default is lookup by Id, Name and UUID */
255
#define vshCommandOptDomain(_ctl, _cmd, _optname, _name) \
256
vshCommandOptDomainBy(_ctl, _cmd, _optname, _name, \
257
VSH_BYID|VSH_BYUUID|VSH_BYNAME)
252
#define vshCommandOptDomain(_ctl, _cmd, _name) \
253
vshCommandOptDomainBy(_ctl, _cmd, _name, VSH_BYID|VSH_BYUUID|VSH_BYNAME)
259
255
static virNetworkPtr vshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd,
260
const char *optname, char **name, int flag);
256
char **name, int flag);
262
258
/* default is lookup by Name and UUID */
263
#define vshCommandOptNetwork(_ctl, _cmd, _optname, _name) \
264
vshCommandOptNetworkBy(_ctl, _cmd, _optname, _name, \
259
#define vshCommandOptNetwork(_ctl, _cmd, _name) \
260
vshCommandOptNetworkBy(_ctl, _cmd, _name, \
265
261
VSH_BYUUID|VSH_BYNAME)
267
263
static virStoragePoolPtr vshCommandOptPoolBy(vshControl *ctl, const vshCmd *cmd,
2873
* XML Building helper for pool-define-as and pool-create-as
2875
static const vshCmdOptDef opts_pool_X_as[] = {
2876
{"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the pool")},
2877
{"type", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("type of the pool")},
2878
{"source-host", VSH_OT_DATA, 0, gettext_noop("source-host for underlying storage")},
2879
{"source-path", VSH_OT_DATA, 0, gettext_noop("source path for underlying storage")},
2880
{"source-dev", VSH_OT_DATA, 0, gettext_noop("source device for underlying storage")},
2881
{"source-name", VSH_OT_DATA, 0, gettext_noop("source name for underlying storage")},
2882
{"target", VSH_OT_DATA, 0, gettext_noop("target for underlying storage")},
2886
static int buildPoolXML(const vshCmd *cmd, char **retname, char **xml) {
2889
char *name, *type, *srcHost, *srcPath, *srcDev, *srcName, *target;
2890
virBuffer buf = VIR_BUFFER_INITIALIZER;
2892
name = vshCommandOptString(cmd, "name", &found);
2895
type = vshCommandOptString(cmd, "type", &found);
2899
srcHost = vshCommandOptString(cmd, "source-host", &found);
2900
srcPath = vshCommandOptString(cmd, "source-path", &found);
2901
srcDev = vshCommandOptString(cmd, "source-dev", &found);
2902
srcName = vshCommandOptString(cmd, "source-name", &found);
2903
target = vshCommandOptString(cmd, "target", &found);
2905
virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
2906
virBufferVSprintf(&buf, " <name>%s</name>\n", name);
2907
if (srcHost || srcPath || srcDev) {
2908
virBufferAddLit(&buf, " <source>\n");
2911
virBufferVSprintf(&buf, " <host name='%s'/>\n", srcHost);
2913
virBufferVSprintf(&buf, " <dir path='%s'/>\n", srcPath);
2915
virBufferVSprintf(&buf, " <device path='%s'/>\n", srcDev);
2917
virBufferVSprintf(&buf, " <name>%s</name>\n", srcName);
2919
virBufferAddLit(&buf, " </source>\n");
2922
virBufferAddLit(&buf, " <target>\n");
2923
virBufferVSprintf(&buf, " <path>%s</path>\n", target);
2924
virBufferAddLit(&buf, " </target>\n");
2926
virBufferAddLit(&buf, "</pool>\n");
2928
if (virBufferError(&buf)) {
2929
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
2933
*xml = virBufferContentAndReset(&buf);
2938
free(virBufferContentAndReset(&buf));
2910
2943
* "pool-create-as" command
2912
2945
static const vshCmdInfo info_pool_create_as[] = {
2913
{"syntax", "pool-create-as <name> <type>"},
2914
2946
{"help", gettext_noop("create a pool from a set of args")},
2915
2947
{"desc", gettext_noop("Create a pool.")},
2919
static const vshCmdOptDef opts_pool_create_as[] = {
2920
{"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the pool")},
2921
{"type", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("type of the pool")},
2922
{"source-host", VSH_OT_DATA, 0, gettext_noop("source-host for underlying storage")},
2923
{"source-path", VSH_OT_DATA, 0, gettext_noop("source path for underlying storage")},
2924
{"source-dev", VSH_OT_DATA, 0, gettext_noop("source device for underlying storage")},
2925
{"target", VSH_OT_DATA, 0, gettext_noop("target for underlying storage")},
2931
2952
cmdPoolCreateAs(vshControl *ctl, const vshCmd *cmd)
2933
2954
virStoragePoolPtr pool;
2936
char *name, *type, *srcHost, *srcPath, *srcDev, *target;
2937
virBuffer buf = VIR_BUFFER_INITIALIZER;
2939
2957
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
2942
name = vshCommandOptString(cmd, "name", &found);
2945
type = vshCommandOptString(cmd, "type", &found);
2949
srcHost = vshCommandOptString(cmd, "source-host", &found);
2950
srcPath = vshCommandOptString(cmd, "source-path", &found);
2951
srcDev = vshCommandOptString(cmd, "source-dev", &found);
2952
target = vshCommandOptString(cmd, "target", &found);
2954
virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
2955
virBufferVSprintf(&buf, " <name>%s</name>\n", name);
2956
if (srcHost || srcPath || srcDev) {
2957
virBufferAddLit(&buf, " <source>\n");
2959
virBufferVSprintf(&buf, " <host name='%s'>\n", srcHost);
2962
virBufferVSprintf(&buf, " <dir path='%s'/>\n", srcPath);
2965
virBufferVSprintf(&buf, " <device path='%s'/>\n", srcDev);
2967
virBufferAddLit(&buf, " </source>\n");
2970
virBufferAddLit(&buf, " <target>\n");
2971
virBufferVSprintf(&buf, " <path>%s</path>\n", target);
2972
virBufferAddLit(&buf, " </target>\n");
2974
virBufferAddLit(&buf, "</pool>\n");
2976
if (virBufferError(&buf)) {
2977
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
2960
if (!buildPoolXML(cmd, &name, &xml))
2980
xml = virBufferContentAndReset(&buf);
2982
2963
pool = virStoragePoolCreateXML(ctl->conn, xml, 0);
3049
3026
* "pool-define-as" command
3051
3028
static const vshCmdInfo info_pool_define_as[] = {
3052
{"syntax", "pool-define-as <name> <type>"},
3053
3029
{"help", gettext_noop("define a pool from a set of args")},
3054
3030
{"desc", gettext_noop("Define a pool.")},
3058
static const vshCmdOptDef opts_pool_define_as[] = {
3059
{"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the pool")},
3060
{"type", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("type of the pool")},
3061
{"source-host", VSH_OT_DATA, 0, gettext_noop("source-host for underlying storage")},
3062
{"source-path", VSH_OT_DATA, 0, gettext_noop("source path for underlying storage")},
3063
{"source-dev", VSH_OT_DATA, 0, gettext_noop("source device for underlying storage")},
3064
{"source-name", VSH_OT_DATA, 0, gettext_noop("source name for underlying storage")},
3065
{"target", VSH_OT_DATA, 0, gettext_noop("target for underlying storage")},
3071
3035
cmdPoolDefineAs(vshControl *ctl, const vshCmd *cmd)
3073
3037
virStoragePoolPtr pool;
3076
char *name, *type, *srcHost, *srcPath, *srcDev, *srcName, *target;
3077
virBuffer buf = VIR_BUFFER_INITIALIZER;
3079
3040
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
3082
name = vshCommandOptString(cmd, "name", &found);
3085
type = vshCommandOptString(cmd, "type", &found);
3089
srcHost = vshCommandOptString(cmd, "source-host", &found);
3090
srcPath = vshCommandOptString(cmd, "source-path", &found);
3091
srcDev = vshCommandOptString(cmd, "source-dev", &found);
3092
srcName = vshCommandOptString(cmd, "source-name", &found);
3093
target = vshCommandOptString(cmd, "target", &found);
3095
virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
3096
virBufferVSprintf(&buf, " <name>%s</name>\n", name);
3097
if (srcHost || srcPath || srcDev || srcName) {
3098
virBufferAddLit(&buf, " <source>\n");
3100
virBufferVSprintf(&buf, " <host>%s</host>\n", srcHost);
3102
virBufferVSprintf(&buf, " <path>%s</path>\n", srcPath);
3104
virBufferVSprintf(&buf, " <device>%s</device>\n", srcDev);
3106
virBufferVSprintf(&buf, " <name>%s</name>\n", srcName);
3108
virBufferAddLit(&buf, " </source>\n");
3111
virBufferAddLit(&buf, " <target>\n");
3112
virBufferVSprintf(&buf, " <path>%s</path>\n", target);
3113
virBufferAddLit(&buf, " </target>\n");
3115
virBufferAddLit(&buf, "</pool>\n");
3118
if (virBufferError(&buf)) {
3119
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
3043
if (!buildPoolXML(cmd, &name, &xml))
3122
xml = virBufferContentAndReset(&buf);
3124
3046
pool = virStoragePoolDefineXML(ctl->conn, xml, 0);
3777
3685
{"pool", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("pool name")},
3778
3686
{"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the volume")},
3779
3687
{"capacity", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("size of the vol with optional k,M,G,T suffix")},
3780
{"allocation", VSH_OT_DATA, 0, gettext_noop("initial allocation size with optional k,M,G,T suffix")},
3781
{"format", VSH_OT_DATA, 0, gettext_noop("file format type raw,bochs,qcow,qcow2,vmdk")},
3688
{"allocation", VSH_OT_STRING, 0, gettext_noop("initial allocation size with optional k,M,G,T suffix")},
3689
{"format", VSH_OT_STRING, 0, gettext_noop("file format type raw,bochs,qcow,qcow2,vmdk")},
3782
3690
{NULL, 0, 0, NULL}
5073
4966
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("domain name, id or uuid")},
5074
4967
{"source", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("source of disk device")},
5075
4968
{"target", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("target of disk device")},
5076
{"driver", VSH_OT_DATA, 0, gettext_noop("driver of disk device")},
5077
{"subdriver", VSH_OT_DATA, 0, gettext_noop("subdriver of disk device")},
5078
{"type", VSH_OT_DATA, 0, gettext_noop("target device type")},
5079
{"mode", VSH_OT_DATA, 0, gettext_noop("mode of device reading and writing")},
4969
{"driver", VSH_OT_STRING, 0, gettext_noop("driver of disk device")},
4970
{"subdriver", VSH_OT_STRING, 0, gettext_noop("subdriver of disk device")},
4971
{"type", VSH_OT_STRING, 0, gettext_noop("target device type")},
4972
{"mode", VSH_OT_STRING, 0, gettext_noop("mode of device reading and writing")},
5080
4973
{NULL, 0, 0, NULL}
5666
5555
{"pool-autostart", cmdPoolAutostart, opts_pool_autostart, info_pool_autostart},
5667
5556
{"pool-build", cmdPoolBuild, opts_pool_build, info_pool_build},
5668
5557
{"pool-create", cmdPoolCreate, opts_pool_create, info_pool_create},
5669
{"pool-create-as", cmdPoolCreateAs, opts_pool_create_as, info_pool_create_as},
5558
{"pool-create-as", cmdPoolCreateAs, opts_pool_X_as, info_pool_create_as},
5670
5559
{"pool-define", cmdPoolDefine, opts_pool_define, info_pool_define},
5671
{"pool-define-as", cmdPoolDefineAs, opts_pool_define_as, info_pool_define_as},
5560
{"pool-define-as", cmdPoolDefineAs, opts_pool_X_as, info_pool_define_as},
5672
5561
{"pool-destroy", cmdPoolDestroy, opts_pool_destroy, info_pool_destroy},
5673
5562
{"pool-delete", cmdPoolDelete, opts_pool_delete, info_pool_delete},
5674
5563
{"pool-dumpxml", cmdPoolDumpXML, opts_pool_dumpxml, info_pool_dumpxml},
5811
5700
vshError(ctl, FALSE, _("command '%s' doesn't exist"), cmdname);
5814
const vshCmdOptDef *opt;
5815
5703
const char *desc = N_(vshCmddefGetInfo(def, "desc"));
5816
5704
const char *help = N_(vshCmddefGetInfo(def, "help"));
5817
const char *syntax = vshCmddefGetInfo(def, "syntax");
5819
5707
fputs(_(" NAME\n"), stdout);
5820
5708
fprintf(stdout, " %s - %s\n", def->name, help);
5823
fputs(_("\n SYNOPSIS\n"), stdout);
5825
fprintf(stdout, " %s\n", syntax);
5827
fprintf(stdout, " %s %s\n", progname, syntax);
5710
fputs(_("\n SYNOPSIS\n"), stdout);
5711
fprintf(stdout, " %s", def->name);
5713
const vshCmdOptDef *opt;
5714
for (opt = def->opts; opt->name; opt++) {
5716
if (opt->type == VSH_OT_BOOL)
5718
else if (opt->type == VSH_OT_INT)
5719
fmt = N_("[--%s <number>]");
5720
else if (opt->type == VSH_OT_STRING)
5721
fmt = N_("[--%s <string>]");
5722
else if (opt->type == VSH_OT_DATA)
5723
fmt = ((opt->flag & VSH_OFLAG_REQ) ? "<%s>" : "[<%s>]");
5727
fprintf(stdout, _(fmt), opt->name);
5730
fputc('\n', stdout);
5733
/* Print the description only if it's not empty. */
5830
5734
fputs(_("\n DESCRIPTION\n"), stdout);
5831
5735
fprintf(stdout, " %s\n", desc);
5833
5738
if (def->opts) {
5739
const vshCmdOptDef *opt;
5834
5740
fputs(_("\n OPTIONS\n"), stdout);
5835
5741
for (opt = def->opts; opt->name; opt++) {
5838
5742
if (opt->type == VSH_OT_BOOL)
5839
5743
snprintf(buf, sizeof(buf), "--%s", opt->name);
5840
5744
else if (opt->type == VSH_OT_INT)
5974
5878
return vshCommandOpt(cmd, name) ? TRUE : FALSE;
5881
/* Determine whether CMD->opts includes an option with name OPTNAME.
5882
If not, give a diagnostic and return false.
5883
If so, return true. */
5885
cmd_has_option (vshControl *ctl, const vshCmd *cmd, const char *optname)
5887
/* Iterate through cmd->opts, to ensure that there is an entry
5888
with name OPTNAME and type VSH_OT_DATA. */
5890
const vshCmdOpt *opt;
5891
for (opt = cmd->opts; opt; opt = opt->next) {
5892
if (STREQ (opt->def->name, optname) && opt->def->type == VSH_OT_DATA) {
5899
vshError(ctl, FALSE,
5900
_("internal error: virsh %s: no %s VSH_OT_DATA option"),
5901
cmd->def->name, optname);
5978
5905
static virDomainPtr
5979
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd, const char *optname,
5906
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
5980
5907
char **name, int flag)
5982
5909
virDomainPtr dom = NULL;
5912
const char *optname = "domain";
5913
if (!cmd_has_option (ctl, cmd, optname))
5986
5916
if (!(n = vshCommandOptString(cmd, optname, NULL))) {
5987
5917
vshError(ctl, FALSE, "%s", _("undefined domain name or id"));
6915
vshUsage(vshControl *ctl, const char *cmdname)
6917
6850
const vshCmdDef *cmd;
6921
fprintf(stdout, _("\n%s [options] [commands]\n\n"
6923
" -c | --connect <uri> hypervisor connection URI\n"
6924
" -r | --readonly connect readonly\n"
6925
" -d | --debug <num> debug level [0-5]\n"
6926
" -h | --help this help\n"
6927
" -q | --quiet quiet mode\n"
6928
" -t | --timing print timing information\n"
6929
" -l | --log <file> output logging to file\n"
6930
" -v | --version program version\n\n"
6931
" commands (non interactive mode):\n"), progname);
6933
for (cmd = commands; cmd->name; cmd++)
6935
" %-15s %s\n", cmd->name, N_(vshCmddefGetInfo(cmd,
6938
fprintf(stdout, "%s",
6939
_("\n (specify help <command> for details about the command)\n\n"));
6942
if (!vshCmddefHelp(ctl, cmdname, TRUE))
6851
fprintf(stdout, _("\n%s [options] [commands]\n\n"
6853
" -c | --connect <uri> hypervisor connection URI\n"
6854
" -r | --readonly connect readonly\n"
6855
" -d | --debug <num> debug level [0-5]\n"
6856
" -h | --help this help\n"
6857
" -q | --quiet quiet mode\n"
6858
" -t | --timing print timing information\n"
6859
" -l | --log <file> output logging to file\n"
6860
" -v | --version program version\n\n"
6861
" commands (non interactive mode):\n"), progname);
6863
for (cmd = commands; cmd->name; cmd++)
6865
" %-15s %s\n", cmd->name, N_(vshCmddefGetInfo(cmd,
6868
fprintf(stdout, "%s",
6869
_("\n (specify help <command> for details about the command)\n\n"));