blob: e95a0c510a2b60ae65dff0566234c38e9663c438 [file] [log] [blame]
JP Abgrall8e9cdda2014-06-16 11:08:45 -07001#!/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
Daniel Rosenberg7e2d5922019-12-17 18:35:51 -08009 [-S] [-C FS_CONFIG] [-f SRC_DIR] [-D PRODUCT_OUT]
Jaegeuk Kim41190082021-06-13 09:00:31 -070010 [-s FILE_CONTEXTS] [-t MOUNT_POINT] [-T TIMESTAMP] [-B block_map]
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -070011 [-L LABEL] [--prjquota] [--casefold] [--compression] [--readonly]
Robin Hsu25d59fe2020-11-03 15:36:08 +080012 [--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
JP Abgrall8e9cdda2014-06-16 11:08:45 -070017EOT
18}
19
20echo "in mkf2fsuserimg.sh PATH=$PATH"
21
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -080022MKFS_OPTS=""
23SLOAD_OPTS=""
Jaegeuk Kim41190082021-06-13 09:00:31 -070024BLOCK_MAP_FILE=""
25BLOCK_MAP_OPT=""
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -080026
JP Abgrall8e9cdda2014-06-16 11:08:45 -070027if [ $# -lt 2 ]; then
28 usage
29 exit 1
30fi
31
32OUTPUT_FILE=$1
33SIZE=$2
34shift; shift
35
Alistair Delvac5c323d2019-10-16 10:40:23 -070036SPARSE_IMG="false"
37if [[ "$1" == "-S" ]]; then
38 MKFS_OPTS+=" -S $SIZE"
39 SLOAD_OPTS+=" -S"
Jaegeuk Kim41190082021-06-13 09:00:31 -070040 BLOCK_MAP_OPT+=" -S -M"
Alistair Delvac5c323d2019-10-16 10:40:23 -070041 SPARSE_IMG="true"
42 shift
43fi
44
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -080045if [[ "$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
Jaegeuk Kim41190082021-06-13 09:00:31 -070082if [[ "$1" == "-B" ]]; then
83 BLOCK_MAP_FILE="$2"
84 shift; shift
85fi
86
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -080087if [[ "$1" == "-L" ]]; then
88 MKFS_OPTS+=" -l $2"
89 shift; shift
90fi
JP Abgrall8e9cdda2014-06-16 11:08:45 -070091
Daniel Rosenbergf9c2a1d2019-12-17 14:46:25 -080092if [[ "$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
Jaegeuk Kim7fcefe02020-10-12 19:47:58 -0700101if [[ "$1" == "--compression" ]]; then
Robin Hsu25d59fe2020-11-03 15:36:08 +0800102 COMPRESS_SUPPORT=1
Jaegeuk Kim7fcefe02020-10-12 19:47:58 -0700103 MKFS_OPTS+=" -O compression,extra_attr"
104 shift;
105fi
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700106if [[ "$1" == "--readonly" ]]; then
107 MKFS_OPTS+=" -O ro"
108 READONLY=1
109 shift;
110fi
Jaegeuk Kim7fcefe02020-10-12 19:47:58 -0700111
Robin Hsu25d59fe2020-11-03 15:36:08 +0800112if [[ "$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
JP Abgrall8e9cdda2014-06-16 11:08:45 -0700133if [ -z $SIZE ]; then
134 echo "Need size of filesystem"
135 exit 2
136fi
137
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700138function _truncate()
139{
140 if [ "$SPARSE_IMG" = "true" ]; then
141 return
142 fi
143
Alistair Delvac5c323d2019-10-16 10:40:23 -0700144 TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE"
145 echo $TRUNCATE_CMD
146 $TRUNCATE_CMD
147 if [ $? -ne 0 ]; then
148 exit 3
149 fi
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700150}
Alistair Delvac5c323d2019-10-16 10:40:23 -0700151
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700152function _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
Alistair Delvac5c323d2019-10-16 10:40:23 -0700162 fi
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -0800163
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700164 SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE"
165 echo $SLOAD_F2FS_CMD
Weizhao Ouyang18750c52023-04-18 15:56:43 +0800166 SLOAD_LOG=`$SLOAD_F2FS_CMD`
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700167 # 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
Weizhao Ouyang18750c52023-04-18 15:56:43 +0800173 MB_SIZE=`echo "$SLOAD_LOG" | grep "Max image size" | awk '{print $5}'`
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700174 SIZE=$(((MB_SIZE + 6) * 1024 * 1024))
175}
176
177_truncate
178_build
179
Jaegeuk Kim41190082021-06-13 09:00:31 -0700180# readonly can reduce the image
181if [ "$READONLY" ]; then
Jaegeuk Kimf3b90a92021-05-20 23:16:10 -0700182 if [ "$SPARSE_IMG" = "true" ]; then
183 MKFS_OPTS+=" -S $SIZE"
184 rm -f $OUTPUT_FILE && touch $OUTPUT_FILE
185 fi
186 _truncate
187 _build
Jaegeuk Kim41190082021-06-13 09:00:31 -0700188 # build block map
189 if [ "$BLOCK_MAP_FILE" ]; then
190 fsck.f2fs $BLOCK_MAP_OPT $OUTPUT_FILE > $BLOCK_MAP_FILE
191 fi
Jaegeuk Kim1e4263b2017-11-28 19:41:30 -0800192fi
Jaegeuk Kim1b6e8b82020-08-17 23:35:51 -0700193exit 0