The Book of Xen

Chris Takemura - Luke S. Crawford

Part 8

Report Chapter
webnovel
webnovel

Xen-sh.e.l.l Although we like our home brew approach to management as described in Chapter7 Chapter7, there are other options for those who believe that software can be perfectly adequate even if they didn't personally write it. Of these, our favorite is the Xen-sh.e.l.l. It's by the author of Xen-tools and is another example of his no-frills approach. We recommend it, not merely because we like Xen-tools or even because it's got a good feature set, because it doesn't have a giant list of dependencies. It's a simple product that does its job well.

It's available at http://xen-tools.org/software/xen-sh.e.l.l/. After you've downloaded it, use the standard unpack && make install process to install it.

At this point there's some configuration that needs to be done. Xen-sh.e.l.l works by taking user commands and running sudo xm sudo xm in response to their input. You'll need to put in response to their input. You'll need to put xm xm into their path or, conversely, alter their path to include into their path or, conversely, alter their path to include xm xm. We took the former approach: #ln-s/usr/sbin/xm/usr/bin We also need to configure /etc/sudoers /etc/sudoers to make sure that the users are allowed to use to make sure that the users are allowed to use sudo sudo to run to run xm xm on their domain (and only their domain). This entails quite a number of additions to the file, one for each command we want to allow: on their domain (and only their domain). This entails quite a number of additions to the file, one for each command we want to allow: marloweALL=NOPa.s.sWD:/usr/sbin/xmcreategoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmcreate-cgoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmdestroygoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmshutdowngoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmlistgoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmconsolegoneril marloweALL=NOPa.s.sWD:/usr/sbin/xmrebootgoneril Then change the sh.e.l.l of the appropriate users to the Xen-sh.e.l.l. For example: #chsh-s/usr/local/bin/xen-login-sh.e.l.lmarlowe To mark that a user is allowed to administer a domain, simply add the user to a line in the domain config file-an elegant and ingenious solution. We'll use the domain goneril goneril as an example: as an example: name='goneril'

xen-sh.e.l.l='marlowe'

Now, when marlowe logs in, he'll be presented with the Xen-sh.e.l.l interface from which he can execute various commands (get a list by typing Help Help).

NoteAlthough Xen-sh.e.l.l reads domain config files to find which domains can be administered by a user, it doesn't actually keep track of the config file's name, as of this writing. Domain configuration filenames, to work with Xen-sh.e.l.l's boot boot command, must be of the form command, must be of the form .cfg .cfg. Thus, goneril's config file must be Thus, goneril's config file must be /etc/xen/goneril.cfg. /etc/xen/goneril.cfg.

To extend the example, let's say that marlowe can administer multiple domains. Simply add the username to both domains, and use the control control command in Xen-sh.e.l.l to switch between them. One of the niceties of Xen-sh.e.l.l is that the command only shows up if it's necessary. command in Xen-sh.e.l.l to switch between them. One of the niceties of Xen-sh.e.l.l is that the command only shows up if it's necessary.

xen-sh.e.l.l[goneril]>controlregan Controlling:regan xen-sh.e.l.l[regan]> Convenient, isn't it?

Really, though, this is just the beginning. The client software for Xen is still in turmoil, with constant development by multiple factions.

You may have noticed that we've left out a couple of prominent frontends. For one, we haven't even mentioned Citrix's offering because we cover it in Chapter11 Chapter11. We also haven't addressed Amazon's EC2, which is probably the nearest thing to utility computing utility computing at present. As always, there's a big field of tools out there, and we're just aiming to make them seem manageable and talk about what works for us. at present. As always, there's a big field of tools out there, and we're just aiming to make them seem manageable and talk about what works for us.

Chapter7.HOSTING UNTRUSTED USERS UNDER XEN: LESSONS FROM THE TRENCHES

Now that we've gone over the basics of Xen administration-storage, networking, provisioning, and management-let's look at applying these basics in practice. This chapter is mostly a case study of our VPS hosting firm, prgmr.com, and the lessons we've learned from renting Xen instances to the public.

The most important lesson of public Xen hosting is that the users can't be trusted to cooperate with you or each other. Some people will always try to seize as much as they can. Our focus will be on preventing this tragedy of the commons.

Advantages for the Users There's exactly one basic reason that a user would want to use a Xen VPS rather than paying to colocate a box in your data center: it's cheap, especially for someone who's just interested in some basic services, rather than ma.s.sive raw performance.

GRID COMPUTING AND VIRTUALIZATIONOne term that you hear fairly often in connection with Xen is grid computing grid computing. The basic idea behind grid computing is that you can quickly and automatically provision and destroy nodes. Amazon's EC2 service is a good example of a grid computing platform that allows you to rent Linux servers by the hour.Grid computing doesn't require virtualization, but the two concepts are fairly closely linked. One could design a system using physical machines and PXEboot for fast, easy, automated provisioning without using Xen, but a virtualization system would make the setup more lightweight, agile, and efficient.There are several open source projects that are attempting to create a standard and open interface to provision "grid computing" resources. One such project is Eucalyptus (http://www.eucalyptus.com/). We feel that standard frameworks like this-that allow you to easily switch between grid computing providers-are essential if "the grid" is to survive.

Xen also gives users nearly all the advantages they'd get from colocating a box: their own publicly routed network interface, their own disk, root access, and so forth. With a 128MB VM, they can run DNS, light mail service, a web server, IRC, SSH, and so on. For lightweight services like these, the power of the box is much less important than its basic existence-just having something available and publicly accessible makes life more convenient.

You also have the basic advantages of virtualization, namely, that hosting one server with 32GB of RAM is a whole lot cheaper than hosting 32 servers with 1GB of RAM each (or even 4 servers with 8GB RAM each). In fact, the price of RAM being what it is, I would argue that it's difficult to even economically justify hosting a general-purpose server with less than 32GB of RAM.

The last important feature of Xen is that, relative to other virtualization systems, it's got a good combination of light weight, strong part.i.tioning, and robust resource controls. Unlike some other virtualization options, it's consistent-a user can rely on getting exactly the amount of memory, disk s.p.a.ce, and network bandwidth that he's signed up for and approximately as much CPU and disk bandwidth.

Shared Resources and Protecting Them from the Users Xen's design is congruent to good security.-Tavis Ormandy http://taviso.decsystem.org/virtsec.pdf It's a ringing endors.e.m.e.nt, by security-boffin standards. By and large, with Xen, we're not worried about keeping people from breaking out of their virtual machines-Xen itself is supposed to provide an appropriate level of isolation. In paravirtualized mode, Xen doesn't expose hardware drivers to domUs, which eliminates one major attack vector.[39] For the most part, securing a dom0 is exactly like securing any other server, except in one area. For the most part, securing a dom0 is exactly like securing any other server, except in one area.

That area of possible concern is in the access controls for shared resources, which are not entirely foolproof. The primary worry is that malicious users could gain more resources than they're ent.i.tled to, or in extreme cases cause denial-of-service attacks by exploiting flaws in Xen's accounting. In other words, we are in the business of enforcing performance isolation, rather than specifically trying to protect the dom0 from attacks via the domUs.

Most of the resource controls that we present here are aimed at users who aren't necessarily malicious-just, perhaps, exuberant.

Tuning CPU Usage The first shared resource of interest is the CPU. While memory and disk size are easy to tune-you can just specify memory in the config file, while disk size is determined by the size of the backing device-fine-grained CPU allocation requires you to adjust the scheduler.

Scheduler Basics The Xen scheduler acts as a referee between the running domains. In some ways it's a lot like the Linux scheduler: It can preempt processes as needed, it tries its best to ensure fair allocation, and it ensures that the CPU wastes as few cycles as possible. As the name suggests, Xen's scheduler schedules domains to run on the physical CPU. These domains, in turn, schedule and run processes from their internal run queues.

Because the dom0 is just another domain as far as Xen's concerned, it's subject to the same scheduling algorithm as the domUs. This can lead to trouble if it's not a.s.signed a high enough weight because the dom0 has to be able to respond to I/O requests. We'll go into more detail on that topic a bit later, after we describe the general procedures for adjusting domain weights.

Xen can use a variety of scheduling algorithms, ranging from the simple to the baroque. Although Xen has shipped with a number of schedulers in the past, we're going to concentrate on the credit scheduler credit scheduler; it's the current default and recommended choice and the only one that the Xen team has indicated any interest in keeping.

The xm dmesg xm dmesg command will tell you, among other things, what scheduler Xen is using. command will tell you, among other things, what scheduler Xen is using.

#xmdmesg

grepscheduler (XEN)Usingscheduler:SMPCreditScheduler(credit) If you want to change the scheduler, you can set it as a boot parameter-to change to the SEDF scheduler, for example, append sched=sedf sched=sedf to the kernel line in GRUB. (That's the Xen kernel, not the dom0 Linux kernel loaded by the first to the kernel line in GRUB. (That's the Xen kernel, not the dom0 Linux kernel loaded by the first module module line.) line.) VCPUs and Physical CPUs For convenience, we consider each Xen domain to have one or more virtual CPUs (VCPUs), which periodically run on the physical CPUs. These are the ent.i.ties that consume credits when run. To examine VCPUs, use xm vcpu-list xm vcpu-list : : #xmvcpu-listhoratio NameIDVCPUsCPUStateTime(s)CPUAffinity horatio1600---140005.6anycpu horatio1612r--139968.3anycpu In this case, the domain has two VCPUs, 0 and 1. VCPU 1 is in the running running state on (physical) CPU 1. Note that Xen will try to spread VCPUs across CPUs as much as possible. Unless you've pinned them manually, VCPUs can occasionally switch CPUs, depending on which physical CPUs are available. state on (physical) CPU 1. Note that Xen will try to spread VCPUs across CPUs as much as possible. Unless you've pinned them manually, VCPUs can occasionally switch CPUs, depending on which physical CPUs are available.

To specify the number of VCPUs for a domain, specify the vcpus= vcpus= directive in the config file. You can also change the number of VCPUs while a domain is running using directive in the config file. You can also change the number of VCPUs while a domain is running using xm vcpu-set xm vcpu-set. However, note that you can decrease the number of VCPUs this way, but you can't increase the number of VCPUs beyond the initial count.

To set the CPU affinity, use xm vcpu-pin xm vcpu-pin . For example, to switch the CPU a.s.signment in the domain horatio horatio, so that VCPU0 runs on CPU2 and VCPU1 runs on CPU0: #xmvcpu-pinhoratio02 #xmvcpu-pinhoratio10 Equivalently, you can pin VCPUs in the domain config file (/etc/xen/horatio, if you're using our standard naming convention) like this: vcpus=2 cpus=[0,2]

This gives the domain two VCPUs, pins the first VCPU to the first physical CPU, and pins the second VCPU to the third physical CPU.

Credit Scheduler The Xen team designed the credit scheduler to minimize wasted CPU time. This makes it a work-conserving work-conserving scheduler, in that it tries to ensure that the CPU will always be working whenever there is work for it to do. scheduler, in that it tries to ensure that the CPU will always be working whenever there is work for it to do.

As a consequence, if there is more real CPU available than the domUs are demanding, all domUs get all the CPU they want. When there is contention-that is, when the domUs in aggregate want more CPU than actually exists-then the scheduler arbitrates fairly between the domains that want CPU.

Xen does its best to do a fair division, but the scheduling isn't perfect by any stretch of the imagination. In particular, cycles spent servicing I/O by domain 0 are not charged to the responsible domain, leading to situations where I/O-intensive clients get a disproportionate share of CPU usage. Nonetheless, you can get pretty good allocation in nonpathological cases. (Also, in our experience, the CPU sits idle most of the time anyway.) The credit scheduler a.s.signs each domain a weight weight and, optionally, a and, optionally, a cap cap. The weight indicates the relative CPU allocation of a domain-if the CPU is scarce, a domain with a weight of 512 will receive twice as much CPU time as a domain with a weight of 256 (the default). The cap sets an absolute limit on the amount of CPU time a domain can use, expressed in hundredths of a CPU. Note that the CPU cap can exceed 100 on multiprocessor hosts.

The scheduler transforms the weight into a credit credit allocation for each VCPU, using a separate accounting thread. As a VCPU runs, it consumes credits. If a VCPU runs out of credits, it only runs when other, more thrifty VCPUs have finished executing, as shown in allocation for each VCPU, using a separate accounting thread. As a VCPU runs, it consumes credits. If a VCPU runs out of credits, it only runs when other, more thrifty VCPUs have finished executing, as shown in Figure7-1 Figure7-1. Periodically, the accounting thread goes through and gives everybody more credits.

Figure7-1.VCPUs wait in two queues: one for VCPUs with credits and the other for those that are over their allotment. Once the first queue is exhausted, the CPU will pull from the second.

In this case, the details are probably less important than the practical application. Using the xm sched-credit xm sched-credit commands, we can adjust CPU allocation on a per-domain basis. For example, here we'll increase a domain's CPU allocation. First, to list the weight and cap for the domain horatio: commands, we can adjust CPU allocation on a per-domain basis. For example, here we'll increase a domain's CPU allocation. First, to list the weight and cap for the domain horatio: #xmsched-credit-dhoratio {'cap':0,'weight':256} Then, to modify the scheduler's parameters: #xmsched-credit-dhoratio-w512 #xmsched-credit-dhoratio {'cap':0,'weight':512} Of course, the value "512" only has meaning relative to the other domains that are running on the machine. Make sure to set all the domains' weights appropriately.

To set the cap for a domain: #xmsched-credit-ddomain-ccap Scheduling for Providers We decided to divide the CPU along the same lines as the available RAM-it stands to reason that a user paying for half the RAM in a box will want more CPU than someone with a 64MB domain. Thus, in our setup, a customer with 25 percent of the RAM also has a minimum share of 25 percent of the CPU cycles.

The simple way to do this is to a.s.sign each CPU a weight equal to the number of megabytes of memory it has and leave the cap empty. The scheduler will then handle converting that into fair proportions. For example, our aforementioned user with half the RAM will get about as much CPU time as the rest of the users put together.

Of course, that's the worst case; that is what the user will get in an environment of constant struggle for the CPU. Idle domains will automatically yield the CPU. If all domains but one are idle, that one can have the entire CPU to itself.

NoteIt's essential to make sure that the dom0 has sufficient CPU to service I/O requests. You can handle this by dedicating a CPU to the dom0 or by giving the dom0 a very high weight-high enough to ensure that it never runs out of credits. At prgmr.com, we handle the problem by weighting each domU with its RAM amount and weighting the dom0 at 6000.

This simple weight = memory formula becomes a bit more complex when dealing with multiprocessor systems because independent systems of CPU allocation come into play. A good rule would be to allocate VCPUs in proportion to memory (and therefore in proportion to weight). For example, a domain with half the RAM on a box with four cores (and hyperthreading turned off) should have at least two VCPUs. Another solution would be to give all domains as many VCPUs as physical processors in the box-this would allow all domains to burst to the full CPU capacity of the physical machine but might lead to increased overhead from context swaps.

Controlling Network Resources Network resource controls are, frankly, essential to any kind of shared hosting operation. Among the many lessons that we've learned from Xen hosting has been that if you provide free bandwidth, some users will exploit it for all it's worth. This isn't a Xen-specific observation, but it's especially noticeable with the sort of cheap VPS hosting Xen lends itself to.

We prefer to use network-bridge network-bridge, since that's the default. For a more thorough look at network-bridge network-bridge, take a look at Chapter5 Chapter5.

Monitoring Network Usage Given that some users will consume as much bandwidth as possible, it's vital to have some way to monitor network traffic.[40]

To monitor network usage, we use BandwidthD on a physical SPAN port. It's a simple tool that counts bytes going through a switch-nothing Xen-specific here. We feel comfortable doing this because our provider doesn't allow anything but IP packets in or out, and our antispoof rules are good enough to protect us from users spoofing their IP on outgoing packets.

A similar approach would be to extend the dom0 is a switch dom0 is a switch a.n.a.logy and use SNMP monitoring software. As mentioned in a.n.a.logy and use SNMP monitoring software. As mentioned in Chapter5 Chapter5, it's important to specify a vifname vifname for each domain if you're doing this. In any case, we'll leave the particulars of bandwidth monitoring up to you. for each domain if you're doing this. In any case, we'll leave the particulars of bandwidth monitoring up to you.

*** You are reading on https://webnovelonline.com ***

ARP CACHE POISONINGIf you use the default network-bridge setup network-bridge setup, you are vulnerable to ARP cache poisoning, just as on any layer 2 switch.The idea is that the interface counters on a layer 2 switch-such as the virtual switch used by network-bridge network-bridge-watch traffic as it pa.s.ses through a particular port. Every time a switch sees an Ethernet frame or ARP is-at, it keeps track of what port and MAC it came from. If it gets a frame destined for a MAC address in its cache, it sends that frame down the proper port (and only the proper port). If the bridge sees a frame destined for a MAC that is not in the cache, it sends that frame to all ports.[41]Clever, no? In most cases this means that you almost never see Ethernet frames destined for other MAC addresses (other than broadcasts, etc.). However, this feature is designed purely as an optimization, not a security measure. As those of you with cable providers who do MAC address verification know quite well, it is fairly trivial to fake a MAC address. This means that a malicious user can fill the (limited in size) ARP cache with bogus MAC addresses, drive out the good data, and force all packets to go down all interfaces. At this point the switch becomes basically a hub, and the counters on all ports will show all traffic for any port.There are two ways we have worked around the problem. You could use Xen's network-route network-route networking model, which doesn't use a virtual bridge. The other approach is to ignore the interface counters and use something like BandwidthD, which bases its accounting on IP packets. networking model, which doesn't use a virtual bridge. The other approach is to ignore the interface counters and use something like BandwidthD, which bases its accounting on IP packets.

We recommend always specifying 2 for the cla.s.s. Other cla.s.ses exist-3 is idle and 1 is realtime-but idle is extremely conservative, while realtime is so aggressive as to have a good chance of locking up the system. The within-cla.s.s priority is aimed at proportional allocation, and is thus much more likely to be what you want.

Let's look at ionice ionice in action. Here we'll test in action. Here we'll test ionice ionice with two different domains, one with the highest normal priority, the other with the lowest. with two different domains, one with the highest normal priority, the other with the lowest.

First, ionice ionice only works with the CFQ I/O scheduler. To check that you're using the CFQ scheduler, run this command in the dom0: only works with the CFQ I/O scheduler. To check that you're using the CFQ scheduler, run this command in the dom0: #cat/sys/block/[sh]d[a-z]*/queue/scheduler noopantic.i.p.atorydeadline[cfq]

noopantic.i.p.atorydeadline[cfq]

The word in brackets is the selected scheduler. If it's not [cfq] [cfq], reboot with the parameter elevator =cfq elevator =cfq.

Next we find the processes we want to ionice ionice. Because we're using tap:aio tap:aio devices in this example, the dom0 process is devices in this example, the dom0 process is tapdisk tapdisk. If we were using phy: phy: devices, it'd be devices, it'd be [xvd ] [xvd ].

#psaux

greptapdisk root10540.50.013588556?Sl05:450:10tapdisk /dev/xen/tapctrlwrite1/dev/xen/tapctrlread1 root11720.60.013592560?Sl05:450:10tapdisk /dev/xen/tapctrlwrite2/dev/xen/tapctrlread2 Now we can ionice ionice our domains. Note that the numbers of the our domains. Note that the numbers of the tapctrl tapctrl devices correspond to the order the domains were started in, not the domain ID. devices correspond to the order the domains were started in, not the domain ID.

#ionice-p1054-c2-n7 #ionice-p1172-c2-n0 To test ionice ionice, let's run a couple of Bonnie++ processes and time them. (After Bonnie++ finishes, we dd dd a load file, just to make sure that conditions for the other domain remain unchanged.) a load file, just to make sure that conditions for the other domain remain unchanged.) prio7domUtmp#/usr/bin/time-vbonnie++-u1&&ddif=/dev/urandomof=load prio0domUtmp#/usr/bin/time-vbonnie++-u1&&ddif=/dev/urandomof=load In the end, according to the wall clock, the domU with priority 0 took 3:32.33 to finish, while the priority 7 domU needed 5:07.98. As you can see, the ionice ionice priorities provide an effective way to do proportional I/O allocation. priorities provide an effective way to do proportional I/O allocation.

The best way to apply ionice ionice is probably to look at CPU allocations and convert them into priority cla.s.ses. Domains with the highest CPU allocation get priority 1, next highest priority 2, and so on. Processes in the dom0 should be ioniced as appropriate. This will ensure a reasonable priority, but not allow big domUs to take over the entirety of the I/O bandwidth. is probably to look at CPU allocations and convert them into priority cla.s.ses. Domains with the highest CPU allocation get priority 1, next highest priority 2, and so on. Processes in the dom0 should be ioniced as appropriate. This will ensure a reasonable priority, but not allow big domUs to take over the entirety of the I/O bandwidth.

Backing Up DomUs As a service provider, one rapidly learns that customers don't do their own backups. When a disk fails (not if-when if-when), customers will expect you to have complete backups of their data, and they'll be very sad if you don't. So let's talk about backups.

Of course, you already have a good idea how to back up physical machines. There are two aspects to backing up Xen domains: First, there's the domain's virtual disk, which we want to back up just as we would a real machine's disk. Second, there's the domain's running state, which can be saved and restored from the dom0. Ordinarily, our use of backup backup refers purely to the disk, as it would with physical machines, but with the advantage that we can use domain snapshots to pause the domain long enough to get a clean disk image. refers purely to the disk, as it would with physical machines, but with the advantage that we can use domain snapshots to pause the domain long enough to get a clean disk image.

We use xm save xm save and LVM snapshots to back up both the domain's storage and running state. LVM snapshots aren't a good way of implementing full copy-on-write because they handle the "out of snapshot s.p.a.ce" case poorly, but they're excellent if you want to preserve a filesystem state long enough to make a consistent backup. and LVM snapshots to back up both the domain's storage and running state. LVM snapshots aren't a good way of implementing full copy-on-write because they handle the "out of snapshot s.p.a.ce" case poorly, but they're excellent if you want to preserve a filesystem state long enough to make a consistent backup.

Our implementation copies the entire disk image using either a plain cp cp (in the case of file-backed domUs) or (in the case of file-backed domUs) or dd dd (for (for phy: phy: devices). This is because we very much want to avoid mounting a possibly unclean filesystem in the dom0, which can cause the entire machine to panic. Besides, if we do a raw device backup, domU administrators will be able to use filesystems (such as ZFS on an OpenSolaris domU) that the dom0 cannot read. devices). This is because we very much want to avoid mounting a possibly unclean filesystem in the dom0, which can cause the entire machine to panic. Besides, if we do a raw device backup, domU administrators will be able to use filesystems (such as ZFS on an OpenSolaris domU) that the dom0 cannot read.

An appropriate script to do as we've described might be: #!/usr/bin/perl ,@stores,@files,@lvs;

$domain=$ARGV[0];

my$destdir="/var/backup/xen/${domain}/"; system"mkdir-p$destdir";

open(FILE,"/etc/xen/$domain"); while(){ if(m/^disk/){ s/.*[s+([^]]+)s*].*/1/; @disks=split(/[,]/);

#discardelementswithouta:,sincetheycan'tbe #backingstorespecifiers while($disks[$n]){ $disks[$n]=~s/['"]//g; push(@stores,"$disks[$n]")if("$disks[$n]"=~m/:/); $n++; } $n=0;

#spliton:andtakeonlythelastfieldifthefirst #isarecognizeddevicespecifier.

while($stores[$n]){ @tmp=split(/:/,$stores[$n]); if(($tmp[0]=~m/file/i)

($tmp[0]=~m/tap/i)){ push(@files,$tmp[$#tmp]); } elsif($tmp[0]=~m/phy/i){ push(@lvs,$tmp[$#tmp]); } $n++; } } } closeFILE;

*** You are reading on https://webnovelonline.com ***

Popular Novel