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] [-d BASE_ALLOC_FILE_IN ] [-A BASE_ALLOC_FILE_OUT ] [-L LABEL] [FILE_CONTEXTS]
10EOT
11}
12
13ENABLE_SPARSE_IMAGE=
14if [ "$1" = "-s" ]; then
15  ENABLE_SPARSE_IMAGE="-s"
16  shift
17fi
18
19if [ $# -lt 5 ]; then
20  usage
21  exit 1
22fi
23
24SRC_DIR=$1
25if [ ! -d $SRC_DIR ]; then
26  echo "Can not find directory $SRC_DIR!"
27  exit 2
28fi
29
30OUTPUT_FILE=$2
31EXT_VARIANT=$3
32MOUNT_POINT=$4
33SIZE=$5
34shift; shift; shift; shift; shift
35
36JOURNAL_FLAGS=
37if [ "$1" = "-j" ]; then
38  if [ "$2" = "0" ]; then
39    JOURNAL_FLAGS="-J"
40  else
41    JOURNAL_FLAGS="-j $2"
42  fi
43  shift; shift
44fi
45
46TIMESTAMP=-1
47if [[ "$1" == "-T" ]]; then
48  TIMESTAMP=$2
49  shift; shift
50fi
51
52FS_CONFIG=
53if [[ "$1" == "-C" ]]; then
54  FS_CONFIG=$2
55  shift; shift
56fi
57
58PRODUCT_OUT=
59if [[ "$1" == "-D" ]]; then
60  PRODUCT_OUT=$2
61  shift; shift
62fi
63
64BLOCK_LIST=
65if [[ "$1" == "-B" ]]; then
66  BLOCK_LIST=$2
67  shift; shift
68fi
69
70BASE_ALLOC_FILE_IN=
71if [[ "$1" == "-d" ]]; then
72  BASE_ALLOC_FILE_IN=$2
73  shift; shift
74fi
75
76BASE_ALLOC_FILE_OUT=
77if [[ "$1" == "-A" ]]; then
78  BASE_ALLOC_FILE_OUT=$2
79  shift; shift
80fi
81
82LABEL=
83if [[ "$1" == "-L" ]]; then
84  LABEL=$2
85  shift; shift
86fi
87
88FC=$1
89
90case $EXT_VARIANT in
91  ext4) ;;
92  *) echo "Only ext4 is supported!"; exit 3 ;;
93esac
94
95if [ -z $MOUNT_POINT ]; then
96  echo "Mount point is required"
97  exit 2
98fi
99
100if [ -z $SIZE ]; then
101  echo "Need size of filesystem"
102  exit 2
103fi
104
105OPT=""
106if [ -n "$FC" ]; then
107  OPT="$OPT -S $FC"
108fi
109if [ -n "$FS_CONFIG" ]; then
110  OPT="$OPT -C $FS_CONFIG"
111fi
112if [ -n "$BLOCK_LIST" ]; then
113  OPT="$OPT -B $BLOCK_LIST"
114fi
115if [ -n "$BASE_ALLOC_FILE_IN" ]; then
116  OPT="$OPT -d $BASE_ALLOC_FILE_IN"
117fi
118if [ -n "$BASE_ALLOC_FILE_OUT" ]; then
119  OPT="$OPT -D $BASE_ALLOC_FILE_OUT"
120fi
121if [ -n "$LABEL" ]; then
122  OPT="$OPT -L $LABEL"
123fi
124
125MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE -T $TIMESTAMP $OPT -l $SIZE $JOURNAL_FLAGS -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR $PRODUCT_OUT"
126echo $MAKE_EXT4FS_CMD
127$MAKE_EXT4FS_CMD
128if [ $? -ne 0 ]; then
129  exit 4
130fi
131