libvirt-sandbox-builder-machine.c 17.5 KB
Newer Older
1 2 3
/*
 * libvirt-sandbox-builder-machine.c: libvirt sandbox configuration
 *
4
 * Copyright (C) 2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 20 21 22 23 24
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>
#include <string.h>
25
#include <sys/utsname.h>
26 27 28

#include "libvirt-sandbox/libvirt-sandbox.h"

29 30 31 32 33 34 35 36 37 38 39 40
/**
 * SECTION: libvirt-sandbox-builder-machine
 * @short_description: Sandbox virtual machine construction
 * @include: libvirt-sandbox/libvirt-sandbox.h
 * @see_also: #GVirSandboxBuilder, #GVirSandboxBuilderMachine
 *
 * Provides an object for creating sandboxes using machine virtualization
 *
 * The GVirSandboxBuilderContainer object provides a way to builder sandboxes
 * using full machine virtualization technologies such as KVM.
 */

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
#define GVIR_SANDBOX_BUILDER_MACHINE_GET_PRIVATE(obj)                         \
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_SANDBOX_TYPE_BUILDER_MACHINE, GVirSandboxBuilderMachinePrivate))

struct _GVirSandboxBuilderMachinePrivate
{
    gboolean unused;
};

G_DEFINE_TYPE(GVirSandboxBuilderMachine, gvir_sandbox_builder_machine, GVIR_SANDBOX_TYPE_BUILDER);


enum {
    PROP_0,
};

enum {
    LAST_SIGNAL
};

//static gint signals[LAST_SIGNAL];

#define GVIR_SANDBOX_BUILDER_MACHINE_ERROR gvir_sandbox_builder_machine_error_quark()

static GQuark
gvir_sandbox_builder_machine_error_quark(void)
{
    return g_quark_from_static_string("gvir-sandbox-builder-machine");
}


static void gvir_sandbox_builder_machine_get_property(GObject *object,
                                                      guint prop_id,
                                                      GValue *value G_GNUC_UNUSED,
                                                      GParamSpec *pspec)
{
#if 0
    GVirSandboxBuilderMachine *builder = GVIR_SANDBOX_BUILDER_MACHINE(object);
    GVirSandboxBuilderMachinePrivate *priv = builder->priv;
#endif

    switch (prop_id) {

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}


static void gvir_sandbox_builder_machine_set_property(GObject *object,
                                                      guint prop_id,
                                                      const GValue *value G_GNUC_UNUSED,
                                                      GParamSpec *pspec)
{
#if 0
    GVirSandboxBuilderMachine *builder = GVIR_SANDBOX_BUILDER_MACHINE(object);
    GVirSandboxBuilderMachinePrivate *priv = builder->priv;
#endif

    switch (prop_id) {

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}


static void gvir_sandbox_builder_machine_finalize(GObject *object)
{
#if 0
    GVirSandboxBuilderMachine *builder = GVIR_SANDBOX_BUILDER_MACHINE(object);
    GVirSandboxBuilderMachinePrivate *priv = builder->priv;
#endif

    G_OBJECT_CLASS(gvir_sandbox_builder_machine_parent_class)->finalize(object);
}


118 119
static gchar *gvir_sandbox_builder_machine_mkinitrd(GVirSandboxConfig *config,
                                                    const gchar *kver,
120 121
                                                    GError **error)
{
122
    GVirSandboxConfigInitrd *initrd = gvir_sandbox_config_initrd_new();
123 124 125 126
    GVirSandboxBuilderInitrd *builder = gvir_sandbox_builder_initrd_new();
    gchar *targetfile = g_strdup_printf("/tmp/libvirt-sandbox-initrd-XXXXXX");
    int fd = -1;

127 128
    gvir_sandbox_config_initrd_set_kver(initrd, kver);
    gvir_sandbox_config_initrd_set_init(initrd, LIBEXECDIR "/libvirt-sandbox-init-qemu");
129

130 131 132 133 134 135
    gvir_sandbox_config_initrd_add_module(initrd, "fscache.ko");
    gvir_sandbox_config_initrd_add_module(initrd, "9pnet.ko");
    gvir_sandbox_config_initrd_add_module(initrd, "9p.ko");
    gvir_sandbox_config_initrd_add_module(initrd, "9pnet_virtio.ko");
    if (gvir_sandbox_config_has_networks(config))
        gvir_sandbox_config_initrd_add_module(initrd, "virtio_net.ko");
136
#if 0
137
    gvir_sandbox_config_initrd_add_module(initrd, "virtio_balloon.ko");
138 139 140 141 142 143 144 145
#endif

    if ((fd = mkstemp(targetfile)) < 0) {
        g_set_error(error, GVIR_SANDBOX_BUILDER_MACHINE_ERROR, 0,
                    "Cannot create initrd %s", targetfile);
        goto cleanup;
    }

146
    if (!gvir_sandbox_builder_initrd_construct(builder, initrd, targetfile, error))
147 148 149 150 151 152 153 154 155
        goto cleanup;

cleanup:
    if (fd != -1)
        close(fd);
    if (*error) {
        g_free(targetfile);
        targetfile = NULL;
    }
156
    g_object_unref(initrd);
157 158 159 160 161
    g_object_unref(builder);
    return targetfile;
}


162
static gchar *gvir_sandbox_builder_machine_cmdline(GVirSandboxConfig *config G_GNUC_UNUSED)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
    GString *str = g_string_new("");
    gchar *ret;

    /* Now kernel args */
    g_string_append(str, " console=ttyS0");
    if (getenv("LIBVIRT_SANDBOX_DEBUG"))
        g_string_append(str, " debug loglevel=10 earlyprintk=ttyS0");
    else
        g_string_append(str, " quiet loglevel=0");

    /* These make boot a little bit faster */
    g_string_append(str, " edd=off");
    g_string_append(str, " printk.time=1");
    g_string_append(str, " noreplace-smp");
    g_string_append(str, " cgroup_disable=memory");
    g_string_append(str, " pci=noearly");

    ret = str->str;
    g_string_free(str, FALSE);
    return ret;
}


187 188 189 190 191 192 193 194 195
static gboolean gvir_sandbox_builder_machine_delete(GVirSandboxCleaner *cleaner G_GNUC_UNUSED,
                                                    GError **error G_GNUC_UNUSED,
                                                    gpointer opaque)
{
    gchar *path = opaque;
    unlink(path);
    return TRUE;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static gboolean gvir_sandbox_builder_machine_construct_domain(GVirSandboxBuilder *builder,
                                                              GVirSandboxConfig *config,
                                                              const gchar *configdir,
                                                              GVirSandboxCleaner *cleaner,
                                                              GVirConfigDomain *domain,
                                                              GError **error)
{
    gchar *mntfile = g_strdup_printf("%s/mounts.cfg", configdir);
    GFile *file = g_file_new_for_path(mntfile);
    GFileOutputStream *fos = g_file_replace(file,
                                            NULL,
                                            FALSE,
                                            G_FILE_CREATE_REPLACE_DESTINATION,
                                            NULL,
                                            error);
    gboolean ret = FALSE;
    GList *tmp, *mounts = NULL;
    int i;

    if (!fos)
        goto cleanup;

    i = 0;
    tmp = mounts = gvir_sandbox_config_get_host_mounts(config);
    while (tmp) {
        GVirSandboxConfigMount *mount = tmp->data;
222
        gchar *key = g_strdup_printf("sandbox:mount%u=%s\n", i,
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 248 249 250 251 252 253 254 255 256 257 258 259
                                     gvir_sandbox_config_mount_get_target(mount));

        if (!g_output_stream_write_all(G_OUTPUT_STREAM(fos), key, strlen(key),
                                       NULL, NULL, error)) {
            g_free(key);
            goto cleanup;
        }

        i++;
        tmp = tmp->next;
    }

    if (!g_output_stream_close(G_OUTPUT_STREAM(fos), NULL, error))
        goto cleanup;

    gvir_sandbox_cleaner_add_action_post_stop(cleaner,
                                              gvir_sandbox_builder_machine_delete,
                                              mntfile,
                                              g_free);
    mntfile = NULL;

    if (!GVIR_SANDBOX_BUILDER_CLASS(gvir_sandbox_builder_machine_parent_class)->
        construct_domain(builder, config, configdir, cleaner, domain, error))
        goto cleanup;

    ret = TRUE;
cleanup:
    g_list_free(mounts);
    if (fos)
        g_object_unref(fos);
    if (!ret)
        g_file_delete(file, NULL, NULL);
    g_object_unref(file);
    g_free(mntfile);
    return ret;
}

260 261
static gboolean gvir_sandbox_builder_machine_construct_basic(GVirSandboxBuilder *builder,
                                                             GVirSandboxConfig *config,
262
                                                             const gchar *configdir,
263 264 265 266 267
                                                             GVirSandboxCleaner *cleaner,
                                                             GVirConfigDomain *domain,
                                                             GError **error)
{
    if (!GVIR_SANDBOX_BUILDER_CLASS(gvir_sandbox_builder_machine_parent_class)->
268
        construct_basic(builder, config, configdir, cleaner, domain, error))
269 270 271 272 273 274 275 276 277 278 279
        return FALSE;

    gvir_config_domain_set_virt_type(domain,
                                     GVIR_CONFIG_DOMAIN_VIRT_KVM);

    return TRUE;
}


static gboolean gvir_sandbox_builder_machine_construct_os(GVirSandboxBuilder *builder,
                                                          GVirSandboxConfig *config,
280
                                                          const gchar *configdir,
281 282 283
                                                          GVirSandboxCleaner *cleaner,
                                                          GVirConfigDomain *domain,
                                                          GError **error)
284
{
285 286
    gchar *kernel = NULL;
    gchar *initrd = NULL;
287
    gchar *cmdline = NULL;
288
    struct utsname uts;
289 290 291
    GVirConfigDomainOs *os;

    if (!GVIR_SANDBOX_BUILDER_CLASS(gvir_sandbox_builder_machine_parent_class)->
292
        construct_os(builder, config, configdir, cleaner, domain, error))
293
        return FALSE;
294 295 296

    uname(&uts);

297
    if (!(initrd = gvir_sandbox_builder_machine_mkinitrd(config, uts.release, error)))
298
        return FALSE;
299 300

    kernel = g_strdup_printf("/boot/vmlinuz-%s", uts.release);
301
    cmdline = gvir_sandbox_builder_machine_cmdline(config);
302 303 304 305 306

    gvir_sandbox_cleaner_add_action_post_start(cleaner,
                                               gvir_sandbox_builder_machine_delete,
                                               initrd,
                                               g_free);
307

308 309 310 311 312 313 314 315
    os = gvir_config_domain_os_new();
    gvir_config_domain_os_set_os_type(os,
                                      GVIR_CONFIG_DOMAIN_OS_TYPE_HVM);
    gvir_config_domain_os_set_arch(os,
                                   gvir_sandbox_config_get_arch(config));
    gvir_config_domain_os_set_kernel(os, kernel);
    gvir_config_domain_os_set_ramdisk(os, initrd);
    gvir_config_domain_os_set_cmdline(os, cmdline);
316

317
    gvir_config_domain_set_os(domain, os);
318

319 320 321
    g_free(kernel);
    g_free(cmdline);
    /* initrd is free'd by the cleaner */
322

323 324
    return TRUE;
}
325

326 327 328

static gboolean gvir_sandbox_builder_machine_construct_features(GVirSandboxBuilder *builder,
                                                                GVirSandboxConfig *config,
329
                                                                const gchar *configdir,
330 331 332 333 334 335 336
                                                                GVirSandboxCleaner *cleaner,
                                                                GVirConfigDomain *domain,
                                                                GError **error)
{
    gchar **features;

    if (!GVIR_SANDBOX_BUILDER_CLASS(gvir_sandbox_builder_machine_parent_class)->
337
        construct_features(builder, config, configdir, cleaner, domain, error))
338 339 340 341 342 343 344 345 346 347 348
        return FALSE;

    features = g_new0(gchar *, 1);
    features[0] = g_strdup("acpi");
    gvir_config_domain_set_features(domain, features);
    g_strfreev(features);
    return TRUE;
}

static gboolean gvir_sandbox_builder_machine_construct_devices(GVirSandboxBuilder *builder,
                                                               GVirSandboxConfig *config,
349
                                                               const gchar *configdir,
350 351 352 353 354
                                                               GVirSandboxCleaner *cleaner,
                                                               GVirConfigDomain *domain,
                                                               GError **error)
{
    GVirConfigDomainFilesys *fs;
355
    GVirConfigDomainInterfaceUser *iface;
356 357
    GVirConfigDomainMemballoon *ball;
    GVirConfigDomainGraphicsSpice *graph;
358 359
    GList *tmp = NULL, *mounts = NULL, *networks = NULL;
    int i;
360 361

    if (!GVIR_SANDBOX_BUILDER_CLASS(gvir_sandbox_builder_machine_parent_class)->
362
        construct_devices(builder, config, configdir, cleaner, domain, error))
363 364 365 366 367 368 369 370 371 372 373 374 375
        return FALSE;

    fs = gvir_config_domain_filesys_new();
    gvir_config_domain_filesys_set_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_MOUNT);
    gvir_config_domain_filesys_set_access_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_ACCESS_PASSTHROUGH);
    gvir_config_domain_filesys_set_source(fs,
                                          gvir_sandbox_config_get_root(config));
    gvir_config_domain_filesys_set_target(fs, "sandbox:root");
    gvir_config_domain_filesys_set_readonly(fs, TRUE);

    gvir_config_domain_add_device(domain,
                                  GVIR_CONFIG_DOMAIN_DEVICE(fs));
    g_object_unref(fs);
376

377 378 379 380 381 382 383 384 385 386 387 388 389 390

    fs = gvir_config_domain_filesys_new();
    gvir_config_domain_filesys_set_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_MOUNT);
    gvir_config_domain_filesys_set_access_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_ACCESS_PASSTHROUGH);
    gvir_config_domain_filesys_set_source(fs, configdir);
    gvir_config_domain_filesys_set_target(fs, "sandbox:config");
    gvir_config_domain_filesys_set_readonly(fs, TRUE);

    gvir_config_domain_add_device(domain,
                                  GVIR_CONFIG_DOMAIN_DEVICE(fs));
    g_object_unref(fs);



391
    tmp = mounts = gvir_sandbox_config_get_host_mounts(config);
392
    i = 0;
393 394
    while (tmp) {
        GVirSandboxConfigMount *mconfig = tmp->data;
395
        gchar *target = g_strdup_printf("sandbox:mount%d", i);
396

397 398 399 400 401
        fs = gvir_config_domain_filesys_new();
        gvir_config_domain_filesys_set_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_MOUNT);
        gvir_config_domain_filesys_set_access_type(fs, GVIR_CONFIG_DOMAIN_FILESYS_ACCESS_PASSTHROUGH);
        gvir_config_domain_filesys_set_source(fs,
                                              gvir_sandbox_config_mount_get_root(mconfig));
402
        gvir_config_domain_filesys_set_target(fs, target);
403

404 405 406
        gvir_config_domain_add_device(domain,
                                      GVIR_CONFIG_DOMAIN_DEVICE(fs));
        g_object_unref(fs);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        g_free(target);

        tmp = tmp->next;
        i++;
    }


    tmp = networks = gvir_sandbox_config_get_networks(config);
    while (tmp) {
        iface = gvir_config_domain_interface_user_new();

        gvir_config_domain_interface_set_model(GVIR_CONFIG_DOMAIN_INTERFACE(iface),
                                               "virtio");

        gvir_config_domain_add_device(domain,
                                      GVIR_CONFIG_DOMAIN_DEVICE(iface));
        g_object_unref(iface);
424 425 426 427 428

        tmp = tmp->next;
    }


429 430 431 432 433 434
    ball = gvir_config_domain_memballoon_new();
    gvir_config_domain_memballoon_set_model(ball,
                                            GVIR_CONFIG_DOMAIN_MEMBALLOON_MODEL_NONE);
    gvir_config_domain_add_device(domain,
                                  GVIR_CONFIG_DOMAIN_DEVICE(ball));
    g_object_unref(ball);
435 436


437 438 439 440 441 442
    if (GVIR_SANDBOX_IS_CONFIG_GRAPHICAL(config)) {
        graph = gvir_config_domain_graphics_spice_new();
        gvir_config_domain_graphics_spice_set_port(graph, -1);
        gvir_config_domain_add_device(domain,
                                      GVIR_CONFIG_DOMAIN_DEVICE(graph));
        g_object_unref(graph);
443 444
    }

445
    return TRUE;
446 447 448 449 450 451 452 453 454 455 456 457
}


static void gvir_sandbox_builder_machine_class_init(GVirSandboxBuilderMachineClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    GVirSandboxBuilderClass *builder_class = GVIR_SANDBOX_BUILDER_CLASS(klass);

    object_class->finalize = gvir_sandbox_builder_machine_finalize;
    object_class->get_property = gvir_sandbox_builder_machine_get_property;
    object_class->set_property = gvir_sandbox_builder_machine_set_property;

458
    builder_class->construct_domain = gvir_sandbox_builder_machine_construct_domain;
459 460 461 462
    builder_class->construct_basic = gvir_sandbox_builder_machine_construct_basic;
    builder_class->construct_os = gvir_sandbox_builder_machine_construct_os;
    builder_class->construct_features = gvir_sandbox_builder_machine_construct_features;
    builder_class->construct_devices = gvir_sandbox_builder_machine_construct_devices;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

    g_type_class_add_private(klass, sizeof(GVirSandboxBuilderMachinePrivate));
}


static void gvir_sandbox_builder_machine_init(GVirSandboxBuilderMachine *builder)
{
    builder->priv = GVIR_SANDBOX_BUILDER_MACHINE_GET_PRIVATE(builder);
}


/**
 * gvir_sandbox_builder_machine_new:
 * @connection: (transfer none): the connection
 *
 * Create a new graphical application sandbox builderuration
 *
 * Returns: (transfer full): a new graphical sandbox builder object
 */
482
GVirSandboxBuilderMachine *gvir_sandbox_builder_machine_new(GVirConnection *connection)
483 484 485 486 487
{
    return GVIR_SANDBOX_BUILDER_MACHINE(g_object_new(GVIR_SANDBOX_TYPE_BUILDER_MACHINE,
                                                     "connection", connection,
                                                     NULL));
}