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] [--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="" 24 25if [ $# -lt 2 ]; then 26 usage 27 exit 1 28fi 29 30OUTPUT_FILE=$1 31SIZE=$2 32shift; shift 33 34SPARSE_IMG="false" 35if [[ "$1" == "-S" ]]; then 36 MKFS_OPTS+=" -S $SIZE" 37 SLOAD_OPTS+=" -S" 38 SPARSE_IMG="true" 39 shift 40fi 41 42if [[ "$1" == "-C" ]]; then 43 SLOAD_OPTS+=" -C $2" 44 shift; shift 45fi 46if [[ "$1" == "-f" ]]; then 47 SLOAD_OPTS+=" -f $2" 48 shift; shift 49fi 50if [[ "$1" == "-D" ]]; then 51 SLOAD_OPTS+=" -p $2" 52 shift; shift 53fi 54if [[ "$1" == "-s" ]]; then 55 SLOAD_OPTS+=" -s $2" 56 shift; shift 57fi 58if [[ "$1" == "-t" ]]; then 59 MOUNT_POINT=$2 60 shift; shift 61fi 62 63if [ -z $MOUNT_POINT ]; then 64 echo "Mount point is required" 65 exit 2 66fi 67 68if [[ ${MOUNT_POINT:0:1} != "/" ]]; then 69 MOUNT_POINT="/"$MOUNT_POINT 70fi 71 72SLOAD_OPTS+=" -t $MOUNT_POINT" 73 74if [[ "$1" == "-T" ]]; then 75 SLOAD_OPTS+=" -T $2" 76 shift; shift 77fi 78 79if [[ "$1" == "-L" ]]; then 80 MKFS_OPTS+=" -l $2" 81 shift; shift 82fi 83 84if [[ "$1" == "--prjquota" ]]; then 85 MKFS_OPTS+=" -O project_quota,extra_attr" 86 shift; 87fi 88if [[ "$1" == "--casefold" ]]; then 89 MKFS_OPTS+=" -O casefold -C utf8" 90 shift; 91fi 92 93if [[ "$1" == "--compression" ]]; then 94 COMPRESS_SUPPORT=1 95 MKFS_OPTS+=" -O compression,extra_attr" 96 shift; 97fi 98if [[ "$1" == "--readonly" ]]; then 99 MKFS_OPTS+=" -O ro" 100 READONLY=1 101 shift; 102fi 103 104if [[ "$1" == "--sldc" ]]; then 105 if [ -z "$COMPRESS_SUPPORT" ]; then 106 echo "--sldc needs --compression flag" 107 exit 3 108 fi 109 SLOAD_OPTS+=" -c" 110 shift 111 SLDC_NUM_ARGS=$1 112 case $SLDC_NUM_ARGS in 113 ''|*[!0-9]*) 114 echo "--sldc needs a number" 115 exit 3 ;; 116 esac 117 shift 118 while [ $SLDC_NUM_ARGS -gt 0 ]; do 119 SLOAD_OPTS+=" $1" 120 shift 121 (( SLDC_NUM_ARGS-- )) 122 done 123fi 124 125if [ -z $SIZE ]; then 126 echo "Need size of filesystem" 127 exit 2 128fi 129 130function _truncate() 131{ 132 if [ "$SPARSE_IMG" = "true" ]; then 133 return 134 fi 135 136 TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE" 137 echo $TRUNCATE_CMD 138 $TRUNCATE_CMD 139 if [ $? -ne 0 ]; then 140 exit 3 141 fi 142} 143 144function _build() 145{ 146 MAKE_F2FS_CMD="make_f2fs -g android $MKFS_OPTS $OUTPUT_FILE" 147 echo $MAKE_F2FS_CMD 148 $MAKE_F2FS_CMD 149 if [ $? -ne 0 ]; then 150 if [ "$SPARSE_IMG" = "false" ]; then 151 rm -f $OUTPUT_FILE 152 fi 153 exit 4 154 fi 155 156 SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE" 157 echo $SLOAD_F2FS_CMD 158 MB_SIZE=`$SLOAD_F2FS_CMD | grep "Max image size" | awk '{print $5}'` 159 # allow 1: Filesystem errors corrected 160 ret=$? 161 if [ $ret -ne 0 ] && [ $ret -ne 1 ]; then 162 rm -f $OUTPUT_FILE 163 exit 4 164 fi 165 SIZE=$(((MB_SIZE + 6) * 1024 * 1024)) 166} 167 168_truncate 169_build 170 171# readonly + compress can reduce the image 172if [ "$READONLY" ] && [ "$COMPRESS_SUPPORT" ]; then 173 if [ "$SPARSE_IMG" = "true" ]; then 174 MKFS_OPTS+=" -S $SIZE" 175 rm -f $OUTPUT_FILE && touch $OUTPUT_FILE 176 fi 177 _truncate 178 _build 179fi 180exit 0 181