Skip to contentSkip to conversation

domUloader

PATCH In review Kurt Garloff 8 participants 21 messages Jan 16, 2006 – Apr 17, 2006 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

cover domUloader

Hi,

one of the troubles with the way xen boots paravirtualized kernels is that you get the kernel and the initrd from domain 0, whereas modules that are later loaded are on the domU filesystem.

This is a management headache: You must ensure that the kernel and initrd you configure in dom0 for domU booting are in sync with the kernel (modules) in domU.

Jeremy Katz has thankfully created some infrastructure to allow plugging in bootloaders instead and contributed pygrub.

I extended the infrastructure a bit and added another bootloader. Unlike pygrub it does not offer a menu and does not parse the grub menu.lst; it's meant for paravirtualized domains and thus we accept that the booted kernel is selected differently. By e.g. a symlink, if one wants to control it from the domU. The bootloader is called domUloader.

domUloader parses the bootentry (passed via --entry=) and the disk setup (passed via --disks=). It then sets up loop devices as needed, scans for partition tables (the exported disks / loop devs can contain partitions) using kpartx (dm) and sets them up, so the kernel and initrd can be copied to a temporary location in dom0. The bootentry may contain a dev: prefix describing the partition (from a domU perspective!) where kernel and initrd are located, followed by kernel filename and (optional) initrd filenames relative to the filesystem on dev:.
The kernel and initrd filename can also be relative to the domU root filesystem. The domUloader than evaluates /etc/fstab found in the root filesystem (passed via --root=) to locate kernel and initrd. Afterwards everything is cleaned up. (We use the destructors, so python reference counting makes sure this also happens when exceptions occur.)

Unlike pygrub, it does use any code to understand filesystems or partitions; the filesystem support comes from the dom0 kernel, whereas kpartx (from multipath-tools) is used for the knowledge of partitions and for setting up device-mapper.

More details by calling domUloader.py --help.

An example config could look like this:
bootentry = hda2:/vmlinuz-xen,/initrd-xen
bootloader = /path/to/domUloader.py
disks = ['phy:VG_Xen/LV_dom5,hda,w', 'file:/var/lib/xen/test,sda,w']
...
assuming LV_dom5 has a second partition with a filesystem containing vmlinuz-xen and initrd-xen in its root fs (the /boot partition).

or
bootentry = /boot/vmlinuz-xen,/boot/initrd-xen bootloader = /path/to/domUloader.py
root = /dev/hda1
disk = ...
assuming that the root filesystem has an /etc/fstab that points the way to /boot/vmlinuz-xen. (Does not need to be a separate FS.)

The following three mails will contain
(1) A patch to xend/XenDomainInfo.py, xend/XenBootloader.py and xm/create.py, making sure that all the needed info is passed to the bootloader and also stored for reuse on rebooting.
(2) A patch to make pygrub accept the new parameters passed by XendBootloader.py (but so far pygrub just ignores them ...)
(3) The domUloader.py script.

Patches are against a working copy (8259) on my laptop; if noone else will, I can create diffs against mercurial tip.

I hope this is useful to someone and can be integrated into the Xen distribution.

Enjoy,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Hi Kurt,

Quoted 7 lines · Kurt Garloff

domUloader parses the bootentry (passed via --entry=) and the disk setup (passed via --disks=). It then sets up loop devices as needed, scans for partition tables (the exported disks / loop devs can contain partitions) using kpartx (dm) and sets them up, so the kernel and initrd can be copied to a temporary location in dom0.

Just to clarify, this means that domU filesystems are being mounted in dom0? I knew there was some security concerns voiced about this many months ago. I think one of the advantages to using libext2 was that it theoritically allowed the filesystem parsing to be done as a non-privileged user.

Regards,

Anthony Liguori

Quoted 58 lines

The bootentry may contain a dev: prefix describing the partition (from a domU perspective!) where kernel and initrd are located, followed by kernel filename and (optional) initrd filenames relative to the filesystem on dev:.
The kernel and initrd filename can also be relative to the domU root filesystem. The domUloader than evaluates /etc/fstab found in the root filesystem (passed via --root=) to locate kernel and initrd. Afterwards everything is cleaned up. (We use the destructors, so python reference counting makes sure this also happens when exceptions occur.)

Unlike pygrub, it does use any code to understand filesystems or partitions; the filesystem support comes from the dom0 kernel, whereas kpartx (from multipath-tools) is used for the knowledge of partitions and for setting up device-mapper.

More details by calling domUloader.py --help.

An example config could look like this:
bootentry = hda2:/vmlinuz-xen,/initrd-xen
bootloader = /path/to/domUloader.py
disks = ['phy:VG_Xen/LV_dom5,hda,w', 'file:/var/lib/xen/test,sda,w']
...
assuming LV_dom5 has a second partition with a filesystem containing vmlinuz-xen and initrd-xen in its root fs (the /boot partition).

or bootentry = /boot/vmlinuz-xen,/boot/initrd-xen bootloader = /path/to/domUloader.py
root = /dev/hda1
disk = ...
assuming that the root filesystem has an /etc/fstab that points the way to /boot/vmlinuz-xen. (Does not need to be a separate FS.)

The following three mails will contain (1) A patch to xend/XenDomainInfo.py, xend/XenBootloader.py and
xm/create.py, making sure that all the needed info is passed
to the bootloader and also stored for reuse on rebooting.
(2) A patch to make pygrub accept the new parameters passed by
XendBootloader.py (but so far pygrub just ignores them ...)
(3) The domUloader.py script.

Patches are against a working copy (8259) on my laptop; if noone else will, I can create diffs against mercurial tip.

I hope this is useful to someone and can be integrated into the Xen distribution.

Enjoy,

------------------------------------------------------------------------

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw

Hi Anthony,

Quoted 2 lines · Anthony Liguori

Just to clarify, this means that domU filesystems are being mounted in dom0?

Correct.

Quoted 4 lines

I knew there was some security concerns voiced about this many months ago. I think one of the advantages to using libext2 was that it theoritically allowed the filesystem parsing to be done as a non-privileged user.

I can see your point.

There's two concerns you could have:

1. When the domU fs gets mounted in dom0, a local user there could get (read-only) access to data that he shouldn't have access to. This can be prevented by mounting under a directory that's not readable to anyone but root. I didn't do this in my patch set, but it's certainly a good idea.
(And dom0 root you need to trust anyway, such is the trust model
in a hybrid virtualization model without encrypting everything.)

2. The filesystem in the domU could be prepared such that the kernel trips over a bug in its filesystem code.
The same can happen if you read the FS with a userspace library of course, but the effects would be less bad -- at least if you would do it with non-root euid.
The downside is that need to use a secondary source for filesystem code, which needs to be maintained and kept in sync, audited, ... And you are limited to the filesystems where you have userspace libraries for.
In a paranoid scenario, you would not load any data from the domU filesystem in any way :-) But I can see why you would choose pygrub over domUloader in a sensitive environment, where you can't trust the domU admins. Point taken.
I still think that in many use scenarios, you would be perfectly fine with domUloader.

Did I catch your concerns?

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 7 lines · Kurt Garloff

2. The filesystem in the domU could be prepared such that the kernel trips over a bug in its filesystem code.
The same can happen if you read the FS with a userspace library of course, but the effects would be less bad -- at least if you would do it with non-root euid.
The downside is that need to use a secondary source for filesystem code, which needs to be maintained and kept in sync, audited, ...

Quoted 8 lines

And you are limited to the filesystems where you have userspace libraries for.
In a paranoid scenario, you would not load any data from the domU filesystem in any way :-) But I can see why you would choose pygrub over domUloader in a sensitive environment, where you can't trust the domU admins. Point taken.
I still think that in many use scenarios, you would be perfectly fine with domUloader.

Have a special kernel that is used just for this, then boot a temporary domU, using this special kernel, read the data you need from the filesystem, then shut it down.

#4 lore raw

Hi Adam,

Quoted 9 lines · Adam Heath
Quoted 6 lines · Kurt Garloff

In a paranoid scenario, you would not load any data from the domU filesystem in any way :-) But I can see why you would choose pygrub over domUloader in a sensitive environment, where you can't trust the domU admins. Point taken.
I still think that in many use scenarios, you would be perfectly fine with domUloader.

Quoted 3 lines

Have a special kernel that is used just for this, then boot a temporary domU, using this special kernel, read the data you need from the filesystem, then shut it down.

Good solution but quite complex ...

I wonder whether it would be easier porting grub to xen.

For now something simple that just works and is secure enough for 90+% of the users does not look so bad to me.

Best,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#5 lore raw
In reply to Kurt Garloff
Quoted 35 lines · Kurt Garloff

Hi Anthony,

Quoted 3 lines

I knew there was some security concerns voiced about this many months ago. I think one of the advantages to using libext2 was that it theoritically allowed the filesystem parsing to be done as a non-privileged user.

I can see your point.

There's two concerns you could have:

1. When the domU fs gets mounted in dom0, a local user there could get (read-only) access to data that he shouldn't have access to. This can be prevented by mounting under a directory that's not readable to anyone but root. I didn't do this in my patch set, but it's certainly a good idea.
(And dom0 root you need to trust anyway, such is the trust model
in a hybrid virtualization model without encrypting everything.)

2. The filesystem in the domU could be prepared such that the kernel trips over a bug in its filesystem code.
The same can happen if you read the FS with a userspace library of course, but the effects would be less bad -- at least if you would do it with non-root euid.
The downside is that need to use a secondary source for filesystem code, which needs to be maintained and kept in sync, audited, ... And you are limited to the filesystems where you have userspace libraries for.
In a paranoid scenario, you would not load any data from the domU filesystem in any way :-) But I can see why you would choose pygrub over domUloader in a sensitive environment, where you can't trust the domU admins. Point taken.
I still think that in many use scenarios, you would be perfectly fine with domUloader.

Quoted 3 lines

Did I catch your concerns?

Yup, just wanted to make sure it was considered :-)

Regards,

Anthony Liguori

#6 lore raw
In reply to Anthony Liguori
Quoted 7 lines · Anthony Liguori
Quoted 6 lines · Kurt Garloff

domUloader parses the bootentry (passed via --entry=) and the disk setup (passed via --disks=). It then sets up loop devices as needed, scans for partition tables (the exported disks / loop devs can contain partitions) using kpartx (dm) and sets them up, so the kernel and initrd can be copied to a temporary location in dom0.

Quoted 5 lines

Just to clarify, this means that domU filesystems are being mounted in dom0? I knew there was some security concerns voiced about this many months ago. I think one of the advantages to using libext2 was that it theoritically allowed the filesystem parsing to be done as a non-privileged user.

The other concern with mounting is that there have been some cases where changes to filesystems have broken reading new filesystems with older kernels. It's a lot easier to get the library that supports more (and less has to be supported, so you're less likely to need to make changes) than to upgrade your kernel for dom0

Jeremy

#7 lore raw

Hi Jeremy,

Quoted 5 lines · Jeremy Katz

The other concern with mounting is that there have been some cases where changes to filesystems have broken reading new filesystems with older kernels. It's a lot easier to get the library that supports more (and less has to be supported, so you're less likely to need to make changes) than to upgrade your kernel for dom0

I tend to disagree.
As the dom0 kernel drives the hardware and hardware drivers seems to be the more prominent reason for moving to new kernel versions, I would assume the dom0 kernel to be updated more likely than the domU kernels.
And actually, filesystem forward compatibility is not that bad.

I'm not saying this can't be an issue, but I suspect it won't be for most people.

Cheers,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#8 lore raw

On a side note, one thing we all have to think about is how a boot loader would work with something like a virtual framebuffer.

It may be time to start thinking about writing a first class domU bootloader. Something that just sets up a page table that maps the pfns linearly and enough XenBus to read from a virtual disk. We can reuse code from grub for filesystem parsing (or even write it from scratch--it's not that hard to just read from a filesystem).

We could also use mini-OS as a base.

Regards,

Anthony Liguori

Quoted 26 lines · Kurt Garloff

Hi Jeremy,

Quoted 7 lines · Jeremy Katz

The other concern with mounting is that there have been some cases where changes to filesystems have broken reading new filesystems with older kernels. It's a lot easier to get the library that supports more (and less has to be supported, so you're less likely to need to make changes) than to upgrade your kernel for dom0

I tend to disagree.
As the dom0 kernel drives the hardware and hardware drivers seems to be the more prominent reason for moving to new kernel versions, I would assume the dom0 kernel to be updated more likely than the domU kernels.
And actually, filesystem forward compatibility is not that bad.

I'm not saying this can't be an issue, but I suspect it won't be for most people.

Cheers,

#9 lore raw
Quoted 2 lines · Anthony Liguori

On a side note, one thing we all have to think about is how a boot loader would work with something like a virtual framebuffer.

Yeah :/

Quoted 7 lines

It may be time to start thinking about writing a first class domU bootloader. Something that just sets up a page table that maps the pfns linearly and enough XenBus to read from a virtual disk. We can reuse code from grub for filesystem parsing (or even write it from scratch--it's not that hard to just read from a filesystem).

We could also use mini-OS as a base.

The problem is where does something like this end? So we add a basic blkfront. Then someone wants to do some form of netboot. Or boot on iSCSI. Or they use something like GFS or OCFS2 which require significantly more infrastructure than most filesystems. And then, there is a world of pain :/

Unfortunately, I am completely convinced that the right thing is to have the kernel for domU inside the domU's filesystem because anything else is just fundamentally not manageable. So, perhaps we do have to just suck it up and go the path of what's essentially mini-OS as a domU "bios"

Jeremy

#10 lore raw

Hi,

Quoted 3 lines · Jeremy Katz

Unfortunately, I am completely convinced that the right thing is to have the kernel for domU inside the domU's filesystem because anything else is just fundamentally not manageable.

I tend to agree. The real trouble starts when the storage needed to boot that domU isn't even visible to the dom0, though --- perhaps because we've got a virtual HBA (say an iSCSI initiator, or virtual FC HBA), connected to a SAN which is filtering by initiator so that only the domU can see the LUN's contents.

Bootstrapping that sort of environment is nasty.

Quoted 3 lines

So, perhaps we do have to just
suck it up and go the path of what's essentially mini-OS as a domU "bios"

If the domU pre-boot is going to have to have enough smarts to run a full iSCSI initiator then we might be better off just biting the bullet and running a proper kernel there with either kexec, or some manner of domU respawn, to boot the correct kernel/initrd once the pre-boot one has downloaded them. Either that, or we basically need to have special cases for /boot to make sure that those files, plus the grub.conf-type kernel args, are registered elsewhere (directory?) for the dom0 to get them from.

--Stephen

#11 lore raw

Hi,

Quoted 8 lines · Rumor has it that on Fri, Jan 20, 2006 at 03:36:33PM -0500 Stephen Tweedie said:

Hi,

Quoted 3 lines · Jeremy Katz

Unfortunately, I am completely convinced that the right thing is to have the kernel for domU inside the domU's filesystem because anything else is just fundamentally not manageable.

Quoted 8 lines

I tend to agree. The real trouble starts when the storage needed to boot that domU isn't even visible to the dom0, though --- perhaps because we've got a virtual HBA (say an iSCSI initiator, or virtual FC HBA), connected to a SAN which is filtering by initiator so that only the domU can see the LUN's contents.

Bootstrapping that sort of environment is nasty.

Indeed. I agree with the domU filesystem approach as well.

How does one boot off of software iSCSI on a physical machine now? There's clearly no boot ROM. I'm guessing people use PXE. Then it's up to the initrd or initramfs to have the iSCSI smarts.

In that case the domU pre-boot would need to support vbd, v-nics and have a PXE client.

Someone would have to serve the PXE requests and handle the images for that of course. But I think treating it more like a BIOS on a physical machine rather than something that boots in ways that a physical machine doesn't. The more like real machines the VMs look the better.

Of course, that would mean support for root on iSCSI in the installer and mkinitrd code.

Anyway, my 2 cents...

Cheers,

Phil

Quoted 19 lines
Quoted 3 lines

So, perhaps we do have to just
suck it up and go the path of what's essentially mini-OS as a domU "bios"

If the domU pre-boot is going to have to have enough smarts to run a full iSCSI initiator then we might be better off just biting the bullet and running a proper kernel there with either kexec, or some manner of domU respawn, to boot the correct kernel/initrd once the pre-boot one has downloaded them. Either that, or we basically need to have special cases for /boot to make sure that those files, plus the grub.conf-type kernel args, are registered elsewhere (directory?) for the dom0 to get them from.

--Stephen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 3 lines
Philip R. Auld, Ph.D.  	        	       Egenera, Inc.    
Software Architect                            165 Forest St.
(508) 858-2628                            Marlboro, MA 01752
#12 lore raw

Hi,

Quoted 2 lines · Philip R. Auld

Of course, that would mean support for root on iSCSI in the installer and mkinitrd code.

I would really handle iSCSI in dom0 and export sdX/hdX to domU. Solves the nasty OOM problem as well and makes your domains know less about the underlaying storage. Which is good in my vision of virtualization.

Best,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#13 lore raw
Quoted 10 lines · Rumor has it that on Mon, Jan 23, 2006 at 03:19:50PM +0100 Kurt Garloff said:

Hi,

Quoted 2 lines · Philip R. Auld

Of course, that would mean support for root on iSCSI in the installer and mkinitrd code.

I would really handle iSCSI in dom0 and export sdX/hdX to domU. Solves the nasty OOM problem as well and makes your domains know less about the underlaying storage. Which is good in my vision of virtualization.

That's how I would do it too, if I was using iSCSI. Actually that is how I do it with FC, and in fact dom0 doesn't know about it either. I was responding to Stephen's comments about the requirements for a pre-boot image.

Cheers,

Phil

Quoted 4 lines

Best,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.
Signature · 3 lines
Philip R. Auld, Ph.D.  	        	       Egenera, Inc.    
Software Architect                            165 Forest St.
(508) 858-2628                            Marlboro, MA 01752
#14 lore raw
In reply to Kurt Garloff

Hi,

For discussion, attached is a patch to do something similar using the pygrub code. It adds another file syntax to xm create, allowing you to say things like "kernel=guest:(vbda)/boot/vmlinuz", where vbda is what the new domain will see its block device as.

Because it's based on pygrub, it only handles ext2fs and reiser for now. Also, it hasn't got partition-table handling, since that's not working in pygrub either, but it could be added if necessary.

The advantage of this is that the dom0 kernel never needs to read the domU filesystems, so you could run the extraction code without so much worrying.

The patch is against -unstable of last month, but I can update it if people are interested.

Tim.

Signature · 3 lines
Tim Deegan                           (My opinions, not the University's)
Systems Research Group
University of Cambridge Computer Laboratory

# HG changeset patch
# User [email protected]
# Node ID 0cdf19cb2a92a7782dd715ae82cfe9a220c1268c
# Parent 8098cc1daac47f5ac371947a669b9ba15c6cf3f4 Added "guest:(dev)/path/to/file" syntax to xm create filenames Added pyfscat script to pygrub, needed for guest: syntax

tools/pygrub/setup.py +1 -1
diff -r 8098cc1daac4 -r 0cdf19cb2a92 tools/pygrub/setup.py
@@ -43,7 +43,7 @@
       author_email='[email protected]',
       license='GPL',
       package_dir={'grub': 'src'},
-      scripts = ["src/pygrub"],
+      scripts = ["src/pygrub", "src/pyfscat"],
       packages=pkgs,
       ext_modules = fsys_mods
       )
tools/python/xen/xm/create.py +50 -2

81 lines not on this page · raw message

tools/pygrub/src/pyfscat new file +82 -0

82 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw
Quoted 4 lines · Jeremy Katz

Sounds reasonable enough, although I'll have to look at it a little closer when I get back from Austin. FWIW, partition table handling in pygrub should work fine (I'm installing to full disk vbds with partition tables regularly)

The partition handling is only enough to find the "active" partition, so it doesn't handle extended partitions. That's not a problem for pygrub, but would need to be done to have the extraction tool handle partitions properly.

Also, it doesn't work if your e2fsprogs are too old to have ext2fs_open2() -- again, not really a bug but the failure mode is a bit ugly, and the version in the Xen 3 tarball has this problem. Is there some way of telling from inside a python script whether the pygrub library is going to be able to read partitions or not?

Tim.

Signature · 3 lines
Tim Deegan                           (My opinions, not the University's)
Systems Research Group
University of Cambridge Computer Laboratory
#17 lore raw

Hi Tim, Jeremy,

Quoted 10 lines · Tim Deegan
Quoted 4 lines · Jeremy Katz

Sounds reasonable enough, although I'll have to look at it a little closer when I get back from Austin. FWIW, partition table handling in pygrub should work fine (I'm installing to full disk vbds with partition tables regularly)

The partition handling is only enough to find the "active" partition, so it doesn't handle extended partitions. That's not a problem for pygrub, but would need to be done to have the extraction tool handle partitions properly.

pygrub does assume there's one whole disk export which contains a DOS style partition table with the /boot partition marked active. If that one is ext2, everything works fine. (The reiser support failed in my testing.)

domUloader is more flexible there. It understands both whole disk devices and partitions, can handle a set of them, finds the /boot partition by having it specified in bootentry or by parsing /etc/fstab on the partition that's been passed to domU with root=.

Maybe we want to import domUloader in pygrub to just get all this, as the handling is nicely abstracted there. You just don't call domUloader.main(argv) then ...

If we do that, the main difference will be that
- pygrub offers an interactive (ncurses) mode
- pygrub users libraries to get files off the FS, which could be somewhat safer and more easily extensible (but currently is not (yet?) very reliable and limits the FS choice).

And I'd really like to advocate for the little changes I have done to XendDomainInfo, XendBootloader and create, so we pass enough information to the bootloader to handle all this.

Quoted 3 lines

Also, it doesn't work if your e2fsprogs are too old to have ext2fs_open2() -- again, not really a bug but the failure mode is a bit ugly,

So I'm not the only that experiences pygrub running OOM?

Quoted 3 lines

and the version in the Xen 3 tarball has this problem. Is there some way of telling from inside a python script whether the pygrub library is going to be able to read partitions or not?

Updated domUloader attached.

Best,

Signature · 1 line
Kurt Garloff, Head Architect, Director SUSE Labs (act.), Novell Inc.

#!/usr/bin/env python
# domUloader.py
"""Loader for kernel and (optional) ramdisk from domU filesystem

Uses bootentry = [dev:]kernel[,initrd] to get a kernel [and initrd]
from a domU filesystem to boot it for Xen.
dev is the disk as seen by domU, filenames are relative to that filesystem. The script uses the disk settings from the config file to find the domU filesystems.
The bootentry is passed to the script using --entry=
Optionally, dev: can be omitted; the script then looks at the
root filesystem, parses /etc/fstab to resolve the path to the kernel [and the initrd]. Obviously, the paths relative to the domU root filesystem needs to be specified for the kernel and initrd filenames.

The root FS is passed using --root=, the filesystem setup in
--disks=. The disks list is a python list
[[uname, dev, mode, backend], [uname, dev, mode, backend], ...] passed as a string. The script writes an sxpr specifying the locations of the copied kernel and initrd into the file specified by --output (default is stdout).

Limitations:
- It is assumed both kernel and initrd are on the same filesystem.
- domUs might use LVM; the script currently does not have support for setting up LVM mappings for domUs; it's not trivial and we might risk namespace conflicts. If you want to use LVM inside domUs, set up a small non-LVM boot partition and specify it in bootentry.

The script uses kpartx (multipath-tools) to create mappings for devices that are exported as whole disk devices that are partitioned.

(c) 01/2006 Novell Inc
License: GNU GPL
Author: Kurt Garloff <[email protected]>
"""

import os, sys, getopt
from xen.xend import sxp
import tempfile

# Global options
quiet = False
verbose = False
tmpdir = '/var/lib/xen/tmp'

# List of partitions
# It's created by setting up the all the devices from the xen disk # config; every entry creates on Wholedisk object, which does necessary

# preparatory steps such as losetup and kpartx -a; then a Partition
# object is setup for every partition (which may be one or several per
# Wholedisk); it references the Wholedisk if needed; python reference
# counting will take care of the cleanup.
partitions = []

# Helper functions

def traildigits(strg):

	"Return the trailing digits, used to split the partition number off"
	idx = len(strg)-1
	while strg[idx].isdigit():
		if len == 0:
			return strg
		idx -= 1
	return strg[idx+1:]	

def isWholedisk(domUname):
"Determines whether dev is a wholedisk dev" return not domUname[-1:].isdigit()

def freeLoopDev():

	"Finds a free loop device; racy!"
	loops = []
	fd = os.popen("losetup -a")
	for ln in fd.readlines():
		loops.append(ln.split(':')[0])
	for nr in range(0,256):
		if "/dev/loop%i" % nr not in loops:
			return "/dev/loop%i" % nr
	return None

def findPart(dev):

	"Find device dev in list of partitions"
	if len(dev) > 5 and dev[:5] == "/dev/":
		dev = dev[5:]
	for part in partitions:
		if dev == part.domname:
			return part
	return None

class Wholedisk:
"Class representing a whole disk that has partitions"
def __init__(self, domname, physdev, loopfile = None):
"c'tor: set up"
self.domname = domname self.physdev = physdev self.loopfile = loopfile
self.mapped = 0
self.pcount = self.scanpartitions()

def loopsetup(self):
"Setup the loop mapping"
if self.loopfile and not self.physdev:
ldev = freeLoopDev()
if not ldev:
raise RuntimeError("domUloader: No free loop device found")
if verbose:
print "domUloader: losetup %s %s" % (ldev, self.loopfile)
fd = os.popen("losetup %s %s" % (ldev, self.loopfile)) if fd.close():
raise RuntimeError("domUloader: Failure setting up loop dev")
self.physdev = ldev

def loopclean(self):
"Delete the loop mapping"
if self.loopfile and self.physdev:
if verbose:
print "domUloader: losetup -d %s" % self.physdev
fd = os.popen("losetup -d %s" % self.physdev) self.physdev = None
return fd.close()

def scanpartitions(self):
"""Scan device for partitions (kpartx -l) and set up data structures,
Returns number of partitions found."""
self.loopsetup()
if not self.physdev:
raise RuntimeError("domUloader: No physical device? %s" % self.__repr__())
# TODO: We could use fdisk -l instead and look at the type of
# partitions; this way we could also detect LVM and support it. fd = os.popen("kpartx -l %s" % self.physdev)
pcount = 0
for line in fd.readlines():
line = line.strip()
(pname, params) = line.split(':')
pno = int(traildigits(pname.strip()))
#if pname.rfind('/') != -1:
# pname = pname[pname.rfind('/')+1:]
#pname = self.physdev[:self.physdev.rfind('/')] + '/' + pname pname = "/dev/mapper/" + pname partitions.append(Partition(self, self.domname + '%i' % pno, pname)) pcount += 1
fd.close()
if not pcount:
if self.loopfile:
ref = self
else:
ref = None
partitions.append(Partition(ref, self.domname, self.physdev))
self.loopclean()
return pcount

def activatepartitions(self):
"Set up loop mapping and device-mapper mappings"
if not self.mapped:
self.loopsetup()
if self.pcount:
if verbose:
print "domUloader: kpartx -a %s" % self.physdev
fd = os.popen("kpartx -a %s" % self.physdev) fd.close()
self.mapped += 1

def deactivatepartitions(self):
"Remove device-mapper mappings and loop mapping"
if not self.mapped:
return
self.mapped -= 1
if not self.mapped:
if self.pcount:
if verbose:
print "domUloader: kpartx -d %s" % self.physdev
fd = os.popen("kpartx -d %s" % self.physdev) fd.close()
self.loopclean()

def __del__(self):
"d'tor: clean up"
self.deactivatepartitions()
self.loopclean()

def __repr__(self):
"string representation for debugging"
strg = "[" + self.domname + "," + self.physdev + "," if self.loopfile:
strg += self.loopfile
strg += "," + str(self.pcount) + ",mapped %ix]" % self.mapped return strg

class Partition:
"""Class representing a domU filesystem (partition) that can be
mounted in dom0"""
def __init__(self, whole = None, domname = None,
physdev = None):
"c'tor: setup"
self.wholedisk = whole
self.domname = domname
self.physdev = physdev
self.mountpoint = None

def __del__(self):
"d'tor: cleanup"
if self.mountpoint:
self.umount()
# Not needed: Refcounting will take care of it. #if self.wholedisk:
# self.wholedisk.deactivatepartitions()

def __repr__(self):
"string representation for debugging"
strg = "[" + self.domname + "," + self.physdev + "," if self.mountpoint:
strg += "mounted on " + self.mountpoint + ","
else:
strg += "not mounted,"
if self.wholedisk:
return strg + self.wholedisk.__repr__() + "]"
else:
return strg + "]"

def mount(self, fstype = None, options = "ro"):
"mount filesystem, sets self.mountpoint"
if self.mountpoint:
return
if self.wholedisk:
self.wholedisk.activatepartitions()
mtpt = tempfile.mkdtemp(prefix = "%s." % self.domname, dir = tmpdir) mopts = ""
if fstype:
mopts += " -t %s" % fstype
mopts += " -o %s" % options
if verbose:
print "domUloader: mount %s %s %s" % (mopts, self.physdev, mtpt)
fd = os.popen("mount %s %s %s" % (mopts, self.physdev, mtpt)) err = fd.close()
if err:
raise RuntimeError("domUloader: Error %i from mount %s %s on %s" % \
(err, mopts, self.physdev, mtpt))
self.mountpoint = mtpt

def umount(self):
"umount filesystem at self.mountpoint"
if not self.mountpoint:
return
if verbose:
print "domUloader: umount %s" % self.mountpoint
fd = os.popen("umount %s" % self.mountpoint) err = fd.close()
os.rmdir(self.mountpoint)
if err:
raise RuntimeError("Error %i from umount %s" % \
(err, self.mountpoint))
self.mountpoint = None
if self.wholedisk:
self.wholedisk.deactivatepartitions()

def setupOneDisk(cfg):
"""Sets up one exported disk (incl. partitions if existing)
@param cfg: 4-tuple (uname, dev, mode, backend)"""
from xen.util.blkif import blkdev_uname_to_file (type, dev) = cfg[0].split(':')
(loopfile, physdev) = (None, None)
if type == "file":
loopfile = dev
elif type == "phy":
physdev = blkdev_uname_to_file(cfg[0])
wdisk = Wholedisk(cfg[1], physdev, loopfile)

def setupDisks(vbds):
"""Create a list of all disks from the disk config:
@param vbds: The disk config as list of 4-tuples
(uname, dev, mode, backend)"""
disks = eval(eval(vbds))
for disk in disks:
setupOneDisk(disk)
if verbose:
print "Partitions: " + str(partitions)

class Fstab:
"Class representing an fstab"

class FstabEntry:
"Class representing one fstab line"
def __init__(self, line):
"c'tor: parses one line"
spline = line.split()
self.dev, self.mtpt, self.fstype, self.opts = \
spline[0], spline[1], spline[2], spline[3]
if len(self.mtpt) > 1:
self.mtpt = self.mtpt.rstrip('/')

def __init__(self, filename):
"c'tor: parses fstab"
self.entries = []
fd = open(filename)
for line in fd.readlines():
line = line.strip()
if len(line) == 0 or line[0] == '#':
continue
self.entries.append(Fstab.FstabEntry(line))

def find(self, fname):
"Looks for matching filesystem in fstab"
matchlen = 0
match = None
fnmlst = fname.split('/')
for fs in self.entries:
entlst = fs.mtpt.split('/')
# '/' needs special treatment :-(
if entlst == ['','']:
entlst = ['']
entln = len(entlst)
if len(fnmlst) >= entln and fnmlst[:entln] == entlst \
and entln > matchlen:
match = fs
matchlen = entln
if not match:
return (None, None)
return (match.dev, match.mtpt)

def fsFromFstab(kernel, initrd, root):
"""Investigate rootFS fstab, check for filesystem that contains the kernel
and return it; also returns adapted kernel and initrd path.
"""
part = findPart(root)
if not part:
raise RuntimeError("domUloader: Root fs %s not exported?" % root)
part.mount()
if not os.access(part.mountpoint + '/etc/fstab', os.R_OK):
part.umount()
raise RuntimeError("domUloader: /etc/fstab not found on %s" % root)
fstab = Fstab(part.mountpoint + '/etc/fstab')
(dev, fs) = fstab.find(kernel)
if not fs:
raise RuntimeError("domUloader: no matching filesystem for image %s found in fstab" % kernel) #return (None, kernel, initrd)
if fs == '/':
ln = 0
# this avoids the stupid /dev/root problem
dev = root
else:
ln = len(fs)
kernel = kernel[ln:]
if initrd:
initrd = initrd[ln:]
if verbose:
print "fsFromFstab: %s %s -- %s,%s" % (dev, fs, kernel, initrd)
return (kernel, initrd, dev)

def parseEntry(entry):
"disects bootentry and returns kernel, initrd, filesys" fs = None
initrd = None
fsspl = entry.split(':')
if len(fsspl) > 1:
fs = fsspl[0]
entry = fsspl[1]
enspl = entry.split(',')
# Prepend '/' if missing
kernel = enspl[0]
if kernel[0] != '/':
kernel = '/' + kernel
if len(enspl) > 1:
initrd = enspl[1]
if initrd[0] != '/':
initrd = '/' + initrd
return kernel, initrd, fs

def copyFile(src, dst):

	"Wrapper for shutil.filecopy"
	import shutil
	if verbose:
		print "domUloader: cp %s %s" % (src, dst)
	stat = os.stat(src)
	if stat.st_size > 16*1024*1024:
		raise RuntimeError("Too large file %s (%s larger than 16MB)" \
			% (src, stat.st_size)) 
	try:
		shutil.copyfile(src, dst)
	except:
		os.unlink(dst)
		raise()

def copyKernelAndInitrd(fs, kernel, initrd):
"""Finds fs in list of partitions, mounts the partition, copies
kernel [and initrd] off to dom0 files, umounts the parition again, and returns sxpr pointing to these copies."""
import shutil
part = findPart(fs)
if not part:
raise RuntimeError("domUloader: Filesystem %s not exported\n" % fs)
part.mount()
try:
(fd, knm) = tempfile.mkstemp(prefix = "vmlinuz.", dir = tmpdir) os.close(fd)
copyFile(part.mountpoint + kernel, knm)
except:
os.unlink(knm)
raise
if not quiet:
print "Copy kernel %s from %s to %s for booting" % \
(kernel, fs, knm)
sxpr = "linux (kernel %s)" % knm
if (initrd):
try:
(fd, inm) = tempfile.mkstemp(prefix = "initrd.", dir = tmpdir) os.close(fd)
copyFile(part.mountpoint + initrd, inm)
except:
os.unlink(knm)
os.unlink(inm)
raise
sxpr += "(ramdisk %s)" % inm
part.umount()
return sxpr

def main(argv):
"Main routine: Parses options etc."
global quiet, verbose, tmpdir
def usage():
"Help output (usage info)"
global verbose, quiet
print >> sys.stderr, "domUloader usage: domUloader --disks=disklist [--root=rootFS]\n" \
+ " --entry=kernel[,initrd] [--output=fd] [--quiet] [--verbose] [--help]\n"
print >> sys.stderr, __doc__

#print "domUloader " + str(argv)
try:
(optlist, args) = getopt.gnu_getopt(argv, 'qvh', \
('disks=', 'root=', 'entry=', 'output=',
'tmpdir=', 'help', 'quiet', 'verbose'))
except:
usage()
sys.exit(1)

entry = None
output = None
root = None
disks = None

for (opt, oarg) in optlist:
if opt in ('-h', '--help'):
usage()
sys.exit(0)
elif opt in ('-q', '--quiet'):
quiet = True
elif opt in ('-v', '--verbose'):
verbose = True
elif opt == '--root':
root = oarg
elif opt == '--output':
output = oarg
elif opt == '--disks':
disks = oarg
elif opt == '--entry':
entry = oarg
elif opt == '--tmpdir':
tmpdir = oarg

if not entry or not disks:
usage()
sys.exit(1)

if output is None or output == "-":
fd = sys.stdout.fileno()
else:
fd = os.open(output, os.O_WRONLY)

if not os.access(tmpdir, os.X_OK):
os.mkdir(tmpdir)
os.chmod(tmpdir, 0750)

# We assume kernel and initrd are on the same FS,
# so only one fs
kernel, initrd, fs = parseEntry(entry)
setupDisks(disks)
if not fs:
if not root:
usage()
raise RuntimeError("domUloader: No root= to parse fstab and no disk in bootentry") sys.exit(1)
kernel, initrd, fs = fsFromFstab(kernel, initrd, root)

sxpr = copyKernelAndInitrd(fs, kernel, initrd) sys.stdout.flush()
os.write(fd, sxpr)

# Call main if called (and not imported)
if __name__ == "__main__":
main(sys.argv)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#18 lore raw
In reply to Tim Deegan
Quoted 4 lines · Tim Deegan

The partition handling is only enough to find the "active" partition, so it doesn't handle extended partitions. That's not a problem for pygrub, but would need to be done to have the extraction tool handle partitions properly.

A new version of my patch is attached that understands DOS partition tables (so long as your libext2fs libraries are up to date.)

I do prefer Kurt's changes to the xm create "bootloader" syntax. They are cleaner than what I've done with pyfscat. I'd like to do a merge of the pygrub extraction code with his bootloader structure, but haven't time to do it properly at the moment.

Tim.

Signature · 3 lines
Tim Deegan                           (My opinions, not the University's)
Systems Research Group
University of Cambridge Computer Laboratory

# HG changeset patch
# User [email protected]
# Node ID e35449fc66de226857fbf7946b2cccadbff23dd7
# Parent c4ae9456a4595f046f08aea2f2e7b3664b50ab82 Added "guest:(dev)/path/to/file" syntax to xm create filenames Added pyfscat script to pygrub, needed for guest: syntax

tools/pygrub/setup.py +1 -1
diff -r c4ae9456a459 -r e35449fc66de tools/pygrub/setup.py
@@ -43,7 +43,7 @@
       author_email='[email protected]',
       license='GPL',
       package_dir={'grub': 'src'},
-      scripts = ["src/pygrub"],
+      scripts = ["src/pygrub", "src/pyfscat"],
       packages=pkgs,
       ext_modules = fsys_mods
       )
tools/python/xen/xm/create.py +56 -2

87 lines not on this page · raw message

tools/pygrub/src/pyfscat new file +169 -0

169 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#19 lore raw
In reply to Kurt Garloff
Quoted 8 lines · Kurt Garloff

I extended the infrastructure a bit and added another bootloader. Unlike pygrub it does not offer a menu and does not parse the grub menu.lst; it's meant for paravirtualized domains and thus we accept that the booted kernel is selected differently. By e.g. a symlink, if one wants to control it from the domU. The bootloader is called domUloader.

Is there a project page for this our tarball / RPM out there that includes domUloader and it's latest updates?

#20 lore raw

Hi Matt,

Quoted 11 lines · Matt Ayres
Quoted 6 lines · Kurt Garloff

I extended the infrastructure a bit and added another bootloader. Unlike pygrub it does not offer a menu and does not parse the grub menu.lst; it's meant for paravirtualized domains and thus we accept that the booted kernel is selected differently. By e.g. a symlink, if one wants to control it from the domU. The bootloader is called domUloader.

Is there a project page for this our tarball / RPM out there that includes domUloader and it's latest updates?

I was hoping to see it merged right away, so I did not set up a project page. Seems I have to do it :-(

The Novell/SUSE RPMs include the domUloader functionality. You can find latest version in our current betas or on
http://forge.novell.com/modules/xfmod/project/?xenpreview

Best,

Signature · 1 line
Kurt Garloff, Head Architect Linux, Novell Inc.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#21 lore raw
Quoted 11 lines · Kurt Garloff

Hi Matt,

Quoted 8 lines · Matt Ayres
Quoted 6 lines · Kurt Garloff

I extended the infrastructure a bit and added another bootloader. Unlike pygrub it does not offer a menu and does not parse the grub menu.lst; it's meant for paravirtualized domains and thus we accept that the booted kernel is selected differently. By e.g. a symlink, if one wants to control it from the domU. The bootloader is called domUloader.

Quoted 9 lines
Quoted 1 line

Is there a project page for this our tarball / RPM out there that includes domUloader and it's latest updates?

I was hoping to see it merged right away, so I did not set up a project page. Seems I have to do it :-(

The Novell/SUSE RPMs include the domUloader functionality. You can find latest version in our current betas or on
http://forge.novell.com/modules/xfmod/project/?xenpreview

Hi Kurt,

Are there any plans to re-diff domUloader against 3.0.2 and post to this list? I've been having quite a difficult time to get your original patches to apply.

Thanks,
Matt