--- lfs_migrate-2.7.1	2016-05-13 12:46:06.828032000 +0000
+++ lfs_migrate.auto-count	2016-05-17 21:37:19.036589000 +0000
@@ -21,8 +21,10 @@
 
 usage() {
     cat -- <<USAGE 1>&2
-usage: lfs_migrate [-c <stripe_count>] [-h] [-l] [-n] [-q] [-R] [-s] [-y] [-0]
+usage: lfs_migrate [-A] [-c <stripe_count>] [-h] [-l] [-n] [-q] [-R] [-s] [-v] [-y] [-0]
                    [file|dir ...]
+    -A restripe file using an automatically selected stripe count
+       currently Stripe Count = Log2(size_in_GB)
     -c <stripe_count>
        restripe file using the specified stripe count
     -h show this usage message
@@ -31,11 +33,11 @@
     -q run quietly (don't print filenames or status)
     -R restripe file using default directory striping
     -s skip file data comparison after migrate
+    -v be verbose and print information about each file
     -y answer 'y' to usage question
     -0 input file names on stdin are separated by a null character
 
-The -c <stripe_count> option may not be specified at the same time as
-the -R option.
+Only one of the '-A', '-c', or '-R' options may be specified at a time.
 
 If a directory is an argument, all files in the directory are migrated.
 If no file/directory is given, the file list is read from standard input.
@@ -48,15 +50,19 @@
 
 OPT_CHECK=y
 OPT_STRIPE_COUNT=""
+OPT_AUTOSTRIPE=""
+OPT_VERBOSE=""
 
-while getopts "c:hlnqRsy0" opt $*; do
+while getopts "Ac:hlnqRsvy0" opt $*; do
     case $opt in
+	A) OPT_AUTOSTRIPE=y;;
 	c) OPT_STRIPE_COUNT=$OPTARG;;
 	l) OPT_NLINK=y;;
 	n) OPT_DRYRUN=n; OPT_YES=y;;
 	q) ECHO=:;;
 	R) OPT_RESTRIPE=y;;
 	s) OPT_CHECK="";;
+	v) OPT_VERBOSE=y;;
 	y) OPT_YES=y;;
 	0) OPT_NULL=y;;
 	h|\?) usage;;
@@ -69,6 +75,16 @@
 	echo "$(basename $0) error: The -c <stripe_count> option may not" 1>&2
 	echo "be specified at the same time as the -R option." 1>&2
 	exit 1
+elif [ "$OPT_STRIPE_COUNT" -a "$OPT_AUTOSTRIPE" ]; then
+	echo ""
+	echo "$(basename $0) error: The -c <stripe_count> option may not" 1>&2
+	echo "be specified at the same time as the -A option." 1>&2
+	exit 1
+elif [ "$OPT_AUTOSTRIPE" -a "$OPT_RESTRIPE" ]; then
+	echo ""
+	echo "$(basename $0) error: The -A option may not be specified at" 1>&2
+	echo "the same time as the -R option." 1>&2
+	exit 1
 fi
 
 if [ -z "$OPT_YES" ]; then
@@ -107,7 +123,7 @@
 		$ECHO -n "$OLDNAME: "
 
 		# avoid duplicate stat if possible
-		TYPE_LINK=($(LANG=C stat -c "%h %F" "$OLDNAME" || true))
+		TYPE_LINK=($(LANG=C stat -c "%h %F %s" "$OLDNAME" || true))
 
 		# skip non-regular files, since they don't have any objects
 		# and there is no point in trying to migrate them.
@@ -127,11 +143,6 @@
 			continue
 		fi
 
-		if [ "$OPT_DRYRUN" ]; then
-			echo -e "dry run, skipped"
-			continue
-		fi
-
 		if [ "$OPT_RESTRIPE" ]; then
 			UNLINK=""
 		else
@@ -140,16 +151,43 @@
 		# then we don't need to do this getstripe/mktemp stuff.
 			UNLINK="-u"
 
-			[ "$OPT_STRIPE_COUNT" ] && COUNT=$OPT_STRIPE_COUNT ||
-				COUNT=$($LFS getstripe -c "$OLDNAME" \
-					2> /dev/null)
 			SIZE=$($LFS getstripe $LFS_SIZE_OPT "$OLDNAME" \
 			       2> /dev/null)
+			if [ "$OPT_AUTOSTRIPE" ]; then
+				FILE_SIZE=${TYPE_LINK[3]}
+				# (math in bash is dumb, so depend on common tools, and there are options for that...)
+				# Stripe Count = Log2(size_in_GB)
+				#COUNT=$(echo $FILE_SIZE | awk '{printf "%.0f\n",log($1/1024/1024/1024)/log(2)}')
+				#COUNT=$(printf "%.0f\n" $(echo "l($FILE_SIZE/1024/1024/1024) / l(2)" | bc -l))
+				COUNT=$(echo "l($FILE_SIZE/1024/1024/1024) / l(2) + 1" | bc -l | cut -d . -f 1)
+				# Stripe Count = size_in_GB
+				#COUNT=$(echo "scale=0; $FILE_SIZE/1024/1024/1024" | bc -l | cut -d . -f 1)
+				[ "$COUNT" -lt 1 ] && COUNT=1
+				# (does it make sense to skip the file if old
+				# and new stripe count are identical?)
+			else
+				[ "$OPT_STRIPE_COUNT" ] && COUNT=$OPT_STRIPE_COUNT ||
+					COUNT=$($LFS getstripe -c "$OLDNAME" \
+						2> /dev/null)
+			fi
 
 			[ -z "$COUNT" -o -z "$SIZE" ] && UNLINK=""
-			SIZE=${LFS_SIZE_OPT}${SIZE}
 		fi
 
+		if [ "$OPT_DRYRUN" ]; then
+			if [ "$OPT_VERBOSE" ]; then
+				echo -e "dry run, would use count=${COUNT} size=${SIZE}"
+			else
+				echo -e "dry run, skipped"
+			fi
+			continue
+		fi
+		if [ "$OPT_VERBOSE" ]; then
+			echo -n "(count=${COUNT} size=${SIZE}) "
+		fi
+
+		[ "$SIZE" ] && SIZE=${LFS_SIZE_OPT}${SIZE}
+
 		# first try to migrate inside lustre
 		# if failed go back to old rsync mode
 		if [[ $RSYNC_MODE == false ]]; then

