2005-11-10 19:12:31 +03:00
/*
2010-06-09 13:00:30 +04:00
* virsh . c : a shell to exercise the libvirt API
2005-11-10 19:12:31 +03:00
*
2015-02-19 16:16:39 +03:00
* Copyright ( C ) 2005 , 2007 - 2015 Red Hat , Inc .
2005-11-10 19:12:31 +03:00
*
2012-07-27 13:39:53 +04:00
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2012-09-21 02:30:55 +04:00
* License along with this library . If not , see
2012-07-27 13:39:53 +04:00
* < http : //www.gnu.org/licenses/>.
2005-11-10 19:12:31 +03:00
*
* Daniel Veillard < veillard @ redhat . com >
2005-12-08 17:22:52 +03:00
* Karel Zak < kzak @ redhat . com >
2006-05-29 19:39:31 +04:00
* Daniel P . Berrange < berrange @ redhat . com >
2005-11-10 19:12:31 +03:00
*/
2008-01-29 21:15:54 +03:00
# include <config.h>
2012-08-18 07:16:04 +04:00
# include "virsh.h"
2007-12-04 21:27:52 +03:00
2005-11-10 19:12:31 +03:00
# include <stdio.h>
2005-12-08 13:23:34 +03:00
# include <stdlib.h>
# include <string.h>
# include <stdarg.h>
2005-12-01 19:35:42 +03:00
# include <unistd.h>
2007-05-23 19:09:19 +04:00
# include <errno.h>
2005-12-08 13:23:34 +03:00
# include <getopt.h>
2005-12-08 17:22:52 +03:00
# include <sys/time.h>
2006-03-30 20:08:13 +04:00
# include <fcntl.h>
2006-09-29 20:23:27 +04:00
# include <locale.h>
2007-11-29 12:18:04 +03:00
# include <time.h>
2007-06-15 19:24:20 +04:00
# include <limits.h>
2007-06-06 16:24:31 +04:00
# include <sys/stat.h>
2007-08-21 14:08:12 +04:00
# include <inttypes.h>
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
# include <signal.h>
2005-12-08 13:23:34 +03:00
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2010-03-09 21:22:22 +03:00
# include <readline / readline.h>
# include <readline / history.h>
2007-12-04 21:27:52 +03:00
# endif
2005-12-08 13:23:34 +03:00
Standardize use of header files, making internal.h primary.
* qemud/internal.h, qemud/qemud.h: Rename this file so it
doesn't conflict with src/internal.h.
* HACKING: Document how header files should be used.
* qemud/Makefile.am: Add src/ directory to includes.
* qemud/event.c, qemud/mdns.c, qemud/qemud.c, qemud/remote.c,
qemud/remote_protocol.c, qemud/remote_protocol.h,
qemud/remote_protocol.x, src/buf.c, src/libvirt.c,
src/nodeinfo.c, src/qemu_conf.c, src/qemu_driver.c,
src/stats_linux.c, src/storage_backend.c, src/storage_backend_fs.c,
src/storage_backend_iscsi.c, src/storage_backend_logical.c,
src/storage_conf.c, src/storage_driver.c, src/util.c,
src/util.h, src/virsh.c, src/virterror.c, src/xend_internal.c,
src/xml.c, tests/reconnect.c, tests/xmlrpctest.c,
tests/qparamtest.c: Standardize use of header files.
* docs/*, po/*: Rebuild docs.
2008-05-23 12:24:41 +04:00
# include "internal.h"
2012-12-13 22:21:53 +04:00
# include "virerror.h"
2012-12-04 16:04:07 +04:00
# include "virbuffer.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2013-04-17 14:19:19 +04:00
# include <libvirt/libvirt-qemu.h>
# include <libvirt/libvirt-lxc.h>
2011-07-19 22:32:58 +04:00
# include "virfile.h"
2010-11-16 17:54:17 +03:00
# include "configmake.h"
2012-12-13 19:49:48 +04:00
# include "virthread.h"
2012-12-12 20:27:01 +04:00
# include "vircommand.h"
2011-12-20 12:35:03 +04:00
# include "conf/domain_conf.h"
2012-01-03 02:03:19 +04:00
# include "virtypedparam.h"
2013-04-03 14:36:23 +04:00
# include "virstring.h"
2005-12-08 13:23:34 +03:00
2013-08-26 13:53:43 +04:00
# include "virsh-console.h"
2012-08-18 08:00:42 +04:00
# include "virsh-domain.h"
2012-08-20 17:46:38 +04:00
# include "virsh-domain-monitor.h"
2012-08-21 00:01:45 +04:00
# include "virsh-host.h"
2012-08-21 00:30:53 +04:00
# include "virsh-interface.h"
2012-08-21 01:43:25 +04:00
# include "virsh-network.h"
2012-08-21 02:23:10 +04:00
# include "virsh-nodedev.h"
2012-08-21 02:56:03 +04:00
# include "virsh-nwfilter.h"
2012-08-21 02:56:53 +04:00
# include "virsh-pool.h"
2012-08-21 03:14:37 +04:00
# include "virsh-secret.h"
2012-08-21 03:29:03 +04:00
# include "virsh-snapshot.h"
2012-08-21 03:41:24 +04:00
# include "virsh-volume.h"
2012-08-18 08:00:42 +04:00
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
/* Gnulib doesn't guarantee SA_SIGINFO support. */
# ifndef SA_SIGINFO
# define SA_SIGINFO 0
# endif
2005-12-08 13:23:34 +03:00
static char * progname ;
2010-11-30 09:37:04 +03:00
static const vshCmdGrp cmdGroups [ ] ;
2015-06-15 19:53:58 +03:00
static const vshClientHooks hooks ;
2009-02-09 17:24:06 +03:00
2010-03-05 12:59:52 +03:00
/*
* Detection of disconnections and automatic reconnection support
*/
2014-10-28 21:38:04 +03:00
static int disconnected ; /* we may have been disconnected */
2010-03-05 12:59:52 +03:00
/*
2015-06-15 19:53:58 +03:00
* virshCatchDisconnect :
2010-03-05 12:59:52 +03:00
*
2012-08-02 21:15:16 +04:00
* We get here when the connection was closed . We can ' t do much in the
* handler , just save the fact it was raised .
2010-03-05 12:59:52 +03:00
*/
2010-03-12 13:39:24 +03:00
static void
2015-09-15 17:46:07 +03:00
virshCatchDisconnect ( virConnectPtr conn ,
2015-06-15 19:53:58 +03:00
int reason ,
2015-09-15 17:46:07 +03:00
void * opaque )
2012-08-02 21:15:16 +04:00
{
2015-09-15 17:46:07 +03:00
if ( reason ! = VIR_CONNECT_CLOSE_REASON_CLIENT ) {
vshControl * ctl = opaque ;
const char * str = " unknown reason " ;
virErrorPtr error ;
char * uri ;
error = virSaveLastError ( ) ;
uri = virConnectGetURI ( conn ) ;
switch ( ( virConnectCloseReason ) reason ) {
case VIR_CONNECT_CLOSE_REASON_ERROR :
str = N_ ( " Disconnected from %s due to I/O error " ) ;
break ;
case VIR_CONNECT_CLOSE_REASON_EOF :
str = N_ ( " Disconnected from %s due to end of file " ) ;
break ;
case VIR_CONNECT_CLOSE_REASON_KEEPALIVE :
str = N_ ( " Disconnected from %s due to keepalive timeout " ) ;
break ;
2015-09-24 01:11:30 +03:00
/* coverity[dead_error_condition] */
2015-09-15 17:46:07 +03:00
case VIR_CONNECT_CLOSE_REASON_CLIENT :
case VIR_CONNECT_CLOSE_REASON_LAST :
break ;
}
vshError ( ctl , _ ( str ) , NULLSTR ( uri ) ) ;
if ( error ) {
virSetError ( error ) ;
virFreeError ( error ) ;
}
2012-08-02 21:15:16 +04:00
disconnected + + ;
2015-09-15 17:46:07 +03:00
}
2010-03-05 12:59:52 +03:00
}
2014-03-06 20:20:11 +04:00
/* Main Function which should be used for connecting.
* This function properly handles keepalive settings . */
virConnectPtr
2015-06-15 19:53:58 +03:00
virshConnect ( vshControl * ctl , const char * uri , bool readonly )
2014-03-06 20:20:11 +04:00
{
virConnectPtr c = NULL ;
int interval = 5 ; /* Default */
int count = 6 ; /* Default */
bool keepalive_forced = false ;
if ( ctl - > keepalive_interval > = 0 ) {
interval = ctl - > keepalive_interval ;
keepalive_forced = true ;
}
if ( ctl - > keepalive_count > = 0 ) {
count = ctl - > keepalive_count ;
keepalive_forced = true ;
}
c = virConnectOpenAuth ( uri , virConnectAuthPtrDefault ,
readonly ? VIR_CONNECT_RO : 0 ) ;
if ( ! c )
return NULL ;
if ( interval > 0 & &
virConnectSetKeepAlive ( c , interval , count ) ! = 0 ) {
if ( keepalive_forced ) {
vshError ( ctl , " %s " ,
_ ( " Cannot setup keepalive on connection "
" as requested, disconnecting " ) ) ;
virConnectClose ( c ) ;
return NULL ;
}
vshDebug ( ctl , VSH_ERR_INFO , " %s " ,
_ ( " Failed to setup keepalive on connection \n " ) ) ;
}
return c ;
}
2010-03-05 12:59:52 +03:00
/*
2015-06-15 19:53:58 +03:00
* virshReconnect :
2010-03-05 12:59:52 +03:00
*
2010-03-12 13:39:24 +03:00
* Reconnect after a disconnect from libvirtd
2010-03-05 12:59:52 +03:00
*
*/
2010-03-12 13:39:24 +03:00
static void
2015-06-15 19:53:58 +03:00
virshReconnect ( vshControl * ctl )
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
{
bool connected = false ;
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
2015-06-15 19:53:58 +03:00
if ( priv - > conn ) {
2013-03-26 13:54:55 +04:00
int ret ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
connected = true ;
2013-03-26 13:54:55 +04:00
2015-06-15 19:53:58 +03:00
virConnectUnregisterCloseCallback ( priv - > conn , virshCatchDisconnect ) ;
ret = virConnectClose ( priv - > conn ) ;
2013-03-26 13:54:55 +04:00
if ( ret < 0 )
vshError ( ctl , " %s " , _ ( " Failed to disconnect from the hypervisor " ) ) ;
else if ( ret > 0 )
vshError ( ctl , " %s " , _ ( " One or more references were leaked after "
" disconnect from the hypervisor " ) ) ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
}
2010-03-05 12:59:52 +03:00
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
priv - > conn = virshConnect ( ctl , priv - > connname , priv - > readonly ) ;
2014-03-06 20:20:11 +04:00
2015-06-15 19:53:58 +03:00
if ( ! priv - > conn ) {
2012-12-13 17:08:00 +04:00
if ( disconnected )
vshError ( ctl , " %s " , _ ( " Failed to reconnect to the hypervisor " ) ) ;
else
vshError ( ctl , " %s " , _ ( " failed to connect to the hypervisor " ) ) ;
2012-08-02 21:15:16 +04:00
} else {
2015-06-15 19:53:58 +03:00
if ( virConnectRegisterCloseCallback ( priv - > conn , virshCatchDisconnect ,
2012-08-02 21:15:16 +04:00
NULL , NULL ) < 0 )
vshError ( ctl , " %s " , _ ( " Unable to register disconnect callback " ) ) ;
if ( connected )
vshError ( ctl , " %s " , _ ( " Reconnected to the hypervisor " ) ) ;
}
2010-03-05 12:59:52 +03:00
disconnected = 0 ;
2015-06-15 19:53:58 +03:00
priv - > useGetInfo = false ;
priv - > useSnapshotOld = false ;
priv - > blockJobNoBytes = false ;
2010-03-05 12:59:52 +03:00
}
2007-02-14 18:44:58 +03:00
2015-06-15 19:53:58 +03:00
int virshStreamSink ( virStreamPtr st ATTRIBUTE_UNUSED ,
const char * bytes , size_t nbytes , void * opaque )
{
int * fd = opaque ;
return safewrite ( * fd , bytes , nbytes ) ;
}
2013-03-27 17:22:47 +04:00
2015-06-15 19:53:58 +03:00
/* ---------------
* Command Connect
* - - - - - - - - - - - - - - -
2013-03-27 17:22:47 +04:00
*/
static const vshCmdOptDef opts_connect [ ] = {
{ . name = " name " ,
2014-12-11 05:46:15 +03:00
. type = VSH_OT_STRING ,
2013-03-27 17:22:47 +04:00
. flags = VSH_OFLAG_EMPTY_OK ,
. help = N_ ( " hypervisor connection URI " )
} ,
{ . name = " readonly " ,
. type = VSH_OT_BOOL ,
. help = N_ ( " read-only connection " )
} ,
{ . name = NULL }
} ;
2015-06-15 19:53:58 +03:00
static const vshCmdInfo info_connect [ ] = {
2013-02-07 19:25:10 +04:00
{ . name = " help " ,
2015-06-15 19:53:58 +03:00
. data = N_ ( " (re)connect to hypervisor " )
2013-02-07 19:25:10 +04:00
} ,
{ . name = " desc " ,
2015-06-15 19:53:58 +03:00
. data = N_ ( " Connect to local hypervisor. This is built-in "
" command after shell start up. " )
2013-01-14 15:26:23 +04:00
} ,
{ . name = NULL }
2009-07-16 18:40:08 +04:00
} ;
2011-04-19 02:37:42 +04:00
static bool
2015-06-15 19:53:58 +03:00
cmdConnect ( vshControl * ctl , const vshCmd * cmd )
2009-07-16 18:40:08 +04:00
{
2015-06-15 19:53:58 +03:00
bool ro = vshCommandOptBool ( cmd , " readonly " ) ;
const char * name = NULL ;
virshControlPtr priv = ctl - > privData ;
2009-07-16 18:40:08 +04:00
2015-06-15 19:53:58 +03:00
if ( priv - > conn ) {
int ret ;
2011-11-30 23:42:20 +04:00
2015-06-15 19:53:58 +03:00
virConnectUnregisterCloseCallback ( priv - > conn , virshCatchDisconnect ) ;
ret = virConnectClose ( priv - > conn ) ;
if ( ret < 0 )
vshError ( ctl , " %s " , _ ( " Failed to disconnect from the hypervisor " ) ) ;
else if ( ret > 0 )
vshError ( ctl , " %s " , _ ( " One or more references were leaked after "
" disconnect from the hypervisor " ) ) ;
priv - > conn = NULL ;
2011-10-11 17:05:52 +04:00
}
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
VIR_FREE ( priv - > connname ) ;
2015-06-15 19:53:58 +03:00
if ( vshCommandOptStringReq ( ctl , cmd , " name " , & name ) < 0 )
return false ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
priv - > connname = vshStrdup ( ctl , name ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
priv - > useGetInfo = false ;
priv - > useSnapshotOld = false ;
priv - > blockJobNoBytes = false ;
priv - > readonly = ro ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
priv - > conn = virshConnect ( ctl , priv - > connname , priv - > readonly ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( ! priv - > conn ) {
vshError ( ctl , " %s " , _ ( " Failed to connect to the hypervisor " ) ) ;
return false ;
}
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( virConnectRegisterCloseCallback ( priv - > conn , virshCatchDisconnect ,
NULL , NULL ) < 0 )
vshError ( ctl , " %s " , _ ( " Unable to register disconnect callback " ) ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
return true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
/* ---------------
* Utils for work with runtime commands data
* - - - - - - - - - - - - - - -
*/
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
static bool
virshConnectionUsability ( vshControl * ctl , virConnectPtr conn )
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
{
2015-06-15 19:53:58 +03:00
if ( ! conn | |
virConnectIsAlive ( conn ) = = 0 ) {
vshError ( ctl , " %s " , _ ( " no valid connection " ) ) ;
return false ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
/* The connection is considered dead only if
* virConnectIsAlive ( ) successfuly says so .
*/
vshResetLibvirtError ( ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
return true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
static void *
virshConnectionHandler ( vshControl * ctl )
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( ! priv - > conn | | disconnected )
virshReconnect ( ctl ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( virshConnectionUsability ( ctl , priv - > conn ) )
return priv - > conn ;
return NULL ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
/* ---------------
* Misc utils
* - - - - - - - - - - - - - - -
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
*/
2015-06-15 19:53:58 +03:00
int
virshDomainState ( vshControl * ctl , virDomainPtr dom , int * reason )
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
{
2015-06-15 19:53:58 +03:00
virDomainInfo info ;
virshControlPtr priv = ctl - > privData ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( reason )
* reason = - 1 ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( ! priv - > useGetInfo ) {
int state ;
if ( virDomainGetState ( dom , & state , reason , 0 ) < 0 ) {
virErrorPtr err = virGetLastError ( ) ;
if ( err & & err - > code = = VIR_ERR_NO_SUPPORT )
priv - > useGetInfo = true ;
else
return - 1 ;
} else {
return state ;
2011-05-09 11:08:06 +04:00
}
}
2015-06-15 19:53:58 +03:00
/* fall back to virDomainGetInfo if virDomainGetState is not supported */
if ( virDomainGetInfo ( dom , & info ) < 0 )
return - 1 ;
else
return info . state ;
2013-08-21 13:02:42 +04:00
}
/*
* Initialize connection .
*/
static bool
2015-06-15 19:53:58 +03:00
virshInit ( vshControl * ctl )
2013-08-21 13:02:42 +04:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
2013-08-27 15:19:24 +04:00
/* Since we have the commandline arguments parsed, we need to
2015-09-03 17:59:01 +03:00
* reload our initial settings to make debugging and readline
* work properly */
vshInitReload ( ctl ) ;
2013-08-27 15:19:24 +04:00
2015-06-15 19:53:58 +03:00
if ( priv - > conn )
2013-08-21 13:02:42 +04:00
return false ;
2007-06-06 16:24:31 +04:00
2006-02-28 00:34:28 +03:00
/* set up the library error handler */
2015-06-15 19:53:58 +03:00
virSetErrorFunc ( NULL , vshErrorHandler ) ;
2006-03-15 15:13:25 +03:00
2011-03-02 19:59:54 +03:00
if ( virEventRegisterDefaultImpl ( ) < 0 )
2011-04-19 02:37:42 +04:00
return false ;
2010-07-27 13:40:30 +04:00
2011-10-11 17:05:52 +04:00
if ( virThreadCreate ( & ctl - > eventLoop , true , vshEventLoop , ctl ) < 0 )
return false ;
ctl - > eventLoopStarted = true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
if ( ( ctl - > eventTimerId = virEventAddTimeout ( - 1 , vshEventTimeout , ctl ,
NULL ) ) < 0 )
return false ;
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
if ( priv - > connname ) {
2015-06-15 19:53:58 +03:00
virshReconnect ( ctl ) ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
/* Connecting to a named connection must succeed, but we delay
* connecting to the default connection until we need it
* ( since the first command might be ' connect ' which allows a
* non - default connection , or might be ' help ' which needs no
* connection ) .
*/
2015-06-15 19:53:58 +03:00
if ( ! priv - > conn ) {
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
vshReportError ( ctl ) ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
return false ;
}
2007-12-05 19:24:22 +03:00
}
2005-12-08 13:23:34 +03:00
2011-04-19 02:37:42 +04:00
return true ;
2005-12-08 13:23:34 +03:00
}
2011-11-30 23:42:20 +04:00
static void
2015-06-15 19:53:58 +03:00
virshDeinitTimer ( int timer ATTRIBUTE_UNUSED , void * opaque ATTRIBUTE_UNUSED )
2011-11-30 23:42:20 +04:00
{
/* nothing to be done here */
}
2005-12-08 13:23:34 +03:00
/*
2008-02-04 17:58:47 +03:00
* Deinitialize virsh
2005-12-08 13:23:34 +03:00
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static bool
2015-06-15 19:53:58 +03:00
virshDeinit ( vshControl * ctl )
2006-03-15 15:13:25 +03:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
vshDeinit ( ctl ) ;
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
VIR_FREE ( priv - > connname ) ;
2015-06-15 19:53:58 +03:00
if ( priv - > conn ) {
2010-11-11 18:15:46 +03:00
int ret ;
2015-06-15 19:53:58 +03:00
virConnectUnregisterCloseCallback ( priv - > conn , virshCatchDisconnect ) ;
ret = virConnectClose ( priv - > conn ) ;
2013-03-26 13:54:55 +04:00
if ( ret < 0 )
vshError ( ctl , " %s " , _ ( " Failed to disconnect from the hypervisor " ) ) ;
else if ( ret > 0 )
vshError ( ctl , " %s " , _ ( " One or more references were leaked after "
" disconnect from the hypervisor " ) ) ;
2005-12-08 13:23:34 +03:00
}
2007-12-01 18:45:25 +03:00
virResetLastError ( ) ;
2011-10-11 17:05:52 +04:00
if ( ctl - > eventLoopStarted ) {
2011-11-30 23:42:20 +04:00
int timer ;
virMutexLock ( & ctl - > lock ) ;
ctl - > quit = true ;
2011-10-11 17:05:52 +04:00
/* HACK: Add a dummy timeout to break event loop */
2015-06-15 19:53:58 +03:00
timer = virEventAddTimeout ( 0 , virshDeinitTimer , NULL , NULL ) ;
2011-11-30 23:42:20 +04:00
virMutexUnlock ( & ctl - > lock ) ;
virThreadJoin ( & ctl - > eventLoop ) ;
2011-10-11 17:05:52 +04:00
if ( timer ! = - 1 )
virEventRemoveTimeout ( timer ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
if ( ctl - > eventTimerId ! = - 1 )
virEventRemoveTimeout ( ctl - > eventTimerId ) ;
2011-10-11 17:05:52 +04:00
ctl - > eventLoopStarted = false ;
}
2011-11-30 23:42:20 +04:00
virMutexDestroy ( & ctl - > lock ) ;
2011-04-19 02:37:42 +04:00
return true ;
2005-12-08 13:23:34 +03:00
}
2006-03-15 15:13:25 +03:00
2005-12-08 13:23:34 +03:00
/*
* Print usage
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static void
2015-06-15 19:53:58 +03:00
virshUsage ( void )
2006-03-15 15:13:25 +03:00
{
2010-11-30 09:37:04 +03:00
const vshCmdGrp * grp ;
2008-08-01 16:19:56 +04:00
const vshCmdDef * cmd ;
2010-11-30 09:37:04 +03:00
2010-10-12 11:14:01 +04:00
fprintf ( stdout , _ ( " \n %s [options]... [<command_string>] "
" \n %s [options]... <command> [args...] \n \n "
2008-12-08 16:14:48 +03:00
" options: \n "
2011-11-22 20:08:05 +04:00
" -c | --connect=URI hypervisor connection URI \n "
" -d | --debug=NUM debug level [0-4] \n "
2014-03-06 19:53:53 +04:00
" -e | --escape <char> set escape sequence for console \n "
2008-12-08 16:14:48 +03:00
" -h | --help this help \n "
2014-03-06 20:20:11 +04:00
" -k | --keepalive-interval=NUM \n "
" keepalive interval in seconds, 0 for disable \n "
" -K | --keepalive-count=NUM \n "
" number of possible missed keepalive messages \n "
2014-03-06 19:53:53 +04:00
" -l | --log=FILE output logging to file \n "
2008-12-08 16:14:48 +03:00
" -q | --quiet quiet mode \n "
2014-03-06 19:53:53 +04:00
" -r | --readonly connect readonly \n "
2008-12-08 16:14:48 +03:00
" -t | --timing print timing information \n "
2011-11-22 20:08:05 +04:00
" -v short version \n "
" -V long version \n "
" --version[=TYPE] version, TYPE is short or long (default short) \n "
2015-06-15 19:53:58 +03:00
" commands (non interactive mode): \n \n " ) , progname ,
progname ) ;
2008-12-08 16:14:48 +03:00
2010-11-30 09:37:04 +03:00
for ( grp = cmdGroups ; grp - > name ; grp + + ) {
2012-03-16 23:23:00 +04:00
fprintf ( stdout , _ ( " %s (help keyword '%s') \n " ) ,
grp - > name , grp - > keyword ) ;
for ( cmd = grp - > commands ; cmd - > name ; cmd + + ) {
if ( cmd - > flags & VSH_CMD_FLAG_ALIAS )
continue ;
2010-11-30 09:37:04 +03:00
fprintf ( stdout ,
2012-03-16 23:23:00 +04:00
" %-30s %s \n " , cmd - > name ,
_ ( vshCmddefGetInfo ( cmd , " help " ) ) ) ;
}
2010-11-30 09:37:04 +03:00
fprintf ( stdout , " \n " ) ;
}
fprintf ( stdout , " %s " ,
_ ( " \n (specify help <group> for details about the commands in the group) \n " ) ) ;
2008-12-08 16:14:48 +03:00
fprintf ( stdout , " %s " ,
_ ( " \n (specify help <command> for details about the command) \n \n " ) ) ;
return ;
2005-12-08 13:23:34 +03:00
}
2010-11-08 17:03:32 +03:00
/*
* Show version and options compiled in
*/
static void
2015-06-15 19:53:58 +03:00
virshShowVersion ( vshControl * ctl ATTRIBUTE_UNUSED )
2010-11-08 17:03:32 +03:00
{
/* FIXME - list a copyright blurb, as in GNU programs? */
vshPrint ( ctl , _ ( " Virsh command line tool of libvirt %s \n " ) , VERSION ) ;
vshPrint ( ctl , _ ( " See web site at %s \n \n " ) , " http://libvirt.org/ " ) ;
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Compiled with support for: \n " ) ) ;
vshPrint ( ctl , " %s " , _ ( " Hypervisors: " ) ) ;
2010-11-08 17:03:32 +03:00
# ifdef WITH_QEMU
2012-09-10 02:07:28 +04:00
vshPrint ( ctl , " QEMU/KVM " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_LXC
vshPrint ( ctl , " LXC " ) ;
# endif
2010-11-08 17:03:32 +03:00
# ifdef WITH_UML
vshPrint ( ctl , " UML " ) ;
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_XEN
vshPrint ( ctl , " Xen " ) ;
# endif
# ifdef WITH_LIBXL
vshPrint ( ctl , " LibXL " ) ;
# endif
2010-11-08 17:03:32 +03:00
# ifdef WITH_OPENVZ
vshPrint ( ctl , " OpenVZ " ) ;
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_VMWARE
vshPrint ( ctl , " VMWare " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_PHYP
vshPrint ( ctl , " PHYP " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_VBOX
vshPrint ( ctl , " VirtualBox " ) ;
2010-11-08 17:03:32 +03:00
# endif
# ifdef WITH_ESX
vshPrint ( ctl , " ESX " ) ;
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_HYPERV
vshPrint ( ctl , " Hyper-V " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_XENAPI
vshPrint ( ctl , " XenAPI " ) ;
2010-11-08 17:03:32 +03:00
# endif
2014-06-10 22:25:10 +04:00
# ifdef WITH_BHYVE
vshPrint ( ctl , " Bhyve " ) ;
# endif
2010-11-08 17:03:32 +03:00
# ifdef WITH_TEST
vshPrint ( ctl , " Test " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Networking: " ) ) ;
2010-11-08 17:03:32 +03:00
# ifdef WITH_REMOTE
vshPrint ( ctl , " Remote " ) ;
# endif
# ifdef WITH_NETWORK
vshPrint ( ctl , " Network " ) ;
# endif
# ifdef WITH_BRIDGE
vshPrint ( ctl , " Bridging " ) ;
# endif
2012-09-18 05:27:06 +04:00
# if defined(WITH_INTERFACE)
2012-06-26 22:31:31 +04:00
vshPrint ( ctl , " Interface " ) ;
2012-09-18 05:27:06 +04:00
# if defined(WITH_NETCF)
vshPrint ( ctl , " netcf " ) ;
2012-09-20 18:24:47 +04:00
# elif defined(WITH_UDEV)
2012-10-06 23:20:25 +04:00
vshPrint ( ctl , " udev " ) ;
2012-09-18 05:27:06 +04:00
# endif
2010-11-08 17:03:32 +03:00
# endif
# ifdef WITH_NWFILTER
vshPrint ( ctl , " Nwfilter " ) ;
# endif
# ifdef WITH_VIRTUALPORT
vshPrint ( ctl , " VirtualPort " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Storage: " ) ) ;
2010-11-08 17:03:32 +03:00
# ifdef WITH_STORAGE_DIR
vshPrint ( ctl , " Dir " ) ;
# endif
# ifdef WITH_STORAGE_DISK
vshPrint ( ctl , " Disk " ) ;
# endif
# ifdef WITH_STORAGE_FS
vshPrint ( ctl , " Filesystem " ) ;
# endif
# ifdef WITH_STORAGE_SCSI
vshPrint ( ctl , " SCSI " ) ;
# endif
# ifdef WITH_STORAGE_MPATH
vshPrint ( ctl , " Multipath " ) ;
# endif
# ifdef WITH_STORAGE_ISCSI
vshPrint ( ctl , " iSCSI " ) ;
# endif
# ifdef WITH_STORAGE_LVM
vshPrint ( ctl , " LVM " ) ;
2012-05-14 13:06:42 +04:00
# endif
# ifdef WITH_STORAGE_RBD
vshPrint ( ctl , " RBD " ) ;
2012-07-18 23:06:58 +04:00
# endif
# ifdef WITH_STORAGE_SHEEPDOG
vshPrint ( ctl , " Sheepdog " ) ;
2013-12-12 07:08:10 +04:00
# endif
# ifdef WITH_STORAGE_GLUSTER
vshPrint ( ctl , " Gluster " ) ;
2010-11-08 17:03:32 +03:00
# endif
vshPrint ( ctl , " \n " ) ;
2012-07-23 07:57:53 +04:00
vshPrint ( ctl , " %s " , _ ( " Miscellaneous: " ) ) ;
2012-09-10 02:07:27 +04:00
# ifdef WITH_LIBVIRTD
vshPrint ( ctl , " Daemon " ) ;
# endif
2012-07-23 07:57:53 +04:00
# ifdef WITH_NODE_DEVICES
vshPrint ( ctl , " Nodedev " ) ;
# endif
# ifdef WITH_SECDRIVER_APPARMOR
vshPrint ( ctl , " AppArmor " ) ;
# endif
# ifdef WITH_SECDRIVER_SELINUX
vshPrint ( ctl , " SELinux " ) ;
# endif
# ifdef WITH_SECRETS
vshPrint ( ctl , " Secrets " ) ;
# endif
# ifdef ENABLE_DEBUG
vshPrint ( ctl , " Debug " ) ;
# endif
# ifdef WITH_DTRACE_PROBES
vshPrint ( ctl , " DTrace " ) ;
# endif
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2012-07-23 07:57:53 +04:00
vshPrint ( ctl , " Readline " ) ;
# endif
# ifdef WITH_DRIVER_MODULES
vshPrint ( ctl , " Modular " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
}
static bool
2015-06-15 19:53:58 +03:00
virshAllowedEscapeChar ( char c )
2012-07-23 07:57:53 +04:00
{
/* Allowed escape characters:
* a - z A - Z @ [ \ ] ^ _
*/
return ( ' a ' < = c & & c < = ' z ' ) | |
( ' @ ' < = c & & c < = ' _ ' ) ;
}
/*
* argv [ ] : virsh [ options ] [ command ]
*
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static bool
2015-06-15 19:53:58 +03:00
virshParseArgv ( vshControl * ctl , int argc , char * * argv )
2012-07-23 07:57:53 +04:00
{
2014-03-06 20:20:11 +04:00
int arg , len , debug , keepalive ;
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 18:09:33 +04:00
size_t i ;
2013-04-29 21:12:17 +04:00
int longindex = - 1 ;
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
2012-07-23 07:57:53 +04:00
struct option opt [ ] = {
2014-03-06 19:53:53 +04:00
{ " connect " , required_argument , NULL , ' c ' } ,
2012-07-23 07:57:53 +04:00
{ " debug " , required_argument , NULL , ' d ' } ,
2014-03-06 19:53:53 +04:00
{ " escape " , required_argument , NULL , ' e ' } ,
2012-07-23 07:57:53 +04:00
{ " help " , no_argument , NULL , ' h ' } ,
2014-03-06 20:20:11 +04:00
{ " keepalive-interval " , required_argument , NULL , ' k ' } ,
{ " keepalive-count " , required_argument , NULL , ' K ' } ,
2014-03-06 19:53:53 +04:00
{ " log " , required_argument , NULL , ' l ' } ,
2012-07-23 07:57:53 +04:00
{ " quiet " , no_argument , NULL , ' q ' } ,
2014-03-06 19:53:53 +04:00
{ " readonly " , no_argument , NULL , ' r ' } ,
2012-07-23 07:57:53 +04:00
{ " timing " , no_argument , NULL , ' t ' } ,
{ " version " , optional_argument , NULL , ' v ' } ,
{ NULL , 0 , NULL , 0 }
} ;
/* Standard (non-command) options. The leading + ensures that no
* argument reordering takes place , so that command options are
* not confused with top - level virsh options . */
2014-03-06 20:20:11 +04:00
while ( ( arg = getopt_long ( argc , argv , " +:c:d:e:hk:K:l:qrtvV " , opt , & longindex ) ) ! = - 1 ) {
2012-07-23 07:57:53 +04:00
switch ( arg ) {
2014-03-06 19:53:53 +04:00
case ' c ' :
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
VIR_FREE ( priv - > connname ) ;
priv - > connname = vshStrdup ( ctl , optarg ) ;
2014-03-06 19:53:53 +04:00
break ;
2012-07-23 07:57:53 +04:00
case ' d ' :
2012-07-26 18:05:51 +04:00
if ( virStrToLong_i ( optarg , NULL , 10 , & debug ) < 0 ) {
2013-04-29 21:12:17 +04:00
vshError ( ctl , _ ( " option %s takes a numeric argument " ) ,
longindex = = - 1 ? " -d " : " --debug " ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_FAILURE ) ;
}
2012-07-26 18:05:51 +04:00
if ( debug < VSH_ERR_DEBUG | | debug > VSH_ERR_ERROR )
vshError ( ctl , _ ( " ignoring debug level %d out of range [%d-%d] " ) ,
debug , VSH_ERR_DEBUG , VSH_ERR_ERROR ) ;
else
ctl - > debug = debug ;
2012-07-23 07:57:53 +04:00
break ;
2014-03-06 19:53:53 +04:00
case ' e ' :
len = strlen ( optarg ) ;
if ( ( len = = 2 & & * optarg = = ' ^ ' & &
2015-06-15 19:53:58 +03:00
virshAllowedEscapeChar ( optarg [ 1 ] ) ) | |
2014-03-06 19:53:53 +04:00
( len = = 1 & & * optarg ! = ' ^ ' ) ) {
2015-06-15 19:53:58 +03:00
priv - > escapeChar = optarg ;
2014-03-06 19:53:53 +04:00
} else {
vshError ( ctl , _ ( " Invalid string '%s' for escape sequence " ) ,
optarg ) ;
exit ( EXIT_FAILURE ) ;
}
break ;
2012-07-23 07:57:53 +04:00
case ' h ' :
2015-06-15 19:53:58 +03:00
virshUsage ( ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_SUCCESS ) ;
break ;
2014-03-06 20:20:11 +04:00
case ' k ' :
2014-08-27 18:20:29 +04:00
if ( virStrToLong_i ( optarg , NULL , 0 , & keepalive ) < 0 ) {
vshError ( ctl ,
_ ( " Invalid value for option %s " ) ,
longindex = = - 1 ? " -k " : " --keepalive-interval " ) ;
exit ( EXIT_FAILURE ) ;
}
if ( keepalive < 0 ) {
vshError ( ctl ,
_ ( " option %s requires a positive integer argument " ) ,
2014-03-06 20:20:11 +04:00
longindex = = - 1 ? " -k " : " --keepalive-interval " ) ;
exit ( EXIT_FAILURE ) ;
}
ctl - > keepalive_interval = keepalive ;
break ;
case ' K ' :
2014-08-27 18:20:29 +04:00
if ( virStrToLong_i ( optarg , NULL , 0 , & keepalive ) < 0 ) {
vshError ( ctl ,
_ ( " Invalid value for option %s " ) ,
longindex = = - 1 ? " -K " : " --keepalive-count " ) ;
exit ( EXIT_FAILURE ) ;
}
if ( keepalive < 0 ) {
vshError ( ctl ,
_ ( " option %s requires a positive integer argument " ) ,
2014-03-06 20:20:11 +04:00
longindex = = - 1 ? " -K " : " --keepalive-count " ) ;
exit ( EXIT_FAILURE ) ;
}
ctl - > keepalive_count = keepalive ;
break ;
2014-03-06 19:53:53 +04:00
case ' l ' :
vshCloseLogFile ( ctl ) ;
ctl - > logfile = vshStrdup ( ctl , optarg ) ;
vshOpenLogFile ( ctl ) ;
break ;
2012-07-23 07:57:53 +04:00
case ' q ' :
ctl - > quiet = true ;
break ;
case ' t ' :
ctl - > timing = true ;
break ;
2014-03-06 19:53:53 +04:00
case ' r ' :
2015-06-15 19:53:58 +03:00
priv - > readonly = true ;
2012-07-23 07:57:53 +04:00
break ;
case ' v ' :
if ( STRNEQ_NULLABLE ( optarg , " long " ) ) {
puts ( VERSION ) ;
exit ( EXIT_SUCCESS ) ;
}
/* fall through */
case ' V ' :
2015-06-15 19:53:58 +03:00
virshShowVersion ( ctl ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_SUCCESS ) ;
2013-02-19 12:50:59 +04:00
case ' : ' :
2013-04-29 21:12:17 +04:00
for ( i = 0 ; opt [ i ] . name ! = NULL ; i + + ) {
2013-05-01 19:07:56 +04:00
if ( opt [ i ] . val = = optopt )
break ;
2013-04-29 21:12:17 +04:00
}
2013-05-01 19:07:56 +04:00
if ( opt [ i ] . name )
vshError ( ctl , _ ( " option '-%c'/'--%s' requires an argument " ) ,
optopt , opt [ i ] . name ) ;
else
vshError ( ctl , _ ( " option '-%c' requires an argument " ) , optopt ) ;
exit ( EXIT_FAILURE ) ;
2013-02-19 12:50:59 +04:00
case ' ? ' :
2013-04-29 21:12:17 +04:00
if ( optopt )
vshError ( ctl , _ ( " unsupported option '-%c'. See --help. " ) , optopt ) ;
else
vshError ( ctl , _ ( " unsupported option '%s'. See --help. " ) , argv [ optind - 1 ] ) ;
2013-02-19 12:50:59 +04:00
exit ( EXIT_FAILURE ) ;
2012-07-23 07:57:53 +04:00
default :
2013-02-19 12:50:59 +04:00
vshError ( ctl , _ ( " unknown option " ) ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_FAILURE ) ;
}
2013-04-29 21:12:17 +04:00
longindex = - 1 ;
2012-07-23 07:57:53 +04:00
}
2015-09-04 12:19:55 +03:00
if ( argc = = optind ) {
ctl - > imode = true ;
} else {
2012-07-23 07:57:53 +04:00
/* parse command */
ctl - > imode = false ;
if ( argc - optind = = 1 ) {
vshDebug ( ctl , VSH_ERR_INFO , " commands: \" %s \" \n " , argv [ optind ] ) ;
return vshCommandStringParse ( ctl , argv [ optind ] ) ;
} else {
return vshCommandArgvParse ( ctl , argc - optind , argv + optind ) ;
}
}
return true ;
}
static const vshCmdDef virshCmds [ ] = {
2015-06-15 19:53:58 +03:00
VSH_CMD_CD ,
VSH_CMD_ECHO ,
VSH_CMD_EXIT ,
VSH_CMD_HELP ,
VSH_CMD_PWD ,
VSH_CMD_QUIT ,
2013-03-27 17:22:47 +04:00
{ . name = " connect " ,
. handler = cmdConnect ,
. opts = opts_connect ,
. info = info_connect ,
. flags = VSH_CMD_FLAG_NOCONNECT
2013-02-07 19:25:10 +04:00
} ,
{ . name = NULL }
2012-07-23 07:57:53 +04:00
} ;
2011-11-22 20:08:05 +04:00
2012-07-23 07:57:53 +04:00
static const vshCmdGrp cmdGroups [ ] = {
2015-06-15 19:53:58 +03:00
{ VIRSH_CMD_GRP_DOM_MANAGEMENT , " domain " , domManagementCmds } ,
{ VIRSH_CMD_GRP_DOM_MONITORING , " monitor " , domMonitoringCmds } ,
{ VIRSH_CMD_GRP_HOST_AND_HV , " host " , hostAndHypervisorCmds } ,
{ VIRSH_CMD_GRP_IFACE , " interface " , ifaceCmds } ,
{ VIRSH_CMD_GRP_NWFILTER , " filter " , nwfilterCmds } ,
{ VIRSH_CMD_GRP_NETWORK , " network " , networkCmds } ,
{ VIRSH_CMD_GRP_NODEDEV , " nodedev " , nodedevCmds } ,
{ VIRSH_CMD_GRP_SECRET , " secret " , secretCmds } ,
{ VIRSH_CMD_GRP_SNAPSHOT , " snapshot " , snapshotCmds } ,
{ VIRSH_CMD_GRP_STORAGE_POOL , " pool " , storagePoolCmds } ,
{ VIRSH_CMD_GRP_STORAGE_VOL , " volume " , storageVolCmds } ,
{ VIRSH_CMD_GRP_VIRSH , " virsh " , virshCmds } ,
2012-07-23 07:57:53 +04:00
{ NULL , NULL , NULL }
} ;
2005-12-08 13:23:34 +03:00
2015-06-15 19:53:58 +03:00
static const vshClientHooks hooks = {
. connHandler = virshConnectionHandler
} ;
2006-03-15 15:13:25 +03:00
int
main ( int argc , char * * argv )
{
vshControl _ctl , * ctl = & _ctl ;
2015-06-15 19:53:58 +03:00
virshControl virshCtl ;
2013-10-09 14:18:15 +04:00
const char * defaultConn ;
2011-04-19 02:37:42 +04:00
bool ret = true ;
2005-12-08 13:23:34 +03:00
2011-05-09 15:57:09 +04:00
memset ( ctl , 0 , sizeof ( vshControl ) ) ;
2015-06-15 19:53:58 +03:00
memset ( & virshCtl , 0 , sizeof ( virshControl ) ) ;
ctl - > name = " virsh " ; /* hardcoded name of the binary */
2011-05-09 15:57:09 +04:00
ctl - > log_fd = - 1 ; /* Initialize log file descriptor */
2011-07-14 15:58:02 +04:00
ctl - > debug = VSH_DEBUG_DEFAULT ;
2015-06-15 19:53:58 +03:00
ctl - > hooks = & hooks ;
2014-03-06 20:20:11 +04:00
/* In order to distinguish default from setting to 0 */
ctl - > keepalive_interval = - 1 ;
ctl - > keepalive_count = - 1 ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
ctl - > eventPipe [ 0 ] = - 1 ;
ctl - > eventPipe [ 1 ] = - 1 ;
ctl - > eventTimerId = - 1 ;
2015-06-15 19:53:58 +03:00
virshCtl . escapeChar = " ^] " ; /* Same default as telnet */
ctl - > privData = & virshCtl ;
if ( ! ( progname = strrchr ( argv [ 0 ] , ' / ' ) ) )
progname = argv [ 0 ] ;
else
progname + + ;
ctl - > progname = progname ;
2011-05-09 15:57:09 +04:00
2006-09-21 19:24:37 +04:00
if ( ! setlocale ( LC_ALL , " " ) ) {
perror ( " setlocale " ) ;
2009-01-29 14:49:33 +03:00
/* failure to setup locale is not fatal */
2006-09-21 19:24:37 +04:00
}
2010-11-16 17:54:17 +03:00
if ( ! bindtextdomain ( PACKAGE , LOCALEDIR ) ) {
2006-09-21 19:24:37 +04:00
perror ( " bindtextdomain " ) ;
2010-11-16 22:01:37 +03:00
return EXIT_FAILURE ;
2006-09-21 19:24:37 +04:00
}
2010-11-16 17:54:17 +03:00
if ( ! textdomain ( PACKAGE ) ) {
2006-09-21 19:24:37 +04:00
perror ( " textdomain " ) ;
2010-11-16 22:01:37 +03:00
return EXIT_FAILURE ;
2006-09-21 19:24:37 +04:00
}
2013-08-29 12:36:00 +04:00
if ( isatty ( STDIN_FILENO ) ) {
ctl - > istty = true ;
2013-09-05 01:57:30 +04:00
# ifndef WIN32
2013-08-29 12:36:00 +04:00
if ( tcgetattr ( STDIN_FILENO , & ctl - > termattr ) < 0 )
ctl - > istty = false ;
2013-09-05 01:57:30 +04:00
# endif
2013-08-29 12:36:00 +04:00
}
2011-11-30 23:42:20 +04:00
if ( virMutexInit ( & ctl - > lock ) < 0 ) {
vshError ( ctl , " %s " , _ ( " Failed to initialize mutex " ) ) ;
return EXIT_FAILURE ;
}
2011-05-09 15:57:09 +04:00
if ( virInitialize ( ) < 0 ) {
vshError ( ctl , " %s " , _ ( " Failed to initialize libvirt " ) ) ;
return EXIT_FAILURE ;
}
2014-04-24 18:57:36 +04:00
virFileActivateDirOverride ( argv [ 0 ] ) ;
2014-11-13 17:20:51 +03:00
if ( ( defaultConn = virGetEnvBlockSUID ( " VIRSH_DEFAULT_CONNECT_URI " ) ) )
admin: Rename virAdmConnect to virAdmDaemon
virAdmConnect was named after virConnect, but after some discussions,
most of the APIs called will be working with remote daemon and starting
them virAdmDaemon will make more sense. Only possibly controversal name
is CloseCallback (de)registration, and connecting to the daemon (which
will still be Open/Close), but even this makes sense if one thinks about
the daemon being opened and closed, e.g. as file, etc.
This way all the APIs working with the daemon will start with
virAdmDaemon prefix, they will accept virAdmDaemonPtr as first parameter
and that will better suit with other namings as well (virDomain*,
virAdmServer*, etc.).
Because in virt-admin, the connection name does not refer to a struct
that would have a connect in its name, also adjust 'connname' in
clients. And because it is not used anywhere in the vsh code, move it
from there into each client.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2015-11-25 18:59:30 +03:00
virshCtl . connname = vshStrdup ( ctl , defaultConn ) ;
2006-08-26 02:40:33 +04:00
2015-09-03 17:52:44 +03:00
if ( ! vshInit ( ctl , cmdGroups , NULL ) )
2015-06-15 19:53:58 +03:00
exit ( EXIT_FAILURE ) ;
2006-03-15 15:13:25 +03:00
2015-06-15 19:53:58 +03:00
if ( ! virshParseArgv ( ctl , argc , argv ) | |
! virshInit ( ctl ) ) {
virshDeinit ( ctl ) ;
2005-12-08 13:23:34 +03:00
exit ( EXIT_FAILURE ) ;
2007-12-01 18:45:25 +03:00
}
2006-03-15 15:13:25 +03:00
2005-12-08 13:23:34 +03:00
if ( ! ctl - > imode ) {
2006-03-15 15:13:25 +03:00
ret = vshCommandRun ( ctl , ctl - > cmd ) ;
2005-12-01 19:35:42 +03:00
} else {
2005-12-08 13:23:34 +03:00
/* interactive mode */
if ( ! ctl - > quiet ) {
2006-05-22 18:38:33 +04:00
vshPrint ( ctl ,
2006-09-21 19:24:37 +04:00
_ ( " Welcome to %s, the virtualization interactive terminal. \n \n " ) ,
2006-03-15 15:13:25 +03:00
progname ) ;
2008-01-16 20:13:23 +03:00
vshPrint ( ctl , " %s " ,
2006-09-21 19:24:37 +04:00
_ ( " Type: 'help' for help with commands \n "
2007-02-07 16:50:18 +03:00
" 'quit' to quit \n \n " ) ) ;
2005-12-08 13:23:34 +03:00
}
2010-01-03 17:45:10 +03:00
2005-12-08 13:23:34 +03:00
do {
2015-06-15 19:53:58 +03:00
const char * prompt = virshCtl . readonly ? VIRSH_PROMPT_RO
: VIRSH_PROMPT_RW ;
2006-03-15 15:13:25 +03:00
ctl - > cmdstr =
2007-12-06 19:36:21 +03:00
vshReadline ( ctl , prompt ) ;
2006-03-15 15:13:25 +03:00
if ( ctl - > cmdstr = = NULL )
break ; /* EOF */
2005-12-08 13:23:34 +03:00
if ( * ctl - > cmdstr ) {
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2005-12-08 13:23:34 +03:00
add_history ( ctl - > cmdstr ) ;
2007-12-04 21:27:52 +03:00
# endif
2010-10-12 11:13:50 +04:00
if ( vshCommandStringParse ( ctl , ctl - > cmdstr ) )
2005-12-08 13:23:34 +03:00
vshCommandRun ( ctl , ctl - > cmd ) ;
}
2010-01-03 19:13:27 +03:00
VIR_FREE ( ctl - > cmdstr ) ;
2006-03-15 15:13:25 +03:00
} while ( ctl - > imode ) ;
2005-12-08 13:23:34 +03:00
2006-03-15 15:13:25 +03:00
if ( ctl - > cmdstr = = NULL )
fputc ( ' \n ' , stdout ) ; /* line break after alone prompt */
2005-12-08 13:23:34 +03:00
}
2006-03-15 15:13:25 +03:00
2015-06-15 19:53:58 +03:00
virshDeinit ( ctl ) ;
2005-12-08 13:23:34 +03:00
exit ( ret ? EXIT_SUCCESS : EXIT_FAILURE ) ;
2005-11-10 19:12:31 +03:00
}