s243a

resize_save_file.sh (draft)

Jan 23rd, 2021 (edited)
1,195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #First part of this script is based on: woof-CE/woof-code/rootfs-skeleton/usr/sbin/resizepfile.sh
  3.  
  4. export TEXTDOMAIN=resizepfile.sh
  5. export OUTPUT_CHARSET=UTF-8
  6.  
  7. . gettext.sh
  8. . /etc/rc.d/PUPSTATE #variables created at bootup by /initrd/usr/sbin/init...
  9. . /etc/DISTRO_SPECS
  10.  
  11. PUPSAVE=$1
  12.  
  13. SAVEPATH=$1
  14. SAVEPATH="$(realpath "$SAVEPATH")"
  15. SAVEFILE="$(basename "$SAVEPATH")"
  16. SAVEFS="`echo -n "$SAVEFILE" | sed -r 's#^(.*)([.])([^.]*)$#\3#'`"
  17. SAVEPART="$(df "$SAVEPATH" | sed -r 's#^(/dev/)([^[:blank:]]*)([[:blank:]].*)$#\2#g' | tail -n1)"
  18.  
  19.  
  20. PERSISTMNTPT=$1 #This is typically the save file.
  21. NAMEPFILE="`basename $SAVEFILE`"
  22.  
  23. append_mnt_id_awk='
  24. function get_mnt_id(mnt_pt,loop){
  25.  if (length(mnt_pt) > 0 && length(loop)>0){
  26.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
  27.  } else if (length(mnt_pt) > 0){
  28.    cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
  29.  } else if (length(loop)>0){
  30.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
  31.  }
  32.  while ((cmd | getline )){
  33.    mnt_id=$1
  34.    break  
  35.  }
  36.  close(cmd)
  37.  return mnt_id
  38. }
  39. {
  40.  mnt_pt=$1
  41.  loop=$2
  42.  mnt_id=get_mnt_id(mnt_pt,loop)
  43.  print mnt_id "|" mnt_pt "|" loop
  44. }'
  45. SAVEPATH="$(realpath "$SAVEPATH")"
  46. loop=$(losetup -a | grep  "${SAVEPATH}"  | sed "s/:.*$//" )
  47. if [ ! -z "$loop" ]; then #
  48.   PERSISTMNTPT="$(findmnt -o TARGET,SOURCE -D -n | grep 'loop5$' | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
  49. else
  50.   PERSISTMNTPT="$2"
  51.   if [ -z "$PERSISTMNTPT" ]; then
  52.     PERSISTMNTPT="$(mktemp -d -p /mnt "$SAVEFILE".XXXX)"  
  53.   fi
  54.   #mkdir -p "$PERSISTMNTPT"
  55.   mount -r -t "$SAVEFS" ${SAVEPATH} "$PERSISTMNTPT"
  56. fi
  57.  
  58.  
  59. HOMELOCATION="$SAVEPATH" #"/initrd${PUP_HOME}${SAVEFILE}"
  60. if [ -d $HOMELOCATION ] ; then
  61.     exec /usr/lib/gtkdialog/box_ok "$(gettext 'Resize personal storage file')" info "<b>$(gettext "Puppy is currently using a savefolder. There is no need to resize it")</b>" " "
  62. fi
  63.  
  64. SIZEFREE=`df -m | grep "$PERSISTMNTPT" | tr -s " " | cut -f 4 -d " "` #free space in ${DISTRO_FILE_PREFIX}save.3fs
  65. ACTUALSIZK=`stat -c %s $HOMELOCATION` #total size of ${DISTRO_FILE_PREFIX}save.3fs
  66. ACTUALSIZE=`expr $ACTUALSIZK \/ 1024 \/ 1024`
  67. APATTERN="/dev/${SAVEPART} "
  68. PARTSIZE=`df -m | grep "$APATTERN" | tr -s " " | cut -f 2 -d " "`
  69. PARTFREE=`df -m | grep "$APATTERN" | tr -s " " | cut -f 4 -d " "`
  70.  
  71. . /usr/lib/gtkdialog/svg_bar 200 "$(($(($ACTUALSIZE-$SIZEFREE))*200/$ACTUALSIZE))" "$ACTUALSIZE Mb / $SIZEFREE Mb $(gettext 'free')"  > /tmp/resizepfile_pfile.svg
  72. . /usr/lib/gtkdialog/svg_bar 200 "$(($(($PARTSIZE-$PARTFREE))*200/$PARTSIZE))" "$PARTSIZE Mb / $PARTFREE Mb $(gettext 'free')"  > /tmp/resizepfile_partition.svg
  73.  
  74. for i in 32 64 128 256 512 1024 2048 4096 8192 12288 16384 24576 32768 65536
  75. do
  76.     [ $i -lt $PARTFREE ] && MBCOMBO="$MBCOMBO <item>${i}</item>"
  77. done
  78.  
  79. x='
  80. <window title="'$(gettext 'Resize Personal Storage File')'" icon-name="gtk-refresh" resizable="false">
  81. <vbox space-expand="true" space-fill="true">
  82.  '"$(/usr/lib/gtkdialog/xml_info fixed puppy_increase.svg 60 "$(eval_gettext "<b>Your personal file is \${NAMEPFILE},</b> 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.")")"'
  83.  <vbox space-expand="true" space-fill="true">
  84.    <frame>      
  85.      <text height-request="5"><label>""</label></text>
  86.      <vbox space-expand="true" space-fill="true">
  87.        <vbox space-expand="false" space-fill="false">
  88.          <hbox>
  89.            <text xalign="0" use-markup="true"><label>"<b>'$(gettext 'Personal File')'</b>: '$NAMEPFILE'"</label></text>
  90.            <text space-expand="true" space-fill="true"><label>""</label></text>
  91.            <pixmap><input file>/tmp/resizepfile_pfile.svg</input></pixmap>
  92.          </hbox>
  93.          <hbox>
  94.            <text xalign="0" use-markup="true"><label>"<b>'$(gettext 'Partition')'</b>: '$SAVEPART'"</label></text>
  95.            <text space-expand="true" space-fill="true"><label>""</label></text>
  96.            <pixmap><input file>/tmp/resizepfile_partition.svg</input></pixmap>
  97.          </hbox>
  98.        </vbox>
  99.        <text height-request="5" space-expand="true" space-fill="true"><label>""</label></text>
  100.        <vbox space-expand="false" space-fill="false">
  101.          <hbox space-expand="true" space-fill="true">
  102.            <text xalign="0" space-expand="true" space-fill="true"><label>'$(eval_gettext "Increase size of \$NAMEPFILE by amount (Mb). You cannot make it smaller.")'</label></text>
  103.            <comboboxtext width-request="100" space-expand="false" space-fill="false">
  104.              <variable>KILOBIG</variable>
  105.              '${MBCOMBO}'
  106.            </comboboxtext>
  107.          </hbox>
  108.        </vbox>
  109.        <text height-request="10"><label>""</label></text>
  110.      </vbox>
  111.    </frame>
  112.  </vbox>
  113.  <hbox space-expand="false" space-fill="false">
  114.    '"`/usr/lib/gtkdialog/xml_pixmap nb`"'
  115.    <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext 'Resizing requires a system reboot')'</b>"</label></text>
  116.    <button space-expand="false" space-fill="false">
  117.      <label>'$(gettext "Cancel")'</label>
  118.      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
  119.      <action type="exit">EXIT_NOW</action>
  120.    </button>
  121.    <button space-expand="false" space-fill="false">
  122.      <label>'$(gettext "Ok")'</label>
  123.      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
  124.      <action type="exit">save</action>
  125.    </button>
  126.  </hbox>
  127. </vbox>
  128. </window>'
  129. export resize="$x"
  130. . /usr/lib/gtkdialog/xml_info gtk > /dev/null #build bg_pixmap for gtk-theme
  131. eval $(gtkdialog -p resize)
  132. PUPSAVERESIZENEW="$(mktemp -p /tmp pupsaveresizenew.XXXX.txt)"
  133. case ${EXIT} in save)
  134.     KILOBIG=$(($KILOBIG * 1024))
  135.     echo "KILOBIG=$KILOBIG" > "$PUPSAVERESIZENEW"
  136.     echo "PUPSAVEFILEX=$SAVEFILE" >> "$PUPSAVERESIZENEW" #131231
  137.     /usr/lib/gtkdialog/box_ok "$(gettext 'Resize personal storage file')" complete "$(eval_gettext "Okay, you have chosen to <b>increase \${NAMEPFILE} by \${KILOBIG} Kbytes</b>, 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.")" " " "<b>$(gettext 'You can keep using Puppy. The change will only happen at reboot.')</b>"
  138.     ;;
  139. esac
  140. set +x
  141. echo "About to resize save file"
  142. read -p "Press Enter to continue"
  143. set -x
  144. #umount "$PERSISTMNTPT"
  145. mount -r -t "$SAVEFS" -o remount ${SAVEPATH} "$PERSISTMNTPT"
  146. source "$PUPSAVERESIZENEW" #See: https://github.com/puppylinux-woof-CE/woof-CE/blob/2caf45160363bdb804cf63055f2ba4eb769869d6/initrd-progs/0initrd/sbin/load_ext_file#L39
  147. 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
  148. SAVEFILE_LOOP=$(losetup -a | grep  "${SAVEPATH}"  | sed "s/:.*$//" )
  149. resize2fs -pf "$SAVEFILE_LOOP" #See https://github.com/puppylinux-woof-CE/woof-CE/blob/2caf45160363bdb804cf63055f2ba4eb769869d6/initrd-progs/0initrd/sbin/load_ext_file#L172
  150.  
  151.  
  152.  
RAW Paste Data