OSDN Git Service

mkuserimg_mke2fs.sh: calculate size automatically
[android-x86/system-extras.git] / ext4_utils / mkuserimg_mke2fs.sh
1 #!/bin/bash
2 #
3 # To call this script, make sure mke2fs is somewhere in PATH
4
5 function usage() {
6 cat<<EOT
7 Usage:
8 mkuserimg.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 ] [-M RSV_PCT] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE]
12              [-U MKE2FS_UUID] [-S MKE2FS_HASH_SEED] [-c] [FILE_CONTEXTS]
13 EOT
14 }
15
16 BLOCKSIZE=4096
17
18 MKE2FS_OPTS=""
19 MKE2FS_EXTENDED_OPTS=""
20 E2FSDROID_OPTS=""
21 E2FSPROGS_FAKE_TIME=""
22
23 if [ "$1" = "-s" ]; then
24   MKE2FS_EXTENDED_OPTS+="android_sparse"
25   shift
26 else
27   E2FSDROID_OPTS+="-e"
28 fi
29
30 if [ $# -lt 5 ]; then
31   usage
32   exit 1
33 fi
34
35 SRC_DIR=$1
36 if [ ! -d $SRC_DIR ]; then
37   echo "Can not find directory $SRC_DIR!"
38   exit 2
39 fi
40
41 OUTPUT_FILE=$2
42 EXT_VARIANT=$3
43 MOUNT_POINT=$4
44 SIZE=$5
45 shift; shift; shift; shift; shift
46
47 if [ "$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
54 fi
55
56 if [[ "$1" == "-T" ]]; then
57   E2FSDROID_OPTS+=" -T $2"
58   E2FSPROGS_FAKE_TIME=$2
59   shift; shift
60 fi
61
62 if [[ "$1" == "-C" ]]; then
63   E2FSDROID_OPTS+=" -C $2"
64   shift; shift
65 fi
66
67 if [[ "$1" == "-D" ]]; then
68   E2FSDROID_OPTS+=" -p $2"
69   shift; shift
70 fi
71
72 if [[ "$1" == "-B" ]]; then
73   E2FSDROID_OPTS+=" -B $2"
74   shift; shift
75 fi
76
77 if [[ "$1" == "-d" ]]; then
78   E2FSDROID_OPTS+=" -d $2"
79   shift; shift
80 fi
81
82 if [[ "$1" == "-A" ]]; then
83   E2FSDROID_OPTS+=" -D $2"
84   shift; shift
85 fi
86
87 if [[ "$1" == "-L" ]]; then
88   MKE2FS_OPTS+=" -L $2"
89   shift; shift
90 fi
91
92 if [[ "$1" == "-i" ]]; then
93   MKE2FS_OPTS+=" -N $2"
94   shift; shift
95 fi
96
97 if [[ "$1" == "-M" ]]; then
98   MKE2FS_OPTS+=" -m $2"
99   shift; shift
100 fi
101
102 if [[ "$1" == "-e" ]]; then
103   if [[ $MKE2FS_EXTENDED_OPTS ]]; then
104     MKE2FS_EXTENDED_OPTS+=","
105   fi
106   MKE2FS_EXTENDED_OPTS+="stripe_width=$(($2/BLOCKSIZE))"
107   shift; shift
108 fi
109
110 if [[ "$1" == "-o" ]]; then
111   if [[ $MKE2FS_EXTENDED_OPTS ]]; then
112     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
117 fi
118
119 if [[ "$1" == "-U" ]]; then
120   MKE2FS_OPTS+=" -U $2"
121   shift; shift
122 fi
123
124 if [[ "$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
130 fi
131
132 if [[ "$1" == "-c" ]]; then
133   E2FSDROID_OPTS+=" -s"
134   shift;
135 fi
136
137 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
138   MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
139 fi
140
141 if [[ $1 ]]; then
142   E2FSDROID_OPTS+=" -S $1"
143 fi
144
145 case $EXT_VARIANT in
146   ext2) ;;
147   ext4) ;;
148   *) echo "Only ext2/4 are supported!"; exit 3 ;;
149 esac
150
151 if [ -z $MOUNT_POINT ]; then
152   echo "Mount point is required"
153   exit 2
154 fi
155
156 if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
157   MOUNT_POINT="/"$MOUNT_POINT
158 fi
159
160 if [ -z $SIZE ]; then
161   echo "Need size of filesystem"
162   exit 2
163 elif [ 0 -eq 0$SIZE ]; then
164   s=$(du -sm $SRC_DIR | cut -f1)
165   SIZE=$(($s / 10 + $s))M
166 fi
167
168 # Round down the filesystem length to be a multiple of the block size
169 SIZE=$((SIZE / BLOCKSIZE))
170
171 # truncate output file since mke2fs will keep verity section in existing file
172 cat /dev/null >$OUTPUT_FILE
173
174 MAKE_EXT4FS_ENV="MKE2FS_CONFIG=./system/extras/ext4_utils/mke2fs.conf"
175 if [[ $E2FSPROGS_FAKE_TIME ]]; then
176   MAKE_EXT4FS_ENV+=" E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
177 fi
178
179 MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
180 echo $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
181 env $MAKE_EXT4FS_ENV $MAKE_EXT4FS_CMD
182 if [ $? -ne 0 ]; then
183   exit 4
184 fi
185
186 if [[ $E2FSPROGS_FAKE_TIME ]]; then
187   E2FSDROID_ENV="E2FSPROGS_FAKE_TIME=$E2FSPROGS_FAKE_TIME"
188 fi
189
190 E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
191 echo $E2FSDROID_ENV $E2FSDROID_CMD
192 env $E2FSDROID_ENV $E2FSDROID_CMD
193 if [ $? -ne 0 ]; then
194   rm -f $OUTPUT_FILE
195   exit 4
196 fi