Ramdisk根文件系统映像的修改与创建
本文简述Ramdisk根文件系统映像的修改以及创建,并附相关脚本以实现自动化配置,而根文件系统的制作过程请网上自行搜索。相关过程尽可能以图的方式展示出来,重在说明操作的过程,仅供参考。
Ramdisk简介
Ramdisk,顾名思义,即内存磁盘。先来摘一段来自百度百科的解释:
虚拟内存盘是通过软件将一部分内存(RAM)模拟为硬盘来使用的一种技术。相对于直接的硬盘文件访问来说,这种技术可以极大的提高在其上进行的文件访问的速度。但是RAM的易失性也意味着当关闭电源后这部分数据将会丢失。但是在一般情况下,传递到RAM盘上的数据都是在硬盘或别处永久贮存的文件的一个拷贝。经由适当的配置,可以实现当系统重启后重新建立虚拟盘。
这种技术在Windows和Linux系统中都可以实现。它并非一个实际的文件系统,而是一种将实际的文件系统装入内存的机制,并且可以作为Linux的根文件系统。将一些经常被访问而又不会更改的文件 ( 如只读的根文件系统 ) 通过 Ramdisk放在内存中,可以明显地提高系统的性能。 在嵌入式环境中,我们将使用 RAMDisk 制作好的 rootfs 压缩后写入 Flash ,启动的时候由Bootloader装载到 RAM 中。在 Linux 的启动阶段, initrd 提供了一套机制,可以将内映像和根文件系统一起载入内存并解压缩,然后挂载到 /(根目录) 下。这种方法操作简单,但是在 RAM 中的文件系统不是压缩的,因此需要占用许多嵌入式系统中稀有资源 RAM。
如何修改根文件系统映像
要修改根文件系统,需去掉u-boot的文件头,解压映像并挂载至设定的挂载点,在其中完成文件及目录的修改,最终压缩并制作成系统所需的根文件系统映像。具体修改步骤 见下:
note:
1. 使用文件转换命令dd来去除uramdisk的文件头,其中bs=4,skip=16意为4bytes作为单次读写的块大小,从文件的起始位置跳过16个这样的块(共64字节);
2. 准备压缩文件系统镜像前,先将当前工作目录从该镜像目录跳出再卸载;
3. mkimage制作ramdisk文件系统时,几个参数说明:-A 设置系统架构 -T 设置镜像类型 -C 设置文件压缩类型 -d 待加工的数据文件
以下为可参考脚本:
#!/bin/bash -e ############################################################################### # # Copyright (C) 2014 - 2018 by Yujiang Lin <lynyujiang@gmail.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # revised by Luego Zhang # 20191101 # ############################################################################### # => Force locale language to be set to English. This avoids issues when doing # text and string processing. export LC_ALL=C LANGUAGE=C LANG=C # => Help and information usage() { echo "Purpose: Modify Existing Ramdisk Image" echo "Version: 20191101v1.1" echo "Usage : $(basename ${BASH_SOURCE}) [option]" echo "options:" echo "--help: Display this help message" exit 0; } expr "$*" : ".*--help" > /dev/null && usage # => Setting The Development Environment Variables #if [ ! "${ZN_CONFIG_DONE}" ]; then # printf "\033[31m[ERROR]\033[0m Please source the settings64.sh script first\n" # exit 1 #fi source ./common.sh # => Filename of the running script. ZN_SCRIPT_NAME="$(basename ${BASH_SOURCE})" ############################################################################### # => The beginning print_info "[ $(date "+%Y/%m/%d %H:%M:%S") ] Starting ${ZN_SCRIPT_NAME}\n" # => Check user input is correct [[ -f $1 ]] || error_exit "(u)ramdisk.image.gz not found\n" # => Set basic info REALPATH=$(realpath $1) read DIRNAME BASENAME <<<$(echo $(dirname ${REALPATH}) $(basename ${REALPATH})) read FILENAME EXTENSION <<<$(echo ${BASENAME%.*} ${BASENAME##*.}) # => Check user input is correct again if [ "${BASENAME}" = "uramdisk.image.gz" ]; then echo_info "Unwrap the image with the u-boot header" dd if=${DIRNAME}/uramdisk.image.gz of=${DIRNAME}/ramdisk.image.gz bs=64 skip=1 else [[ "${BASENAME}" = "ramdisk.image.gz" ]] || error_exit "(u)ramdisk.image.gz - not fount" fi # => echo_info "(1/6) Create a mount point for ramdisk.image" RAMDISK_MOUNTPOINT=$(mktemp -d ${DIRNAME}/ramdisk.XXXXXX) || error_exit "Could not create a mount point" # => echo_info "(2/6) Extract the initrd image from the gzip archive" gunzip ${DIRNAME}/ramdisk.image.gz && chmod u+rwx ${DIRNAME}/ramdisk.image # => echo_info "(3/6) Mount the initrd image as a loop back device at ${RAMDISK_MOUNTPOINT}" sudo mount -o loop ${DIRNAME}/ramdisk.image ${RAMDISK_MOUNTPOINT} [[ $? -ne 0 ]] && gunzip ${DIRNAME}/ramdisk.image && error_exit "Could not mount the ramdisk" # => Make changes in the mounted filesystem. cat << EOF (4/6) Ready for anything, you can modify existing RAM disk image Note: When finished, enter "exit" to exit bash, then the script will handle other remaining work EOF /bin/bash # => echo_info "(5/6) Umount the initrd image and compress the image." sudo umount ${RAMDISK_MOUNTPOINT} && gzip ${DIRNAME}/ramdisk.image # => echo_info "(6/6) Wrapping the image with a U-Boot header and remove temp files" if [ "${BASENAME}" = "uramdisk.image.gz" ]; then if type mkimage >/dev/null 2>&1; then echo_info "Wrapping the image with a U-Boot header" mkimage -A arm -T ramdisk -C gzip -d ${DIRNAME}/ramdisk.image.gz \ ${DIRNAME}/uramdisk.image.gz else error_exit "Missing mkimage command" fi fi rm -rf $RAMDISK_MOUNTPOINT # => The end print_info "[ $(date "+%Y/%m/%d %H:%M:%S") ] Finished ${ZN_SCRIPT_NAME}\n" ################################################################
如何创建根文件系统映像
如果没有uramdisk的映像文件,则需要从创建一个空白映像开始,自己制作好根文件系统(相关内容在网上自己获取),将该根文件系统写入空白映像,最后封装成u-boot支持的系统映像文件,具体流程见下图:
note:
此处需注意制作的映像大小就与内核中的设置相匹配。(通过make menuconfig ARCH=ARM打开设置界面,Device Drivers => Block Devices => Default Ram Disk Size (kbytes), 修改选项前数字,本例已修改为64MB,即65536)
以下为参考脚本文件:(使用前将common.sh拷贝至同一目录下)
#!/bin/bash ############################################################################### # 版 权:米联客 # 技术社区:www.osrc.cn # 功能描述:一些常用函数 # 版 本 号:V1.0 ############################################################################### # => Writing a Warning Message to the Console Window echo_warn() { local msg="$1" printf "\033[33m[WARNING] \033[0m"; printf "$msg\n"; } export -f echo_warn # => Writing a Infomation Message to the Console Window echo_info() { local msg="$1" printf "\033[32m[INFO] \033[0m"; printf "$msg\n"; } export -f echo_info # => Writing a Error Message to the Console Window echo_error() { local msg="$1" printf "\033[31m[ERROR] \033[0m"; printf "$msg\n"; } export -f echo_error # => Writing a Warning Message to the Console Window print_warn() { local msg="$1" printf "\033[33m$msg\033[0m"; } export -f print_warn # => Writing a Infomation Message to the Console Window print_info() { local msg="$1" printf "\033[32m$msg\033[0m"; } export -f print_info # => Writing a Error Message to the Console Window print_error() { local msg="$1" printf "\033[31m$msg\033[0m"; } export -f print_error # => Writing a Error Message to the Console Window and exit error_exit() { local msg="$1" printf "\033[31m[ERROR] \033[0m"; printf "$msg\n"; exit 1; } export -f error_exit
#!/bin/bash -e ############################################################################### # # Copyright (C) 2014 - 2018 by Yujiang Lin <lynyujiang@gmail.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # history: # 191106 - This script has been verified. # ############################################################################### # => Force locale language to be set to English. This avoids issues when doing # text and string processing. export LC_ALL=C LANGUAGE=C LANG=C # => Help and information usage() { printf " NAME $(basename ${BASH_SOURCE}) - Create a new uramdisk.image.gz with a given folder of filesystem SYNOPSIS $(basename ${BASH_SOURCE}) [option] DESCRIPTION Create an empty image and eject the given rootfs into the image. Output the file like 'uramdisk.image.gz' finally. NOTE First copy the given filesystem to the path of the script and modify the relative files in advance. Then execute the script. OPTIONS --help Display this help message EXAMPLE Version : 191107v1.2 " exit 0; } expr "$*" : ".*--help" > /dev/null && usage # => Setting The Development Environment Variables #if [ ! "${ZN_CONFIG_DONE}" ]; then # printf "\033[31m[ERROR]\033[0m Please source the settings64.sh script first\n" # exit 1 #fi source common.sh # => Filename of the running script. ZN_SCRIPT_NAME="$(basename ${BASH_SOURCE})" ZN_SCRIPT_PATH="$(dirname ${BASH_SOURCE})" # => Check if the rootfs.tar.gz exists. [[ -f ${ZN_SCRIPT_PATH}/rootfs.tar.gz ]] || error_exit "rootfs.tar.gz is not found" ############################################################################### # expand capacity of the ramdisk image to 64MB 191106 IMAGE_CAPACITY_MB=64 # => beginning echo "\033[32m[ $(date "+%Y/%m/%d %H:%M:%S") ]\033[0m Starting ${ZN_SCRIPT_NAME}" MOUNT_POINT=$(mktemp -d $ZN_SCRIPT_PATH/ramdisk.XXXXXX) echo_info "Generate the Root filesystem" # => echo_info "(1/8) Create an empty ramdisk image" dd if=/dev/zero of=./ramdisk.image bs=1024 count=$((${IMAGE_CAPACITY_MB}*1024)) # => echo_info "(2/8) Create an ext2/ext3/ext4 file system" sudo mke2fs -t ext4 -F ./ramdisk.image -L ramdisk -b 1024 -m 0 # => echo_info "(3/8) To disable fsck check on ./ramdisk.image" sudo tune2fs -c 0 -i 0 ./ramdisk.image # => echo_info "(4/8) Mount the ramdisk image as a loop back device" sudo mount -o loop ./ramdisk.image ${MOUNT_POINT} # => echo_info "(5/8) Make changes in the mounted filesystem" sudo tar zxf ./rootfs.tar.gz -C ${MOUNT_POINT} cat <<EOF Please check the root filesystem. Then enter "exit" to return the process. EOF /bin/bash # => echo_info "(6/8) Unmount the ramdisk and compress it" sudo umount ${MOUNT_POINT} && gzip ./ramdisk.image # => echo_info "(7/8) Wrapping the image with a U-Boot header" type mkimage >/dev/null 2>&1 || error_exit "Missing mkimage command" mkimage -A arm -T ramdisk -C gzip -d ./ramdisk.image.gz ./uramdisk.image.gz # => echo_info "(8/8) Housekeeping..." sudo rm -f ./ramdisk.image.gz sudo rm -f ./ramdisk.image sudo rm -rf ${MOUNT_POINT} # => The end printf "\033[32m[ $(date "+%Y/%m/%d %H:%M:%S") ]\033[0m Finished ${ZN_SCRIPT_NAME}\n"
参考: