[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [RFC]output log messages to the user console and logfile by creating a fifo file at XendBootloader.py
Hello everyone! When pygub failed, we could not get the detail log. So I'd like the feature of putting the detail log of pygrub to logfile and user console. I created a fifo file shared by XendBootloader.py and pygrub to output some messages from the pygrub into the user console. I can't think out of any other easy way to realize it. If you have a better idea, could you tell me some details about it? Regards. Sun Shuchun diff --git a/pygrub.bak b/pygrub index 3a44846..3fd370d 100755 --- a/pygrub.bak +++ b/pygrub @@ -25,8 +25,20 @@ import fsimage import grub.GrubConf import grub.LiloConf +import traceback + PYGRUB_VER = 0.6 +def pipe_file_w(str): + fd_kiharu = os.open(output_kiharu, os.O_RDWR) + try: + buf = os.write(fd_kiharu, "\n"+str) + os.close(fd_kiharu) + except: + logging.error("pipe file IO: %s", traceback.format_exc()) + return False + return True + def enable_cursor(ison): if ison: val = 2 @@ -39,21 +51,43 @@ def enable_cursor(ison): pass def is_disk_image(file): - fd = os.open(file, os.O_RDONLY) - buf = os.read(fd, 512) - os.close(fd) + #return False + try: + fd = os.open(file, os.O_RDONLY) + buf = os.read(fd, 512) + os.close(fd) + except IOError, msg: + logging.error("[is_disk_image]: %s", msg) + + flag1=(len(buf) >= 512) + flag2=(struct.unpack("H", buf[0x1fe: 0x200]) == (0xaa55,)) - if len(buf) >= 512 and \ - struct.unpack("H", buf[0x1fe: 0x200]) == (0xaa55,): + if flag1 and flag2: return True + + if not flag1 and flag2: + str="[is_disk_image]: boot image file size > 512Byte" + logging.error(str) + elif not flag2 and flag1: + str="[is_disk_image]: boot image file is not end with '0xaa55'" + logging.error(str) + else: + str="[is_disk_image]: file("+file+")is not a correct disk image format" + logging.error(str) + pipe_file_w(str) return False def get_active_partition(file): """Find the offset for the start of the first active partition " "in the disk image file.""" - - fd = os.open(file, os.O_RDONLY) - buf = os.read(fd, 512) + try: + fd = os.open(file, os.O_RDONLY) + buf = os.read(fd, 512) + except IOError. msg: + str="[get_active_partition]: %s"+msg + pipe_file_w(str) + logging.error(str) + for poff in (446, 462, 478, 494): # partition offsets # active partition has 0x80 as the first byte if struct.unpack("<c", buf[poff:poff+1]) == ('\x80',): @@ -70,11 +104,13 @@ V_ROOT=0x2 def get_solaris_slice(file, offset): """Find the root slice in a Solaris VTOC.""" - fd = os.open(file, os.O_RDONLY) os.lseek(fd, offset + (DK_LABEL_LOC * SECTOR_SIZE), 0) buf = os.read(fd, 512) if struct.unpack("<H", buf[508:510])[0] != DKL_MAGIC: + str="[get_solaris_slice]: A suitable root slice does not exist at file(%s)"+file + pipe_file_w(str) + logging.error(str) raise RuntimeError, "Invalid disklabel magic" nslices = struct.unpack("<H", buf[30:32])[0] @@ -85,7 +121,9 @@ def get_solaris_slice(file, offset): slicesect = struct.unpack("<L", buf[sliceoff+4:sliceoff+8])[0] if slicetag == V_ROOT: return slicesect * SECTOR_SIZE - + str="[get_solaris_slice]: A root slice does not exist at file(%s)"+file + pipe_file_w(str) + logging.error(str) raise RuntimeError, "No root slice found" def get_fs_offset_gpt(file): @@ -101,10 +139,16 @@ FDISK_PART_GPT=0xee def get_fs_offset(file): if not is_disk_image(file): + str="[get_fs_offset]: is_disk_image(file)=0, return 0" + pipe_file_w(str) + logging.error(str) return 0 partbuf = get_active_partition(file) - if len(partbuf) == 0: + if len(partbuf) == 0: + str="[get_fs_offset]: Unable to find active partition on disk" + pipe_file_w(str) + logging.error(str) raise RuntimeError, "Unable to find active partition on disk" offset = struct.unpack("<L", partbuf[8:12])[0] * SECTOR_SIZE @@ -368,6 +412,9 @@ class Grub: we're being given a raw config file rather than a disk image.""" if not os.access(fn, os.R_OK): + str="Unable to access %s"+fn + pipe_file_w(str) + logging.error(str) raise RuntimeError, "Unable to access %s" %(fn,) if platform.machine() == 'ia64': @@ -398,6 +445,9 @@ class Grub: self.cf.filename = f break if self.__dict__.get('cf', None) is None: + str="couldn't find bootloader config file in the image provided." + pipe_file_w(str) + logging.error(str) raise RuntimeError, "couldn't find bootloader config file in the image provided." f = fs.open_file(self.cf.filename) buf = f.read() @@ -552,6 +602,9 @@ def run_grub(file, entry, fs, arg): if sel == -1: print "No kernel image selected!" + str="[run_grub]: No kernel image selected" + pipe_file_w(str) + logging.error(str) sys.exit(1) try: @@ -571,7 +624,7 @@ def run_grub(file, entry, fs, arg): # If nothing has been specified, look for a Solaris domU. If found, perform the # necessary tweaks. -def sniff_solaris(fs, cfg): +def sniff_solaris(fs, cfg): if not fs.file_exists("/platform/i86xpv/kernel/unix"): return cfg @@ -620,8 +673,13 @@ def sniff_netware(fs, cfg): return cfg if __name__ == "__main__": - sel = None + logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(levelname)s %(message)s', + datefmt='%a,%d %b %Y %H:%M:%S', + filename='/var/log/xen/pygrub.log', + filemode='w') + def usage(): print >> sys.stderr, "Usage: %s [-q|--quiet] [-i|--interactive] [--output=] [--kernel=] [--ramdisk=] [--args=] [--entry=] <image>" %(sys.argv[0],) @@ -677,8 +735,10 @@ if __name__ == "__main__": if output is None or output == "-": fd = sys.stdout.fileno() else: - fd = os.open(output, os.O_WRONLY) - + fd = os.open(output, os.O_WRONLY) + output_kiharu = output + "kiharu" + # debug if isconfig: chosencfg = run_grub(file, entry, fs, incfg["args"]) @@ -690,11 +750,21 @@ if __name__ == "__main__": # if boot filesystem is set then pass to fsimage.open bootfsargs = '"%s"' % incfg["args"] - bootfsgroup = re.findall('zfs-bootfs=(.*?)[\s\,\"]', bootfsargs) + bootfsgroup = re.findall('zfs-bootfs=(.*?)[\s\,\"]', bootfsargs) if bootfsgroup: - fs = fsimage.open(file, get_fs_offset(file), bootfsgroup[0]) + try: + fs = fsimage.open(file, get_fs_offset(file), bootfsgroup[0]) + except: + str="__main_\:%s"+traceback.format_exc() + pipe_file_w(str) + logging.error(str) else: - fs = fsimage.open(file, get_fs_offset(file)) + try: + fs = fsimage.open(file, get_fs_offset(file)) + except: + str="__main__:%s"+traceback.format_exc() + pipe_file_w(str) + logging.error(str) chosencfg = sniff_solaris(fs, incfg) @@ -704,34 +774,50 @@ if __name__ == "__main__": if not chosencfg["kernel"]: chosencfg = run_grub(file, entry, fs, incfg["args"]) - data = fs.open_file(chosencfg["kernel"]).read() - (tfd, bootcfg["kernel"]) = tempfile.mkstemp(prefix="boot_kernel.", - dir="/var/run/xend/boot") - os.write(tfd, data) - os.close(tfd) - - if chosencfg["ramdisk"]: - data = fs.open_file(chosencfg["ramdisk"],).read() - (tfd, bootcfg["ramdisk"]) = tempfile.mkstemp(prefix="boot_ramdisk.", - dir="/var/run/xend/boot") + try: + data = fs.open_file(chosencfg["kernel"]).read() + (tfd, bootcfg["kernel"]) = tempfile.mkstemp(prefix="boot_kernel.", + dir="/var/run/xend/boot") os.write(tfd, data) os.close(tfd) - else: - initrd = None - - sxp = "linux (kernel %s)" % bootcfg["kernel"] - if bootcfg["ramdisk"]: - sxp += "(ramdisk %s)" % bootcfg["ramdisk"] - if chosencfg["args"]: - zfsinfo = fsimage.getbootstring(fs) - if zfsinfo is None: - sxp += "(args \"%s\")" % chosencfg["args"] + except: + str="__main__: %s"+traceback.format_exc() + pipe_file_w(str) + logging.error(str) + + try: + if chosencfg["ramdisk"]: + data = fs.open_file(chosencfg["ramdisk"],).read() + (tfd, bootcfg["ramdisk"]) = tempfile.mkstemp(prefix="boot_ramdisk.", + dir="/var/run/xend/boot") + os.write(tfd, data) + os.close(tfd) else: - e = re.compile("zfs-bootfs=[\w\-\.\:@/]+" ) - (chosencfg["args"],count) = e.subn(zfsinfo, chosencfg["args"]) - if count == 0: - chosencfg["args"] += " -B %s" % zfsinfo - sxp += "(args \"%s\")" % (chosencfg["args"]) + initrd = None + except: + str="__main__: %s"+traceback.format_exc() + pipe_file_w(str) + logging.error(str) + + try: + sxp = "linux (kernel %s)" % bootcfg["kernel"] + + if bootcfg["ramdisk"]: + sxp += "(ramdisk %s)" % bootcfg["ramdisk"] + if chosencfg["args"]: + zfsinfo = fsimage.getbootstring(fs) + if zfsinfo is None: + sxp += "(args \"%s\")" % chosencfg["args"] + else: + e = re.compile("zfs-bootfs=[\w\-\.\:@/]+" ) + (chosencfg["args"],count) = e.subn(zfsinfo, chosencfg["args"]) + if count == 0: + chosencfg["args"] += " -B %s" % zfsinfo + sxp += "(args \"%s\")" % (chosencfg["args"]) + except: + str="__main__: %s"+traceback.format_exc() + pipe_file_w(str) + logging.error(str) sys.stdout.flush() os.write(fd, sxp) diff --git a/XendBootloader.py.bak b/XendBootloader.py index 20bade2..6137b94 100644 --- a/XendBootloader.py.bak +++ b/XendBootloader.py @@ -52,6 +52,10 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', fifo = "/var/run/xend/boot/xenbl.%s" %(random.randint(0, 32000),) try: os.mkfifo(fifo, 0600) + kiharu_fifo=fifo+"kiharu" + os.mkfifo(kiharu_fifo, 0600) + except OSError, e: if (e.errno != errno.EEXIST): raise @@ -116,7 +120,7 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', oshelp.close_fds() os.execvpe(args[0], args, env) except OSError, e: - print e + print e pass os._exit(1) @@ -132,12 +136,17 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', while True: try: r = os.open(fifo, os.O_RDONLY) + r_kiharu = os.open(kiharu_fifo, os.O_RDONLY) except OSError, e: if e.errno == errno.EINTR: continue break fcntl.fcntl(r, fcntl.F_SETFL, os.O_NDELAY); + fcntl.fcntl(r_kiharu, fcntl.F_SETFL, os.O_NDELAY) + ret_kiharu = "" ret = "" inbuf=""; outbuf=""; @@ -162,7 +171,9 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', wsel = wsel + [m1] if len(inbuf) != 0: wsel = wsel + [m2] - sel = select.select([r, m1, m2], wsel, []) + #sel = select.select([r, m1, m2], wsel, []) + sel = select.select([r,r_kiharu,m1,m2], wsel, []) try: if m1 in sel[0]: s = os.read(m1, 16) @@ -186,6 +197,10 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', if r in sel[0]: s = os.read(r, 128) ret = ret + s + s_kiharu = os.read(r_kiharu, 1024) + ret_kiharu = ret_kiharu + s_kiharu + if len(s) == 0: break del inbuf @@ -194,6 +209,8 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', os.close(r) os.close(m2) os.close(m1) + os.close(r_kiharu) if os.uname()[0] == 'SunOS' or os.uname()[0] == 'NetBSD': os.close(s1) os.unlink(fifo) @@ -210,7 +227,9 @@ def bootloader(blexec, disk, dom, quiet = False, blargs = '', kernel = '', dom.bootloader_pid = None if len(ret) == 0: - msg = "Boot loader didn't return any data!kiharu test1" + msg = "Boot loader didn't return any data!" + msg = msg + ret_kiharu log.error(msg) raise VmError, msg _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |