blob: 811913a6cc58f63afc476b35b38d9806daf439b1 [file] [log] [blame]
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -08001#!/bin/bash
2#
3# To call this script, make sure mke2fs 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 ] [FILE_CONTEXTS]
12EOT
13}
14
15MKE2FS_OPTS=""
16E2FSDROID_OPTS=""
17
18if [ "$1" = "-s" ]; then
19 MKE2FS_OPTS+="-E android_sparse"
20 shift
21fi
22
23if [ $# -lt 5 ]; then
24 usage
25 exit 1
26fi
27
28SRC_DIR=$1
29if [ ! -d $SRC_DIR ]; then
30 echo "Can not find directory $SRC_DIR!"
31 exit 2
32fi
33
34OUTPUT_FILE=$2
35EXT_VARIANT=$3
36MOUNT_POINT=$4
37SIZE=$5
38shift; shift; shift; shift; shift
39
40if [ "$1" = "-j" ]; then
41 if [ "$2" = "0" ]; then
42 MKE2FS_OPTS+="-O ^has_journal"
43 else
44 MKE2FS_OPTS+="-J size=$2"
45 fi
46 shift; shift
47fi
48
49if [[ "$1" == "-T" ]]; then
50 E2FSDROID_OPTS+=" -T $2"
51 shift; shift
52fi
53
54if [[ "$1" == "-C" ]]; then
55 E2FSDROID_OPTS+=" -C $2"
56 shift; shift
57fi
58
59if [[ "$1" == "-D" ]]; then
60 E2FSDROID_OPTS+=" -p $2"
61 shift; shift
62fi
63
64if [[ "$1" == "-B" ]]; then
65 E2FSDROID_OPTS+=" -B $2"
66 shift; shift
67fi
68
69if [[ "$1" == "-d" ]]; then
70 E2FSDROID_OPTS+=" -d $2"
71 shift; shift
72fi
73
74if [[ "$1" == "-A" ]]; then
75 E2FSDROID_OPTS+=" -D $2"
76 shift; shift
77fi
78
79if [[ "$1" == "-L" ]]; then
80 MKE2FS_OPTS+=" -L $2"
81 shift; shift
82fi
83
84if [[ "$1" == "-i" ]]; then
85 MKE2FS_OPTS+=" -N $2"
86 shift; shift
87fi
88
89E2FSDROID_OPTS+=" -S $1"
90
91case $EXT_VARIANT in
92 ext4) ;;
93 *) echo "Only ext4 is supported!"; exit 3 ;;
94esac
95
96if [ -z $MOUNT_POINT ]; then
97 echo "Mount point is required"
98 exit 2
99fi
100
101if [ -z $SIZE ]; then
102 echo "Need size of filesystem"
103 exit 2
104fi
105
106BLOCKSIZE=4096
107# Round down the filesystem length to be a multiple of the block size
108SIZE=$((SIZE / BLOCKSIZE))
109
110MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
111echo $MAKE_EXT4FS_CMD
112$MAKE_EXT4FS_CMD
113if [ $? -ne 0 ]; then
114 exit 4
115fi
116
117E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a /$MOUNT_POINT $OUTPUT_FILE"
118echo $E2FSDROID_CMD
119$E2FSDROID_CMD
120if [ $? -ne 0 ]; then
121 rm -f $OUTPUT_FILE
122 exit 4
123fi