1#!/bin/bash
2#
3# To call this script, make sure mksquashfs is somewhere in PATH
4
5function usage() {
6cat<<EOT
7Usage:
8${0##*/} SRC_DIR OUTPUT_FILE [-s] [-m MOUNT_POINT] [-d PRODUCT_OUT] [-C FS_CONFIG ] [-c FILE_CONTEXTS] [-B BLOCK_MAP_FILE] [-b BLOCK_SIZE] [-z COMPRESSOR] [-zo COMPRESSOR_OPT] [-t COMPRESS_THRESHOLD] [-w WHITELIST_FILE] [-a]
9EOT
10}
11
12echo "in mksquashfsimage.sh PATH=$PATH"
13
14if [ $# -lt 2 ]; then
15    usage
16    exit 1
17fi
18
19SRC_DIR=$1
20if [ ! -d $SRC_DIR ]; then
21  echo "Can not find directory $SRC_DIR!"
22  exit 2
23fi
24OUTPUT_FILE=$2
25shift; shift
26
27SPARSE=false
28if [[ "$1" == "-s" ]]; then
29    SPARSE=true
30    shift;
31fi
32
33MOUNT_POINT=
34if [[ "$1" == "-m" ]]; then
35    MOUNT_POINT=$2
36    shift; shift
37fi
38
39PRODUCT_OUT=
40if [[ "$1" == "-d" ]]; then
41    PRODUCT_OUT=$2
42    shift; shift
43fi
44
45FS_CONFIG=
46if [[ "$1" == "-C" ]]; then
47    FS_CONFIG=$2
48    shift; shift
49fi
50
51FILE_CONTEXTS=
52if [[ "$1" == "-c" ]]; then
53    FILE_CONTEXTS=$2
54    shift; shift
55fi
56
57BLOCK_MAP_FILE=
58if [[ "$1" == "-B" ]]; then
59    BLOCK_MAP_FILE=$2
60    shift; shift
61fi
62
63BLOCK_SIZE=131072
64if [[ "$1" == "-b" ]]; then
65    BLOCK_SIZE=$2
66    shift; shift
67fi
68
69COMPRESSOR="lz4"
70COMPRESSOR_OPT="-Xhc"
71if [[ "$1" == "-z" ]]; then
72    COMPRESSOR=$2
73    COMPRESSOR_OPT=
74    shift; shift
75fi
76
77if [[ "$1" == "-zo" ]]; then
78    COMPRESSOR_OPT=$2
79    shift; shift
80fi
81
82COMPRESS_THRESHOLD=0
83if [[ "$1" == "-t" ]]; then
84    COMPRESS_THRESHOLD=$2
85    shift; shift
86fi
87
88WHITELIST_FILE=
89if [[ "$1" == "-w" ]]; then
90    WHITELIST_FILE=$2
91    shift; shift
92fi
93
94DISABLE_4K_ALIGN=false
95if [[ "$1" == "-a" ]]; then
96    DISABLE_4K_ALIGN=true
97    shift;
98fi
99
100OPT=""
101if [ -n "$MOUNT_POINT" ]; then
102  OPT="$OPT -mount-point $MOUNT_POINT"
103fi
104if [ -n "$PRODUCT_OUT" ]; then
105  OPT="$OPT -product-out $PRODUCT_OUT"
106fi
107if [ -n "$FS_CONFIG" ]; then
108  OPT="$OPT -fs-config-file $FS_CONFIG"
109fi
110if [ -n "$FILE_CONTEXTS" ]; then
111  OPT="$OPT -context-file $FILE_CONTEXTS"
112fi
113if [ -n "$BLOCK_MAP_FILE" ]; then
114  OPT="$OPT -block-map $BLOCK_MAP_FILE"
115fi
116if [ -n "$BLOCK_SIZE" ]; then
117  OPT="$OPT -b $BLOCK_SIZE"
118fi
119if [ -n "$COMPRESS_THRESHOLD" ]; then
120  OPT="$OPT -t $COMPRESS_THRESHOLD"
121fi
122if [ "$DISABLE_4K_ALIGN" = true ]; then
123  OPT="$OPT -disable-4k-align"
124fi
125if [ -n "$WHITELIST_FILE" ]; then
126    OPT="$OPT -whitelist $WHITELIST_FILE"
127fi
128
129MAKE_SQUASHFS_CMD="mksquashfs $SRC_DIR/ $OUTPUT_FILE -no-progress -comp $COMPRESSOR $COMPRESSOR_OPT -no-exports -noappend -no-recovery -no-fragments -no-duplicates -android-fs-config $OPT"
130echo $MAKE_SQUASHFS_CMD
131$MAKE_SQUASHFS_CMD
132
133if [ $? -ne 0 ]; then
134    exit 4
135fi
136
137SPARSE_SUFFIX=".sparse"
138if [ "$SPARSE" = true ]; then
139    img2simg $OUTPUT_FILE $OUTPUT_FILE$SPARSE_SUFFIX
140    if [ $? -ne 0 ]; then
141        exit 4
142    fi
143    mv $OUTPUT_FILE$SPARSE_SUFFIX $OUTPUT_FILE
144fi
145
146