debuggers.hg

view tools/libxl/libxl_utils.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents abc717dc6c2a
children 37e4d8e11554
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; version 2.1 only. with the special
8 * exception on linking described in file LICENSE.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 */
16 #include "libxl_osdeps.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <xs.h>
23 #include <xenctrl.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
30 #include "libxl_utils.h"
31 #include "libxl_internal.h"
34 unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned int smp_cpus)
35 {
36 /* 256 pages (1MB) per vcpu,
37 plus 1 page per MiB of RAM for the P2M map,
38 plus 1 page per MiB of RAM to shadow the resident processes.
39 This is higher than the minimum that Xen would allocate if no value
40 were given (but the Xen minimum is for safety, not performance).
41 */
42 return 4 * (256 * smp_cpus + 2 * (maxmem_kb / 1024));
43 }
45 char *libxl_domid_to_name(struct libxl_ctx *ctx, uint32_t domid)
46 {
47 unsigned int len;
48 char path[strlen("/local/domain") + 12];
49 char *s;
51 snprintf(path, sizeof(path), "/local/domain/%d/name", domid);
52 s = xs_read(ctx->xsh, XBT_NULL, path, &len);
53 libxl_ptr_add(ctx, s);
54 return s;
55 }
57 int libxl_name_to_domid(struct libxl_ctx *ctx, char *name, uint32_t *domid)
58 {
59 int i, nb_domains;
60 char *domname;
61 struct libxl_dominfo *dominfo;
63 dominfo = libxl_list_domain(ctx, &nb_domains);
64 if (!dominfo)
65 return ERROR_NOMEM;
67 for (i = 0; i < nb_domains; i++) {
68 domname = libxl_domid_to_name(ctx, dominfo[i].domid);
69 if (!domname)
70 continue;
71 if (strcmp(domname, name) == 0) {
72 *domid = dominfo[i].domid;
73 return 0;
74 }
75 }
76 return -1;
77 }
79 int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid)
80 {
81 char * stubdom_id_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, guest_domid)));
82 if (stubdom_id_s)
83 return atoi(stubdom_id_s);
84 else
85 return 0;
86 }
88 int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid)
89 {
90 char *target, *endptr;
91 uint32_t value;
93 target = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)));
94 if (!target)
95 return 0;
96 value = strtol(target, &endptr, 10);
97 if (*endptr != '\0')
98 return 0;
99 if (target_domid)
100 *target_domid = value;
101 return 1;
102 }
104 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
105 {
106 struct stat stat_buf;
107 char *logfile, *logfile_new;
108 int i;
110 logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
111 if (stat(logfile, &stat_buf) == 0) {
112 /* file exists, rotate */
113 logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.10", name);
114 unlink(logfile);
115 for (i = 9; i > 0; i--) {
116 logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i);
117 logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i + 1);
118 rename(logfile, logfile_new);
119 }
120 logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
121 logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name);
122 rename(logfile, logfile_new);
123 }
124 *full_name = strdup(logfile);
125 return 0;
126 }
128 int libxl_string_to_phystype(struct libxl_ctx *ctx, char *s, libxl_disk_phystype *phystype)
129 {
130 char *p;
131 int rc = 0;
133 if (!strcmp(s, "phy")) {
134 *phystype = PHYSTYPE_PHY;
135 } else if (!strcmp(s, "file")) {
136 *phystype = PHYSTYPE_FILE;
137 } else if (!strcmp(s, "tap")) {
138 p = strchr(s, ':');
139 if (!p) {
140 rc = -1;
141 goto out;
142 }
143 p++;
144 if (!strcmp(p, "aio")) {
145 *phystype = PHYSTYPE_AIO;
146 } else if (!strcmp(p, "vhd")) {
147 *phystype = PHYSTYPE_VHD;
148 } else if (!strcmp(p, "qcow")) {
149 *phystype = PHYSTYPE_QCOW;
150 } else if (!strcmp(p, "qcow2")) {
151 *phystype = PHYSTYPE_QCOW2;
152 }
153 }
154 out:
155 return rc;
156 }