1#!/bin/bash -x
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 [FILE_CONTEXTS]
9EOT
10}
11
12echo "in mkuserimg.sh PATH=$PATH"
13
14ENABLE_SPARSE_IMAGE=
15if [ "$1" = "-s" ]; then
16  ENABLE_SPARSE_IMAGE="-s"
17  shift
18fi
19
20if [ $# -ne 5 -a $# -ne 6 ]; then
21  usage
22  exit 1
23fi
24
25SRC_DIR=$1
26if [ ! -d $SRC_DIR ]; then
27  echo "Can not find directory $SRC_DIR!"
28  exit 2
29fi
30
31OUTPUT_FILE=$2
32EXT_VARIANT=$3
33MOUNT_POINT=$4
34SIZE=$5
35FC=$6
36
37case $EXT_VARIANT in
38  ext4) ;;
39  *) echo "Only ext4 is supported!"; exit 3 ;;
40esac
41
42if [ -z $MOUNT_POINT ]; then
43  echo "Mount point is required"
44  exit 2
45fi
46
47if [ -z $SIZE ]; then
48  echo "Need size of filesystem"
49  exit 2
50fi
51
52if [ -n "$FC" ]; then
53    FCOPT="-S $FC"
54fi
55
56MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE $FCOPT -l $SIZE -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR"
57echo $MAKE_EXT4FS_CMD
58$MAKE_EXT4FS_CMD
59if [ $? -ne 0 ]; then
60  exit 4
61fi
62