• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# To call this script, make sure make_f2fs is somewhere in PATH
4
5function usage() {
6cat<<EOT
7Usage:
8${0##*/} OUTPUT_FILE SIZE
9         [-S] [-C FS_CONFIG] [-f SRC_DIR] [-D PRODUCT_OUT]
10         [-s FILE_CONTEXTS] [-t MOUNT_POINT] [-T TIMESTAMP] [-B block_map]
11         [-L LABEL] [--prjquota] [--casefold] [--compression] [--readonly]
12         [--sldc <num> [sload compression sub-options]]
13<num>: number of the sload compression args, e.g.  -a LZ4 counts as 2
14       when sload compression args are not given, <num> must be 0,
15       and the default flags will be used.
16Note: must conserve the option order
17EOT
18}
19
20echo "in mkf2fsuserimg.sh PATH=$PATH"
21
22MKFS_OPTS=""
23SLOAD_OPTS=""
24BLOCK_MAP_FILE=""
25BLOCK_MAP_OPT=""
26
27if [ $# -lt 2 ]; then
28  usage
29  exit 1
30fi
31
32OUTPUT_FILE=$1
33SIZE=$2
34shift; shift
35
36SPARSE_IMG="false"
37if [[ "$1" == "-S" ]]; then
38  MKFS_OPTS+=" -S $SIZE"
39  SLOAD_OPTS+=" -S"
40  BLOCK_MAP_OPT+=" -S -M"
41  SPARSE_IMG="true"
42  shift
43fi
44
45if [[ "$1" == "-C" ]]; then
46  SLOAD_OPTS+=" -C $2"
47  shift; shift
48fi
49if [[ "$1" == "-f" ]]; then
50  SLOAD_OPTS+=" -f $2"
51  shift; shift
52fi
53if [[ "$1" == "-D" ]]; then
54  SLOAD_OPTS+=" -p $2"
55  shift; shift
56fi
57if [[ "$1" == "-s" ]]; then
58  SLOAD_OPTS+=" -s $2"
59  shift; shift
60fi
61if [[ "$1" == "-t" ]]; then
62  MOUNT_POINT=$2
63  shift; shift
64fi
65
66if [ -z $MOUNT_POINT ]; then
67  echo "Mount point is required"
68  exit 2
69fi
70
71if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
72  MOUNT_POINT="/"$MOUNT_POINT
73fi
74
75SLOAD_OPTS+=" -t $MOUNT_POINT"
76
77if [[ "$1" == "-T" ]]; then
78  SLOAD_OPTS+=" -T $2"
79  shift; shift
80fi
81
82if [[ "$1" == "-B" ]]; then
83  BLOCK_MAP_FILE="$2"
84  shift; shift
85fi
86
87if [[ "$1" == "-L" ]]; then
88  MKFS_OPTS+=" -l $2"
89  shift; shift
90fi
91
92if [[ "$1" == "--prjquota" ]]; then
93  MKFS_OPTS+=" -O project_quota,extra_attr"
94  shift;
95fi
96if [[ "$1" == "--casefold" ]]; then
97  MKFS_OPTS+=" -O casefold -C utf8"
98  shift;
99fi
100
101if [[ "$1" == "--compression" ]]; then
102  COMPRESS_SUPPORT=1
103  MKFS_OPTS+=" -O compression,extra_attr"
104  shift;
105fi
106if [[ "$1" == "--readonly" ]]; then
107  MKFS_OPTS+=" -O ro"
108  READONLY=1
109  shift;
110fi
111
112if [[ "$1" == "--sldc" ]]; then
113  if [ -z "$COMPRESS_SUPPORT" ]; then
114    echo "--sldc needs --compression flag"
115    exit 3
116  fi
117  SLOAD_OPTS+=" -c"
118  shift
119  SLDC_NUM_ARGS=$1
120  case $SLDC_NUM_ARGS in
121    ''|*[!0-9]*)
122      echo "--sldc needs a number"
123      exit 3 ;;
124  esac
125  shift
126  while [ $SLDC_NUM_ARGS -gt 0 ]; do
127    SLOAD_OPTS+=" $1"
128    shift
129    (( SLDC_NUM_ARGS-- ))
130  done
131fi
132
133if [ -z $SIZE ]; then
134  echo "Need size of filesystem"
135  exit 2
136fi
137
138function _truncate()
139{
140  if [ "$SPARSE_IMG" = "true" ]; then
141    return
142  fi
143
144  TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE"
145  echo $TRUNCATE_CMD
146  $TRUNCATE_CMD
147  if [ $? -ne 0 ]; then
148    exit 3
149  fi
150}
151
152function _build()
153{
154  MAKE_F2FS_CMD="make_f2fs -g android $MKFS_OPTS $OUTPUT_FILE"
155  echo $MAKE_F2FS_CMD
156  $MAKE_F2FS_CMD
157  if [ $? -ne 0 ]; then
158    if [ "$SPARSE_IMG" = "false" ]; then
159      rm -f $OUTPUT_FILE
160    fi
161    exit 4
162  fi
163
164  SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE"
165  echo $SLOAD_F2FS_CMD
166  SLOAD_LOG=`$SLOAD_F2FS_CMD`
167  # allow 1: Filesystem errors corrected
168  ret=$?
169  if [ $ret -ne 0 ] && [ $ret -ne 1 ]; then
170    rm -f $OUTPUT_FILE
171    exit 4
172  fi
173  MB_SIZE=`echo "$SLOAD_LOG" | grep "Max image size" | awk '{print $5}'`
174  SIZE=$(((MB_SIZE + 6) * 1024 * 1024))
175}
176
177_truncate
178_build
179
180# readonly can reduce the image
181if [ "$READONLY" ]; then
182  if [ "$SPARSE_IMG" = "true" ]; then
183    MKFS_OPTS+=" -S $SIZE"
184    rm -f $OUTPUT_FILE && touch $OUTPUT_FILE
185  fi
186  _truncate
187  _build
188  # build block map
189  if [ "$BLOCK_MAP_FILE" ]; then
190    fsck.f2fs $BLOCK_MAP_OPT $OUTPUT_FILE > $BLOCK_MAP_FILE
191  fi
192fi
193exit 0
194