lxc_fuse.c 11.5 KB
Newer Older
G
Gao feng 已提交
1
/*
2
 * Copyright (C) 2014 Red Hat, Inc.
G
Gao feng 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Copyright (C) 2012 Fujitsu Limited.
 *
 * lxc_fuse.c: fuse filesystem support for libvirt lxc
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <mntent.h>
J
Ján Tomko 已提交
26
#include <unistd.h>
G
Gao feng 已提交
27 28

#include "lxc_fuse.h"
29
#include "lxc_cgroup.h"
30
#include "virerror.h"
31
#include "virfile.h"
32
#include "virbuffer.h"
33
#include "virstring.h"
34
#include "viralloc.h"
J
Ján Tomko 已提交
35
#include "virutil.h"
G
Gao feng 已提交
36 37 38

#define VIR_FROM_THIS VIR_FROM_LXC

39
#if WITH_FUSE
G
Gao feng 已提交
40

41 42
static const char *fuse_meminfo_path = "/meminfo";

G
Gao feng 已提交
43 44
static int lxcProcGetattr(const char *path, struct stat *stbuf)
{
45
    g_autofree char *mempath = NULL;
46
    struct stat sb;
47 48
    struct fuse_context *context = fuse_get_context();
    virDomainDefPtr def = (virDomainDefPtr)context->private_data;
G
Gao feng 已提交
49 50

    memset(stbuf, 0, sizeof(struct stat));
51
    mempath = g_strdup_printf("/proc/%s", path);
52

G
Gao feng 已提交
53 54 55
    if (STREQ(path, "/")) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
56
    } else if (STREQ(path, fuse_meminfo_path)) {
57 58
        if (stat(mempath, &sb) < 0)
            return -errno;
59

60 61
        stbuf->st_uid = def->idmap.uidmap ? def->idmap.uidmap[0].target : 0;
        stbuf->st_gid = def->idmap.gidmap ? def->idmap.gidmap[0].target : 0;
62 63 64 65 66 67 68 69
        stbuf->st_mode = sb.st_mode;
        stbuf->st_nlink = 1;
        stbuf->st_blksize = sb.st_blksize;
        stbuf->st_blocks = sb.st_blocks;
        stbuf->st_size = sb.st_size;
        stbuf->st_atime = sb.st_atime;
        stbuf->st_ctime = sb.st_ctime;
        stbuf->st_mtime = sb.st_mtime;
70
    } else {
71
        return -ENOENT;
72
    }
G
Gao feng 已提交
73

74
    return 0;
G
Gao feng 已提交
75 76 77 78
}

static int lxcProcReaddir(const char *path, void *buf,
                          fuse_fill_dir_t filler,
J
Ján Tomko 已提交
79 80
                          off_t offset G_GNUC_UNUSED,
                          struct fuse_file_info *fi G_GNUC_UNUSED)
G
Gao feng 已提交
81
{
82
    if (STRNEQ(path, "/"))
G
Gao feng 已提交
83 84 85 86
        return -ENOENT;

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
87
    filler(buf, fuse_meminfo_path + 1, NULL, 0);
G
Gao feng 已提交
88 89 90 91

    return 0;
}

J
Ján Tomko 已提交
92 93
static int lxcProcOpen(const char *path G_GNUC_UNUSED,
                       struct fuse_file_info *fi G_GNUC_UNUSED)
G
Gao feng 已提交
94
{
95
    if (STRNEQ(path, fuse_meminfo_path))
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
        return -ENOENT;

    if ((fi->flags & 3) != O_RDONLY)
        return -EACCES;

    return 0;
}

static int lxcProcHostRead(char *path, char *buf, size_t size, off_t offset)
{
    int fd;
    int res;

    fd = open(path, O_RDONLY);
    if (fd == -1)
        return -errno;

    if ((res = pread(fd, buf, size, offset)) < 0)
        res = -errno;

    VIR_FORCE_CLOSE(fd);
    return res;
}

static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
                              char *buf, size_t size, off_t offset)
{
    int res;
    FILE *fd = NULL;
125
    g_autofree char *line = NULL;
126 127 128 129 130
    size_t n;
    struct virLXCMeminfo meminfo;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    virBufferPtr new_meminfo = &buffer;

131 132 133 134
    if (virLXCCgroupGetMeminfo(&meminfo) < 0) {
        virErrorSetErrnoFromLastError();
        return -errno;
    }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    fd = fopen(hostpath, "r");
    if (fd == NULL) {
        virReportSystemError(errno, _("Cannot open %s"), hostpath);
        res = -errno;
        goto cleanup;
    }

    if (fseek(fd, offset, SEEK_SET) < 0) {
        virReportSystemError(errno, "%s", _("fseek failed"));
        res = -errno;
        goto cleanup;
    }

    res = -1;
150
    while (getline(&line, &n, fd) > 0) {
151
        char *ptr = strchr(line, ':');
152 153 154 155 156 157
        if (!ptr)
            continue;
        *ptr = '\0';

        if (STREQ(line, "MemTotal") &&
            (virMemoryLimitIsSet(def->mem.hard_limit) ||
158
             virDomainDefGetMemoryTotal(def))) {
159 160 161 162
            virBufferAsprintf(new_meminfo, "MemTotal:       %8llu kB\n",
                              meminfo.memtotal);
        } else if (STREQ(line, "MemFree") &&
                   (virMemoryLimitIsSet(def->mem.hard_limit) ||
163
                    virDomainDefGetMemoryTotal(def))) {
164 165
            virBufferAsprintf(new_meminfo, "MemFree:        %8llu kB\n",
                              (meminfo.memtotal - meminfo.memusage));
166 167
        } else if (STREQ(line, "MemAvailable") &&
                   (virMemoryLimitIsSet(def->mem.hard_limit) ||
168
                    virDomainDefGetMemoryTotal(def))) {
169 170 171 172 173
            /* MemAvailable is actually MemFree + SRReclaimable +
               some other bits, but MemFree is the closest approximation
               we have */
            virBufferAsprintf(new_meminfo, "MemAvailable:   %8llu kB\n",
                              (meminfo.memtotal - meminfo.memusage));
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        } else if (STREQ(line, "Buffers")) {
            virBufferAsprintf(new_meminfo, "Buffers:        %8d kB\n", 0);
        } else if (STREQ(line, "Cached")) {
            virBufferAsprintf(new_meminfo, "Cached:         %8llu kB\n",
                              meminfo.cached);
        } else if (STREQ(line, "Active")) {
            virBufferAsprintf(new_meminfo, "Active:         %8llu kB\n",
                              (meminfo.active_anon + meminfo.active_file));
        } else if (STREQ(line, "Inactive")) {
            virBufferAsprintf(new_meminfo, "Inactive:       %8llu kB\n",
                              (meminfo.inactive_anon + meminfo.inactive_file));
        } else if (STREQ(line, "Active(anon)")) {
            virBufferAsprintf(new_meminfo, "Active(anon):   %8llu kB\n",
                              meminfo.active_anon);
        } else if (STREQ(line, "Inactive(anon)")) {
            virBufferAsprintf(new_meminfo, "Inactive(anon): %8llu kB\n",
                              meminfo.inactive_anon);
        } else if (STREQ(line, "Active(file)")) {
            virBufferAsprintf(new_meminfo, "Active(file):   %8llu kB\n",
                              meminfo.active_file);
        } else if (STREQ(line, "Inactive(file)")) {
            virBufferAsprintf(new_meminfo, "Inactive(file): %8llu kB\n",
                              meminfo.inactive_file);
        } else if (STREQ(line, "Unevictable")) {
            virBufferAsprintf(new_meminfo, "Unevictable:    %8llu kB\n",
                              meminfo.unevictable);
        } else if (STREQ(line, "SwapTotal") &&
                   virMemoryLimitIsSet(def->mem.swap_hard_limit)) {
            virBufferAsprintf(new_meminfo, "SwapTotal:      %8llu kB\n",
                              (meminfo.swaptotal - meminfo.memtotal));
        } else if (STREQ(line, "SwapFree") &&
                   virMemoryLimitIsSet(def->mem.swap_hard_limit)) {
            virBufferAsprintf(new_meminfo, "SwapFree:       %8llu kB\n",
                              (meminfo.swaptotal - meminfo.memtotal -
                               meminfo.swapusage + meminfo.memusage));
209 210 211 212 213 214
        } else if (STREQ(line, "Slab")) {
            virBufferAsprintf(new_meminfo, "Slab:           %8d kB\n", 0);
        } else if (STREQ(line, "SReclaimable")) {
            virBufferAsprintf(new_meminfo, "SReclaimable:   %8d kB\n", 0);
        } else if (STREQ(line, "SUnreclaim")) {
            virBufferAsprintf(new_meminfo, "SUnreclaim:     %8d kB\n", 0);
215 216 217
        } else {
            *ptr = ':';
            virBufferAdd(new_meminfo, line, -1);
218
        }
219

220
    }
221 222 223 224
    res = strlen(virBufferCurrentContent(new_meminfo));
    if (res > size)
        res = size;
    memcpy(buf, virBufferCurrentContent(new_meminfo), res);
225

226
 cleanup:
227 228 229
    virBufferFreeAndReset(new_meminfo);
    VIR_FORCE_FCLOSE(fd);
    return res;
G
Gao feng 已提交
230 231
}

J
Ján Tomko 已提交
232 233 234 235 236
static int lxcProcRead(const char *path G_GNUC_UNUSED,
                       char *buf G_GNUC_UNUSED,
                       size_t size G_GNUC_UNUSED,
                       off_t offset G_GNUC_UNUSED,
                       struct fuse_file_info *fi G_GNUC_UNUSED)
G
Gao feng 已提交
237
{
238
    int res = -ENOENT;
239
    g_autofree char *hostpath = NULL;
240 241 242
    struct fuse_context *context = NULL;
    virDomainDefPtr def = NULL;

243
    hostpath = g_strdup_printf("/proc/%s", path);
244 245 246 247 248 249 250 251 252 253

    context = fuse_get_context();
    def = (virDomainDefPtr)context->private_data;

    if (STREQ(path, fuse_meminfo_path)) {
        if ((res = lxcProcReadMeminfo(hostpath, def, buf, size, offset)) < 0)
            res = lxcProcHostRead(hostpath, buf, size, offset);
    }

    return res;
G
Gao feng 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

static struct fuse_operations lxcProcOper = {
    .getattr = lxcProcGetattr,
    .readdir = lxcProcReaddir,
    .open    = lxcProcOpen,
    .read    = lxcProcRead,
};

static void lxcFuseDestroy(virLXCFusePtr fuse)
{
    virMutexLock(&fuse->lock);
    fuse_unmount(fuse->mountpoint, fuse->ch);
    fuse_destroy(fuse->fuse);
    fuse->fuse = NULL;
    virMutexUnlock(&fuse->lock);
}

static void lxcFuseRun(void *opaque)
{
    virLXCFusePtr fuse = opaque;

    if (fuse_loop(fuse->fuse) < 0)
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("fuse_loop failed"));

    lxcFuseDestroy(fuse);
}

int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def)
{
    int ret = -1;
    struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
    virLXCFusePtr fuse = NULL;

    if (VIR_ALLOC(fuse) < 0)
        goto cleanup;

    fuse->def = def;

    if (virMutexInit(&fuse->lock) < 0)
        goto cleanup2;

297
    fuse->mountpoint = g_strdup_printf("%s/%s.fuse/", LXC_STATE_DIR, def->name);
G
Gao feng 已提交
298 299 300 301 302 303 304 305 306 307

    if (virFileMakePath(fuse->mountpoint) < 0) {
        virReportSystemError(errno, _("Cannot create %s"),
                             fuse->mountpoint);
        goto cleanup1;
    }

    /* process name is libvirt_lxc */
    if (fuse_opt_add_arg(&args, "libvirt_lxc") == -1 ||
        fuse_opt_add_arg(&args, "-odirect_io") == -1 ||
308
        fuse_opt_add_arg(&args, "-oallow_other") == -1 ||
G
Gao feng 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        fuse_opt_add_arg(&args, "-ofsname=libvirt") == -1)
        goto cleanup1;

    fuse->ch = fuse_mount(fuse->mountpoint, &args);
    if (fuse->ch == NULL)
        goto cleanup1;

    fuse->fuse = fuse_new(fuse->ch, &args, &lxcProcOper,
                          sizeof(lxcProcOper), fuse->def);
    if (fuse->fuse == NULL) {
        fuse_unmount(fuse->mountpoint, fuse->ch);
        goto cleanup1;
    }

    ret = 0;
324
 cleanup:
G
Gao feng 已提交
325 326 327
    fuse_opt_free_args(&args);
    *f = fuse;
    return ret;
328
 cleanup1:
G
Gao feng 已提交
329 330
    VIR_FREE(fuse->mountpoint);
    virMutexDestroy(&fuse->lock);
331
 cleanup2:
G
Gao feng 已提交
332 333 334 335
    VIR_FREE(fuse);
    goto cleanup;
}

336 337 338 339 340 341 342 343 344 345 346
int lxcStartFuse(virLXCFusePtr fuse)
{
    if (virThreadCreate(&fuse->thread, false, lxcFuseRun,
                        (void *)fuse) < 0) {
        lxcFuseDestroy(fuse);
        return -1;
    }

    return 0;
}

G
Gao feng 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
void lxcFreeFuse(virLXCFusePtr *f)
{
    virLXCFusePtr fuse = *f;
    /* lxcFuseRun thread create success */
    if (fuse) {
        /* exit fuse_loop, lxcFuseRun thread may try to destroy
         * fuse->fuse at the same time,so add a lock here. */
        virMutexLock(&fuse->lock);
        if (fuse->fuse)
            fuse_exit(fuse->fuse);
        virMutexUnlock(&fuse->lock);

        VIR_FREE(fuse->mountpoint);
        VIR_FREE(*f);
    }
}
#else
J
Ján Tomko 已提交
364 365
int lxcSetupFuse(virLXCFusePtr *f G_GNUC_UNUSED,
                  virDomainDefPtr def G_GNUC_UNUSED)
G
Gao feng 已提交
366 367 368 369
{
    return 0;
}

J
Ján Tomko 已提交
370
int lxcStartFuse(virLXCFusePtr f G_GNUC_UNUSED)
371
{
372
    return 0;
373 374
}

J
Ján Tomko 已提交
375
void lxcFreeFuse(virLXCFusePtr *f G_GNUC_UNUSED)
G
Gao feng 已提交
376 377 378
{
}
#endif