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]
11         [-L LABEL] [--prjquota] [--casefold]
12EOT
13}
14
15echo "in mkf2fsuserimg.sh PATH=$PATH"
16
17MKFS_OPTS=""
18SLOAD_OPTS=""
19
20if [ $# -lt 2 ]; then
21  usage
22  exit 1
23fi
24
25OUTPUT_FILE=$1
26SIZE=$2
27shift; shift
28
29SPARSE_IMG="false"
30if [[ "$1" == "-S" ]]; then
31  MKFS_OPTS+=" -S $SIZE"
32  SLOAD_OPTS+=" -S"
33  SPARSE_IMG="true"
34  shift
35fi
36
37if [[ "$1" == "-C" ]]; then
38  SLOAD_OPTS+=" -C $2"
39  shift; shift
40fi
41if [[ "$1" == "-f" ]]; then
42  SLOAD_OPTS+=" -f $2"
43  shift; shift
44fi
45if [[ "$1" == "-D" ]]; then
46  SLOAD_OPTS+=" -p $2"
47  shift; shift
48fi
49if [[ "$1" == "-s" ]]; then
50  SLOAD_OPTS+=" -s $2"
51  shift; shift
52fi
53if [[ "$1" == "-t" ]]; then
54  MOUNT_POINT=$2
55  shift; shift
56fi
57
58if [ -z $MOUNT_POINT ]; then
59  echo "Mount point is required"
60  exit 2
61fi
62
63if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
64  MOUNT_POINT="/"$MOUNT_POINT
65fi
66
67SLOAD_OPTS+=" -t $MOUNT_POINT"
68
69if [[ "$1" == "-T" ]]; then
70  SLOAD_OPTS+=" -T $2"
71  shift; shift
72fi
73
74if [[ "$1" == "-L" ]]; then
75  MKFS_OPTS+=" -l $2"
76  shift; shift
77fi
78
79if [[ "$1" == "--prjquota" ]]; then
80  MKFS_OPTS+=" -O project_quota,extra_attr"
81  shift;
82fi
83if [[ "$1" == "--casefold" ]]; then
84  MKFS_OPTS+=" -O casefold -C utf8"
85  shift;
86fi
87
88if [ -z $SIZE ]; then
89  echo "Need size of filesystem"
90  exit 2
91fi
92
93if [ "$SPARSE_IMG" = "false" ]; then
94  TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE"
95  echo $TRUNCATE_CMD
96  $TRUNCATE_CMD
97  if [ $? -ne 0 ]; then
98    exit 3
99  fi
100fi
101
102MAKE_F2FS_CMD="make_f2fs -g android $MKFS_OPTS $OUTPUT_FILE"
103echo $MAKE_F2FS_CMD
104$MAKE_F2FS_CMD
105if [ $? -ne 0 ]; then
106  if [ "$SPARSE_IMG" = "false" ]; then
107    rm -f $OUTPUT_FILE
108  fi
109  exit 4
110fi
111
112SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE"
113echo $SLOAD_F2FS_CMD
114$SLOAD_F2FS_CMD
115if [ $? -ne 0 ]; then
116  rm -f $OUTPUT_FILE
117  exit 4
118fi
119