summaryrefslogtreecommitdiff
path: root/f2fs_utils/mkf2fsuserimg.sh
blob: 4290c50b3d1c3e727af203ac468f7a7a1e9b2c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
#
# To call this script, make sure make_f2fs is somewhere in PATH

function usage() {
cat<<EOT
Usage:
${0##*/} OUTPUT_FILE SIZE
         [-S] [-C FS_CONFIG] [-f SRC_DIR] [-D PRODUCT_OUT]
         [-s FILE_CONTEXTS] [-t MOUNT_POINT] [-T TIMESTAMP] [-B block_map]
         [-L LABEL] [--prjquota] [--casefold] [--compression] [--readonly]
         [--sldc <num> [sload compression sub-options]] [-b <block_size>]
<num>: number of the sload compression args, e.g.  -a LZ4 counts as 2
       when sload compression args are not given, <num> must be 0,
       and the default flags will be used.
Note: must conserve the option order
EOT
}

echo "in mkf2fsuserimg.sh PATH=$PATH"

MKFS_OPTS=""
SLOAD_OPTS=""
BLOCK_MAP_FILE=""
BLOCK_MAP_OPT=""

if [ $# -lt 2 ]; then
  usage
  exit 1
fi

OUTPUT_FILE=$1
SIZE=$2
shift; shift

SPARSE_IMG="false"
if [[ "$1" == "-S" ]]; then
  MKFS_OPTS+=" -S $SIZE"
  SLOAD_OPTS+=" -S"
  BLOCK_MAP_OPT+=" -S -M"
  SPARSE_IMG="true"
  shift
fi

if [[ "$1" == "-C" ]]; then
  SLOAD_OPTS+=" -C $2"
  shift; shift
fi
if [[ "$1" == "-f" ]]; then
  SLOAD_OPTS+=" -f $2"
  shift; shift
fi
if [[ "$1" == "-D" ]]; then
  SLOAD_OPTS+=" -p $2"
  shift; shift
fi
if [[ "$1" == "-s" ]]; then
  SLOAD_OPTS+=" -s $2"
  shift; shift
fi
if [[ "$1" == "-t" ]]; then
  MOUNT_POINT=$2
  shift; shift
fi

if [ -z $MOUNT_POINT ]; then
  echo "Mount point is required"
  exit 2
fi

if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
  MOUNT_POINT="/"$MOUNT_POINT
fi

SLOAD_OPTS+=" -t $MOUNT_POINT"

if [[ "$1" == "-T" ]]; then
  SLOAD_OPTS+=" -T $2"
  shift; shift
fi

if [[ "$1" == "-B" ]]; then
  BLOCK_MAP_FILE="$2"
  shift; shift
fi

if [[ "$1" == "-L" ]]; then
  MKFS_OPTS+=" -l $2"
  shift; shift
fi

if [[ "$1" == "--prjquota" ]]; then
  MKFS_OPTS+=" -O project_quota,extra_attr"
  shift;
fi
if [[ "$1" == "--casefold" ]]; then
  MKFS_OPTS+=" -O casefold -C utf8"
  shift;
fi

if [[ "$1" == "--compression" ]]; then
  COMPRESS_SUPPORT=1
  MKFS_OPTS+=" -O compression,extra_attr"
  shift;
fi
if [[ "$1" == "--readonly" ]]; then
  MKFS_OPTS+=" -O ro"
  READONLY=1
  shift;
fi

if [[ "$1" == "--sldc" ]]; then
  if [ -z "$COMPRESS_SUPPORT" ]; then
    echo "--sldc needs --compression flag"
    exit 3
  fi
  SLOAD_OPTS+=" -c"
  shift
  SLDC_NUM_ARGS=$1
  case $SLDC_NUM_ARGS in
    ''|*[!0-9]*)
      echo "--sldc needs a number"
      exit 3 ;;
  esac
  shift
  while [ $SLDC_NUM_ARGS -gt 0 ]; do
    SLOAD_OPTS+=" $1"
    shift
    (( SLDC_NUM_ARGS-- ))
  done
fi

if [[ "$1" == "-b" ]]; then
  shift
  BLOCKSIZE=$1
  case $BLOCKSIZE in
    ''|*[!0-9]*)
      echo "-b needs a number"
      exit 3 ;;
  esac
  shift
  MKFS_OPTS+=" -b $BLOCKSIZE"
  MKFS_OPTS+=" -w $BLOCKSIZE"
fi

if [ -z $SIZE ]; then
  echo "Need size of filesystem"
  exit 2
fi

function _truncate()
{
  if [ "$SPARSE_IMG" = "true" ]; then
    return
  fi

  TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE"
  echo $TRUNCATE_CMD
  $TRUNCATE_CMD
  if [ $? -ne 0 ]; then
    exit 3
  fi
}

function _build()
{
  MAKE_F2FS_CMD="make_f2fs -g android $MKFS_OPTS $OUTPUT_FILE"
  echo $MAKE_F2FS_CMD
  $MAKE_F2FS_CMD
  if [ $? -ne 0 ]; then
    if [ "$SPARSE_IMG" = "false" ]; then
      rm -f $OUTPUT_FILE
    fi
    exit 4
  fi

  SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE"
  echo $SLOAD_F2FS_CMD
  SLOAD_LOG=`$SLOAD_F2FS_CMD`
  # allow 1: Filesystem errors corrected
  ret=$?
  if [ $ret -ne 0 ] && [ $ret -ne 1 ]; then
    rm -f $OUTPUT_FILE
    exit 4
  fi
  MB_SIZE=`echo "$SLOAD_LOG" | grep "Max image size" | awk '{print $5}'`
  SIZE=$(((MB_SIZE + 6) * 1024 * 1024))
}

_truncate
_build

# readonly can reduce the image
if [ "$READONLY" ]; then
  if [ "$SPARSE_IMG" = "true" ]; then
    MKFS_OPTS+=" -S $SIZE"
    rm -f $OUTPUT_FILE && touch $OUTPUT_FILE
  fi
  _truncate
  _build
  # build block map
  if [ "$BLOCK_MAP_FILE" ]; then
    fsck.f2fs $BLOCK_MAP_OPT $OUTPUT_FILE > $BLOCK_MAP_FILE
  fi
fi
exit 0