1 #include "xen_backend.h"
4 /* ------------------------------------------------------------- */
8 QTAILQ_ENTRY(xs_dirs
) list
;
10 static QTAILQ_HEAD(xs_dirs_head
, xs_dirs
) xs_cleanup
= QTAILQ_HEAD_INITIALIZER(xs_cleanup
);
12 static void xen_config_cleanup_dir(char *dir
)
16 d
= g_malloc(sizeof(*d
));
18 QTAILQ_INSERT_TAIL(&xs_cleanup
, d
, list
);
21 void xen_config_cleanup(void)
25 QTAILQ_FOREACH(d
, &xs_cleanup
, list
) {
26 xs_rm(xenstore
, 0, d
->xs_dir
);
30 /* ------------------------------------------------------------- */
32 static int xen_config_dev_mkdir(char *dev
, int p
)
34 struct xs_permissions perms
[2] = {{
35 .id
= 0, /* set owner: dom0 */
41 if (!xs_mkdir(xenstore
, 0, dev
)) {
42 xen_be_printf(NULL
, 0, "xs_mkdir %s: failed\n", dev
);
45 xen_config_cleanup_dir(g_strdup(dev
));
47 if (!xs_set_permissions(xenstore
, 0, dev
, perms
, 2)) {
48 xen_be_printf(NULL
, 0, "xs_set_permissions %s: failed\n", dev
);
54 static int xen_config_dev_dirs(const char *ftype
, const char *btype
, int vdev
,
55 char *fe
, char *be
, int len
)
59 dom
= xs_get_domain_path(xenstore
, xen_domid
);
60 snprintf(fe
, len
, "%s/device/%s/%d", dom
, ftype
, vdev
);
63 dom
= xs_get_domain_path(xenstore
, 0);
64 snprintf(be
, len
, "%s/backend/%s/%d/%d", dom
, btype
, xen_domid
, vdev
);
67 xen_config_dev_mkdir(fe
, XS_PERM_READ
| XS_PERM_WRITE
);
68 xen_config_dev_mkdir(be
, XS_PERM_READ
);
72 static int xen_config_dev_all(char *fe
, char *be
)
76 xenstore_write_str(fe
, "protocol", xen_protocol
);
78 xenstore_write_int(fe
, "state", XenbusStateInitialising
);
79 xenstore_write_int(fe
, "backend-id", 0);
80 xenstore_write_str(fe
, "backend", be
);
83 xenstore_write_str(be
, "domain", qemu_name
? qemu_name
: "no-name");
84 xenstore_write_int(be
, "online", 1);
85 xenstore_write_int(be
, "state", XenbusStateInitialising
);
86 xenstore_write_int(be
, "frontend-id", xen_domid
);
87 xenstore_write_str(be
, "frontend", fe
);
92 /* ------------------------------------------------------------- */
94 int xen_config_dev_blk(DriveInfo
*disk
)
96 char fe
[256], be
[256], device_name
[32];
97 int vdev
= 202 * 256 + 16 * disk
->unit
;
98 int cdrom
= disk
->media_cd
;
99 const char *devtype
= cdrom
? "cdrom" : "disk";
100 const char *mode
= cdrom
? "r" : "w";
101 const char *filename
= qemu_opt_get(disk
->opts
, "file");
103 snprintf(device_name
, sizeof(device_name
), "xvd%c", 'a' + disk
->unit
);
104 xen_be_printf(NULL
, 1, "config disk %d [%s]: %s\n",
105 disk
->unit
, device_name
, filename
);
106 xen_config_dev_dirs("vbd", "qdisk", vdev
, fe
, be
, sizeof(fe
));
109 xenstore_write_int(fe
, "virtual-device", vdev
);
110 xenstore_write_str(fe
, "device-type", devtype
);
113 xenstore_write_str(be
, "dev", device_name
);
114 xenstore_write_str(be
, "type", "file");
115 xenstore_write_str(be
, "params", filename
);
116 xenstore_write_str(be
, "mode", mode
);
119 return xen_config_dev_all(fe
, be
);
122 int xen_config_dev_nic(NICInfo
*nic
)
124 char fe
[256], be
[256];
127 snprintf(mac
, sizeof(mac
), "%02x:%02x:%02x:%02x:%02x:%02x",
128 nic
->macaddr
.a
[0], nic
->macaddr
.a
[1], nic
->macaddr
.a
[2],
129 nic
->macaddr
.a
[3], nic
->macaddr
.a
[4], nic
->macaddr
.a
[5]);
130 xen_be_printf(NULL
, 1, "config nic %d: mac=\"%s\"\n", nic
->vlan
->id
, mac
);
131 xen_config_dev_dirs("vif", "qnic", nic
->vlan
->id
, fe
, be
, sizeof(fe
));
134 xenstore_write_int(fe
, "handle", nic
->vlan
->id
);
135 xenstore_write_str(fe
, "mac", mac
);
138 xenstore_write_int(be
, "handle", nic
->vlan
->id
);
139 xenstore_write_str(be
, "mac", mac
);
142 return xen_config_dev_all(fe
, be
);
145 int xen_config_dev_vfb(int vdev
, const char *type
)
147 char fe
[256], be
[256];
149 xen_config_dev_dirs("vfb", "vfb", vdev
, fe
, be
, sizeof(fe
));
152 xenstore_write_str(be
, "type", type
);
155 return xen_config_dev_all(fe
, be
);
158 int xen_config_dev_vkbd(int vdev
)
160 char fe
[256], be
[256];
162 xen_config_dev_dirs("vkbd", "vkbd", vdev
, fe
, be
, sizeof(fe
));
163 return xen_config_dev_all(fe
, be
);
166 int xen_config_dev_console(int vdev
)
168 char fe
[256], be
[256];
170 xen_config_dev_dirs("console", "console", vdev
, fe
, be
, sizeof(fe
));
171 return xen_config_dev_all(fe
, be
);