def get_grub_info_new(root_dir): """ get grub info: type - grub or grub2, conf file path :return: tuple(type[1, 2], fn), error:(-1, '') """ _logger.debug("get_grub_info_new enter: {}".format(root_dir)) grub_ver_cmd = ["grub-install -v", # legacy grub, ver 0.97 "grub-install -V", # ubuntu always use this "grub2-install -V", # grub2 "grub-install.unsupported -v" # SuSE take over by YaST ] grub_ver_cmd_grep = ["|grep '[0]\.'", "|grep '[1-9]\.'"] grub_ver = -1 for cmd in grub_ver_cmd: for i, grep in enumerate(grub_ver_cmd_grep): res, out = crutil.wrap_getstatusoutput(cmd + grep) if res == 0: grub_ver = i + 1 break if grub_ver != -1: break _logger.debug("crunch: get_grub_info find grub_ver={}".format(grub_ver)) # 这个列表是有顺序的, 首先从grub, 再到efi进行枚举. # 而且早期版本, 通常menu.lst->./grub.conf, centos5.x, oracle linux 5.x grub_files = [(1, "boot/grub/grub.conf"), (2, "boot/grub/grub.cfg"), # ubuntu, grub2, dir name is grub (2, "boot/grub2/grub.conf"), (2, "boot/grub2/grub.cfg"), (1, "boot/grub/menu.lst"), (1, "boot/grub2/menu.lst"), (2, "boot/efi/EFI/centos/grub.cfg"), (2, "boot/efi/EFI/redhat/grub.cfg"), (1, "boot/efi/EFI/SuSE/elilo.conf"), # suse 11 (1, "boot/efi/EFI/sles/grub.cfg"), # sles 12, 这个确实是正在的grub, 但source /grub2/grub.cfg (1, "boot/efi/EFI/ubuntu/grub.cfg"), (1, "etc/grub.conf"), (2, "etc/grub2.cfg"), (2, "etc/grub.cfg") ] _logger.debug("crunch: get_grub_info find grub file from: {}".format(grub_files)) grub_fn = "" for (ver, file) in grub_files: abs_file = os.path.join(root_dir, file) if os.path.exists(abs_file): grub_ver = ver if grub_ver == -1 else grub_ver grub_fn = abs_file if os.path.islink(grub_fn): grub_fn = os.path.join(os.path.dirname(abs_file), os.readlink(abs_file)) break _logger.debug("get_grub_info_new leaved: {} {}".format(grub_ver, grub_fn)) return grub_ver, grub_fn
def get_def_init_fns_by_find(root_dir, knl_ver='', use_efi_first=True): _logger.debug('get_def_init_fns begin') _logger.debug('1 check input params') if os.path.isdir(root_dir) is False: _logger.error( '1.1 check root_dir failed: root_dir={rd}'.format(rd=root_dir)) return -11, [] if knl_ver == '': tmp_res, knl_ver = crutil.wrap_getstatusoutput('uname -r') if len(knl_ver) == 0: _logger.debug( '1.2 get knl_ver failed: knl_ver={kv}'.format(kv=knl_ver)) return -12, [] for i, names in enumerate(initrd_name_conf): boot_dir = initlib.get_boot_dir(root_dir) initrd_name = names['initrd'].format(knlver=knl_ver) vmlinuz_name = names['vmlinuz'].format(knlver=knl_ver) efi_dir = names['efi_dir'] releases_list = names['release'] _logger.info( 'boot_dir: {b}, efi_dir: {e}, release: {r}, initrd: {i}, vmlinuz: {v}' .format(b=boot_dir, e=efi_dir, r=releases_list, i=initrd_name, v=vmlinuz_name)) if use_efi_first: for release in releases_list: efi_bios_initrd_name = os.path.join(boot_dir, efi_dir, release, initrd_name) efi_bios_vmlinuz_name = os.path.join(boot_dir, efi_dir, release, vmlinuz_name) _logger.info('use_efi_first: try find {fn}'.format( fn=efi_bios_initrd_name)) if os.path.isfile(efi_bios_initrd_name): _logger.info('find {fn}, {fn1} exist'.format( fn=efi_bios_initrd_name, fn1=efi_bios_vmlinuz_name)) return 0, [efi_bios_vmlinuz_name, efi_bios_initrd_name] # 如果非efi版本, efi路径肯定搜索不到, 此时,应该用非efi版本 leg_bios_initrd_name = os.path.join(boot_dir, initrd_name) leg_bios_vmlinuz_name = os.path.join(boot_dir, vmlinuz_name) if os.path.isfile(leg_bios_initrd_name): _logger.info('find {fn}, {fn1} exist'.format( fn=leg_bios_initrd_name, fn1=leg_bios_vmlinuz_name)) return 0, [leg_bios_vmlinuz_name, leg_bios_initrd_name] _logger.error('get_def_init_fns failed end') return -1, []
def grep(self, chk_str): """ linux shell: cat filename grep -c chk_str :param chk_str: :return: """ cmd = 'cat {fn} | grep -c "{cs}"'.format(fn=self.__conf_fn, cs=chk_str) tmp_res, out_str = crutil.wrap_getstatusoutput(cmd) if tmp_res != 0: # if not found or other error, the status is not 0 return 0 return int(out_str)
def file_ln(self, chk_str): conf_fn = self.__conf_fn cmd = 'file {arg1}'.format(arg1=conf_fn) tmp_res, out_str = crutil.wrap_getstatusoutput(cmd) if tmp_res != 0: return False, out_str if out_str.find(chk_str) != -1: return True, 'ok' if out_str.find('symbolic link') == -1: return False, 'dismatched' ln = os.readlink(conf_fn) fn = os.path.isabs(ln) and ln or os.path.join(os.path.dirname(conf_fn), ln) cmd = 'file {arg1}'.format(arg1=fn) tmp_res, out_str = crutil.wrap_getstatusoutput(cmd) if tmp_res != 0: return False, out_str if out_str.find(chk_str) != -1: return True, 'ok' return False, 'dismatched'
def file(self, chk_str): """ linux shell: file filename, check output str :param chk_str: :return: bool """ cmd = 'file {arg1}'.format(arg1=self.__conf_fn) tmp_res, out_str = crutil.wrap_getstatusoutput(cmd) if platform.system() == 'Windows': ls = chk_str.split(' ') for cs in ls: if out_str.find(cs) == -1: return False return True else: if out_str.find(chk_str) != -1: return True else: return False
def get_grub_info(root_dir): """ get grub info: type - grub or grub2, conf file path :return: tuple(type[1, 2], fn), error:(-1, '') """ _logger.debug('crunch: get_grub_info begin') _logger.debug('crunch: get_grub_info root_dir=' + root_dir) ver = -1 conf_fn = '' for i in range(0, 1): rtdir_boot = get_boot_dir(root_dir) conf_fn = os.path.join(rtdir_boot, 'grub2', 'grub.cfg') if os.path.isfile(conf_fn): ver = 2 break # ubuntu 16.04 conf_fn = os.path.join(rtdir_boot, 'grub', 'grub.cfg') if os.path.isfile(conf_fn): ver = 2 break conf_fn = os.path.join(rtdir_boot, 'grub', 'grub.conf') if os.path.isfile(conf_fn): ver = 1 break conf_fn = os.path.join(rtdir_boot, 'grub', 'menu.lst') if os.path.isfile(conf_fn): ver = 1 break conf_fn = os.path.join(rtdir_boot, 'efi', 'EFI', 'centos', 'grub.cfg') if os.path.isfile(conf_fn): ver = 2 break conf_fn = os.path.join(rtdir_boot, 'efi', 'EFI', 'redhat', 'grub.cfg') if os.path.isfile(conf_fn): ver = 2 break etc_link = os.path.join(root_dir, r'/etc/grub.conf') if os.path.exists(etc_link) and os.path.islink(etc_link): # the readlink return maybe ../boot/grub2.conf or ./boot/grub2.conf conf_fn = join_link_path(root_dir, os.readlink(etc_link)) if os.path.isfile(conf_fn): ver = 1 break etc_link = os.path.join(root_dir, r'/etc/grub2.cfg') if os.path.exists(etc_link) and os.path.islink(etc_link): conf_fn = join_link_path(root_dir, os.readlink(etc_link)) if os.path.isfile(conf_fn): ver = 2 break # the grub2 link in /etc is grub2.cfg, link to /boot/grub2/grub.cfg cmd = r"find {etc} |grep -E '(grub2\.cfg|grub\.conf)'".format(etc=os.path.join(root_dir, '/etc')) tmp_res, out_str = crutil.wrap_getstatusoutput(cmd) if tmp_res != 0 or os.path.isfile(out_str) is False: # the find result is absolute path, do not join break conf_fn = join_link_path(root_dir, os.readlink(out_str)) if os.path.isfile(os.path.join(root_dir, conf_fn)) is False: break if conf_fn.find('grub2.cfg') != -1: ver = 2 break else: ver = 1 break if ver == -1: ver, conf_fn = get_grub_info_new(root_dir) _logger.debug("use get_grub_info_new: {}, {}".format(ver, conf_fn)) _logger.debug('get_grub_info:ver={arg1},fn={arg2}'.format(arg1=ver, arg2=os.path.join(root_dir, conf_fn))) _logger.debug('crunch: get_grub_info succ end') return ver, conf_fn