⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bootloader.py

📁 xen虚拟机源代码安装包
💻 PY
📖 第 1 页 / 共 2 页
字号:
                    if ctr == index:                        within_title = 1                    else:                        within_title = 0                    ctr = ctr + 1                elif within_title and self.module_re.match(line):                    start = line.find("module")                    l = line[start+6:len(line)]                    l = l.lstrip()                    if l[0] == '/':                        prefix = "/"                    else:                        prefix = ""                    prefix = get_prefix()                    module_line = "\tmodule %s%s\n" % (prefix,binpolname)                else:                    if module_line != "" and not found:                        os.write(tmp_fd, module_line)                        found = True                os.write(tmp_fd, line)            if module_line != "" and not found:                if ord(line[-1]) not in [ 10 ]:                    os.write(tmp_fd, '\n')                os.write(tmp_fd, module_line)                found = True            shutil.move(boot_file, boot_file+"_save")            shutil.copyfile(tmp_grub, boot_file)            os.close(tmp_fd)            try:                os.remove(tmp_grub)            except:                pass        finally:            self.__bootfile_lock.release()        return found    def rm_policy_from_boottitle(self, index, unamelist):        """ Remove a policy from the given title. A list of possible policies            must be given to detect what module to remove        """        found = False        ctr = 0        within_title = 0        prefix = get_prefix()        namelist = [prefix+name for name in unamelist]        try:            boot_file = self.__get_bootfile()        except:            return False        try:            self.__bootfile_lock.acquire()            grub_fd = open(boot_file)            (tmp_fd, tmp_grub) = tempfile.mkstemp()            for line in grub_fd:                omit_line = False                if self.title_re.match(line):                    if ctr == index:                        within_title = 1                    else:                        within_title = 0                    ctr = ctr + 1                if within_title and self.module_re.match(line):                    if self.policy_re.match(line):                        start = line.find("module")                        pol = line[start+6:len(line)]                        pol = pol.strip()                        if pol in namelist:                            omit_line = True                            found = True                if not omit_line:                    os.write(tmp_fd, line)            if found:                shutil.move(boot_file, boot_file+"_save")                shutil.copyfile(tmp_grub, boot_file)            os.close(tmp_fd)            try:                os.remove(tmp_grub)            except:                pass        finally:            self.__bootfile_lock.release()        return found    def set_kernel_attval(self, index, att, val):        """            Append an attribut/value pair to the kernel line.            @param index : The index of the title to modify            @param att   : The attribute to add            @param val   : The value to add. If no value or the special value                           '<>' is given, then the attribute will be removed.                           If an empty value is given, then only the attribute                           is added in the format "att", otherwise "att=val"                           is added.        """        found = False        ctr = 0        within_title = 0        try:            boot_file = self.__get_bootfile()        except:            False        try:            self.__bootfile_lock.acquire()            grub_fd = open(boot_file)            (tmp_fd, tmp_grub) = tempfile.mkstemp()            for line in grub_fd:                if self.title_re.match(line):                    if ctr == index:                        within_title = 1                    else:                        within_title = 0                    ctr = ctr + 1                if within_title and self.kernel_re.match(line):                    nitems = []                    items = line.split(" ")                    i = 0                    while i < len(items):                        el = items[i].split("=",1)                        if el[0] != att:                            nitems.append(items[i].rstrip("\n"))                        i += 1                    if val == "":                        nitems.append("%s" % (att))                    elif val != None and val != "<>":                        nitems.append("%s=%s" % (att,val))                    line = " ".join(nitems) + "\n"                os.write(tmp_fd, line)            shutil.move(boot_file, boot_file+"_save")            shutil.copyfile(tmp_grub, boot_file)            os.close(tmp_fd)            try:                os.remove(tmp_grub)            except:                pass        finally:            self.__bootfile_lock.release()        return found    def get_kernel_val(self, index, att):        """            Get an attribute's value from the kernel line.            @param index : The index of the title to get the attribute/value from            @param att   : The attribute to read the value of        """        ctr = 0        within_title = 0        try:            boot_file = self.__get_bootfile()        except:            return None        try:            self.__bootfile_lock.acquire()            grub_fd = open(boot_file)            for line in grub_fd:                if self.title_re.match(line):                    if ctr == index:                        within_title = 1                    else:                        within_title = 0                    ctr = ctr + 1                if within_title and self.kernel_re.match(line):                    line = line.strip()                    items = line.split(" ")                    i = 0                    while i < len(items):                        el = items[i].split("=",1)                        if el[0] == att:                            if len(el) == 1:                                return "<>"                            return el[1]                        i += 1        finally:            self.__bootfile_lock.release()        return None # Not foundclass LatePolicyLoader(Bootloader):    """ A fake bootloader file that holds the policy to load automatically        once xend has started up and the Domain-0 label to set. """    def __init__(self):        self.__bootfile_lock = threading.RLock()        self.PATH = security.security_dir_prefix        self.FILENAME = self.PATH + "/xen_boot_policy"        self.DEFAULT_TITLE = "ANY"        self.POLICY_ATTR = "POLICY"        Bootloader.__init__(self)    def probe(self):        try:            _dir=os.path.dirname(self.FILENAME)            mkdir.parents(_dir, stat.S_IRWXU)        except:            return False        return True    def get_default_title(self):        return self.DEFAULT_TITLE    def get_boot_policies(self):        policies = {}        try:            self.__bootfile_lock.acquire()            res = self.__loadcontent()            pol = res.get( self.POLICY_ATTR )            if pol:                policies.update({ self.DEFAULT_TITLE : pol })        finally:            self.__bootfile_lock.release()        return policies    def add_boot_policy(self, index, binpolname):        try:            self.__bootfile_lock.acquire()            res = self.__loadcontent()            if binpolname.endswith(".bin"):                binpolname = binpolname[0:-4]            res[ self.POLICY_ATTR ] = binpolname            self.__writecontent(res)        finally:            self.__bootfile_lock.release()        return True    def rm_policy_from_boottitle(self, index, unamelist):        try:            self.__bootfile_lock.acquire()            res = self.__loadcontent()            if self.POLICY_ATTR in res:                del(res[self.POLICY_ATTR])            self.__writecontent(res)        finally:            self.__bootfile_lock.release()        return True    def set_kernel_attval(self, index, att, val):        try:            self.__bootfile_lock.acquire()            res = self.__loadcontent()            res[att] = val            self.__writecontent(res)        finally:            self.__bootfile_lock.release()        return True    def get_kernel_val(self, index, att):        try:            self.__bootfile_lock.acquire()            res = self.__loadcontent()            return res.get(att)        finally:            self.__bootfile_lock.release()    def __loadcontent(self):        res={}        try:            file = open(self.FILENAME)            for line in file:                tmp = line.split("=",1)                if len(tmp) == 2:                   res[tmp[0]] = tmp[1].strip()            file.close()        except:            pass        return res    def __writecontent(self, items):        rc = True        try:            file = open(self.FILENAME,"w")            if file:                for key, value in items.items():                    file.write("%s=%s\n" % (str(key),str(value)))                file.close()        except:            rc = False        return rc__bootloader = Bootloader()def init():    global __bootloader    grub = Grub()    if grub.probe() == True:        __bootloader = grub    else:        late = LatePolicyLoader()        if late.probe() == True:            __bootloader = late

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -