Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1394733
  • 博文数量: 343
  • 博客积分: 13098
  • 博客等级: 上将
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-06 00:35
文章存档

2012年(131)

2011年(31)

2010年(53)

2009年(23)

2008年(62)

2007年(2)

2006年(36)

2005年(5)

分类: 虚拟化

2012-05-12 22:04:06

About

This document describes the format of the entries in XenStore, how and what they're used for, and how third-party apps should use XenStore as a management interface. There are also code examples provided showing how to use the XenStore API.

Overview

XenStore is a hierarchical namespace (similar to sysfs or Open Firmware) which is shared between domains. The interdomain communication primitives exposed by Xen are very low-level (virtual IRQ and shared memory). XenStore is implemented on top of these primitives and provides some higher level operations (read a key, write a key, enumerate a directory, notify when a key changes value).

XenStore is a database, hosted by domain 0, that supports transactions and atomic operations. It's accessible by either a Unix domain socket in Domain-0, a kernel-level API, or an ioctl interface via /proc/xen/xenbus. XenStore should always be accessed through the functions defined in . XenStore is used to store information about the domains during their execution and as a mechanism of creating and controlling Domain-U devices.

XenBus is the in-kernel API used by virtual I/O drivers to interact with XenStore.

General Format

There are three main paths in :

  • /vm - stores configuration information about domain

  • /local/domain - stores information about the domain on the local node (domid, etc.)

  • /tool - stores information for the various tools

/vm

The /vm path stores configuration information for a domain. This information doesn't change and is indexed by the domain's UUID. A /vm entry contains the following information:

  • ssidref - ssid reference for domain

  • uuid - uuid of the domain (somewhat redundant)

  • on_reboot - the action to take on a domain reboot request (destroy or restart)

  • on_poweroff - the action to take on a domain halt request (destroy or restart)

  • on_crash - the action to take on a domain crash (destroy or restart)

  • vcpus - the number of allocated vcpus for the domain

  • memory - the amount of memory (in megabytes) for the domain Note: appears to sometimes be empty for domain-0

  • vcpu_avail - the number of active vcpus for the domain (vcpus - number of disabled vcpus)

  • name - the name of the domain

/vm//image/

The image path is only available for Domain-Us and contains:

  • ostype - identifies the builder type (linux or vmx)

  • kernel - path to kernel on domain-0

  • cmdline - command line to pass to domain-U kernel

  • ramdisk - path to ramdisk on domain-0

/local

The /local path currently only contains one directory, /local/domain that is indexed by domain id. It contains the running domain information. The reason to have two storage areas is that during migration, the uuid doesn't change but the domain id does. The /local/domain directory can be created and populated before finalizing the migration enabling localhost=>localhost migration.

/local/domain/

This path contains:

  • cpu_time - xend start time (this is only around for domain-0)

  • handle - private handle for xend

  • name - see /vm

  • on_reboot - see /vm

  • on_poweroff - see /vm

  • on_crash - see /vm

  • vm - the path to the VM directory for the domain

  • domid - the domain id (somewhat redundant)

  • running - indicates that the domain is currently running

  • memory/ - a directory for memory information

    • target - target memory size for the domain (in kilobytes)

  • cpu - the current CPU the domain is pinned to (empty for domain-0?)

  • cpu_weight - the weight assigned to the domain

  • vcpu_avail - a bitmap telling the domain whether it may use a given VCPU

  • online_vcpus - how many vcpus are currently online

  • vcpus - the total number of vcpus allocated to the domain

  • console/ - a directory for console information

    • ring-ref - the grant table reference of the console ring queue

    • port - the event channel being used for the console ring queue (local port)

    • tty - the current tty the console data is being exposed of

    • limit - the limit (in bytes) of console data to buffer

  • backend/ - a directory containing all backends the domain hosts

    • vbd/ - a directory containing vbd backends

      • / - a directory containing vbd's for domid

        • / - a directory for a particular virtual-device on domid

          • frontend-id - domain id of frontend

          • frontend - the path to the frontend domain

          • physical-device - backend device number

          • sector-size - backend sector size

          • sectors - backend number of sectors

          • info - device information flags. 1=cdrom, 2=removable, 4=read-only

          • domain - name of frontend domain

          • params - parameters for device

          • type - the type of the device

          • dev - frontend virtual device (as given by the user)

          • node - backend device node (output from block creation script)

          • hotplug-status - connected or error (output from block creation script)

          • state - communication state across to the frontend. 0=unknown, 1=initialising, 2=init. wait, 3=initialised, 4=connected, 5=closing, 6=closed

    • vif/ - a directory containing vif backends

      • / - a directory containing vif's for domid

        • number>/ - a directory for each vif

          • frontend-id - the domain id of the frontend

          • frontend - the path to the frontend

          • mac - the mac address of the vif

          • bridge - the bridge the vif is connected to

          • handle - the handle of the vif

          • script - the script used to create/stop the vif

          • domain - the name of the frontend

          • hotplug-status - connected or error (output from block creation script)

          • state - communication state across to the frontend. 0=unknown, 1=initialising, 2=init. wait, 3=initialised, 4=connected, 5=closing, 6=closed

  • device/ - a directory containing the frontend devices for the domain

    • vbd/ - a directory containing vbd frontend devices for the domain

      • / - a directory containing the vbd frontend for virtual-device

        • virtual-device - the device number of the frontend device

        • device-type - the device type ("disk", "cdrom", "floppy")

        • backend-id - the domain id of the backend

        • backend - the path of the backend in the store (/local/domain path)

        • ring-ref - the grant table reference for the block request ring queue

        • event-channel - the event channel used for the block request ring queue

        • state - communication state across to the backend. 0=unknown, 1=initialising, 2=init. wait, 3=initialised, 4=connected, 5=closing, 6=closed

    • vif/ - a directory containing vif frontend devices for the domain

      • / - a directory for vif id frontend device for the domain

        • backend-id - the backend domain id

        • mac - the mac address of the vif

        • handle - the internal vif handle

        • backend - a path to the backend's store entry

        • tx-ring-ref - the grant table reference for the transmission ring queue

        • rx-ring-ref - the grant table reference for the receiving ring queue

        • event-channel - the event channel used for the two ring queues

        • state - communication state across to the backend. 0=unknown, 1=initialising, 2=init. wait, 3=initialised, 4=connected, 5=closing, 6=closed

  • device-misc/ - miscellanous information for devices

    • vif/ - miscellanous information for vif devices

      • nextDeviceID - the next device id to use

  • store/ - per-domain information for the store

    • port - the event channel used for the store ring queue

    • ring-ref - the grant table reference used for the store's communication channel

  • image - private xend information

Interacting with the XenStore

The interface provides transaction based reads and writes to points in the xenstore hierarchy. Watches can be set at points in the hierarchy and an individual watch will be triggered when anything at or below that point in the hierachy changes. A watch is registered with a callback function and a "token". The "token" can be a pointer to any piece of data. The callback function is invoked with the of the changed node and the "token".

The interface is centered around the idea of a central polling loop that reads watches, providing the path, callback, and token, and invoking the callback.

API Usage Examples

These code snippets should provide a helpful starting point.

C


点击(此处)折叠或打开

  1. #include <xs.h> // don't forget to link with libxenstore.so

  2. struct xs_handle *xs;
  3. xs_transaction_t th;
  4. char *path;
  5. int fd;
  6. fd_set set;
  7. int er;
  8. struct timeval tv = {.tv_sec = 0, .tv_usec = 0 };
  9. char **vec;
  10. unsigned int num_strings;
  11. char * buf;
  12. unsigned int len;
  13. /* Get a connection to the daemon */
  14. xs = xs_daemon_open();
  15. if ( xs == NULL ) error();
  16. /* Get the local domain path */
  17. path = xs_get_domain_path(xs, domid); // replace "domid" with a valid domain ID (or one which will become valid)
  18. if ( path == NULL ) error();
  19. /* Make space for our node on the path */
  20. path = realloc(path, strlen(path) + strlen("/mynode") + 1);
  21. if ( path == NULL ) error();
  22. strcat(path, "/mynode");
  23. /* Create a watch on /local/domain/%d/mynode. */
  24. er = xs_watch(xs, path, "mytoken");
  25. if ( er == 0 ) error();
  26. /* We are notified of read availability on the watch via the
  27.  * file descriptor.
  28.  */
  29. fd = xs_fileno(xs);
  30. while (1)
  31. {
  32.     /* TODO (TimPost), show a simpler example with poll()
  33.      * in a modular style, using a simple callback. Most
  34.      * people think 'inotify' when they see 'watches'. */
  35.     FD_ZERO(&set);
  36.     FD_SET(fd, &set);
  37.     /* Poll for data. */
  38.     if ( select(fd + 1, &set, NULL, NULL, &tv) > 0
  39.          && FD_ISSET(fd, &set))
  40.     {
  41.         /* num_strings will be set to the number of elements in vec
  42.          * (typically, 2 - the watched path and the token) */
  43.         vec = xs_read_watch(xs, &num_strings);
  44.         if ( !vec ) error();
  45.         printf("vec contents: %s|%s\n", vec[XS_WATCH_PATH],
  46.                                         vec[XS_WATCH_TOKEN]);
  47.         /* Prepare a transaction and do a read. */
  48.         th = xs_transaction_start(xs);
  49.         buf = xs_read(xs, th, vec[XS_WATCH_PATH], &len);
  50.         xs_transaction_end(xs, th, false);
  51.         if ( buf )
  52.         {
  53.             printf("buflen: %d\nbuf: %s\n", len, buf);
  54.         }
  55.         /* Prepare a transaction and do a write. */
  56.         th = xs_transaction_start(xs);
  57.         er = xs_write(xs, th, path, "somestuff", strlen("somestuff"));
  58.         xs_transaction_end(xs, th, false);
  59.         if ( er == 0 ) error();
  60.     }
  61. }
  62. /* Cleanup */
  63. close(fd);
  64. xs_daemon_close(xs);
  65. free(path);

Python


点击(此处)折叠或打开

  1.   # xsutil provides access to xshandle() which allows you to use
  2.   # something closer to the C-style API,
  3.   # however it does not support polling in the same manner.
  4.   from xen.xend.xenstore.xsutil import *

  5.   # xswatch provides a callback interface for the watches. I
  6.   # similar interface exists for C within xenbus.
  7.   from xen.xend.xenstore.xswatch import *
  8.   xs = xshandle() # From xsutil
  9.   # replace 1 with a domain ID which is valid (or will become valid)
  10.   path = xs.get_domain_path(1) + "/mynode"

  11.   # Watch functions take the path as the first argument,
  12.   # all other arguments that are passed via the xswatch call are also included.
  13.   # Note that "path" does not point to the watched path but to the changed
  14.   # path (i.e. the watched path or any node below)!
  15.   def watch_func(path, xs):
  16.       # Read the data th = xs.transaction_start()
  17.       buf = xs.read(th, path) xs.transaction_end(th)
  18.       print("Got %s" % buf)

  19.       # Write back "somestuff" (as we change the path value,
  20.       # our callback function will then be called recursively because we return
  21.       th = xs.transaction_start()
  22.       xs.write(th, path, "somestuff")
  23.       xs.transaction_end(th)

  24.       # The watch will be removed if False is returned
  25.       # (e.g. if you want the callback to be called only once)
  26.       return True

  27.   # This will start a thread which waits for changes in watched paths
  28.   # and calls the appropriate callback functions if something changed
  29.   mywatch = xswatch(path, watch_func, xs) # pass "xs" as argument to the watch function

You can use direct Read/Write or gather calls via xstransact.

By default the python xsutil.xshandle() is a shared global handle. xswatch uses this handle with a blocking read_watch call. Because the read_watch function is protected by a per-handle mutex, multiple calls will be interleaved and you probably do not want this behavior. If you would like a blocking mechanism, you might consider introducing a semaphore in the callback function that can be used to block code execution. You need to be sure to handle failure cases and not block indefinitely. For instance, the "@releaseDomain" watch will be triggered on domain destruction for watches within the /local/domain/* trees.

It is also possible -- currently indirectly -- to get a fresh handle within python and block on read_watch in the main execution path. This may be necessary if you want to block waiting for a node value in a code path initialed by an xswatch callback.

阅读(693) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~