JP Abgrall | 8e9cdda | 2014-06-16 11:08:45 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # To call this script, make sure make_f2fs is somewhere in PATH |
| 4 | |
| 5 | function usage() { |
| 6 | cat<<EOT |
| 7 | Usage: |
| 8 | ${0##*/} OUTPUT_FILE SIZE |
Daniel Rosenberg | 7e2d592 | 2019-12-17 18:35:51 -0800 | [diff] [blame] | 9 | [-S] [-C FS_CONFIG] [-f SRC_DIR] [-D PRODUCT_OUT] |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 10 | [-s FILE_CONTEXTS] [-t MOUNT_POINT] [-T TIMESTAMP] [-B block_map] |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 11 | [-L LABEL] [--prjquota] [--casefold] [--compression] [--readonly] |
Robin Hsu | 25d59fe | 2020-11-03 15:36:08 +0800 | [diff] [blame] | 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. |
| 16 | Note: must conserve the option order |
JP Abgrall | 8e9cdda | 2014-06-16 11:08:45 -0700 | [diff] [blame] | 17 | EOT |
| 18 | } |
| 19 | |
| 20 | echo "in mkf2fsuserimg.sh PATH=$PATH" |
| 21 | |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 22 | MKFS_OPTS="" |
| 23 | SLOAD_OPTS="" |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 24 | BLOCK_MAP_FILE="" |
| 25 | BLOCK_MAP_OPT="" |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 26 | |
JP Abgrall | 8e9cdda | 2014-06-16 11:08:45 -0700 | [diff] [blame] | 27 | if [ $# -lt 2 ]; then |
| 28 | usage |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | OUTPUT_FILE=$1 |
| 33 | SIZE=$2 |
| 34 | shift; shift |
| 35 | |
Alistair Delva | c5c323d | 2019-10-16 10:40:23 -0700 | [diff] [blame] | 36 | SPARSE_IMG="false" |
| 37 | if [[ "$1" == "-S" ]]; then |
| 38 | MKFS_OPTS+=" -S $SIZE" |
| 39 | SLOAD_OPTS+=" -S" |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 40 | BLOCK_MAP_OPT+=" -S -M" |
Alistair Delva | c5c323d | 2019-10-16 10:40:23 -0700 | [diff] [blame] | 41 | SPARSE_IMG="true" |
| 42 | shift |
| 43 | fi |
| 44 | |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 45 | if [[ "$1" == "-C" ]]; then |
| 46 | SLOAD_OPTS+=" -C $2" |
| 47 | shift; shift |
| 48 | fi |
| 49 | if [[ "$1" == "-f" ]]; then |
| 50 | SLOAD_OPTS+=" -f $2" |
| 51 | shift; shift |
| 52 | fi |
| 53 | if [[ "$1" == "-D" ]]; then |
| 54 | SLOAD_OPTS+=" -p $2" |
| 55 | shift; shift |
| 56 | fi |
| 57 | if [[ "$1" == "-s" ]]; then |
| 58 | SLOAD_OPTS+=" -s $2" |
| 59 | shift; shift |
| 60 | fi |
| 61 | if [[ "$1" == "-t" ]]; then |
| 62 | MOUNT_POINT=$2 |
| 63 | shift; shift |
| 64 | fi |
| 65 | |
| 66 | if [ -z $MOUNT_POINT ]; then |
| 67 | echo "Mount point is required" |
| 68 | exit 2 |
| 69 | fi |
| 70 | |
| 71 | if [[ ${MOUNT_POINT:0:1} != "/" ]]; then |
| 72 | MOUNT_POINT="/"$MOUNT_POINT |
| 73 | fi |
| 74 | |
| 75 | SLOAD_OPTS+=" -t $MOUNT_POINT" |
| 76 | |
| 77 | if [[ "$1" == "-T" ]]; then |
| 78 | SLOAD_OPTS+=" -T $2" |
| 79 | shift; shift |
| 80 | fi |
| 81 | |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 82 | if [[ "$1" == "-B" ]]; then |
| 83 | BLOCK_MAP_FILE="$2" |
| 84 | shift; shift |
| 85 | fi |
| 86 | |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 87 | if [[ "$1" == "-L" ]]; then |
| 88 | MKFS_OPTS+=" -l $2" |
| 89 | shift; shift |
| 90 | fi |
JP Abgrall | 8e9cdda | 2014-06-16 11:08:45 -0700 | [diff] [blame] | 91 | |
Daniel Rosenberg | f9c2a1d | 2019-12-17 14:46:25 -0800 | [diff] [blame] | 92 | if [[ "$1" == "--prjquota" ]]; then |
| 93 | MKFS_OPTS+=" -O project_quota,extra_attr" |
| 94 | shift; |
| 95 | fi |
| 96 | if [[ "$1" == "--casefold" ]]; then |
| 97 | MKFS_OPTS+=" -O casefold -C utf8" |
| 98 | shift; |
| 99 | fi |
| 100 | |
Jaegeuk Kim | 7fcefe0 | 2020-10-12 19:47:58 -0700 | [diff] [blame] | 101 | if [[ "$1" == "--compression" ]]; then |
Robin Hsu | 25d59fe | 2020-11-03 15:36:08 +0800 | [diff] [blame] | 102 | COMPRESS_SUPPORT=1 |
Jaegeuk Kim | 7fcefe0 | 2020-10-12 19:47:58 -0700 | [diff] [blame] | 103 | MKFS_OPTS+=" -O compression,extra_attr" |
| 104 | shift; |
| 105 | fi |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 106 | if [[ "$1" == "--readonly" ]]; then |
| 107 | MKFS_OPTS+=" -O ro" |
| 108 | READONLY=1 |
| 109 | shift; |
| 110 | fi |
Jaegeuk Kim | 7fcefe0 | 2020-10-12 19:47:58 -0700 | [diff] [blame] | 111 | |
Robin Hsu | 25d59fe | 2020-11-03 15:36:08 +0800 | [diff] [blame] | 112 | if [[ "$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 |
| 131 | fi |
| 132 | |
JP Abgrall | 8e9cdda | 2014-06-16 11:08:45 -0700 | [diff] [blame] | 133 | if [ -z $SIZE ]; then |
| 134 | echo "Need size of filesystem" |
| 135 | exit 2 |
| 136 | fi |
| 137 | |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 138 | function _truncate() |
| 139 | { |
| 140 | if [ "$SPARSE_IMG" = "true" ]; then |
| 141 | return |
| 142 | fi |
| 143 | |
Alistair Delva | c5c323d | 2019-10-16 10:40:23 -0700 | [diff] [blame] | 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 |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 150 | } |
Alistair Delva | c5c323d | 2019-10-16 10:40:23 -0700 | [diff] [blame] | 151 | |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 152 | function _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 Delva | c5c323d | 2019-10-16 10:40:23 -0700 | [diff] [blame] | 162 | fi |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 163 | |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 164 | SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE" |
| 165 | echo $SLOAD_F2FS_CMD |
Weizhao Ouyang | 18750c5 | 2023-04-18 15:56:43 +0800 | [diff] [blame] | 166 | SLOAD_LOG=`$SLOAD_F2FS_CMD` |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 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 |
Weizhao Ouyang | 18750c5 | 2023-04-18 15:56:43 +0800 | [diff] [blame] | 173 | MB_SIZE=`echo "$SLOAD_LOG" | grep "Max image size" | awk '{print $5}'` |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 174 | SIZE=$(((MB_SIZE + 6) * 1024 * 1024)) |
| 175 | } |
| 176 | |
| 177 | _truncate |
| 178 | _build |
| 179 | |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 180 | # readonly can reduce the image |
| 181 | if [ "$READONLY" ]; then |
Jaegeuk Kim | f3b90a9 | 2021-05-20 23:16:10 -0700 | [diff] [blame] | 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 |
Jaegeuk Kim | 4119008 | 2021-06-13 09:00:31 -0700 | [diff] [blame] | 188 | # build block map |
| 189 | if [ "$BLOCK_MAP_FILE" ]; then |
| 190 | fsck.f2fs $BLOCK_MAP_OPT $OUTPUT_FILE > $BLOCK_MAP_FILE |
| 191 | fi |
Jaegeuk Kim | 1e4263b | 2017-11-28 19:41:30 -0800 | [diff] [blame] | 192 | fi |
Jaegeuk Kim | 1b6e8b8 | 2020-08-17 23:35:51 -0700 | [diff] [blame] | 193 | exit 0 |