blob: 1d5c0afc359870214ec71be696df005617578055 [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]
Patrick Tjine2f989a2018-03-23 13:38:05 -070011 [-i INODES ] [-M RSV_PCT] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE]
Jin Qian30cb1712017-08-28 17:28:49 -070012 [-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
Patrick Tjine2f989a2018-03-23 13:38:05 -070097if [[ "$1" == "-M" ]]; then
98 MKE2FS_OPTS+=" -m $2"
99 shift; shift
100fi
101
Connor O'Brien923a9282017-01-05 16:53:58 -0800102if [[ "$1" == "-e" ]]; then
Ben Fennema22650522017-03-08 14:21:00 -0800103 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -0800104 MKE2FS_EXTENDED_OPTS+=","
105 fi
106 MKE2FS_EXTENDED_OPTS+="stripe_width=$(($2/BLOCKSIZE))"
107 shift; shift
108fi
109
110if [[ "$1" == "-o" ]]; then
Ben Fennema22650522017-03-08 14:21:00 -0800111 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -0800112 MKE2FS_EXTENDED_OPTS+=","
113 fi
114 # stride should be the max of 8kb and the logical block size
115 MKE2FS_EXTENDED_OPTS+="stride=$((($2 > 8192 ? $2 : 8192) / BLOCKSIZE))"
116 shift; shift
117fi
118
Jin Qian30cb1712017-08-28 17:28:49 -0700119if [[ "$1" == "-U" ]]; then
120 MKE2FS_OPTS+=" -U $2"
121 shift; shift
122fi
123
124if [[ "$1" == "-S" ]]; then
125 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
126 MKE2FS_EXTENDED_OPTS+=","
127 fi
128 MKE2FS_EXTENDED_OPTS+="hash_seed=$2"
129 shift; shift
130fi
131
Ben Fennema22650522017-03-08 14:21:00 -0800132if [[ $MKE2FS_EXTENDED_OPTS ]]; then
Connor O'Brien923a9282017-01-05 16:53:58 -0800133 MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
134fi
135
Ben Fennema22650522017-03-08 14:21:00 -0800136if [[ $1 ]]; then
137 E2FSDROID_OPTS+=" -S $1"
138fi
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800139
140case $EXT_VARIANT in
141 ext4) ;;
142 *) echo "Only ext4 is supported!"; exit 3 ;;
143esac
144
145if [ -z $MOUNT_POINT ]; then
146 echo "Mount point is required"
147 exit 2
148fi
149
Jin Qiane42388d2016-12-14 21:50:38 -0800150if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
151 MOUNT_POINT="/"$MOUNT_POINT
152fi
153
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800154if [ -z $SIZE ]; then
155 echo "Need size of filesystem"
156 exit 2
157fi
158
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800159# Round down the filesystem length to be a multiple of the block size
160SIZE=$((SIZE / BLOCKSIZE))
161
Jin Qiane42388d2016-12-14 21:50:38 -0800162# truncate output file since mke2fs will keep verity section in existing file
163cat /dev/null >$OUTPUT_FILE
164
Jin Qian30cb1712017-08-28 17:28:49 -0700165MAKE_EXT4FS_ENV="MKE2FS_CONFIG=./system/extras/ext4_utils/mke2fs.conf"
166if [[ $E2FSPROGS_FAKE_TIME ]]; then
167 MAKE_EXT4FS_ENV+=" E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
168fi
169
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800170MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
Jin Qian30cb1712017-08-28 17:28:49 -0700171echo $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
172env $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800173if [ $? -ne 0 ]; then
174 exit 4
175fi
176
Jin Qian30cb1712017-08-28 17:28:49 -0700177if [[ $E2FSPROGS_FAKE_TIME ]]; then
178 E2FSDROID_ENV="E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
179fi
180
Jin Qiane42388d2016-12-14 21:50:38 -0800181E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
Jin Qian30cb1712017-08-28 17:28:49 -0700182echo $E2FSDROID_ENV $E2FSDROID_CMD
183env $E2FSDROID_ENV $E2FSDROID_CMD
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800184if [ $? -ne 0 ]; then
185 rm -f $OUTPUT_FILE
186 exit 4
187fi