ia64/xen-unstable

changeset 5145:d61ceec31dfd

bitkeeper revision 1.1547 (42945455UNorq1ha3va8g7OVydNVmw)

XendDomain.py:
Improve error handling.
XendCheckpoint.py:
Switch restore to use os functions to read from the state file.
Simplify code.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed May 25 10:32:53 2005 +0000 (2005-05-25)
parents c0d620b026e7
children de3abc161c24
files tools/python/xen/xend/XendCheckpoint.py tools/python/xen/xend/XendDomain.py
line diff
     1.1 --- a/tools/python/xen/xend/XendCheckpoint.py	Wed May 25 10:10:05 2005 +0000
     1.2 +++ b/tools/python/xen/xend/XendCheckpoint.py	Wed May 25 10:32:53 2005 +0000
     1.3 @@ -23,15 +23,23 @@ PATH_XC_RESTORE = "/usr/libexec/xen/xc_r
     1.4  sizeof_int = calcsize("i")
     1.5  sizeof_unsigned_long = calcsize("L")
     1.6  
     1.7 +def write_exact(fd, buf, errmsg):
     1.8 +    if os.write(fd, buf) != len(buf):
     1.9 +        raise XendError(errmsg)
    1.10 +
    1.11 +def read_exact(fd, size, errmsg):
    1.12 +    buf = os.read(fd, size)
    1.13 +    if len(buf) != size:
    1.14 +        raise XendError(errmsg)
    1.15 +    return buf
    1.16 +
    1.17  def save(xd, fd, dominfo):
    1.18 -    if os.write(fd, SIGNATURE) != len(SIGNATURE):
    1.19 -        raise XendError("could not write guest state file: signature")
    1.20 +    write_exact(fd, SIGNATURE, "could not write guest state file: signature")
    1.21  
    1.22      config = sxp.to_string(dominfo.sxpr())
    1.23 -    if os.write(fd, pack("!i", len(config))) != sizeof_int:
    1.24 -        raise XendError("could not write guest state file: config len")
    1.25 -    if os.write(fd, config) != len(config):
    1.26 -        raise XendError("could not write guest state file: config")
    1.27 +    write_exact(fd, pack("!i", len(config)),
    1.28 +                "could not write guest state file: config len")
    1.29 +    write_exact(fd, config, "could not write guest state file: config")
    1.30  
    1.31      cmd = [PATH_XC_SAVE, str(xc.handle()), str(fd),
    1.32             dominfo.id]
    1.33 @@ -72,72 +80,61 @@ def save(xd, fd, dominfo):
    1.34      return None
    1.35  
    1.36  def restore(xd, fd):
    1.37 -    try:
    1.38 -        signature = fd.read_exact(len(SIGNATURE),
    1.39 -            "not a valid guest state file: signature read")
    1.40 -        if signature != SIGNATURE:
    1.41 -            raise XendError("not a valid guest state file: found '%s'" %
    1.42 -                            signature)
    1.43 -    
    1.44 -        l = fd.read_exact(sizeof_int,
    1.45 -                          "not a valid guest state file: config size read")
    1.46 -        vmconfig_size = unpack("!i", l)[0]
    1.47 -        vmconfig_buf = fd.read_exact(vmconfig_size,
    1.48 -            "not a valid guest state file: config read")
    1.49 -    
    1.50 -        p = sxp.Parser()
    1.51 -        p.input(vmconfig_buf)
    1.52 -        if not p.ready:
    1.53 -            raise XendError("not a valid guest state file: config parse")
    1.54 -    
    1.55 -        vmconfig = p.get_val()
    1.56 -        dominfo = xd.domain_configure(vmconfig)
    1.57 -    
    1.58 -        l = fd.read_exact(sizeof_unsigned_long,
    1.59 -                          "not a valid guest state file: pfn count read")
    1.60 -        nr_pfns = unpack("=L", l)[0]   # XXX endianess
    1.61 -        if nr_pfns > 1024*1024:     # XXX
    1.62 -            raise XendError(
    1.63 -                "not a valid guest state file: pfn count out of range")
    1.64 -    
    1.65 -        # XXXcl hack: fd.tell will sync up the object and
    1.66 -        #             underlying file descriptor
    1.67 -        ignore = fd.tell()
    1.68 -    
    1.69 -        cmd = [PATH_XC_RESTORE, str(xc.handle()), str(fd.fileno()),
    1.70 -               dominfo.id, str(nr_pfns)]
    1.71 -        log.info("[xc_restore] " + join(cmd))
    1.72 -        child = xPopen3(cmd, True, -1, [fd.fileno(), xc.handle()])
    1.73 -        child.tochild.close()
    1.74 -    
    1.75 -        lasterr = ""
    1.76 -        p = select.poll()
    1.77 -        p.register(child.fromchild.fileno())
    1.78 -        p.register(child.childerr.fileno())
    1.79 -        while True:
    1.80 -            r = p.poll()
    1.81 -            for (fd, event) in r:
    1.82 -                if not event & select.POLLIN:
    1.83 -                    continue
    1.84 -                if fd == child.childerr.fileno():
    1.85 -                    l = child.childerr.readline()
    1.86 -                    log.error(l.rstrip())
    1.87 -                    lasterr = l.rstrip()
    1.88 -                if fd == child.fromchild.fileno():
    1.89 -                    l = child.fromchild.readline()
    1.90 -                    log.info(l.rstrip())
    1.91 -            if filter(lambda (fd, event): event & select.POLLHUP, r):
    1.92 -                break
    1.93 -    
    1.94 -        if child.wait() >> 8 == 127:
    1.95 -            lasterr = "popen %s failed" % PATH_XC_RESTORE
    1.96 -        if child.wait() != 0:
    1.97 -            raise XendError("xc_restore failed: %s" % lasterr)
    1.98 -    
    1.99 -        return dominfo
   1.100 +    signature = read_exact(fd, len(SIGNATURE),
   1.101 +        "not a valid guest state file: signature read")
   1.102 +    if signature != SIGNATURE:
   1.103 +        raise XendError("not a valid guest state file: found '%s'" %
   1.104 +                        signature)
   1.105 +
   1.106 +    l = read_exact(fd, sizeof_int,
   1.107 +                   "not a valid guest state file: config size read")
   1.108 +    vmconfig_size = unpack("!i", l)[0]
   1.109 +    vmconfig_buf = read_exact(fd, vmconfig_size,
   1.110 +        "not a valid guest state file: config read")
   1.111 +
   1.112 +    p = sxp.Parser()
   1.113 +    p.input(vmconfig_buf)
   1.114 +    if not p.ready:
   1.115 +        raise XendError("not a valid guest state file: config parse")
   1.116 +
   1.117 +    vmconfig = p.get_val()
   1.118 +    dominfo = xd.domain_configure(vmconfig)
   1.119 +
   1.120 +    l = read_exact(fd, sizeof_unsigned_long,
   1.121 +                   "not a valid guest state file: pfn count read")
   1.122 +    nr_pfns = unpack("=L", l)[0]   # XXX endianess
   1.123 +    if nr_pfns > 1024*1024:     # XXX
   1.124 +        raise XendError(
   1.125 +            "not a valid guest state file: pfn count out of range")
   1.126  
   1.127 -    except IOError, ex:
   1.128 -        if ex.errno == errno.ENOENT:
   1.129 -            raise XendError("can't open guest state file %s" % src)
   1.130 -        else:
   1.131 -            raise
   1.132 +    cmd = [PATH_XC_RESTORE, str(xc.handle()), str(fd),
   1.133 +           dominfo.id, str(nr_pfns)]
   1.134 +    log.info("[xc_restore] " + join(cmd))
   1.135 +    child = xPopen3(cmd, True, -1, [fd, xc.handle()])
   1.136 +    child.tochild.close()
   1.137 +
   1.138 +    lasterr = ""
   1.139 +    p = select.poll()
   1.140 +    p.register(child.fromchild.fileno())
   1.141 +    p.register(child.childerr.fileno())
   1.142 +    while True:
   1.143 +        r = p.poll()
   1.144 +        for (fd, event) in r:
   1.145 +            if not event & select.POLLIN:
   1.146 +                continue
   1.147 +            if fd == child.childerr.fileno():
   1.148 +                l = child.childerr.readline()
   1.149 +                log.error(l.rstrip())
   1.150 +                lasterr = l.rstrip()
   1.151 +            if fd == child.fromchild.fileno():
   1.152 +                l = child.fromchild.readline()
   1.153 +                log.info(l.rstrip())
   1.154 +        if filter(lambda (fd, event): event & select.POLLHUP, r):
   1.155 +            break
   1.156 +
   1.157 +    if child.wait() >> 8 == 127:
   1.158 +        lasterr = "popen %s failed" % PATH_XC_RESTORE
   1.159 +    if child.wait() != 0:
   1.160 +        raise XendError("xc_restore failed: %s" % lasterr)
   1.161 +
   1.162 +    return dominfo
     2.1 --- a/tools/python/xen/xend/XendDomain.py	Wed May 25 10:10:05 2005 +0000
     2.2 +++ b/tools/python/xen/xend/XendDomain.py	Wed May 25 10:32:53 2005 +0000
     2.3 @@ -5,6 +5,7 @@
     2.4   Nothing here is persistent (across reboots).
     2.5   Needs to be persistent for one uptime.
     2.6  """
     2.7 +import errno
     2.8  import os
     2.9  import scheduler
    2.10  import sys
    2.11 @@ -323,16 +324,14 @@ class XendDomain:
    2.12          @param progress: output progress if true
    2.13          """
    2.14  
    2.15 -        class XendFile(file):
    2.16 -            def read_exact(self, size, error_msg):
    2.17 -                buf = self.read(size)
    2.18 -                if len(buf) != size:
    2.19 -                    raise XendError(error_msg)
    2.20 -                return buf
    2.21 +        try:
    2.22 +            fd = os.open(src, os.O_RDONLY)
    2.23  
    2.24 -        fd = XendFile(src, 'rb')
    2.25 +            return XendCheckpoint.restore(self, fd)
    2.26  
    2.27 -        return XendCheckpoint.restore(self, fd)
    2.28 +        except OSError, ex:
    2.29 +            raise XendError("can't read guest state file %s: %s" %
    2.30 +                            (src, ex[1]))
    2.31  
    2.32      def domain_get(self, id):
    2.33          """Get up-to-date info about a domain.
    2.34 @@ -530,8 +529,9 @@ class XendDomain:
    2.35  
    2.36              return XendCheckpoint.save(self, fd, dominfo)
    2.37  
    2.38 -        except:
    2.39 -            raise
    2.40 +        except OSError, ex:
    2.41 +            raise XendError("can't write guest state file %s: %s" %
    2.42 +                            (dst, ex[1]))
    2.43  
    2.44      def domain_pincpu(self, id, vcpu, cpumap):
    2.45          """Set which cpus vcpu can use