ia64/xen-unstable

view tools/misc/xend @ 16359:0ebac8576495

xend: Allow for non-existent xen-python-path binary.
On Solaris, xen-python-path is unnecessary.

Signed-off-by: John Levon <john.levon@sun.com>
author Keir Fraser <keir@xensource.com>
date Thu Nov 08 15:32:14 2007 +0000 (2007-11-08)
parents 468a30d74bd6
children 3e3b0418905f
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
3 #============================================================================
4 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
5 # Copyright (C) 2005-2006 XenSource Inc
6 #============================================================================
8 """Xen management daemon.
9 Provides console server and HTTP management api.
11 Run:
12 xend start
14 Restart:
15 xend restart
17 The daemon is stopped with:
18 xend stop
20 The daemon should reconnect to device control interfaces
21 and recover its state when restarted.
23 On Solaris, the daemons are SMF managed, and you should not attempt
24 to start xend by hand.
25 """
26 import fcntl
27 import glob
28 import os
29 import os.path
30 import sys
31 import socket
32 import signal
33 import time
34 import commands
36 xpp = os.path.join(os.path.dirname(sys.argv[0], 'xen-python-path'))
37 if os.path.exists(xpp):
38 result = commands.getstatusoutput(xpp)
39 if result[0] != 0:
40 print >>sys.stderr, result[1]
41 sys.exit(1)
43 sys.path.append(result[1])
45 from xen.xend.server import SrvDaemon
47 class CheckError(ValueError):
48 pass
50 def hline():
51 print >>sys.stderr, "*" * 70
53 def msg(message):
54 print >>sys.stderr, "*" * 3, message
56 def check_logging():
57 """Check python logging is installed and raise an error if not.
58 Logging is standard from Python 2.3 on.
59 """
60 try:
61 import logging
62 except ImportError:
63 hline()
64 msg("Python logging is not installed.")
65 msg("Use 'make install-logging' at the xen root to install.")
66 msg("")
67 msg("Alternatively download and install from")
68 msg("http://www.red-dove.com/python_logging.html")
69 hline()
70 raise CheckError("logging is not installed")
72 def check_user():
73 """Check that the effective user id is 0 (root).
74 """
75 if os.geteuid() != 0:
76 hline()
77 msg("Xend must be run as root.")
78 hline()
79 raise CheckError("invalid user")
81 def start_xenstored():
82 pidfname = "/var/run/xenstore.pid"
83 try:
84 f = open(pidfname, "a")
85 try:
86 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
87 rootdir = os.getenv("XENSTORED_ROOTDIR") or "/var/lib/xenstored"
88 for i in glob.glob(rootdir + "/tdb*"):
89 try:
90 os.unlink(i)
91 except:
92 pass
93 os.unlink(pidfname)
94 except:
95 pass
96 f.close()
97 except:
98 pass
99 XENSTORED_TRACE = os.getenv("XENSTORED_TRACE")
100 cmd = "xenstored --pid-file /var/run/xenstore.pid"
101 if XENSTORED_TRACE:
102 cmd += " -T /var/log/xen/xenstored-trace.log"
103 s,o = commands.getstatusoutput(cmd)
105 def start_consoled():
106 if os.fork() == 0:
107 os.execvp('xenconsoled', ['xenconsoled'])
109 def start_blktapctrl():
110 if os.fork() == 0:
111 os.execvp('blktapctrl', ['blktapctrl'])
113 def main():
114 try:
115 check_logging()
116 check_user()
117 except CheckError:
118 sys.exit(1)
120 daemon = SrvDaemon.instance()
121 if not sys.argv[1:]:
122 print 'usage: %s {start|stop|reload|restart}' % sys.argv[0]
123 elif sys.argv[1] == 'start':
124 if os.uname()[0] != "SunOS":
125 start_xenstored()
126 start_consoled()
127 start_blktapctrl()
128 return daemon.start()
129 elif sys.argv[1] == 'trace_start':
130 start_xenstored()
131 start_consoled()
132 start_blktapctrl()
133 return daemon.start(trace=1)
134 elif sys.argv[1] == 'stop':
135 return daemon.stop()
136 elif sys.argv[1] == 'reload':
137 return daemon.reloadConfig()
138 elif sys.argv[1] == 'restart':
139 start_xenstored()
140 start_consoled()
141 start_blktapctrl()
142 return daemon.stop() or daemon.start()
143 elif sys.argv[1] == 'status':
144 return daemon.status()
145 else:
146 print 'not an option:', sys.argv[1]
147 return 1
149 if __name__ == '__main__':
150 sys.exit(main())