|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright (c) Citrix Systems 2007-2011 |
| 4 | +# Author: Gianni Tedesco and Dave Scott |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation; version 2.1 only. with the special |
| 9 | +# exception on linking described in file LICENSE. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | + |
| 16 | +from xen.lowlevel import xs |
| 17 | + |
| 18 | +def do_cloexec(fd): |
| 19 | + from fcntl import fcntl, F_GETFD, F_SETFD, FD_CLOEXEC |
| 20 | + |
| 21 | + ret = fcntl(fd, F_GETFD, FD_CLOEXEC) |
| 22 | + if ret < 0: |
| 23 | + return |
| 24 | + fcntl(fd, F_SETFD, ret|FD_CLOEXEC) |
| 25 | + |
| 26 | +def write_dm_pid(domid, pid): |
| 27 | + store = xs.xs() |
| 28 | + store.write('', '/local/domain/%d/qemu-pid'%domid, '%d'%pid) |
| 29 | + |
| 30 | +def is_sdk(): |
| 31 | + try: |
| 32 | + store = xs.xs(socket=True) |
| 33 | + domid_as_dom0 = int(store.read('', 'domid')) |
| 34 | + store.close() |
| 35 | + assert domid_as_dom0 == 0 |
| 36 | + store = xs.xs(xenbus=True) |
| 37 | + domid_on_host = int(store.read('', 'domid')) |
| 38 | + store.close() |
| 39 | + return domid_on_host != domid_as_dom0 |
| 40 | + except: |
| 41 | + return False |
| 42 | + |
| 43 | +def fake_dm(domid): |
| 44 | + from time import sleep |
| 45 | + from os import getpid |
| 46 | + |
| 47 | + store = xs.xs() |
| 48 | + |
| 49 | + store.write('', '/local/domain/%d/qemu-pid'%domid, '%d'%getpid()) |
| 50 | + store.write('', '/local/domain/%d/device-misc/dm-ready'%domid, '1') |
| 51 | + store.write('', '/local/domain/%d/console/vnc-port'%domid, '%d'%(domid)) |
| 52 | + |
| 53 | + while True: |
| 54 | + sleep(600) |
| 55 | + |
| 56 | + return 0 |
| 57 | + |
| 58 | +def cleanup(domid): |
| 59 | + from xen.lowlevel import xc |
| 60 | + hcall = xc.xc() |
| 61 | + |
| 62 | + print 'Unexpected termination, cleaning up...' |
| 63 | + |
| 64 | + try: |
| 65 | + hcall.domain_destroy(domid) |
| 66 | + except xc.Error: |
| 67 | + # we could have raced with another domain shutdown |
| 68 | + pass |
| 69 | + |
| 70 | +def enable_core_dumps(): |
| 71 | + from resource import getrlimit, RLIMIT_CORE, setrlimit |
| 72 | + |
| 73 | + limit = 64 * 1024 * 1024 |
| 74 | + oldlimits = getrlimit(RLIMIT_CORE) |
| 75 | + setrlimit(RLIMIT_CORE, (limit, oldlimits[1])) |
| 76 | + return limit |
| 77 | + |
| 78 | +def main(argv): |
| 79 | + import os |
| 80 | + |
| 81 | + qemu_env = os.environ |
| 82 | + qemu_dm = '/usr/lib/xen/bin/qemu-dm' |
| 83 | + domid = int(argv[1]) |
| 84 | + qemu_args = ['qemu-dm-%d'%domid] + argv[2:] |
| 85 | + try: |
| 86 | + vnc_idx = qemu_args.index("-vnc") |
| 87 | + del qemu_args[vnc_idx: vnc_idx+2] |
| 88 | + qemu_args.insert(vnc_idx, "0.0.0.0:1") |
| 89 | + qemu_args.insert(vnc_idx, "-vnc") |
| 90 | + except ValueError, IndexError: |
| 91 | + pass |
| 92 | + |
| 93 | + if is_sdk() is True: |
| 94 | + return fake_dm(domid) |
| 95 | + |
| 96 | + print "qemu-dm-wrapper in python:" |
| 97 | + print "Using domid: %d" % domid |
| 98 | + print "Arguments: %s" % " ".join(argv[1:]) |
| 99 | + print "everything else is from qemu-dm:" |
| 100 | + |
| 101 | + core_dump_limit = enable_core_dumps() |
| 102 | + print "core dump limit: %d" % core_dump_limit |
| 103 | + |
| 104 | + write_dm_pid(domid, os.getpid()) |
| 105 | + |
| 106 | + os.dup2(1, 2) |
| 107 | + os.execve(qemu_dm, qemu_args, qemu_env) |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + from sys import argv |
| 111 | + raise SystemExit, main(argv) |
0 commit comments