Make pygrub cope better with big files in the guest. Only read the first megabyte of a configuration file (grub etc.) Read the kernel and ramdisk files from the guest in one megabyte pieces so pygrub doesn't grow too large if they are large. If there are problems writing the temporary copies of the kernel and ramdisk files delete them and exit. Signed-off-by: Michael Young --- xen-4.2.0/tools/pygrub/src/pygrub.orig 2012-05-12 16:40:48.000000000 +0100 +++ xen-4.2.0/tools/pygrub/src/pygrub 2012-05-16 00:18:44.738000020 +0100 @@ -28,6 +28,7 @@ import grub.ExtLinuxConf PYGRUB_VER = 0.6 +fs_read_max=1048576 def enable_cursor(ison): if ison: @@ -448,7 +449,8 @@ if self.__dict__.get('cf', None) is None: raise RuntimeError, "couldn't find bootloader config file in the image provided." f = fs.open_file(self.cf.filename) - buf = f.read() + # limit read size to avoid pathological cases + buf = f.read(fs_read_max) del f self.cf.parse(buf) @@ -824,21 +826,42 @@ if not_really: bootcfg["kernel"] = "" % chosencfg["kernel"] else: - data = fs.open_file(chosencfg["kernel"]).read() + datafile = fs.open_file(chosencfg["kernel"]) (tfd, bootcfg["kernel"]) = tempfile.mkstemp(prefix="boot_kernel.", dir=output_directory) - os.write(tfd, data) + dataoff=0 + data=datafile.read(fs_read_max) + while len(data)>0: + try: + os.write(tfd, data) + except: + print "error writing temporary copy of kernel" + os.unlink(tfd) + sys.exit(1) + dataoff+=len(data) + data=datafile.read(fs_read_max,dataoff) os.close(tfd) if chosencfg["ramdisk"]: if not_really: bootcfg["ramdisk"] = "" % chosencfg["ramdisk"] else: - data = fs.open_file(chosencfg["ramdisk"],).read() - (tfd, bootcfg["ramdisk"]) = tempfile.mkstemp( + datafile = fs.open_file(chosencfg["ramdisk"],) + (tfd2, bootcfg["ramdisk"]) = tempfile.mkstemp( prefix="boot_ramdisk.", dir=output_directory) - os.write(tfd, data) - os.close(tfd) + dataoff=0 + data=datafile.read(fs_read_max) + while len(data)>0: + try: + os.write(tfd2, data) + except: + print "error writing temporary copy of ramdisk" + os.unlink(tfd2) + os.unlink(tfd) + sys.exit(1) + dataoff+=len(data) + data=datafile.read(fs_read_max,dataoff) + os.close(tfd2) else: initrd = None