ia64/xen-unstable

view tools/misc/xend @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
3 #============================================================================
4 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
5 #============================================================================
7 """Xen management daemon. Lives in /usr/sbin.
8 Provides console server and HTTP management api.
10 Run:
11 xend start
13 Restart:
14 xend restart
16 The daemon is stopped with:
17 xend stop
19 The daemon should reconnect to device control interfaces
20 and recover its state when restarted.
21 """
22 import os
23 import sys
24 import socket
25 import signal
26 import time
27 import commands
29 XCS_PATH = "/var/lib/xen/xcs_socket"
30 XCS_EXEC = "/usr/sbin/xcs"
31 XCS_PIDFILE = "/var/run/xcs.pid"
32 XCS_ARGS = (XCS_EXEC, "-p", XCS_PIDFILE)
34 # add fallback path for non-native python path installs if needed
35 sys.path.append('/usr/lib/python')
36 sys.path.append('/usr/lib64/python')
37 from xen.xend.server import SrvDaemon
39 class CheckError(ValueError):
40 pass
42 def hline():
43 print >>sys.stderr, "*" * 70
45 def msg(message):
46 print >>sys.stderr, "*" * 3, message
48 def check_logging():
49 """Check python logging is installed and raise an error if not.
50 Logging is standard from Python 2.3 on.
51 """
52 try:
53 import logging
54 except ImportError:
55 hline()
56 msg("Python logging is not installed.")
57 msg("Use 'make install-logging' at the xen root to install.")
58 msg("")
59 msg("Alternatively download and install from")
60 msg("http://www.red-dove.com/python_logging.html")
61 hline()
62 raise CheckError("logging is not installed")
64 def check_user():
65 """Check that the effective user id is 0 (root).
66 """
67 if os.geteuid() != 0:
68 hline()
69 msg("Xend must be run as root.")
70 hline()
71 raise CheckError("invalid user")
73 def xcs_running():
74 """ See if the control switch is running.
75 """
76 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
77 try:
78 s.connect( (XCS_PATH) )
79 s.close()
80 except:
81 try:
82 os.remove(XCS_PIDFILE)
83 except:
84 pass
85 return 0
86 return 1
88 def start_xcs():
89 if (not xcs_running()):
90 if os.fork() == 0 :
91 if not os.path.isdir(os.path.dirname(XCS_PATH)):
92 os.makedirs(os.path.dirname(XCS_PATH))
93 try:
94 os.execvp(XCS_EXEC, XCS_ARGS)
95 except:
96 hline()
97 msg("Tried to start xcs, but failed. Is it installed?")
98 hline()
99 raise CheckError("couldn't start xcs")
100 for n in range(10) :
101 if (xcs_running()):
102 break
103 time.sleep(0.1)
104 else :
105 hline()
106 msg("Failed to start the control interface switch.")
107 hline()
108 raise CheckError("xcs not running")
110 def stop_xcs():
111 try:
112 xcs_pidfile = open(XCS_PIDFILE)
113 xcs_pid = int(xcs_pidfile.read().strip())
114 os.kill(xcs_pid, signal.SIGTERM)
115 xcs_pidfile.close()
116 except:
117 return
119 def start_xenstored():
120 XENSTORED_TRACE = os.getenv("XENSTORED_TRACE")
121 cmd = "/usr/sbin/xenstored --pid-file=/var/run/xenstore.pid"
122 if XENSTORED_TRACE:
123 cmd += " -T /var/log/xenstored-trace.log"
124 s,o = commands.getstatusoutput(cmd)
126 def start_consoled():
127 if os.fork() == 0:
128 os.execvp('/usr/sbin/xenconsoled', ['/usr/sbin/xenconsoled'])
130 def main():
131 try:
132 check_logging()
133 check_user()
134 except CheckError:
135 sys.exit(1)
137 daemon = SrvDaemon.instance()
138 if not sys.argv[1:]:
139 print 'usage: %s {start|stop|restart}' % sys.argv[0]
140 elif os.fork():
141 pid, status = os.wait()
142 return status >> 8
143 elif sys.argv[1] == 'start':
144 start_xcs()
145 start_xenstored()
146 start_consoled()
147 return daemon.start()
148 elif sys.argv[1] == 'trace_start':
149 start_xcs()
150 start_xenstored()
151 start_consoled()
152 return daemon.start(trace=1)
153 elif sys.argv[1] == 'stop':
154 stop_xcs()
155 return daemon.stop()
156 elif sys.argv[1] == 'restart':
157 stop_xcs()
158 start_xcs()
159 start_xenstored()
160 start_consoled()
161 return daemon.stop() or daemon.start()
162 elif sys.argv[1] == 'status':
163 return daemon.status()
164 else:
165 print 'not an option:', sys.argv[1]
166 return 1
168 if __name__ == '__main__':
169 sys.exit(main())