blob: 018b24b78ed1bae459b4f8da02f0de3e1e09312d [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
Jin Qiane42388d2016-12-14 21:50:38 -0800101if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
102 MOUNT_POINT="/"$MOUNT_POINT
103fi
104
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800105if [ -z $SIZE ]; then
106 echo "Need size of filesystem"
107 exit 2
108fi
109
110BLOCKSIZE=4096
111# Round down the filesystem length to be a multiple of the block size
112SIZE=$((SIZE / BLOCKSIZE))
113
Jin Qiane42388d2016-12-14 21:50:38 -0800114# truncate output file since mke2fs will keep verity section in existing file
115cat /dev/null >$OUTPUT_FILE
116
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800117MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
118echo $MAKE_EXT4FS_CMD
119$MAKE_EXT4FS_CMD
120if [ $? -ne 0 ]; then
121 exit 4
122fi
123
Jin Qiane42388d2016-12-14 21:50:38 -0800124E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800125echo $E2FSDROID_CMD
126$E2FSDROID_CMD
127if [ $? -ne 0 ]; then
128 rm -f $OUTPUT_FILE
129 exit 4
130fi