qemu_conf.c 13.1 KB
Newer Older
D
Daniel P. Berrange 已提交
1
/*
2
 * qemu_conf.c: QEMU configuration management
D
Daniel P. Berrange 已提交
3
 *
4
 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Red Hat, Inc.
D
Daniel P. Berrange 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

D
Daniel P. Berrange 已提交
26 27 28 29 30
#include <dirent.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
31
#include <stdlib.h>
D
Daniel P. Berrange 已提交
32 33 34
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
35
#include <sys/wait.h>
36
#include <arpa/inet.h>
37
#include <sys/utsname.h>
38
#include <mntent.h>
D
Daniel P. Berrange 已提交
39

40
#include "virterror_internal.h"
41
#include "qemu_conf.h"
42
#include "qemu_capabilities.h"
43
#include "qemu_bridge_filter.h"
44
#include "uuid.h"
45
#include "buf.h"
D
Daniel P. Berrange 已提交
46
#include "conf.h"
47
#include "util.h"
48
#include "memory.h"
J
Jim Meyering 已提交
49
#include "verify.h"
50 51
#include "datatypes.h"
#include "xml.h"
52
#include "nodeinfo.h"
53
#include "logging.h"
54
#include "network.h"
55
#include "macvtap.h"
56
#include "cpu/cpu.h"
57
#include "domain_nwfilter.h"
58
#include "files.h"
59
#include "configmake.h"
60

61 62
#define VIR_FROM_THIS VIR_FROM_QEMU

63 64 65 66 67 68 69 70 71 72
void qemuDriverLock(struct qemud_driver *driver)
{
    virMutexLock(&driver->lock);
}
void qemuDriverUnlock(struct qemud_driver *driver)
{
    virMutexUnlock(&driver->lock);
}


D
Daniel P. Berrange 已提交
73 74 75 76
int qemudLoadDriverConfig(struct qemud_driver *driver,
                          const char *filename) {
    virConfPtr conf;
    virConfValuePtr p;
77 78
    char *user;
    char *group;
79
    int i;
D
Daniel P. Berrange 已提交
80

81 82
    /* Setup critical defaults */
    driver->dynamicOwnership = 1;
83
    driver->clearEmulatorCapabilities = 1;
84

85
    if (!(driver->vncListen = strdup("127.0.0.1"))) {
86
        virReportOOMError();
87 88
        return -1;
    }
89
    if (!(driver->vncTLSx509certdir = strdup(SYSCONFDIR "/pki/libvirt-vnc"))) {
90
        virReportOOMError();
D
Daniel P. Berrange 已提交
91 92 93
        return -1;
    }

94 95 96 97
    if (!(driver->spiceListen = strdup("127.0.0.1"))) {
        virReportOOMError();
        return -1;
    }
98 99
    if (!(driver->spiceTLSx509certdir
          = strdup(SYSCONFDIR "/pki/libvirt-spice"))) {
100 101 102 103
        virReportOOMError();
        return -1;
    }

104
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
105 106 107 108 109 110
    /* For privileged driver, try and find hugepage mount automatically.
     * Non-privileged driver requires admin to create a dir for the
     * user, chown it, and then let user configure it manually */
    if (driver->privileged &&
        !(driver->hugetlbfs_mount = virFileFindMountPoint("hugetlbfs"))) {
        if (errno != ENOENT) {
111
            virReportSystemError(errno, "%s",
112 113 114 115 116 117 118
                                 _("unable to find hugetlbfs mountpoint"));
            return -1;
        }
    }
#endif


D
Daniel P. Berrange 已提交
119 120 121
    /* Just check the file is readable before opening it, otherwise
     * libvirt emits an error.
     */
122 123 124 125
    if (access (filename, R_OK) == -1) {
        VIR_INFO("Could not read qemu config file %s", filename);
        return 0;
    }
D
Daniel P. Berrange 已提交
126

127
    conf = virConfReadFile (filename, 0);
128 129 130
    if (!conf) {
        return -1;
    }
D
Daniel P. Berrange 已提交
131 132 133


#define CHECK_TYPE(name,typ) if (p && p->type != (typ)) {               \
134
        qemuReportError(VIR_ERR_INTERNAL_ERROR,                         \
135 136
                        "%s: %s: expected type " #typ,                  \
                        filename, (name));                              \
D
Daniel P. Berrange 已提交
137 138 139 140
        virConfFree(conf);                                              \
        return -1;                                                      \
    }

141 142 143 144
    p = virConfGetValue (conf, "vnc_auto_unix_socket");
    CHECK_TYPE ("vnc_auto_unix_socket", VIR_CONF_LONG);
    if (p) driver->vncAutoUnixSocket = p->l;

D
Daniel P. Berrange 已提交
145 146 147 148 149 150 151 152 153 154 155
    p = virConfGetValue (conf, "vnc_tls");
    CHECK_TYPE ("vnc_tls", VIR_CONF_LONG);
    if (p) driver->vncTLS = p->l;

    p = virConfGetValue (conf, "vnc_tls_x509_verify");
    CHECK_TYPE ("vnc_tls_x509_verify", VIR_CONF_LONG);
    if (p) driver->vncTLSx509verify = p->l;

    p = virConfGetValue (conf, "vnc_tls_x509_cert_dir");
    CHECK_TYPE ("vnc_tls_x509_cert_dir", VIR_CONF_STRING);
    if (p && p->str) {
156
        VIR_FREE(driver->vncTLSx509certdir);
D
Daniel P. Berrange 已提交
157
        if (!(driver->vncTLSx509certdir = strdup(p->str))) {
158
            virReportOOMError();
D
Daniel P. Berrange 已提交
159 160 161 162 163 164 165 166
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "vnc_listen");
    CHECK_TYPE ("vnc_listen", VIR_CONF_STRING);
    if (p && p->str) {
J
Jim Meyering 已提交
167
        VIR_FREE(driver->vncListen);
168
        if (!(driver->vncListen = strdup(p->str))) {
169
            virReportOOMError();
170 171 172
            virConfFree(conf);
            return -1;
        }
D
Daniel P. Berrange 已提交
173 174
    }

175 176 177 178 179
    p = virConfGetValue (conf, "vnc_password");
    CHECK_TYPE ("vnc_password", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->vncPassword);
        if (!(driver->vncPassword = strdup(p->str))) {
180
            virReportOOMError();
181 182 183 184 185
            virConfFree(conf);
            return -1;
        }
    }

186
    p = virConfGetValue (conf, "security_driver");
J
Jim Meyering 已提交
187
    CHECK_TYPE ("security_driver", VIR_CONF_STRING);
188 189
    if (p && p->str) {
        if (!(driver->securityDriverName = strdup(p->str))) {
190
            virReportOOMError();
191 192 193 194 195
            virConfFree(conf);
            return -1;
        }
    }

196 197 198 199 200 201 202 203 204
    p = virConfGetValue (conf, "vnc_sasl");
    CHECK_TYPE ("vnc_sasl", VIR_CONF_LONG);
    if (p) driver->vncSASL = p->l;

    p = virConfGetValue (conf, "vnc_sasl_dir");
    CHECK_TYPE ("vnc_sasl_dir", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->vncSASLdir);
        if (!(driver->vncSASLdir = strdup(p->str))) {
205
            virReportOOMError();
206 207 208 209 210
            virConfFree(conf);
            return -1;
        }
    }

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    p = virConfGetValue (conf, "spice_tls");
    CHECK_TYPE ("spice_tls", VIR_CONF_LONG);
    if (p) driver->spiceTLS = p->l;

    p = virConfGetValue (conf, "spice_tls_x509_cert_dir");
    CHECK_TYPE ("spice_tls_x509_cert_dir", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spiceTLSx509certdir);
        if (!(driver->spiceTLSx509certdir = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "spice_listen");
    CHECK_TYPE ("spice_listen", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spiceListen);
        if (!(driver->spiceListen = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "spice_password");
    CHECK_TYPE ("spice_password", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spicePassword);
        if (!(driver->spicePassword = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

248 249 250
    p = virConfGetValue (conf, "user");
    CHECK_TYPE ("user", VIR_CONF_STRING);
    if (!(user = strdup(p && p->str ? p->str : QEMU_USER))) {
251
        virReportOOMError();
252 253 254
        virConfFree(conf);
        return -1;
    }
255
    if (virGetUserID(user, &driver->user) < 0) {
256 257 258 259 260 261
        VIR_FREE(user);
        virConfFree(conf);
        return -1;
    }
    VIR_FREE(user);

262

263 264 265
    p = virConfGetValue (conf, "group");
    CHECK_TYPE ("group", VIR_CONF_STRING);
    if (!(group = strdup(p && p->str ? p->str : QEMU_GROUP))) {
266
        virReportOOMError();
267 268 269
        virConfFree(conf);
        return -1;
    }
270
    if (virGetGroupID(group, &driver->group) < 0) {
271 272 273 274 275 276
        VIR_FREE(group);
        virConfFree(conf);
        return -1;
    }
    VIR_FREE(group);

277 278 279 280 281 282

    p = virConfGetValue (conf, "dynamic_ownership");
    CHECK_TYPE ("dynamic_ownership", VIR_CONF_LONG);
    if (p) driver->dynamicOwnership = p->l;


283 284 285 286 287 288 289
    p = virConfGetValue (conf, "cgroup_controllers");
    CHECK_TYPE ("cgroup_controllers", VIR_CONF_LIST);
    if (p) {
        virConfValuePtr pp;
        for (i = 0, pp = p->list; pp; ++i, pp = pp->next) {
            int ctl;
            if (pp->type != VIR_CONF_STRING) {
290
                VIR_ERROR(_("cgroup_controllers must be a list of strings"));
291 292 293 294 295
                virConfFree(conf);
                return -1;
            }
            ctl = virCgroupControllerTypeFromString(pp->str);
            if (ctl < 0) {
296
                VIR_ERROR(_("Unknown cgroup controller '%s'"), pp->str);
297 298 299 300 301 302 303 304
                virConfFree(conf);
                return -1;
            }
            driver->cgroupControllers |= (1 << ctl);
        }
    } else {
        driver->cgroupControllers =
            (1 << VIR_CGROUP_CONTROLLER_CPU) |
305
            (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
306 307
            (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
            (1 << VIR_CGROUP_CONTROLLER_BLKIO);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    }
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
        if (driver->cgroupControllers & (1 << i)) {
            VIR_INFO("Configured cgroup controller '%s'",
                     virCgroupControllerTypeToString(i));
        }
    }

    p = virConfGetValue (conf, "cgroup_device_acl");
    CHECK_TYPE ("cgroup_device_acl", VIR_CONF_LIST);
    if (p) {
        int len = 0;
        virConfValuePtr pp;
        for (pp = p->list; pp; pp = pp->next)
            len++;
        if (VIR_ALLOC_N(driver->cgroupDeviceACL, 1+len) < 0) {
324
            virReportOOMError();
325 326 327 328 329
            virConfFree(conf);
            return -1;
        }
        for (i = 0, pp = p->list; pp; ++i, pp = pp->next) {
            if (pp->type != VIR_CONF_STRING) {
330
                VIR_ERROR(_("cgroup_device_acl must be a list of strings"));
331 332 333 334 335
                virConfFree(conf);
                return -1;
            }
            driver->cgroupDeviceACL[i] = strdup (pp->str);
            if (driver->cgroupDeviceACL[i] == NULL) {
336
                virReportOOMError();
337 338 339 340 341 342 343 344
                virConfFree(conf);
                return -1;
            }

        }
        driver->cgroupDeviceACL[i] = NULL;
    }

345 346 347 348 349
    p = virConfGetValue (conf, "save_image_format");
    CHECK_TYPE ("save_image_format", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->saveImageFormat);
        if (!(driver->saveImageFormat = strdup(p->str))) {
350
            virReportOOMError();
351 352 353 354 355
            virConfFree(conf);
            return -1;
        }
    }

356 357 358 359 360 361 362 363 364 365 366
    p = virConfGetValue (conf, "dump_image_format");
    CHECK_TYPE ("dump_image_format", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->dumpImageFormat);
        if (!(driver->dumpImageFormat = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

H
Hu Tao 已提交
367 368 369 370 371 372 373 374 375 376 377
    p = virConfGetValue (conf, "auto_dump_path");
    CHECK_TYPE ("auto_dump_path", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->autoDumpPath);
        if (!(driver->autoDumpPath = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

378 379 380 381 382
     p = virConfGetValue (conf, "hugetlbfs_mount");
     CHECK_TYPE ("hugetlbfs_mount", VIR_CONF_STRING);
     if (p && p->str) {
         VIR_FREE(driver->hugetlbfs_mount);
         if (!(driver->hugetlbfs_mount = strdup(p->str))) {
383
             virReportOOMError();
384 385 386 387 388
             virConfFree(conf);
             return -1;
         }
     }

389 390
    p = virConfGetValue (conf, "mac_filter");
    CHECK_TYPE ("mac_filter", VIR_CONF_LONG);
391
    if (p && p->l) {
392 393 394
        driver->macFilter = p->l;
        if (!(driver->ebtables = ebtablesContextNew("qemu"))) {
            driver->macFilter = 0;
395
            virReportSystemError(errno,
S
Stefan Berger 已提交
396
                                 _("failed to enable mac filter in '%s'"),
397 398 399 400
                                 __FILE__);
        }

        if ((errno = networkDisableAllFrames(driver))) {
401
            virReportSystemError(errno,
402 403 404 405 406
                         _("failed to add rule to drop all frames in '%s'"),
                                 __FILE__);
        }
    }

407 408 409 410
    p = virConfGetValue (conf, "relaxed_acs_check");
    CHECK_TYPE ("relaxed_acs_check", VIR_CONF_LONG);
    if (p) driver->relaxedACS = p->l;

411 412 413 414
    p = virConfGetValue (conf, "vnc_allow_host_audio");
    CHECK_TYPE ("vnc_allow_host_audio", VIR_CONF_LONG);
    if (p) driver->vncAllowHostAudio = p->l;

415 416 417 418
    p = virConfGetValue (conf, "clear_emulator_capabilities");
    CHECK_TYPE ("clear_emulator_capabilities", VIR_CONF_LONG);
    if (p) driver->clearEmulatorCapabilities = p->l;

419 420 421 422
    p = virConfGetValue (conf, "allow_disk_format_probing");
    CHECK_TYPE ("allow_disk_format_probing", VIR_CONF_LONG);
    if (p) driver->allowDiskFormatProbing = p->l;

423 424 425 426
    p = virConfGetValue (conf, "set_process_name");
    CHECK_TYPE ("set_process_name", VIR_CONF_LONG);
    if (p) driver->setProcessName = p->l;

427 428 429 430
    p = virConfGetValue(conf, "max_processes");
    CHECK_TYPE("max_processes", VIR_CONF_LONG);
    if (p) driver->maxProcesses = p->l;

D
Daniel P. Berrange 已提交
431 432 433
    virConfFree (conf);
    return 0;
}