ia64/xen-unstable

view tools/python/xen/xend/XendBootloader.py @ 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents 509285182efb
children 245f7ce8763e
line source
1 #
2 # XendBootloader.py - Framework to run a boot loader for picking the kernel
3 #
4 # Copyright 2005-2006 Red Hat, Inc.
5 # Jeremy Katz <katzj@redhat.com>
6 #
7 # This software may be freely redistributed under the terms of the GNU
8 # general public license.
9 #
10 # You should have received a copy of the GNU General Public License
11 # along with this program; if not, write to the Free Software
12 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
13 #
15 import os, select, errno
16 import random
17 import shlex
18 from xen.xend import sxp
20 from XendLogging import log
21 from XendError import VmError
23 def bootloader(blexec, disk, quiet = 0, blargs = None, imgcfg = None):
24 """Run the boot loader executable on the given disk and return a
25 config image.
26 @param blexec Binary to use as the boot loader
27 @param disk Disk to run the boot loader on.
28 @param quiet Run in non-interactive mode, just booting the default.
29 @param blargs Arguments to pass to the bootloader."""
31 if not os.access(blexec, os.X_OK):
32 msg = "Bootloader isn't executable"
33 log.error(msg)
34 raise VmError(msg)
35 if not os.access(disk, os.R_OK):
36 msg = "Disk isn't accessible"
37 log.error(msg)
38 raise VmError(msg)
40 while True:
41 fifo = "/var/lib/xen/xenbl.%s" % random.randint(0, 32000)
42 if not os.path.exists(fifo):
43 break
44 os.mkfifo(fifo, 0600)
46 child = os.fork()
47 if (not child):
48 args = [ blexec ]
49 if quiet:
50 args.append("-q")
51 args.append("--output=%s" % fifo)
52 if blargs is not None:
53 args.extend(shlex.split(blargs))
54 args.append(disk)
56 try:
57 os.execvp(args[0], args)
58 except OSError, e:
59 print e
60 pass
61 os._exit(1)
63 while 1:
64 try:
65 r = os.open(fifo, os.O_RDONLY)
66 except OSError, e:
67 if e.errno == errno.EINTR:
68 continue
69 break
70 ret = ""
71 while 1:
72 select.select([r], [], [])
73 s = os.read(r, 1024)
74 ret = ret + s
75 if len(s) == 0:
76 break
78 os.waitpid(child, 0)
79 os.close(r)
80 os.unlink(fifo)
82 if len(ret) == 0:
83 msg = "Boot loader didn't return any data!"
84 log.error(msg)
85 raise VmError, msg
87 pin = sxp.Parser()
88 pin.input(ret)
89 pin.input_eof()
90 blcfg = pin.val
92 if imgcfg is None:
93 return blcfg
94 else:
95 c = sxp.merge(blcfg, imgcfg)
96 return c