• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2020 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16current_user_name=$(whoami)
17if [ "$current_user_name" != "root" ]
18then
19    echo "请使用root用户执行该脚本,程序退出"
20    exit 1
21fi
22
23# 检查输入参数数量
24if [ $# -lt 3 ]
25then
26    echo "输入参数不足3个,程序退出!"
27    exit 1
28fi
29
30set +e
31mtd_str=$(dpkg -l|grep mtd-utils)
32set -e
33
34if [ "$mtd_str" ]
35then
36    echo "已经安装了mtd-utils"
37else
38    echo "开始安装mtd-utils"
39    apt-get install mtd-utils
40    echo "mtd-utils安装成功"
41fi
42
43mtd_device="/dev/mtdblock0"
44if [ ! -b "$mtd_device" ]
45then
46    # 安装并初始化mtd模块
47    echo "安装并初始化mtd模块"
48    modprobe mtdblock
49    modprobe mtdram total_size=102400
50    chmod 666 /dev/mtdblock0
51    echo "mtd模块操作完成"
52else
53    echo "mtd模块已安装"
54fi
55echo "环境部署完成"
56
57old_img_path=$1
58# 检查文件是否存在
59if [ ! -e "$old_img_path" ]
60then
61    echo "Line:$LINENO,ERROR:$old_img_path can not be found, exit!"
62    exit 1
63fi
64
65# 检查输入数据目录
66input_dir=$2
67if [ ! -d "$input_dir" ]
68then
69    echo "Line:$LINENO,ERROR:intput_dir $input_dir can not be found, exit!"
70    exit 1
71fi
72
73# 检查输出目录
74output_dir=$3
75output_filepath="$output_dir/new_rootfs.img"
76if [ ! -d "$output_dir" ]
77then
78    echo "Line:$LINENO,INFO:output_dir $output_dir not exists and it will be created!"
79    mkdir -p $output_dir
80fi
81
82# 检查挂在目录
83mount_dir="$HOME/mnt"
84if [ ! -d "$mount_dir" ]
85then
86    echo "Line:$LINENO,WARN:$mount_dir not exists and it will be created!"
87    mkdir -p $mount_dir
88fi
89
90set +e
91mount_flag=$(df|grep "$mount_dir")
92set -e
93# 将块设备挂载到本地目录,并赋予相关权限
94if [ "$mount_flag" ]
95then
96    echo "$mount_dir 已挂在其他设备,卸载该目录"
97    umount $mount_dir
98    # 将镜像文件写入块设备
99    dd if=$old_img_path of=$mtd_device
100    mount -t jffs2 $mtd_device $mount_dir
101    chmod -R 755 $mount_dir
102    echo "$mtd_device 挂在到本地成功"
103else
104    dd if=$old_img_path of=$mtd_device
105    mount -t jffs2 $mtd_device $mount_dir
106    chmod -R 755 $mount_dir
107    echo "$mtd_device 挂在到本地成功"
108fi
109
110if [ ! -d "$mount_dir/bin" ]
111then
112    echo "Line:$LINENO,INFO:$mount_dir/bin not exists and it will be created!"
113    mkdir -p $mount_dir/bin
114fi
115# 遍历目录拷贝数据文件
116for file_name in $(ls $input_dir)
117do
118    file_path=$input_dir"/"$file_name
119    if [ -f "$file_path" ]
120    then
121        \cp $file_path $mount_dir/bin
122        echo "文件$file_name 拷贝成功"
123    fi
124done
125
126# 制作jffs2镜像文件
127/usr/sbin/mkfs.jffs2 -q -d $mount_dir -o $output_filepath
128chmod 755 $output_filepath
129echo "新镜像 $output_filepath 制作成功"
130# 卸载块设备
131umount $mount_dir
132echo "$mount_dir 卸载成功"
133modprobe -r mtdram
134modprobe -r mtdblock
135echo "mtd模块卸载成功"
136echo "Normal completion,OK!"
137exit 0
138