OSDN Git Service

Merge "simpleperf: update simpleperf prebuilts to build 4194070." am: c35e698dfc
[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 ] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE] [FILE_CONTEXTS]
12 EOT
13 }
14
15 BLOCKSIZE=4096
16
17 MKE2FS_OPTS=""
18 MKE2FS_EXTENDED_OPTS=""
19 E2FSDROID_OPTS=""
20
21 if [ "$1" = "-s" ]; then
22   MKE2FS_EXTENDED_OPTS+="android_sparse"
23   shift
24 else
25   E2FSDROID_OPTS+="-e"
26 fi
27
28 if [ $# -lt 5 ]; then
29   usage
30   exit 1
31 fi
32
33 SRC_DIR=$1
34 if [ ! -d $SRC_DIR ]; then
35   echo "Can not find directory $SRC_DIR!"
36   exit 2
37 fi
38
39 OUTPUT_FILE=$2
40 EXT_VARIANT=$3
41 MOUNT_POINT=$4
42 SIZE=$5
43 shift; shift; shift; shift; shift
44
45 if [ "$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
52 fi
53
54 if [[ "$1" == "-T" ]]; then
55   E2FSDROID_OPTS+=" -T $2"
56   shift; shift
57 fi
58
59 if [[ "$1" == "-C" ]]; then
60   E2FSDROID_OPTS+=" -C $2"
61   shift; shift
62 fi
63
64 if [[ "$1" == "-D" ]]; then
65   E2FSDROID_OPTS+=" -p $2"
66   shift; shift
67 fi
68
69 if [[ "$1" == "-B" ]]; then
70   E2FSDROID_OPTS+=" -B $2"
71   shift; shift
72 fi
73
74 if [[ "$1" == "-d" ]]; then
75   E2FSDROID_OPTS+=" -d $2"
76   shift; shift
77 fi
78
79 if [[ "$1" == "-A" ]]; then
80   E2FSDROID_OPTS+=" -D $2"
81   shift; shift
82 fi
83
84 if [[ "$1" == "-L" ]]; then
85   MKE2FS_OPTS+=" -L $2"
86   shift; shift
87 fi
88
89 if [[ "$1" == "-i" ]]; then
90   MKE2FS_OPTS+=" -N $2"
91   shift; shift
92 fi
93
94 if [[ "$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
100 fi
101
102 if [[ "$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
109 fi
110
111 if [[ $MKE2FS_EXTENDED_OPTS ]]; then
112   MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
113 fi
114
115 if [[ $1 ]]; then
116   E2FSDROID_OPTS+=" -S $1"
117 fi
118
119 case $EXT_VARIANT in
120   ext4) ;;
121   *) echo "Only ext4 is supported!"; exit 3 ;;
122 esac
123
124 if [ -z $MOUNT_POINT ]; then
125   echo "Mount point is required"
126   exit 2
127 fi
128
129 if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
130   MOUNT_POINT="/"$MOUNT_POINT
131 fi
132
133 if [ -z $SIZE ]; then
134   echo "Need size of filesystem"
135   exit 2
136 fi
137
138 # Round down the filesystem length to be a multiple of the block size
139 SIZE=$((SIZE / BLOCKSIZE))
140
141 # truncate output file since mke2fs will keep verity section in existing file
142 cat /dev/null >$OUTPUT_FILE
143
144 MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
145 echo $MAKE_EXT4FS_CMD
146 MKE2FS_CONFIG=./system/extras/ext4_utils/mke2fs.conf $MAKE_EXT4FS_CMD
147 if [ $? -ne 0 ]; then
148   exit 4
149 fi
150
151 E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
152 echo $E2FSDROID_CMD
153 $E2FSDROID_CMD
154 if [ $? -ne 0 ]; then
155   rm -f $OUTPUT_FILE
156   exit 4
157 fi