3.3. Working with VIFs and VBDs
In this section we walk through a few more complex scenarios, describing informally how various tasks involving virtual storage
and network devices can be accomplished using the API.
3.3.1. Creating disks and attaching them to VMs
Let's start by considering how to make a new blank disk image and attach it to a running VM. We will assume that we already
have ourselves a running VM, and we know its corresponding API object reference (e.g. we may have created this VM using the procedure described in
the previous section, and had the server return its reference to us.) We will also assume that we have authenticated with the XenServer installation
and have a corresponding session reference. Indeed in the rest of this chapter, for the sake of brevity, we will stop mentioning sessions altogether.
3.3.1.1. Creating a new blank disk image
The first step is to instantiate the disk image on physical storage. We do this via a call to VDI.create(...).
The VDI.create call takes a number of parameters, including:
name_label and name_description: a human-readable name/description for the disk (e.g. for convenient display in the UI etc.). These fields can be left blank if desired.
SR: the object reference of the Storage Repository representing the physical storage in which the VDI's bits will be placed.
read_only: setting this field to true indicates that the VDI can only be attached to VMs in a read-only fashion. (Attempting to attach a VDI with its read_only field set to true in a read/write fashion results in error.)
Invoking the VDI.create call causes the XenServer installation to create a blank disk image on physical storage, create an associated VDI object (the datamodel instance that refers to the disk image on physical storage) and return a reference to this newly created VDI object.
The way in which the disk image is represented on physical storage depends on the type of the SR in which the created VDI resides. For example, if the SR is of type "lvm" then the new disk image will be rendered as an LVM volume; if the SR is of type "nfs" then the new disk image will be a sparse VHD file created on an NFS filer. (You can query the SR type through the API using the SR.get_type(..) call.)
3.3.1.2. Attaching the disk image to a VM
So far we have a running VM (that we assumed the existence of at the start of this example) and a fresh VDI that we just created.
Right now, these are both independent objects that exist on the XenServer host, but there is nothing linking them together.
So our next step is to create such a link, associating the VDI with our VM.
The attachment is formed by creating a new "connector" object called a VBD (Virtual Block Device). To create
our VBD we invoke the VBD.create(...) call. The VBD.create(..) call takes a number of parameters
including:
VM: the object reference of the VM to which the VDI is to be attached
VDI: the object reference of the VDI that is to be attached
mode: specifies whether the VDI is to be attached in a read-only or a read-write fashion
userdevice: specifies the block device inside the guest through which applications running inside the VM will be able to read/write the VDI's bits.
type: specifies whether the VDI should be presented inside the VM as a regular disk or as a CD. (Note that this particular field has more meaning for Windows VMs than it does for Linux VMs, but will not explore this level of detail in this chapter.)
Invoking VBD.create makes a VBD object on the XenServer installation and returns its object reference.
However, this call in itself does not have any side-effects on the running VM (i.e. if you go and look inside the running VM you will
see that the block device has not been created). The fact that the VBD object exists but that the block device in the guest is
not active, is reflected by the fact that the VBD object's currently_attached field is set to false.
For expository purposes,
Figure 3.2, “A VM object with 2 associated VDIs” presents a graphical example that shows the relationship between VMs, VBDs, VDIs and
SRs. In this instance a VM object has 2 attached VDIs: there are 2 VBD objects that
form the connections between the VM object and its VDIs; and the VDIs reside within the same SR.
3.3.1.3. Hotplugging the VBD
If we rebooted the VM at this stage then, after rebooting, the block device corresponding to the VBD would appear: on boot,
XenServer queries all a VM's VBDs and actively attaches each of the corresponding VDIs.
Rebooting the VM is all very well, but recall that we wanted to attach a newly created blank disk to a running VM.
This can be achieved by invoking the plug method on the newly created VBD object. When the plug call
returns successfully, the block device to which the VBD relates will have appeared inside the running VM -- i.e. from the perspective of the running
VM, the guest operating system is lead to believe that a new disk device has just been hot plugged.
Mirroring this fact in the managed world of the API, the currently_attached field of the VBD is set to true.
Unsurprisingly, the VBD plug method has a dual called "unplug". Invoking the unplug method
on a VBD object causes the associated block device to be hot unplugged from a running VM, setting the
currently_attached field of the VBD object to false accordingly.
3.3.2. Creating and attaching Network Devices to VMs
The API calls involved in configuring virtual network interfaces in VMs are similar in many respects to the calls involved
in configuring virtual disk devices. For this reason we will not run through a full example of how one can create network interfaces
using the API object-model; instead we will use this section just to outline briefly the symmetry between virtual networking device
and virtual storage device configuration.
The networking analogue of the VBD class is the VIF class. Just as a VBD is the API representation of a block device inside
a VM, a VIF (Virtual Network Device) is the API representation of a network device inside a VM. Whereas VBDs associate
VM objects with VDI objects, VIFs associate VM objects with Network objects. Just like VBDs, VIFs have a currently_attached
field that determines whether or not the network device (inside the guest) associated with the VIF is currently active or not. And as we
saw with VBDs, at VM boot-time the VM's VIFs are queried and a corresponding network device for each created inside the booting VM.
Similarly, VIFs also have plug and unplug methods for hot plugging/unplugging network devices in/out of
running VMs.
3.3.3. Host configuration for networking and storage
We have seen that the VBD and VIF classes are used to manage configuration of block devices and network devices (respectively) inside VMs.
To manage host configuration of storage and networking there are two analogous classes: PBD (Physical Block Device)
and PIF (Physical [network] InterFace).
3.3.3.1. Host storage configuration: PBDs
Let us start by considering the PBD class.
As well as connecting a host, h, to an SR, s, a PBD object also contains a
device_config field. This contains the low-level parameters required by h in order
to configure the physical storage device(s) that comprise s. The contents of the device_config
field depends on the type of the SR to which the PBD is connected. (Executing "xe sm-list" will show a list
of possible SR types; the configuration field in this enumeration specifies the device_config parameters that
each SR type expects.)
For example, imagine we have an SR object, s of type "nfs" (representing a directory on an NFS filer within which VDIs are stored as VHD files);
and let's say that we want a host, h, to be able to access s. In this case we may invoke PBD.create(...)
specifying h, s and a value for the device_config parameter that is the following map:
("server", "my_nfs_server.xensource.com"), ("serverpath", "/scratch/mysrs/sr1"). This tells the XenServer installation that SR s
is accessible on host h, and further that in order to access s the host needs to mount directory
"/scratch/mysrs/sr1" on NFS server "my_nfs_server.xensource.com".
Like VBD objects, PBD objects also have a field called currently_attached. Storage repositories can be attached and
detached from a given host by invoking PBD.plug and PBD.unplug methods respectively.
3.3.3.2. Host networking configuration: PIFs
Host network configuration is specified by virtue of PIF objects. If a PIF object connects a network object, n,
to a host object h then the network corresponding to n is bridged onto a physical
interface (or a physical interface + VLAN tag) specified by the fields of the PIF object.
For example, imagine a PIF object exists
connecting host h to a network n, and that device field of the PIF
object is set to "eth0". This means that all packets on network n are bridged to the NIC in the host
corresponding to host network device "eth0".
|