1#!/bin/bash 2# 3# To call this script, make sure make_ext4fs is somewhere in PATH 4 5function usage() { 6cat<<EOT 7Usage: 8mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE 9 [-T TIMESTAMP] [-C FS_CONFIG] [-B BLOCK_LIST_FILE] [FILE_CONTEXTS] 10EOT 11} 12 13ENABLE_SPARSE_IMAGE= 14if [ "$1" = "-s" ]; then 15 ENABLE_SPARSE_IMAGE="-s" 16 shift 17fi 18 19if [ $# -lt 5 ]; then 20 usage 21 exit 1 22fi 23 24SRC_DIR=$1 25if [ ! -d $SRC_DIR ]; then 26 echo "Can not find directory $SRC_DIR!" 27 exit 2 28fi 29 30OUTPUT_FILE=$2 31EXT_VARIANT=$3 32MOUNT_POINT=$4 33SIZE=$5 34shift; shift; shift; shift; shift 35 36TIMESTAMP=-1 37if [[ "$1" == "-T" ]]; then 38 TIMESTAMP=$2 39 shift; shift 40fi 41 42FS_CONFIG= 43if [[ "$1" == "-C" ]]; then 44 FS_CONFIG=$2 45 shift; shift 46fi 47 48BLOCK_LIST= 49if [[ "$1" == "-B" ]]; then 50 BLOCK_LIST=$2 51 shift; shift 52fi 53 54FC=$1 55 56case $EXT_VARIANT in 57 ext4) ;; 58 *) echo "Only ext4 is supported!"; exit 3 ;; 59esac 60 61if [ -z $MOUNT_POINT ]; then 62 echo "Mount point is required" 63 exit 2 64fi 65 66if [ -z $SIZE ]; then 67 echo "Need size of filesystem" 68 exit 2 69fi 70 71OPT="" 72if [ -n "$FC" ]; then 73 OPT="$OPT -S $FC" 74fi 75if [ -n "$FS_CONFIG" ]; then 76 OPT="$OPT -C $FS_CONFIG" 77fi 78if [ -n "$BLOCK_LIST" ]; then 79 OPT="$OPT -B $BLOCK_LIST" 80fi 81 82MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE -T $TIMESTAMP $OPT -l $SIZE -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR" 83echo $MAKE_EXT4FS_CMD 84$MAKE_EXT4FS_CMD 85if [ $? -ne 0 ]; then 86 exit 4 87fi 88