本文简述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 待加工的数据文件

以下为可参考脚本:

  1. #!/bin/bash -e
  2. ###############################################################################
  3. #
  4. # Copyright (C) 2014 - 2018 by Yujiang Lin <lynyujiang@gmail.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # revised by Luego Zhang
  12. #
  13. #
  14. ###############################################################################
  15. # => Force locale language to be set to English. This avoids issues when doing
  16. # text and string processing.
  17. export LC_ALL=C LANGUAGE=C LANG=C
  18.  
  19. # => Help and information
  20. usage() {
  21. echo "Purpose: Modify Existing Ramdisk Image"
  22. echo "Version: 20191101v1.1"
  23. echo "Usage : $(basename ${BASH_SOURCE}) [option]"
  24. echo "options:"
  25. echo "--help: Display this help message"
  26. exit 0;
  27. }
  28. expr "$*" : ".*--help" > /dev/null && usage
  29.  
  30. # => Setting The Development Environment Variables
  31. #if [ ! "${ZN_CONFIG_DONE}" ]; then
  32. # printf "\033[31m[ERROR]\033[0m Please source the settings64.sh script first\n"
  33. # exit 1
  34. #fi
  35. source ./common.sh
  36.  
  37. # => Filename of the running script.
  38. ZN_SCRIPT_NAME="$(basename ${BASH_SOURCE})"
  39.  
  40. ###############################################################################
  41. # => The beginning
  42. print_info "[ $(date "+%Y/%m/%d %H:%M:%S") ] Starting ${ZN_SCRIPT_NAME}\n"
  43.  
  44. # => Check user input is correct
  45. [[ -f $1 ]] || error_exit "(u)ramdisk.image.gz not found\n"
  46.  
  47. # => Set basic info
  48. REALPATH=$(realpath $1)
  49. read DIRNAME BASENAME <<<$(echo $(dirname ${REALPATH}) $(basename ${REALPATH}))
  50. read FILENAME EXTENSION <<<$(echo ${BASENAME%.*} ${BASENAME##*.})
  51.  
  52. # => Check user input is correct again
  53. if [ "${BASENAME}" = "uramdisk.image.gz" ]; then
  54. echo_info "Unwrap the image with the u-boot header"
  55. dd if=${DIRNAME}/uramdisk.image.gz of=${DIRNAME}/ramdisk.image.gz bs=64 skip=1
  56. else
  57. [[ "${BASENAME}" = "ramdisk.image.gz" ]] || error_exit "(u)ramdisk.image.gz - not fount"
  58. fi
  59.  
  60. # =>
  61. echo_info "(1/6) Create a mount point for ramdisk.image"
  62. RAMDISK_MOUNTPOINT=$(mktemp -d ${DIRNAME}/ramdisk.XXXXXX) || error_exit "Could not create a mount point"
  63.  
  64. # =>
  65. echo_info "(2/6) Extract the initrd image from the gzip archive"
  66. gunzip ${DIRNAME}/ramdisk.image.gz && chmod u+rwx ${DIRNAME}/ramdisk.image
  67.  
  68. # =>
  69. echo_info "(3/6) Mount the initrd image as a loop back device at ${RAMDISK_MOUNTPOINT}"
  70. sudo mount -o loop ${DIRNAME}/ramdisk.image ${RAMDISK_MOUNTPOINT}
  71. [[ $? -ne 0 ]] && gunzip ${DIRNAME}/ramdisk.image && error_exit "Could not mount the ramdisk"
  72.  
  73. # => Make changes in the mounted filesystem.
  74. cat << EOF
  75.  
  76. (4/6) Ready for anything, you can modify existing RAM disk image
  77.  
  78. Note: When finished, enter "exit" to exit bash, then the script will handle other remaining work
  79.  
  80. EOF
  81.  
  82. /bin/bash
  83.  
  84. # =>
  85. echo_info "(5/6) Umount the initrd image and compress the image."
  86. sudo umount ${RAMDISK_MOUNTPOINT} && gzip ${DIRNAME}/ramdisk.image
  87.  
  88. # =>
  89. echo_info "(6/6) Wrapping the image with a U-Boot header and remove temp files"
  90. if [ "${BASENAME}" = "uramdisk.image.gz" ]; then
  91. if type mkimage >/dev/null 2>&1; then
  92. echo_info "Wrapping the image with a U-Boot header"
  93. mkimage -A arm -T ramdisk -C gzip -d ${DIRNAME}/ramdisk.image.gz \
  94. ${DIRNAME}/uramdisk.image.gz
  95. else
  96. error_exit "Missing mkimage command"
  97. fi
  98. fi
  99.  
  100. rm -rf $RAMDISK_MOUNTPOINT
  101.  
  102. # => The end
  103. print_info "[ $(date "+%Y/%m/%d %H:%M:%S") ] Finished ${ZN_SCRIPT_NAME}\n"
  104. ################################################################

customize_ramdisk.sh

如何创建根文件系统映像

如果没有uramdisk的映像文件,则需要从创建一个空白映像开始,自己制作好根文件系统(相关内容在网上自己获取),将该根文件系统写入空白映像,最后封装成u-boot支持的系统映像文件,具体流程见下图:

note:

此处需注意制作的映像大小就与内核中的设置相匹配。(通过make menuconfig ARCH=ARM打开设置界面,Device Drivers => Block Devices => Default Ram Disk Size (kbytes), 修改选项前数字,本例已修改为64MB,即65536)

以下为参考脚本文件:(使用前将common.sh拷贝至同一目录下)

  1. #!/bin/bash
  2. ###############################################################################
  3. # 版 权:米联客
  4. # 技术社区:www.osrc.cn
  5. # 功能描述:一些常用函数
  6. # 版 本 号:V1.0
  7. ###############################################################################
  8. # => Writing a Warning Message to the Console Window
  9. echo_warn() {
  10. local msg="$1"
  11. printf "\033[33m[WARNING] \033[0m";
  12. printf "$msg\n";
  13. }
  14. export -f echo_warn
  15.  
  16. # => Writing a Infomation Message to the Console Window
  17. echo_info() {
  18. local msg="$1"
  19. printf "\033[32m[INFO] \033[0m";
  20. printf "$msg\n";
  21. }
  22. export -f echo_info
  23.  
  24. # => Writing a Error Message to the Console Window
  25. echo_error() {
  26. local msg="$1"
  27. printf "\033[31m[ERROR] \033[0m";
  28. printf "$msg\n";
  29. }
  30. export -f echo_error
  31.  
  32. # => Writing a Warning Message to the Console Window
  33. print_warn() {
  34. local msg="$1"
  35. printf "\033[33m$msg\033[0m";
  36. }
  37. export -f print_warn
  38.  
  39. # => Writing a Infomation Message to the Console Window
  40. print_info() {
  41. local msg="$1"
  42. printf "\033[32m$msg\033[0m";
  43. }
  44. export -f print_info
  45.  
  46. # => Writing a Error Message to the Console Window
  47. print_error() {
  48. local msg="$1"
  49. printf "\033[31m$msg\033[0m";
  50. }
  51. export -f print_error
  52.  
  53. # => Writing a Error Message to the Console Window and exit
  54. error_exit() {
  55. local msg="$1"
  56. printf "\033[31m[ERROR] \033[0m";
  57. printf "$msg\n";
  58. exit 1;
  59. }
  60. export -f error_exit

common.sh

  1. #!/bin/bash -e
  2. ###############################################################################
  3. #
  4. # Copyright (C) 2014 - 2018 by Yujiang Lin <lynyujiang@gmail.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # history:
  12. # 191106 - This script has been verified.
  13. #
  14. ###############################################################################
  15. # => Force locale language to be set to English. This avoids issues when doing
  16. # text and string processing.
  17. export LC_ALL=C LANGUAGE=C LANG=C
  18.  
  19. # => Help and information
  20. usage() {
  21. printf "
  22. NAME
  23. $(basename ${BASH_SOURCE}) - Create a new uramdisk.image.gz with a
  24. given folder of filesystem
  25.  
  26. SYNOPSIS
  27. $(basename ${BASH_SOURCE}) [option]
  28.  
  29. DESCRIPTION
  30. Create an empty image and eject the given rootfs into the image.
  31. Output the file like 'uramdisk.image.gz' finally.
  32.  
  33. NOTE
  34. First copy the given filesystem to the path of the script and modify
  35. the relative files in advance. Then execute the script.
  36.  
  37. OPTIONS
  38. --help Display this help message
  39.  
  40. EXAMPLE
  41.  
  42. Version : 191107v1.2
  43. "
  44. exit 0;
  45. }
  46. expr "$*" : ".*--help" > /dev/null && usage
  47.  
  48. # => Setting The Development Environment Variables
  49. #if [ ! "${ZN_CONFIG_DONE}" ]; then
  50. # printf "\033[31m[ERROR]\033[0m Please source the settings64.sh script first\n"
  51. # exit 1
  52. #fi
  53.  
  54. source common.sh
  55.  
  56. # => Filename of the running script.
  57. ZN_SCRIPT_NAME="$(basename ${BASH_SOURCE})"
  58. ZN_SCRIPT_PATH="$(dirname ${BASH_SOURCE})"
  59. # => Check if the rootfs.tar.gz exists.
  60. [[ -f ${ZN_SCRIPT_PATH}/rootfs.tar.gz ]] || error_exit "rootfs.tar.gz is not found"
  61. ###############################################################################
  62.  
  63. # expand capacity of the ramdisk image to 64MB 191106
  64. IMAGE_CAPACITY_MB=64
  65.  
  66. # => beginning
  67. echo "\033[32m[ $(date "+%Y/%m/%d %H:%M:%S") ]\033[0m Starting ${ZN_SCRIPT_NAME}"
  68. MOUNT_POINT=$(mktemp -d $ZN_SCRIPT_PATH/ramdisk.XXXXXX)
  69.  
  70. echo_info "Generate the Root filesystem"
  71. # =>
  72. echo_info "(1/8) Create an empty ramdisk image"
  73. dd if=/dev/zero of=./ramdisk.image bs=1024 count=$((${IMAGE_CAPACITY_MB}*1024))
  74. # =>
  75. echo_info "(2/8) Create an ext2/ext3/ext4 file system"
  76. sudo mke2fs -t ext4 -F ./ramdisk.image -L ramdisk -b 1024 -m 0
  77. # =>
  78. echo_info "(3/8) To disable fsck check on ./ramdisk.image"
  79. sudo tune2fs -c 0 -i 0 ./ramdisk.image
  80. # =>
  81. echo_info "(4/8) Mount the ramdisk image as a loop back device"
  82. sudo mount -o loop ./ramdisk.image ${MOUNT_POINT}
  83. # =>
  84. echo_info "(5/8) Make changes in the mounted filesystem"
  85. sudo tar zxf ./rootfs.tar.gz -C ${MOUNT_POINT}
  86. cat <<EOF
  87.  
  88. Please check the root filesystem.
  89. Then enter "exit" to return the process.
  90.  
  91. EOF
  92. /bin/bash
  93. # =>
  94. echo_info "(6/8) Unmount the ramdisk and compress it"
  95. sudo umount ${MOUNT_POINT} && gzip ./ramdisk.image
  96. # =>
  97. echo_info "(7/8) Wrapping the image with a U-Boot header"
  98. type mkimage >/dev/null 2>&1 || error_exit "Missing mkimage command"
  99. mkimage -A arm -T ramdisk -C gzip -d ./ramdisk.image.gz ./uramdisk.image.gz
  100. # =>
  101. echo_info "(8/8) Housekeeping..."
  102. sudo rm -f ./ramdisk.image.gz
  103. sudo rm -f ./ramdisk.image
  104. sudo rm -rf ${MOUNT_POINT}
  105. # => The end
  106. printf "\033[32m[ $(date "+%Y/%m/%d %H:%M:%S") ]\033[0m Finished ${ZN_SCRIPT_NAME}\n"

create_image_for ramdisk

参考:

如何将Zynq-7000自带的ramdisk8M文件系统扩到ramdisk24M制作

米联客https://www.uisrc.com/portal.php

Ramdisk根文件系统映像的修改与创建的更多相关文章

  1. 使用mkbootfs制作ramdisk根文件系统

    span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...

  2. zju(4)使用busybox制作根文件系统

    1.实验目的 1.学习和掌握busybox相关知识及应用: 2.学会使用交叉编译器定制一个busybox: 3.利用该busybox制作一个文件系统: 4.熟悉根文件系统组织结构: 5.定制.编译ra ...

  3. 从ramdisk根文件系统启动Linux成功,及使用initramfs启动linux

    下面两篇文章是ARM9论坛上的讲解ramdisk文件系统的很不错的文章 今天做了个试验,让Linux2.6.29.4从ramdisk根文件系统启动成功,总结一下. 其中涉及的内容较多,很多东西不再详述 ...

  4. 从ramdisk根文件系统启动Linux 二

    今天做了个试验,让Linux2.6.29.4从ramdisk根文件系统启动成功,总结一下.其中涉及的内容较多,很多东西不再详述,如需深入研究请查阅相关资料(百度或谷歌一下一大堆). 开发环境:Fedo ...

  5. 从ramdisk根文件系统启动Linux成功

    这几天参考国嵌的实验手册和网上的资料完成了u-boot定制.内核定制.ramdisk根文件系统的制作,并成功.趁热打铁,总结一下.本文引用了很多网络上的文章,就不一一注明了.感谢各大侠的帮助,如有雷同 ...

  6. 制作ramdisk-u.img根文件系统

    具体步骤如下:1.解压内核源码树解压linux-2.6.29-mini2440-20090708.tgz到自己的工作目录,会生成一个友善之臂修改过的并且有几个mini2440默认配置文件的内核源码目录 ...

  7. TI Davinci DM6446开发攻略——根文件系统的裁剪和移植

    一.补充文件系统知识 Linux根文件系统是存放tool软件.lib文件.script(脚本).配置文件.其他特殊文件.自己开发的应用程序的地方.嵌入式linux的根文件系统rootfs就像windo ...

  8. linux 内核移植和根文件系统的制作【转载】

    原文地址:http://www.cnblogs.com/hnrainll/archive/2011/06/09/2076214.html 1.1 Linux内核基础知识 在动手进行Linux内核移植之 ...

  9. linux 内核移植和根文件系统的制作

    1.1 Linux内核基础知识 在动手进行Linux内核移植之前,非常有必要对Linux内核进行一定的了解,下面从Linux内核的版本和分类说起. 1.1.1  Linux版本 Linux内核的版本号 ...

随机推荐

  1. Docker 更改容器映射端口

    1.编辑容器的配置文件进行更改端口: docker run 运行启动时 -p 可以指定容器启动映射端口 ( ) 可以编辑配置文件 进行修改:(需要重启docker 服务 不止是是容器 才能生效.只能重 ...

  2. JavaScript原始类型转换和进制转换

    1.JavaScript转换包括:强制转换和基本转换 如: var  str = 'A',num=10,nu=null,t=true,und=undefined,x; //注意:定义的x未被初始化:默 ...

  3. public private protected 修饰符整理

    1.public定义的类或方法:任何类的实例都可以访问 2.private定义的属性和方法:只能该类内部使用:如果子类要访问父类的private属性:必须实现__set()和__get()方法: 3. ...

  4. Ubuntu下python3安装tkinter包

    case1: 首先sudo apt-get update(如果不更新很有可能找不到tkinter),然后sudo apt-get install python3-tk,安装完成后就可以使用了. cas ...

  5. go语言Mac下编译安装语言包

    这两天公司成立了go语言学习兴趣小组,慕名参与了学习.目前对于go是0基础,只知道它可以做高并发.效率快.编译简单.母语是C. go的安装有多种形式,编译安装是比较慢的一个,今天我就记录一下学习go编 ...

  6. Mac上安装Python3虚拟环境(VirtualEnv)教程

    如果已经安装好pip3,那么执行命令安装virtualenv环境 pip3 install virtualenv 安装完成检测版本是否安装成功 virtualenv --version 创建新目录 M ...

  7. 关于js函数解释(包括内嵌,对象等)

    常用写法: function add(a,b) { return a + b; } alert(add(1,2)); // 结果 3 当我们这么定义函数的时候,函数内容会被编译(但不会立即执行,除非我 ...

  8. MyBatis(十):Mybatis 几种批量操作的对比

    本章主要讲解几种批量处理的用法及对别,批量处理一般用法包含以下几种: 1)普通foreach处理(没循环一次执行一次与mysql服务器交互操作),实际上也是采用的ExecutorType.SIMPLE ...

  9. clumsy 模拟网络丢包延迟

    https://www.cnblogs.com/bodboy/p/6015530.html clumsy 能在 Windows 平台下人工造成不稳定的网络状况,方便你调试应用程序在极端网络状况下的表现 ...

  10. vs2015 如何更改背景主题颜色

    打开vs2015 步骤:工具--> 选项 -->环境-->常规-->主题设置