mkuserimg_mke2fs.sh revision ba223bdc3f8a0f09165fb9036bbbcf44cdd394e0
1#!/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]
11             [-i INODES ] [-e ERASE_BLOCK_SIZE] [-o FLASH_BLOCK_SIZE] [FILE_CONTEXTS]
12EOT
13}
14
15BLOCKSIZE=4096
16
17MKE2FS_OPTS=""
18MKE2FS_EXTENDED_OPTS=""
19E2FSDROID_OPTS=""
20
21if [ "$1" = "-s" ]; then
22  MKE2FS_EXTENDED_OPTS+="android_sparse"
23  shift
24else
25  E2FSDROID_OPTS+="-e"
26fi
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
45# selinux requires ext_attr.
46MKE2FS_OPTS+="-O ext_attr "
47
48if [ "$1" = "-j" ]; then
49  if [ "$2" = "0" ]; then
50    MKE2FS_OPTS+="-O ^has_journal"
51  else
52    MKE2FS_OPTS+="-J size=$2"
53  fi
54  shift; shift
55fi
56
57if [[ "$1" == "-T" ]]; then
58  E2FSDROID_OPTS+=" -T $2"
59  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
97if [[ "$1" == "-e" ]]; then
98  if [[ MKE2FS_EXTENDED_OPTS ]]; then
99    MKE2FS_EXTENDED_OPTS+=","
100  fi
101  MKE2FS_EXTENDED_OPTS+="stripe_width=$(($2/BLOCKSIZE))"
102  shift; shift
103fi
104
105if [[ "$1" == "-o" ]]; then
106  if [[ MKE2FS_EXTENDED_OPTS ]]; then
107    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
114if [[ MKE2FS_EXTENDED_OPTS ]]; then
115  MKE2FS_OPTS+=" -E $MKE2FS_EXTENDED_OPTS"
116fi
117
118E2FSDROID_OPTS+=" -S $1"
119
120case $EXT_VARIANT in
121  ext4) ;;
122  *) echo "Only ext4 is supported!"; exit 3 ;;
123esac
124
125if [ -z $MOUNT_POINT ]; then
126  echo "Mount point is required"
127  exit 2
128fi
129
130if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
131  MOUNT_POINT="/"$MOUNT_POINT
132fi
133
134if [ -z $SIZE ]; then
135  echo "Need size of filesystem"
136  exit 2
137fi
138
139# Round down the filesystem length to be a multiple of the block size
140SIZE=$((SIZE / BLOCKSIZE))
141
142# truncate output file since mke2fs will keep verity section in existing file
143cat /dev/null >$OUTPUT_FILE
144
145MAKE_EXT4FS_CMD="mke2fs $MKE2FS_OPTS -t $EXT_VARIANT -b $BLOCKSIZE $OUTPUT_FILE $SIZE"
146echo $MAKE_EXT4FS_CMD
147$MAKE_EXT4FS_CMD
148if [ $? -ne 0 ]; then
149  exit 4
150fi
151
152E2FSDROID_CMD="e2fsdroid $E2FSDROID_OPTS -f $SRC_DIR -a $MOUNT_POINT $OUTPUT_FILE"
153echo $E2FSDROID_CMD
154$E2FSDROID_CMD
155if [ $? -ne 0 ]; then
156  rm -f $OUTPUT_FILE
157  exit 4
158fi
159