blob: f8aef217c06aa230523d4d34b24349499b809c89 [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
Jin Qian434347a2017-01-06 16:39:38 -080019 MKE2FS_OPTS+="-E android_sparse"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080020 shift
Jin Qian434347a2017-01-06 16:39:38 -080021else
22 E2FSDROID_OPTS+="-e"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080023fi
24
25if [ $# -lt 5 ]; then
26 usage
27 exit 1
28fi
29
30SRC_DIR=$1
31if [ ! -d $SRC_DIR ]; then
32 echo "Can not find directory $SRC_DIR!"
33 exit 2
34fi
35
36OUTPUT_FILE=$2
37EXT_VARIANT=$3
38MOUNT_POINT=$4
39SIZE=$5
40shift; shift; shift; shift; shift
41
42if [ "$1" = "-j" ]; then
43 if [ "$2" = "0" ]; then
44 MKE2FS_OPTS+="-O ^has_journal"
45 else
46 MKE2FS_OPTS+="-J size=$2"
47 fi
48 shift; shift
49fi
50
51if [[ "$1" == "-T" ]]; then
52 E2FSDROID_OPTS+=" -T $2"
53 shift; shift
54fi
55
56if [[ "$1" == "-C" ]]; then
57 E2FSDROID_OPTS+=" -C $2"
58 shift; shift
59fi
60
61if [[ "$1" == "-D" ]]; then
62 E2FSDROID_OPTS+=" -p $2"
63 shift; shift
64fi
65
66if [[ "$1" == "-B" ]]; then
67 E2FSDROID_OPTS+=" -B $2"
68 shift; shift
69fi
70
71if [[ "$1" == "-d" ]]; then
72 E2FSDROID_OPTS+=" -d $2"
73 shift; shift
74fi
75
76if [[ "$1" == "-A" ]]; then
77 E2FSDROID_OPTS+=" -D $2"
78 shift; shift
79fi
80
81if [[ "$1" == "-L" ]]; then
82 MKE2FS_OPTS+=" -L $2"
83 shift; shift
84fi
85
86if [[ "$1" == "-i" ]]; then
87 MKE2FS_OPTS+=" -N $2"
88 shift; shift
89fi
90
91E2FSDROID_OPTS+=" -S $1"
92
93case $EXT_VARIANT in
94 ext4) ;;
95 *) echo "Only ext4 is supported!"; exit 3 ;;
96esac
97
98if [ -z $MOUNT_POINT ]; then
99 echo "Mount point is required"
100 exit 2
101fi
102
Jin Qiane42388d2016-12-14 21:50:38 -0800103if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
104 MOUNT_POINT="/"$MOUNT_POINT
105fi
106
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800107if [ -z $SIZE ]; then
108 echo "Need size of filesystem"
109 exit 2
110fi
111
112BLOCKSIZE=4096
113# Round down the filesystem length to be a multiple of the block size
114SIZE=$((SIZE / BLOCKSIZE))
115
Jin Qiane42388d2016-12-14 21:50:38 -0800116# truncate output file since mke2fs will keep verity section in existing file
117cat /dev/null >$OUTPUT_FILE
118
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800119MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
120echo $MAKE_EXT4FS_CMD
121$MAKE_EXT4FS_CMD
122if [ $? -ne 0 ]; then
123 exit 4
124fi
125
Jin Qiane42388d2016-12-14 21:50:38 -0800126E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800127echo $E2FSDROID_CMD
128$E2FSDROID_CMD
129if [ $? -ne 0 ]; then
130 rm -f $OUTPUT_FILE
131 exit 4
132fi