From: Shida Zhang <zhangshida@xxxxxxxxxx> #usage: ./frag.sh $dev $dir $size_k $filename #!/bin/bash cleanup() { echo "Ctrl+C detected. Killing child processes..." >&2 pkill -P $$ # Kill all child processes echo "exit...umount ${test_dev}" >&2 umount ${test_dev} exit 1 } trap cleanup SIGINT SIGTERM test_dev=$1 if [ -z $test_dev ]; then echo "test_dev cant be null" echo "usage: ./create_file.sh [test_dev] [test_dir] [file_size_k]" exit 1 fi test_mnt=$2 if [ -z $test_mnt ]; then echo "test_mnt cant be null" echo "usage: ./create_file.sh [test_dev] [test_dir] [file_size_k]" exit 1 fi file_size_k=$3 if [ -z ${file_size_k} ]; then echo "file_size_k cant be null" echo "usage: ./create_file.sh [test_dev] [test_dir] [file_size_k]" exit 1 fi echo "test_dev:${test_dev} test_mnt:${test_mnt} fize_size:${file_size_k}KB" #mkfs.xfs -f ${test_dev} if [ $5 -eq 0 ]; then echo "mount ${test_dev} ${test_mnt}" mount $test_dev $test_mnt else echo "mount -o af1=1 ${test_dev} ${test_mnt}" mount -o af1=1 $test_dev $test_mnt fi # Parameters FILE=${test_mnt}/"$4" # File name echo "$FILE" if [ -z ${FILE} ]; then FILE=${test_mnt}/"fragmented_file" # File name fi TOTAL_SIZE=${file_size_k} # Total size in KB CHUNK_SIZE=4 # Size of each punch operation in KB # Create a big file with allocated space xfs_io -f -c "falloc 0 $((TOTAL_SIZE))k" $FILE # Calculate total number of punches needed NUM_PUNCHES=$(( TOTAL_SIZE / (CHUNK_SIZE * 2) )) last_percentage=-1 # Punch holes alternately to create fragmentation for ((i=0; i<NUM_PUNCHES; i++)); do OFFSET=$(( i * CHUNK_SIZE * 2 * 1024 )) xfs_io -c "fpunch $OFFSET ${CHUNK_SIZE}k" $FILE # Calculate current percentage and print if changed PERCENTAGE=$(( (i + 1) * 100 / NUM_PUNCHES )) if [ "$PERCENTAGE" -ne "$last_percentage" ]; then #echo "Processing...${PERCENTAGE}%" last_percentage=$PERCENTAGE fi done # Verify the extent list (to see fragmentation) # echo "Extent list for the file:" # xfs_bmap -v $FILE df -Th ${test_mnt} echo "umount ${test_dev}" umount $test_dev xfs_db -c 'freesp' $test_dev