blob: 8dbbcb54f29c4a06bf6fc5363f711b719d586c45 [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]
Connor O'Brien923a9282017-01-05 16:53:58 -080011 [-i INODES ] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE] [FILE_CONTEXTS]
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080012EOT
13}
14
Connor O'Brien923a9282017-01-05 16:53:58 -080015BLOCKSIZE=4096
16
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080017MKE2FS_OPTS=""
Connor O'Brien923a9282017-01-05 16:53:58 -080018MKE2FS_EXTENDED_OPTS=""
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080019E2FSDROID_OPTS=""
20
21if [ "$1" = "-s" ]; then
Connor O'Brien923a9282017-01-05 16:53:58 -080022 MKE2FS_EXTENDED_OPTS+="android_sparse"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080023 shift
Jin Qian434347a2017-01-06 16:39:38 -080024else
25 E2FSDROID_OPTS+="-e"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -080026fi
27
28if [ $# -lt 5 ]; then
29 usage
30 exit 1
31fi
32
33SRC_DIR=$1
34if [ ! -d $SRC_DIR ]; then
35 echo "Can not find directory $SRC_DIR!"
36 exit 2
37fi
38
39OUTPUT_FILE=$2
40EXT_VARIANT=$3
41MOUNT_POINT=$4
42SIZE=$5
43shift; shift; shift; shift; shift
44
45if [ "$1" = "-j" ]; then
46 if [ "$2" = "0" ]; then
47 MKE2FS_OPTS+="-O ^has_journal"
48 else
49 MKE2FS_OPTS+="-J size=$2"
50 fi
51 shift; shift
52fi
53
54if [[ "$1" == "-T" ]]; then
55 E2FSDROID_OPTS+=" -T $2"
56 shift; shift
57fi
58
59if [[ "$1" == "-C" ]]; then
60 E2FSDROID_OPTS+=" -C $2"
61 shift; shift
62fi
63
64if [[ "$1" == "-D" ]]; then
65 E2FSDROID_OPTS+=" -p $2"
66 shift; shift
67fi
68
69if [[ "$1" == "-B" ]]; then
70 E2FSDROID_OPTS+=" -B $2"
71 shift; shift
72fi
73
74if [[ "$1" == "-d" ]]; then
75 E2FSDROID_OPTS+=" -d $2"
76 shift; shift
77fi
78
79if [[ "$1" == "-A" ]]; then
80 E2FSDROID_OPTS+=" -D $2"
81 shift; shift
82fi
83
84if [[ "$1" == "-L" ]]; then
85 MKE2FS_OPTS+=" -L $2"
86 shift; shift
87fi
88
89if [[ "$1" == "-i" ]]; then
90 MKE2FS_OPTS+=" -N $2"
91 shift; shift
92fi
93
Connor O'Brien923a9282017-01-05 16:53:58 -080094if [[ "$1" == "-e" ]]; then
95 if [[ MKE2FS_EXTENDED_OPTS ]]; then
96 MKE2FS_EXTENDED_OPTS+=","
97 fi
98 MKE2FS_EXTENDED_OPTS+="stripe_width=$(($2/BLOCKSIZE))"
99 shift; shift
100fi
101
102if [[ "$1" == "-o" ]]; then
103 if [[ MKE2FS_EXTENDED_OPTS ]]; then
104 MKE2FS_EXTENDED_OPTS+=","
105 fi
106 # stride should be the max of 8kb and the logical block size
107 MKE2FS_EXTENDED_OPTS+="stride=$((($2 > 8192 ? $2 : 8192) / BLOCKSIZE))"
108 shift; shift
109fi
110
111if [[ MKE2FS_EXTENDED_OPTS ]]; then
112 MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
113fi
114
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800115E2FSDROID_OPTS+=" -S $1"
116
117case $EXT_VARIANT in
118 ext4) ;;
119 *) echo "Only ext4 is supported!"; exit 3 ;;
120esac
121
122if [ -z $MOUNT_POINT ]; then
123 echo "Mount point is required"
124 exit 2
125fi
126
Jin Qiane42388d2016-12-14 21:50:38 -0800127if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
128 MOUNT_POINT="/"$MOUNT_POINT
129fi
130
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800131if [ -z $SIZE ]; then
132 echo "Need size of filesystem"
133 exit 2
134fi
135
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800136# Round down the filesystem length to be a multiple of the block size
137SIZE=$((SIZE / BLOCKSIZE))
138
Jin Qiane42388d2016-12-14 21:50:38 -0800139# truncate output file since mke2fs will keep verity section in existing file
140cat /dev/null >$OUTPUT_FILE
141
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800142MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
143echo $MAKE_EXT4FS_CMD
144$MAKE_EXT4FS_CMD
145if [ $? -ne 0 ]; then
146 exit 4
147fi
148
Jin Qiane42388d2016-12-14 21:50:38 -0800149E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
Adrien Schildknecht3f6ea672016-11-15 22:33:29 -0800150echo $E2FSDROID_CMD
151$E2FSDROID_CMD
152if [ $? -ne 0 ]; then
153 rm -f $OUTPUT_FILE
154 exit 4
155fi