1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#include <config.h>
#include "virerror.h"
#include "virfile.h"
#include "storage_backend_vstorage.h"
#include "virlog.h"
#include "virutil.h"
#include <mntent.h>
#include <paths.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include "storage_util.h"
#define VIR_FROM_THIS VIR_FROM_STORAGE
VIR_LOG_INIT("storage.storage_backend_vstorage");
/**
* @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
virStorageBackendVzPoolBuild(virStoragePoolObj *pool,
unsigned int flags)
{
virCheckFlags(0, -1);
return virStorageBackendBuildLocal(pool);
}
static int
virStorageBackendVzPoolStart(virStoragePoolObj *pool)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
g_autofree char *grp_name = NULL;
g_autofree char *usr_name = NULL;
g_autofree char *mode = NULL;
g_autoptr(virCommand) cmd = NULL;
int ret;
/* Check the permissions */
if (def->target.perms.mode == (mode_t)-1)
def->target.perms.mode = VIR_STORAGE_DEFAULT_POOL_PERM_MODE;
if (def->target.perms.uid == (uid_t)-1)
def->target.perms.uid = geteuid();
if (def->target.perms.gid == (gid_t)-1)
def->target.perms.gid = getegid();
/* Convert ids to names because vstorage uses names */
if (!(grp_name = virGetGroupName(def->target.perms.gid)))
return -1;
if (!(usr_name = virGetUserName(def->target.perms.uid)))
return -1;
mode = g_strdup_printf("%o", def->target.perms.mode);
cmd = virCommandNewArgList("vstorage-mount",
"-c", def->source.name,
def->target.path,
"-m", mode,
"-g", grp_name, "-u", usr_name,
NULL);
/* 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;
}
static int
virStorageBackendVzIsMounted(virStoragePoolObj *pool)
{
int ret = -1;
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
FILE *mtab;
struct mntent ent;
char buf[1024];
g_autofree char *cluster = NULL;
cluster = g_strdup_printf("vstorage://%s", def->source.name);
if ((mtab = fopen(_PATH_MOUNTED, "r")) == NULL) {
virReportSystemError(errno,
_("cannot read mount list '%1$s'"),
_PATH_MOUNTED);
goto cleanup;
}
while ((getmntent_r(mtab, &ent, buf, sizeof(buf))) != NULL) {
if (STREQ(ent.mnt_dir, def->target.path) &&
STREQ(ent.mnt_fsname, cluster)) {
ret = 1;
goto cleanup;
}
}
ret = 0;
cleanup:
VIR_FORCE_FCLOSE(mtab);
return ret;
}
static int
virStorageBackendVzPoolStop(virStoragePoolObj *pool)
{
virStoragePoolDef *def = virStoragePoolObjGetDef(pool);
int rc;
g_autoptr(virCommand) cmd = NULL;
/* Short-circuit if already unmounted */
if ((rc = virStorageBackendVzIsMounted(pool)) != 1)
return rc;
cmd = virCommandNewArgList("umount", def->target.path, NULL);
return virCommandRun(cmd, NULL);
}
/*
* Check whether the cluster is mounted
*/
static int
virStorageBackendVzCheck(virStoragePoolObj *pool,
bool *isActive)
{
int ret = -1;
*isActive = false;
if ((ret = virStorageBackendVzIsMounted(pool)) != 0) {
if (ret < 0)
return -1;
*isActive = true;
}
return 0;
}
virStorageBackend virStorageBackendVstorage = {
.type = VIR_STORAGE_POOL_VSTORAGE,
.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,
};
int
virStorageBackendVstorageRegister(void)
{
return virStorageBackendRegister(&virStorageBackendVstorage);
}
|