一键教会OpenHarmony-4.1-Release的small系统的rootfs制作过程

itopen组织
1、提供OpenHarmony优雅实用的小工具
2、手把手适配riscv + qemu + linux的三方库移植
3、未来计划riscv + qemu + ohos的三方库移植 + 小程序开发
4、一切拥抱开源,拥抱国产化

一、rootfs制作命令

small系统的rootfs制作脚本为build/ohos/packages/fs_process.py,制作的命令为
build/ohos/packages/fs_process.py
--product dayu900
--root-path /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/
--out-path /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900
--log-path /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/build.log
--product-path /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/vendor/hihope/dayu900

二、fs_process.py文件解析

该文件主要就是class Packer的解析,因此解析每一个接口即可

2.1 init解析

源代码如下

def __init__(self, packer_args) -> None:
    self.config = Config() # 该类在build/hb/resources/config.py
    self.replace_items = {
        r'${product_name}': self.config.product,
        r'${root_path}': self.config.root_path,
        r'${out_path}': self.config.out_path
    }
    self.packing_process = [
        self.mv_usr_libs, self.create_fs_dirs, self.fs_link,
        self.fs_filemode, self.fs_make_cmd, self.fs_tear_down
    ]
    self.fs_cfg = None
    self.chmod_dirs = []

源代码功能

初始化参数

源代码解析

  • 调用Config类获取product_name、root_path、out_path
    Config类是通过解析ohos_config.json文件获取了上面的相关参数,该文件在编译开始或者执行hb set时自动生成并位于代码根目录下
  • self.replace_items解析结果如下
    • ${product_name}: dayu900
    • ${root_path}: /home/wen_fei/OpenHarmony/dayu900-v4.0-release
    • ${out_path}: /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900

2.2 fs_make解析

源代码如下

def fs_make(self, cmd_args):
    fs_cfg_path = os.path.join(self.config.product_path, 'fs.yml')
    if not os.path.isfile(fs_cfg_path):
        LogUtil.hb_info(f'{fs_cfg_path} not found, stop packing fs. '
                      'If the product does not need to be packaged, ignore it.')
        return
    if self.config.fs_attr is None:
        LogUtil.hb_info('component compiling, no need to pack fs')
        return

    fs_cfg_list = IoUtil.read_yaml_file(fs_cfg_path)
    for fs_cfg in fs_cfg_list:
        self.fs_cfg = self.fs_attr_process(fs_cfg)
        if self.fs_cfg.get('fs_dir_name', None) is None:
            continue
        for fs_process_func in self.packing_process:
            fs_process_func()

源代码功能

设置self.fs_cfg并调用处理函数

源代码解析

  • 获取fs_cfg_path文件:/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/vendor/hihope/dayu900/fs.yml

  • 通过IoUtil.read_yaml_file接口解析fs_cfg_path文件获得fs_cfg为

  • 调用self.packing_process中的6个函数并执行

    self.mv_usr_libs
    self.create_fs_dirs
    self.fs_link
    self.fs_filemode
    self.fs_make_cmd
    self.fs_tear_down
    

2.3 mv_usr_libs解析

源代码如下

def mv_usr_libs(self):
    src_path = self.config.out_path
    libs = [lib for lib in os.listdir(src_path) if self.is_lib(lib)]
    target_path = os.path.join(src_path, 'usr', 'lib')
    os.makedirs(target_path, exist_ok=True)

    for lib in libs:
        source_file = os.path.join(src_path, lib)
        target_file = os.path.join(target_path, lib)
        print("[wwx1101856]: source_file: {}, target_file: {}".format(source_file, target_file))
        shutil.move(source_file, target_file)

源代码功能

移动so文件到${src_path}/usr/lib目录下

源代码解析

  • 获取src_path路径:/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900
  • 获取src_path路径下的所有so文件名
  • 创建usr/lib目录:${src_path}/usr/lib
  • 将src_path路径下所有的so文件移动到${src_path}/usr/lib目录下

2.4 create_fs_dirs解析

源代码如下

def create_fs_dirs(self):
    fs_path = os.path.join(self.config.out_path,
                           self.fs_cfg.get('fs_dir_name', 'rootfs'))
    exist_ok, with_rm = self.is_incr(self.fs_cfg.get('fs_incr', None))
    print("fs_path: {}".format(fs_path))
    if with_rm and os.path.exists(fs_path):
        shutil.rmtree(fs_path)
    os.makedirs(fs_path, exist_ok=exist_ok)
    self.replace_items[r'${fs_dir}'] = fs_path

    for fs_dir in self.fs_cfg.get('fs_dirs', []):
        source_dir = fs_dir.get('source_dir', '')
        target_dir = fs_dir.get('target_dir', '')
        if target_dir == '':
            continue

        source_path = self.fs_dirs_replace(source_dir,
                                           self.config.out_path)
        target_path = self.fs_dirs_replace(target_dir, fs_path)

        if source_dir == '' or not os.path.exists(source_path):
            os.makedirs(target_path, exist_ok=True)
            target_mode_tuple = (target_path, fs_dir.get('dir_mode', 755))
            print("[wwx1101856]: target_mode_tuple: {}".format(target_mode_tuple))
            self.chmod_dirs.append(target_mode_tuple)
            continue

        print("[wwx1101856]: source_path: {}, target_path: {}, fs_dir: {}".format(source_path, target_path, fs_dir))
        self.copy_files(source_path, target_path, fs_dir)

源代码功能

创建rootfs目录并根据yml的内容拷贝目录和文件到rootfs中

源代码解析

  • 获取fs_path路径:/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs
  • 设置exist_ok, with_rm两个flag的值
    因fs_cfg中无'fs_incr'的key,所以这两个值为False
  • 创建fs_path路径
  • 在self.replace_items中添加一对key-value:
    self.replace_items[r'${fs_dir}'] = fs_path
  • 根据fs_cfg中fs_dirs的路径和mode创建目录
  • 获取source_dir和target_dir
  • 将source_dir和target_dir中的变量替换成对应的路径,例如${root_path}替换成/home/wen_fei/OpenHarmony/dayu900-v4.0-release
  • 创建target_path目录
  • 将目录的路径和mode值存到self.chmod_dirs中,后面有专门步骤去修改mode
  • 将source_path内容拷贝到target_path

2.5 fs_link解析

源代码如下

def fs_link(self):
    fs_symlink = self.fs_cfg.get('fs_symlink', [])
    for symlink in fs_symlink:
        source, _ = self.replace(symlink.get('source', ''))
        link_name, _ = self.replace(symlink.get('link_name', ''))
        if os.path.exists(link_name):
            os.remove(link_name)
        os.symlink(source, link_name)

源代码功能

根据yml的内容创建软链接文件

源代码解析

  • 获取fs_cfg中fs_symlink的value
  • 根据fs_symlink中的source和link_name创建软链接文件

2.6 fs_filemode解析

源代码如下

def fs_filemode(self):
    fs_filemode = self.fs_cfg.get('fs_filemode', [])
    for filestat in fs_filemode:
        file_dir = os.path.join(self.replace_items[r'${fs_dir}'],
                                filestat.get('file_dir', ''))
        file_mode = filestat.get('file_mode', 0)
        if os.path.exists(file_dir) and file_mode > 0:
            self.chmod_dirs.append((file_dir, file_mode))

    for file_dir, file_mode in self.chmod_dirs:
        self.chmod(file_dir, file_mode)

源代码功能

设置目录和文件的的mode

源代码解析

  • 获取fs_cfg中fs_filemode的value
  • 获取fs_filemode中的file_dir和file_mode并添加到self.chmod_dirs中
  • 将self.chmod_dirs中的所有文件设置其对应的mode值

2.7 fs_make_cmd解析

源代码如下

def fs_make_cmd(self):
    fs_make_cmd = self.fs_cfg.get('fs_make_cmd', [])
    log_path = self.config.log_path

    for cmd in fs_make_cmd:
        cmd, _ = self.replace(cmd)
        cmd = cmd.split(' ')
        SystemUtil.exec_command(cmd, log_path=log_path)

源代码功能

获取制作文件系统的脚本并执行

源代码解析

  • 获取fs_cfg中fs_filemode的value
  • 从self.config.log_path中获取log文件路径: /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/build.log
  • 从fs_make_cmd中获取制作镜像的命令
    dayu900只有一条命令
    ${root_path}/build/lite/make_rootfs/rootfsimg_linux.sh ${fs_dir} ext4 96

2.8 fs_tear_down解析

源代码如下

def fs_tear_down(self):
    while len(self.chmod_dirs):
        tfile = self.chmod_dirs.pop()[0]

        if os.path.isfile(tfile):
            self.chmod(tfile, 555)
        elif os.path.isdir(tfile):
            self.chmod(tfile, 755)

源代码功能

再次检查并设置self.chmod_dirs中文件的mode,在执行fs_make_cmd时因需要会将某些文件和目录的mode修改

三、rootfsimg_linux.sh文件解析

3.1 运行命令

rootfsimg_linux.sh /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs ext4 96

3.2 功能介绍

总结一句话

# 如果是jffs2格式,则执行下面命令
mkfs.jffs2 -q -o /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_jffs2.img -d /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs --pagesize=4096 --devtable /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/build/lite/make_rootfs/rootfs_linux.config

# 如果是vfat格式,则执行下面命令
dd if=/dev/zero of=/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_vfat.img count=xxx bs=512
mkfs.vfat /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_vfat.img -s 4 -f 2 -S 512 > /dev/null
mcopy -i /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_vfat.img /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs/* -/ ::/

# 如果是ext4格式,则执行下面命令
dd if=/dev/zero of=/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_vfat.img bs=512 count=xxx
mkfs.ext4 /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs_vfat.img
/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/third_party/e2fsprogs/install.sh /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/third_party/e2fsprogs
/home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/third_party/e2fsprogs/e2fsprogs/contrib/populate-extfs.sh /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/out/dayu900/dayu900/rootfs /home/itopen/OpenHarmony/OpenHarmony-v4.0-Release/build/lite/make_rootfs/rootfs_linux.config

四、文件来源

4.1 C库文件

● libc++.so来源prebuilts/clang/ohos/linux-x86_64/llvm/lib/arm-linux-ohos/libc++.so
● libc.so来源third_party/musl的代码编译

4.2 usr/lib库文件

来源于各个模块编译出来的库,small系统编译出来的都是so文件不是z.so文件

4.3 bin文件

  • 来源于toybox

  • 各模块的server

  • 其他模块编译出的可执行程序

  • dev_tools/bin下的可执行程序

你可能感兴趣的:(一键教会OpenHarmony-4.1-Release的small系统的rootfs制作过程)