#!/bin/bash
#
#    STEPS TO RECREATE PARTITION RESIZING PROBLEM
#    --------------------------------------------
# 
#
# 1)  Check for proper command line arguments and set variables

if [ $# -ne 2 ]; then
    echo "Usage:  $0 dev-path number-of-tries"
    echo "        where: dev-path is the device path to a hard disk device"
    echo "                        that will be erased for this test."
    echo "               number-of-tries is the number of attempts to perform"
    echo "                               of this test."
    echo "  ***   WARNING!   ***"
    echo "        Only choose a device that has no important"
    echo "        data on it.  The data will be lost!"
    exit 1
fi

DEV=$1
echo "=== Using device ${DEV} for this test."
NUMTRIES=$2
PARTNUM=1
SECTSTART=63
SECTSIZE=96389
let SECTEND=SECTSTART+SECTSIZE-1
SECTNEWSIZE=48132
PROGNAME=resize-using-libparted

#2)  Start with a blank partition table.

echo === dd if=/dev/zero of=${DEV} bs=512 count=1
dd if=/dev/zero of=${DEV} bs=512 count=1

#3)  Check if kernel sees partition table changes.

echo === blockdev --rereadpt ${DEV}
blockdev --rereadpt ${DEV}
if [ $? -ne 0 ]; then
  echo "Failed to re-read partition table"
  exit 2
fi

#4)  Create an MSDOS disk label using parted.

echo === parted -s ${DEV} mklabel msdos
parted -s ${DEV} mklabel msdos

#5)  Create a ~50 MB primary partition for an NTFS file system.

echo === parted -s ${DEV} mkpart primary ntfs ${SECTSTART}s ${SECTEND}s
parted -s ${DEV} mkpart primary ntfs ${SECTSTART}s ${SECTEND}s
echo === blockdev --rereadpt ${DEV}
blockdev --rereadpt ${DEV}
if [ $? -ne 0 ]; then
  echo "Failed to re-read partition table"
  exit 3
fi
echo === udevadm settle
udevadm settle
echo === blockdev --getsize ${DEV}${PARTNUM}
blockdev --getsize ${DEV}${PARTNUM}
if [ $? -ne 0 ]; then
  echo "Failed to read device size for ${DEV}${PARTNUM}."
  exit 4
fi

#
# Loop through the following steps
#
k=0
while [ $k -lt $NUMTRIES ]; do
let k=k+1
echo "--------------------------------------------------"
echo "---               Iteration $k                 ---"
echo "--------------------------------------------------"

#6)  Decrease partition size

echo === ./resize-using-libparted ${DEV} ${PARTNUM} ${SECTSTART} ${SECTNEWSIZE}
./resize-using-libparted ${DEV} ${PARTNUM} ${SECTSTART} ${SECTNEWSIZE}
if [ $? -ne 0 ]; then
  break
fi
echo === udevadm settle
udevadm settle

#7)  Check if kernel sees partition table changes.

#echo === blockdev --rereadpt ${DEV}
#blockdev --rereadpt ${DEV}
#if [ $? -ne 0 ]; then
#  echo "Failed to re-read partition table"
#  exit 4
#fi
echo === blockdev --getsize ${DEV}${PARTNUM}
blockdev --getsize ${DEV}${PARTNUM}
if [ $? -ne 0 ]; then
  break
fi
# Generate a random number between 0 and 4
NUMBER=$[ ( $RANDOM % 5 ) ]
echo === sleep $NUMBER
sleep $NUMBER

#8)  Increase partition size

echo === ./resize-using-libparted ${DEV} ${PARTNUM} ${SECTSTART} ${SECTSIZE}
./resize-using-libparted ${DEV} ${PARTNUM} ${SECTSTART} ${SECTSIZE}
if [ $? -ne 0 ]; then
  break
fi
echo === udevadm settle
udevadm settle

#9)  Check if kernel sees partition table changes.

#echo === blockdev --rereadpt ${DEV}
#blockdev --rereadpt ${DEV}
#if [ $? -ne 0 ]; then
#  echo "Failed to re-read partition table"
#  exit 5
#fi
echo === blockdev --getsize ${DEV}${PARTNUM}
blockdev --getsize ${DEV}${PARTNUM}
if [ $? -ne 0 ]; then
  break
fi
# Generate a random number between 0 and 4
NUMBER=$[ ( $RANDOM % 5 ) ]
echo === sleep $NUMBER
sleep $NUMBER

#
# End of loop
#
done
