blob: ea73ff704dd204dac5ac3ce32b1fd7d77d1a82e0 [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]
Jin Qian30cb1712017-08-28 17:28:49 -070011 [-i INODES ] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE]
12 [-U MKE2FS_UUID] [-S MKE2FS_HASH_SEED] [FILE_CONTEXTS]
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080013EOT
14}
15
Connor O'Brien923a9282017-01-05 16:53:58 -080016BLOCKSIZE=4096
17
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080018MKE2FS_OPTS=""
Connor O'Brien923a9282017-01-05 16:53:58 -080019MKE2FS_EXTENDED_OPTS=""
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080020E2FSDROID_OPTS=""
Jin Qian30cb1712017-08-28 17:28:49 -070021E2FSPROGS_FAKE_TIME=""
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080022
23if [ "$1" = "-s" ]; then
Connor O'Brien923a9282017-01-05 16:53:58 -080024 MKE2FS_EXTENDED_OPTS+="android_sparse"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080025 shift
Jin Qian434347a2017-01-06 16:39:38 -080026else
27 E2FSDROID_OPTS+="-e"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080028fi
29
30if [ $# -lt 5 ]; then
31 usage
32 exit 1
33fi
34
35SRC_DIR=$1
36if [ ! -d $SRC_DIR ]; then
37 echo "Can not find directory $SRC_DIR!"
38 exit 2
39fi
40
41OUTPUT_FILE=$2
42EXT_VARIANT=$3
43MOUNT_POINT=$4
44SIZE=$5
45shift; shift; shift; shift; shift
46
47if [ "$1" = "-j" ]; then
48 if [ "$2" = "0" ]; then
49 MKE2FS_OPTS+="-O ^has_journal"
50 else
51 MKE2FS_OPTS+="-J size=$2"
52 fi
53 shift; shift
54fi
55
56if [[ "$1" == "-T" ]]; then
57 E2FSDROID_OPTS+=" -T $2"
Jin Qian30cb1712017-08-28 17:28:49 -070058 E2FSPROGS_FAKE_TIME=$2
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080059 shift; shift
60fi
61
62if [[ "$1" == "-C" ]]; then
63 E2FSDROID_OPTS+=" -C $2"
64 shift; shift
65fi
66
67if [[ "$1" == "-D" ]]; then
68 E2FSDROID_OPTS+=" -p $2"
69 shift; shift
70fi
71
72if [[ "$1" == "-B" ]]; then
73 E2FSDROID_OPTS+=" -B $2"
74 shift; shift
75fi
76
77if [[ "$1" == "-d" ]]; then
78 E2FSDROID_OPTS+=" -d $2"
79 shift; shift
80fi
81
82if [[ "$1" == "-A" ]]; then
83 E2FSDROID_OPTS+=" -D $2"
84 shift; shift
85fi
86
87if [[ "$1" == "-L" ]]; then
88 MKE2FS_OPTS+=" -L $2"
89 shift; shift
90fi
91
92if [[ "$1" == "-i" ]]; then
93 MKE2FS_OPTS+=" -N $2"
94 shift; shift
95fi
96
Connor O'Brien923a9282017-01-05 16:53:58 -080097if [[ "$1" == "-e" ]]; then
Ben Fennema22650522017-03-08 14:21:00 -080098 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -080099 MKE2FS_EXTENDED_OPTS+=","
100 fi
101 MKE2FS_EXTENDED_OPTS+="stripe_width=$(($2/BLOCKSIZE))"
102 shift; shift
103fi
104
105if [[ "$1" == "-o" ]]; then
Ben Fennema22650522017-03-08 14:21:00 -0800106 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -0800107 MKE2FS_EXTENDED_OPTS+=","
108 fi
109 # stride should be the max of 8kb and the logical block size
110 MKE2FS_EXTENDED_OPTS+="stride=$((($2 > 8192 ? $2 : 8192) / BLOCKSIZE))"
111 shift; shift
112fi
113
Jin Qian30cb1712017-08-28 17:28:49 -0700114if [[ "$1" == "-U" ]]; then
115 MKE2FS_OPTS+=" -U $2"
116 shift; shift
117fi
118
119if [[ "$1" == "-S" ]]; then
120 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
121 MKE2FS_EXTENDED_OPTS+=","
122 fi
123 MKE2FS_EXTENDED_OPTS+="hash_seed=$2"
124 shift; shift
125fi
126
Ben Fennema22650522017-03-08 14:21:00 -0800127if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -0800128 MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
129fi
130
Ben Fennema22650522017-03-08 14:21:00 -0800131if [[ $1 ]]; then
132 E2FSDROID_OPTS+=" -S $1"
133fi
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800134
135case $EXT_VARIANT in
136 ext4) ;;
137 *) echo "Only ext4 is supported!"; exit 3 ;;
138esac
139
140if [ -z $MOUNT_POINT ]; then
141 echo "Mount point is required"
142 exit 2
143fi
144
Jin Qiane42388d2016-12-14 21:50:38 -0800145if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
146 MOUNT_POINT="/"$MOUNT_POINT
147fi
148
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800149if [ -z $SIZE ]; then
150 echo "Need size of filesystem"
151 exit 2
152fi
153
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800154# Round down the filesystem length to be a multiple of the block size
155SIZE=$((SIZE / BLOCKSIZE))
156
Jin Qiane42388d2016-12-14 21:50:38 -0800157# truncate output file since mke2fs will keep verity section in existing file
158cat /dev/null >$OUTPUT_FILE
159
Jin Qian30cb1712017-08-28 17:28:49 -0700160MAKE_EXT4FS_ENV="MKE2FS_CONFIG=./system/extras/ext4_utils/mke2fs.conf"
161if [[ $E2FSPROGS_FAKE_TIME ]]; then
162 MAKE_EXT4FS_ENV+=" E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
163fi
164
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800165MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
Jin Qian30cb1712017-08-28 17:28:49 -0700166echo $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
167env $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800168if [ $? -ne 0 ]; then
169 exit 4
170fi
171
Jin Qian30cb1712017-08-28 17:28:49 -0700172if [[ $E2FSPROGS_FAKE_TIME ]]; then
173 E2FSDROID_ENV="E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
174fi
175
Jin Qiane42388d2016-12-14 21:50:38 -0800176E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
Jin Qian30cb1712017-08-28 17:28:49 -0700177echo $E2FSDROID_ENV $E2FSDROID_CMD
178env $E2FSDROID_ENV $E2FSDROID_CMD
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800179if [ $? -ne 0 ]; then
180 rm -f $OUTPUT_FILE
181 exit 4
182fi