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 | #============================================================================ |
---|
7 | |
---|
8 | """Xen management daemon. |
---|
9 | Provides console server and HTTP management api. |
---|
10 | |
---|
11 | Run: |
---|
12 | xend start |
---|
13 | |
---|
14 | Restart: |
---|
15 | xend restart |
---|
16 | |
---|
17 | The daemon is stopped with: |
---|
18 | xend stop |
---|
19 | |
---|
20 | The daemon should reconnect to device control interfaces |
---|
21 | and recover its state when restarted. |
---|
22 | |
---|
23 | On Solaris, the daemons are SMF managed, and you should not attempt |
---|
24 | to start xend by hand. |
---|
25 | """ |
---|
26 | import os |
---|
27 | import os.path |
---|
28 | import sys |
---|
29 | import socket |
---|
30 | import signal |
---|
31 | import time |
---|
32 | import commands |
---|
33 | |
---|
34 | result = commands.getstatusoutput(os.path.join(os.path.dirname(sys.argv[0]), |
---|
35 | 'xen-python-path')) |
---|
36 | if result[0] != 0: |
---|
37 | print >>sys.stderr, result[1] |
---|
38 | sys.exit(1) |
---|
39 | |
---|
40 | sys.path.append(result[1]) |
---|
41 | |
---|
42 | from xen.xend.server import SrvDaemon |
---|
43 | |
---|
44 | class CheckError(ValueError): |
---|
45 | pass |
---|
46 | |
---|
47 | def hline(): |
---|
48 | print >>sys.stderr, "*" * 70 |
---|
49 | |
---|
50 | def msg(message): |
---|
51 | print >>sys.stderr, "*" * 3, message |
---|
52 | |
---|
53 | def check_logging(): |
---|
54 | """Check python logging is installed and raise an error if not. |
---|
55 | Logging is standard from Python 2.3 on. |
---|
56 | """ |
---|
57 | try: |
---|
58 | import logging |
---|
59 | except ImportError: |
---|
60 | hline() |
---|
61 | msg("Python logging is not installed.") |
---|
62 | msg("Use 'make install-logging' at the xen root to install.") |
---|
63 | msg("") |
---|
64 | msg("Alternatively download and install from") |
---|
65 | msg("http://www.red-dove.com/python_logging.html") |
---|
66 | hline() |
---|
67 | raise CheckError("logging is not installed") |
---|
68 | |
---|
69 | def check_user(): |
---|
70 | """Check that the effective user id is 0 (root). |
---|
71 | """ |
---|
72 | if os.geteuid() != 0: |
---|
73 | hline() |
---|
74 | msg("Xend must be run as root.") |
---|
75 | hline() |
---|
76 | raise CheckError("invalid user") |
---|
77 | |
---|
78 | def start_xenstored(): |
---|
79 | XENSTORED_TRACE = os.getenv("XENSTORED_TRACE") |
---|
80 | cmd = "xenstored --pid-file /var/run/xenstore.pid" |
---|
81 | if XENSTORED_TRACE: |
---|
82 | cmd += " -T /var/log/xen/xenstored-trace.log" |
---|
83 | s,o = commands.getstatusoutput(cmd) |
---|
84 | |
---|
85 | def start_consoled(): |
---|
86 | if os.fork() == 0: |
---|
87 | os.execvp('xenconsoled', ['xenconsoled']) |
---|
88 | |
---|
89 | def start_blktapctrl(): |
---|
90 | if os.fork() == 0: |
---|
91 | os.execvp('blktapctrl', ['blktapctrl']) |
---|
92 | |
---|
93 | def main(): |
---|
94 | try: |
---|
95 | check_logging() |
---|
96 | check_user() |
---|
97 | except CheckError: |
---|
98 | sys.exit(1) |
---|
99 | |
---|
100 | daemon = SrvDaemon.instance() |
---|
101 | if not sys.argv[1:]: |
---|
102 | print 'usage: %s {start|stop|reload|restart}' % sys.argv[0] |
---|
103 | elif sys.argv[1] == 'start': |
---|
104 | if os.uname()[0] != "SunOS": |
---|
105 | start_xenstored() |
---|
106 | start_consoled() |
---|
107 | start_blktapctrl() |
---|
108 | return daemon.start() |
---|
109 | elif sys.argv[1] == 'trace_start': |
---|
110 | start_xenstored() |
---|
111 | start_consoled() |
---|
112 | start_blktapctrl() |
---|
113 | return daemon.start(trace=1) |
---|
114 | elif sys.argv[1] == 'stop': |
---|
115 | return daemon.stop() |
---|
116 | elif sys.argv[1] == 'reload': |
---|
117 | return daemon.reloadConfig() |
---|
118 | elif sys.argv[1] == 'restart': |
---|
119 | start_xenstored() |
---|
120 | start_consoled() |
---|
121 | start_blktapctrl() |
---|
122 | return daemon.stop() or daemon.start() |
---|
123 | elif sys.argv[1] == 'status': |
---|
124 | return daemon.status() |
---|
125 | else: |
---|
126 | print 'not an option:', sys.argv[1] |
---|
127 | return 1 |
---|
128 | |
---|
129 | if __name__ == '__main__': |
---|
130 | sys.exit(main()) |
---|