#!/bin/bash
#First part of this script is based on: woof-CE/woof-code/rootfs-skeleton/usr/sbin/resizepfile.sh
export TEXTDOMAIN=resizepfile.sh
export OUTPUT_CHARSET=UTF-8
. gettext.sh
. /etc/rc.d/PUPSTATE #variables created at bootup by /initrd/usr/sbin/init...
. /etc/DISTRO_SPECS
PUPSAVE=$1
SAVEPATH=$1
SAVEPATH="$(realpath "$SAVEPATH")"
SAVEFILE="$(basename "$SAVEPATH")"
SAVEFS="`echo -n "$SAVEFILE" | sed -r 's#^(.*)([.])([^.]*)$#\3#'`"
SAVEPART="$(df "$SAVEPATH" | sed -r 's#^(/dev/)([^[:blank:]]*)([[:blank:]].*)$#\2#g' | tail -n1)"
PERSISTMNTPT=$1 #This is typically the save file.
NAMEPFILE="`basename $SAVEFILE`"
append_mnt_id_awk='
function get_mnt_id(mnt_pt,loop){
if (length(mnt_pt) > 0 && length(loop)>0){
cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
} else if (length(mnt_pt) > 0){
cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
} else if (length(loop)>0){
cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
}
while ((cmd | getline )){
mnt_id=$1
break
}
close(cmd)
return mnt_id
}
{
mnt_pt=$1
loop=$2
mnt_id=get_mnt_id(mnt_pt,loop)
print mnt_id "|" mnt_pt "|" loop
}'
SAVEPATH="$(realpath "$SAVEPATH")"
loop=$(losetup -a | grep "${SAVEPATH}" | sed "s/:.*$//" )
if [ ! -z "$loop" ]; then #
PERSISTMNTPT="$(findmnt -o TARGET,SOURCE -D -n | grep 'loop5$' | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
else
PERSISTMNTPT="$2"
if [ -z "$PERSISTMNTPT" ]; then
PERSISTMNTPT="$(mktemp -d -p /mnt "$SAVEFILE".XXXX)"
fi
#mkdir -p "$PERSISTMNTPT"
mount -r -t "$SAVEFS" ${SAVEPATH} "$PERSISTMNTPT"
fi
HOMELOCATION="$SAVEPATH" #"/initrd${PUP_HOME}${SAVEFILE}"
if [ -d $HOMELOCATION ] ; then
exec /usr/lib/gtkdialog/box_ok "$(gettext 'Resize personal storage file')" info "$(gettext "Puppy is currently using a savefolder. There is no need to resize it")" " "
fi
SIZEFREE=`df -m | grep "$PERSISTMNTPT" | tr -s " " | cut -f 4 -d " "` #free space in ${DISTRO_FILE_PREFIX}save.3fs
ACTUALSIZK=`stat -c %s $HOMELOCATION` #total size of ${DISTRO_FILE_PREFIX}save.3fs
ACTUALSIZE=`expr $ACTUALSIZK \/ 1024 \/ 1024`
APATTERN="/dev/${SAVEPART} "
PARTSIZE=`df -m | grep "$APATTERN" | tr -s " " | cut -f 2 -d " "`
PARTFREE=`df -m | grep "$APATTERN" | tr -s " " | cut -f 4 -d " "`
. /usr/lib/gtkdialog/svg_bar 200 "$(($(($ACTUALSIZE-$SIZEFREE))*200/$ACTUALSIZE))" "$ACTUALSIZE Mb / $SIZEFREE Mb $(gettext 'free')" > /tmp/resizepfile_pfile.svg
. /usr/lib/gtkdialog/svg_bar 200 "$(($(($PARTSIZE-$PARTFREE))*200/$PARTSIZE))" "$PARTSIZE Mb / $PARTFREE Mb $(gettext 'free')" > /tmp/resizepfile_partition.svg
for i in 32 64 128 256 512 1024 2048 4096 8192 12288 16384 24576 32768 65536
do
[ $i -lt $PARTFREE ] && MBCOMBO="$MBCOMBO ${i}"
done
x='
'"$(/usr/lib/gtkdialog/xml_info fixed puppy_increase.svg 60 "$(eval_gettext "Your personal file is \${NAMEPFILE}, and this contains user data, configuration files, email, newsgroup cache, history files and installed packages...")" "$(eval_gettext "If you see that you are running low on space in \$NAMEPFILE, you can make it bigger, but of course there must be enough space in \$SAVEPART.")")"'
/tmp/resizepfile_pfile.svg/tmp/resizepfile_partition.svgKILOBIG
'${MBCOMBO}'
'"`/usr/lib/gtkdialog/xml_pixmap nb`"'
'
export resize="$x"
. /usr/lib/gtkdialog/xml_info gtk > /dev/null #build bg_pixmap for gtk-theme
eval $(gtkdialog -p resize)
PUPSAVERESIZENEW="$(mktemp -p /tmp pupsaveresizenew.XXXX.txt)"
case ${EXIT} in save)
KILOBIG=$(($KILOBIG * 1024))
echo "KILOBIG=$KILOBIG" > "$PUPSAVERESIZENEW"
echo "PUPSAVEFILEX=$SAVEFILE" >> "$PUPSAVERESIZENEW" #131231
/usr/lib/gtkdialog/box_ok "$(gettext 'Resize personal storage file')" complete "$(eval_gettext "Okay, you have chosen to increase \${NAMEPFILE} by \${KILOBIG} Kbytes, however as the file is currently in use, it will happen at reboot.")" " " "$(gettext 'Technical notes:')" "$(eval_gettext "The required size increase has been written to file pupsaveresizenew.txt, in partition \${SAVEPART} (currently mounted on /mnt/home).")" "$(gettext 'File pupsaveresizenew.txt will be read at bootup and the resize performed then pupsaveresizenew.txt will be deleted.')" "$(eval_gettext "WARNING: If you have multiple \${DISTRO_FILE_PREFIX}save files, be sure to select the same one when you reboot.")" " " "$(gettext 'You can keep using Puppy. The change will only happen at reboot.')"
;;
esac
set +x
echo "About to resize save file"
read -p "Press Enter to continue"
set -x
#umount "$PERSISTMNTPT"
mount -r -t "$SAVEFS" -o remount ${SAVEPATH} "$PERSISTMNTPT"
source "$PUPSAVERESIZENEW" #See: https://github.com/puppylinux-woof-CE/woof-CE/blob/2caf45160363bdb804cf63055f2ba4eb769869d6/initrd-progs/0initrd/sbin/load_ext_file#L39
dd if=/dev/zero bs=1024 count=$KILOBIG >> "${SAVEPATH}" #Appends zeros. See https://github.com/puppylinux-woof-CE/woof-CE/blob/2caf45160363bdb804cf63055f2ba4eb769869d6/initrd-progs/0initrd/sbin/load_ext_file#L52
SAVEFILE_LOOP=$(losetup -a | grep "${SAVEPATH}" | sed "s/:.*$//" )
resize2fs -pf "$SAVEFILE_LOOP" #See https://github.com/puppylinux-woof-CE/woof-CE/blob/2caf45160363bdb804cf63055f2ba4eb769869d6/initrd-progs/0initrd/sbin/load_ext_file#L172