2017-01-17 14:10:55 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "storage_backend_vstorage.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virstring.h"
|
2020-07-07 11:58:17 +00:00
|
|
|
#include "virutil.h"
|
2017-01-17 14:10:57 +00:00
|
|
|
#include <mntent.h>
|
2019-10-16 11:22:14 +00:00
|
|
|
#include <paths.h>
|
2017-01-17 14:10:57 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2020-07-07 11:58:17 +00:00
|
|
|
#include <unistd.h>
|
2017-01-26 16:44:26 +00:00
|
|
|
#include "storage_util.h"
|
2017-01-17 14:10:55 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("storage.storage_backend_vstorage");
|
|
|
|
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @pool storage pool to build
|
|
|
|
* @flags controls the pool formatting behaviour
|
|
|
|
*
|
|
|
|
* Does not support @flags, if provided an error will occur.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
static int
|
2018-01-26 13:31:58 +00:00
|
|
|
virStorageBackendVzPoolBuild(virStoragePoolObjPtr pool,
|
2017-01-17 14:10:57 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
|
|
|
return virStorageBackendBuildLocal(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-01-26 13:31:58 +00:00
|
|
|
virStorageBackendVzPoolStart(virStoragePoolObjPtr pool)
|
2017-01-17 14:10:57 +00:00
|
|
|
{
|
2017-07-26 18:36:46 +00:00
|
|
|
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *grp_name = NULL;
|
|
|
|
g_autofree char *usr_name = NULL;
|
|
|
|
g_autofree char *mode = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-05-24 14:35:47 +00:00
|
|
|
int ret;
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
/* Check the permissions */
|
2018-04-25 12:42:34 +00:00
|
|
|
if (def->target.perms.mode == (mode_t)-1)
|
2017-07-26 18:36:46 +00:00
|
|
|
def->target.perms.mode = VIR_STORAGE_DEFAULT_POOL_PERM_MODE;
|
2018-04-25 12:42:34 +00:00
|
|
|
if (def->target.perms.uid == (uid_t)-1)
|
2017-07-26 18:36:46 +00:00
|
|
|
def->target.perms.uid = geteuid();
|
2018-04-25 12:42:34 +00:00
|
|
|
if (def->target.perms.gid == (gid_t)-1)
|
2017-07-26 18:36:46 +00:00
|
|
|
def->target.perms.gid = getegid();
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
/* Convert ids to names because vstorage uses names */
|
|
|
|
|
2017-07-26 18:36:46 +00:00
|
|
|
if (!(grp_name = virGetGroupName(def->target.perms.gid)))
|
2019-01-31 17:09:38 +00:00
|
|
|
return -1;
|
2017-01-17 14:10:57 +00:00
|
|
|
|
2017-07-26 18:36:46 +00:00
|
|
|
if (!(usr_name = virGetUserName(def->target.perms.uid)))
|
2019-01-31 17:09:38 +00:00
|
|
|
return -1;
|
2017-01-17 14:10:57 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
mode = g_strdup_printf("%o", def->target.perms.mode);
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
cmd = virCommandNewArgList(VSTORAGE_MOUNT,
|
2017-07-26 18:36:46 +00:00
|
|
|
"-c", def->source.name,
|
|
|
|
def->target.path,
|
2017-01-17 14:10:57 +00:00
|
|
|
"-m", mode,
|
|
|
|
"-g", grp_name, "-u", usr_name,
|
|
|
|
NULL);
|
|
|
|
|
2019-05-24 14:35:47 +00:00
|
|
|
/* Mounting a shared FS might take a long time. Don't hold
|
|
|
|
* the pool locked meanwhile. */
|
|
|
|
virObjectUnlock(pool);
|
|
|
|
ret = virCommandRun(cmd, NULL);
|
|
|
|
virObjectLock(pool);
|
|
|
|
|
|
|
|
return ret;
|
2017-01-17 14:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageBackendVzIsMounted(virStoragePoolObjPtr pool)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2017-07-26 18:36:46 +00:00
|
|
|
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
|
2017-01-17 14:10:57 +00:00
|
|
|
FILE *mtab;
|
|
|
|
struct mntent ent;
|
|
|
|
char buf[1024];
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *cluster = NULL;
|
2017-01-17 14:10:57 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
cluster = g_strdup_printf("vstorage://%s", def->source.name);
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
if ((mtab = fopen(_PATH_MOUNTED, "r")) == NULL) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot read mount list '%s'"),
|
|
|
|
_PATH_MOUNTED);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((getmntent_r(mtab, &ent, buf, sizeof(buf))) != NULL) {
|
|
|
|
|
2017-07-26 18:36:46 +00:00
|
|
|
if (STREQ(ent.mnt_dir, def->target.path) &&
|
2017-01-17 14:10:57 +00:00
|
|
|
STREQ(ent.mnt_fsname, cluster)) {
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_FCLOSE(mtab);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-01-26 13:31:58 +00:00
|
|
|
virStorageBackendVzPoolStop(virStoragePoolObjPtr pool)
|
2017-01-17 14:10:57 +00:00
|
|
|
{
|
2017-07-26 18:36:46 +00:00
|
|
|
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
|
2017-01-17 14:10:57 +00:00
|
|
|
int rc;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
/* Short-circuit if already unmounted */
|
|
|
|
if ((rc = virStorageBackendVzIsMounted(pool)) != 1)
|
|
|
|
return rc;
|
|
|
|
|
2017-07-26 18:36:46 +00:00
|
|
|
cmd = virCommandNewArgList(UMOUNT, def->target.path, NULL);
|
2019-01-31 18:16:44 +00:00
|
|
|
return virCommandRun(cmd, NULL);
|
2017-01-17 14:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the cluster is mounted
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virStorageBackendVzCheck(virStoragePoolObjPtr pool,
|
|
|
|
bool *isActive)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
*isActive = false;
|
|
|
|
if ((ret = virStorageBackendVzIsMounted(pool)) != 0) {
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
*isActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-17 14:10:55 +00:00
|
|
|
virStorageBackend virStorageBackendVstorage = {
|
|
|
|
.type = VIR_STORAGE_POOL_VSTORAGE,
|
2017-01-17 14:10:57 +00:00
|
|
|
|
|
|
|
.buildPool = virStorageBackendVzPoolBuild,
|
|
|
|
.startPool = virStorageBackendVzPoolStart,
|
|
|
|
.stopPool = virStorageBackendVzPoolStop,
|
|
|
|
.deletePool = virStorageBackendDeleteLocal,
|
|
|
|
.refreshPool = virStorageBackendRefreshLocal,
|
|
|
|
.checkPool = virStorageBackendVzCheck,
|
|
|
|
.buildVol = virStorageBackendVolBuildLocal,
|
|
|
|
.buildVolFrom = virStorageBackendVolBuildFromLocal,
|
|
|
|
.createVol = virStorageBackendVolCreateLocal,
|
|
|
|
.refreshVol = virStorageBackendVolRefreshLocal,
|
|
|
|
.deleteVol = virStorageBackendVolDeleteLocal,
|
|
|
|
.resizeVol = virStorageBackendVolResizeLocal,
|
|
|
|
.uploadVol = virStorageBackendVolUploadLocal,
|
|
|
|
.downloadVol = virStorageBackendVolDownloadLocal,
|
|
|
|
.wipeVol = virStorageBackendVolWipeLocal,
|
2017-01-17 14:10:55 +00:00
|
|
|
};
|
2017-01-13 15:50:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virStorageBackendVstorageRegister(void)
|
|
|
|
{
|
|
|
|
return virStorageBackendRegister(&virStorageBackendVstorage);
|
|
|
|
}
|