#!/usr/bin/bash
#
# rootfs-resize :: Fedora ARM root partition resize service script
#
# Version 0.3 - 2012-06-12
#
# CTyler <ctyler@fedoraproject.org> 2012-01-30, 2012-06-08/12
# Brendan Conoboy <blc@redhat.com> 2012-05-06
# birger <birger@birger.sh> 2012-05-06
#
#

#
#    rootfs-reszie - SD card resize service
#
#    Copyright (C)2012 Chris Tyler and others (see above)
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
#    MA  02110-1301  USA
#

#
# If /.nofsresize exists, or 'nofsresize' was passed
# as a kernel command line argument, disable this
# service and exit.
#
# If /.rootfs-repartition exists, repartition /
#
# If /.rootfs-resize exists, resize /
#

set -e
PATH=/usr/bin:/usr/sbin
export PATH

# If the kernel commandline contains "nofsresize" or
# the file /.nofsresize exist, then disable this service
# and exit

if grep --silent -i "nofsresize" /proc/cmdline || [[ -f /.nofsresize ]] ; then
  systemctl disable rootfs-resize.service
  exit 0
fi

# If the resize flag file does not exist, then exit

if [[ ! -f /.rootfs-repartition && ! -f /.rootfs-resize ]] ; then
  exit 0
fi

declare -a kcmdline
kcmdline=( $( < /proc/cmdline ) )
for arg in "${kcmdline[@]}" ; do
  if [[ "X${arg:0:5}" == "Xroot=" ]] ; then
    PDEV=${arg:5}
    break
  fi
done

case $PDEV in
  LABEL*|UUID*)
    rootat="/dev/disk/*/${PDEV:6}"
    rootdev="`readlink $rootat`"
    cd "`dirname $rootat`/`dirname $rootdev`"
    PDEV="`pwd`/`basename $rootdev`"
  ;;
esac

case $PDEV in
  /dev/sd*)
    DEV="${PDEV%%[0-9]*}"
    PNUM="${PDEV#${DEV}}"
  ;;
  /dev/mmcblk*)
    DEV="${PDEV%%p[0-9]*}"
    PNUM="${PDEV#${DEV}p}"
  ;;
  *)
    echo "$0: Unknown root device type $PDEV. Exiting"
    exit 1
  ;;
esac

if [[ X$DEV = X ]] || [[ X$PDEV = X ]] || [[ X$PNUM = X ]]; then
  echo "$0: Was unable to determine device, partition, or number. Exiting."
  exit 1
fi

if [[ -f /.rootfs-repartition ]]; then

  if [[ "$(fdisk -l "$DEV"|grep -c "^/dev/")" -ne 2 ]] ; then
    echo "$0: Partition count on $DEV is not 2, exiting."
    exit 0
  fi

  echo "$0: Phase 1 - Changing the parition size." 
  echo "$0: Using $PDEV on $DEV for resize"

  echo -e "d\n$PNUM\nn\np\n$PNUM\n\n\np\nw\n"|fdisk $DEV >/dev/null 2>&1 || true
  echo "$0: Repartition done, but resize must wait: Will occur on reboot."
  touch /.rootfs-resize

  rm -f /.rootfs-repartition
elif [[ -f /.rootfs-resize ]]; then
  echo "$0: Phase 2 - Resizing the root partition."
  ionice -n 7 resize2fs -p $PDEV

  rm -f /.rootfs-resize
  echo "$0: Partition resizing completed."
fi

