1#!/bin/bash
2#
3# To call this script, make sure make_ext4fs is somewhere in PATH
4
5function usage() {
6cat<<EOT
7Usage:
8mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE [-j <journal_size>]
9             [-T TIMESTAMP] [-C FS_CONFIG] [-D PRODUCT_OUT] [-B BLOCK_LIST_FILE]
10             [-d BASE_ALLOC_FILE_IN ] [-A BASE_ALLOC_FILE_OUT ] [-L LABEL]
11             [-i INODES ] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE] [FILE_CONTEXTS]
12EOT
13}
14
15ENABLE_SPARSE_IMAGE=
16if [ "$1" = "-s" ]; then
17  ENABLE_SPARSE_IMAGE="-s"
18  shift
19fi
20
21if [ $# -lt 5 ]; then
22  usage
23  exit 1
24fi
25
26SRC_DIR=$1
27if [ ! -d $SRC_DIR ]; then
28  echo "Can not find directory $SRC_DIR!"
29  exit 2
30fi
31
32OUTPUT_FILE=$2
33EXT_VARIANT=$3
34MOUNT_POINT=$4
35SIZE=$5
36shift; shift; shift; shift; shift
37
38JOURNAL_FLAGS=
39if [ "$1" = "-j" ]; then
40  if [ "$2" = "0" ]; then
41    JOURNAL_FLAGS="-J"
42  else
43    JOURNAL_FLAGS="-j $2"
44  fi
45  shift; shift
46fi
47
48TIMESTAMP=-1
49if [[ "$1" == "-T" ]]; then
50  TIMESTAMP=$2
51  shift; shift
52fi
53
54FS_CONFIG=
55if [[ "$1" == "-C" ]]; then
56  FS_CONFIG=$2
57  shift; shift
58fi
59
60PRODUCT_OUT=
61if [[ "$1" == "-D" ]]; then
62  PRODUCT_OUT=$2
63  shift; shift
64fi
65
66BLOCK_LIST=
67if [[ "$1" == "-B" ]]; then
68  BLOCK_LIST=$2
69  shift; shift
70fi
71
72BASE_ALLOC_FILE_IN=
73if [[ "$1" == "-d" ]]; then
74  BASE_ALLOC_FILE_IN=$2
75  shift; shift
76fi
77
78BASE_ALLOC_FILE_OUT=
79if [[ "$1" == "-A" ]]; then
80  BASE_ALLOC_FILE_OUT=$2
81  shift; shift
82fi
83
84LABEL=
85if [[ "$1" == "-L" ]]; then
86  LABEL=$2
87  shift; shift
88fi
89
90INODES=
91if [[ "$1" == "-i" ]]; then
92  INODES=$2
93  shift; shift
94fi
95
96ERASE_SIZE=
97if [[ "$1" == "-e" ]]; then
98    ERASE_SIZE=$2
99    shift; shift
100fi
101
102FLASH_BLOCK_SIZE=
103if [[ "$1" == "-o" ]]; then
104    FLASH_BLOCK_SIZE=$2
105    shift; shift
106fi
107FC=$1
108
109case $EXT_VARIANT in
110  ext4) ;;
111  *) echo "Only ext4 is supported!"; exit 3 ;;
112esac
113
114if [ -z $MOUNT_POINT ]; then
115  echo "Mount point is required"
116  exit 2
117fi
118
119if [ -z $SIZE ]; then
120  echo "Need size of filesystem"
121  exit 2
122fi
123
124OPT=""
125if [ -n "$FC" ]; then
126  OPT="$OPT -S $FC"
127fi
128if [ -n "$FS_CONFIG" ]; then
129  OPT="$OPT -C $FS_CONFIG"
130fi
131if [ -n "$BLOCK_LIST" ]; then
132  OPT="$OPT -B $BLOCK_LIST"
133fi
134if [ -n "$BASE_ALLOC_FILE_IN" ]; then
135  OPT="$OPT -d $BASE_ALLOC_FILE_IN"
136fi
137if [ -n "$BASE_ALLOC_FILE_OUT" ]; then
138  OPT="$OPT -D $BASE_ALLOC_FILE_OUT"
139fi
140if [ -n "$LABEL" ]; then
141  OPT="$OPT -L $LABEL"
142fi
143if [ -n "$INODES" ]; then
144  OPT="$OPT -i $INODES"
145fi
146if [ -n "$ERASE_SIZE" ]; then
147  OPT="$OPT -e $ERASE_SIZE"
148fi
149if [ -n "$FLASH_BLOCK_SIZE" ]; then
150  OPT="$OPT -o $FLASH_BLOCK_SIZE"
151fi
152
153MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE -T $TIMESTAMP $OPT -l $SIZE $JOURNAL_FLAGS -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR $PRODUCT_OUT"
154echo $MAKE_EXT4FS_CMD
155$MAKE_EXT4FS_CMD
156if [ $? -ne 0 ]; then
157  exit 4
158fi
159