s243a

/usr/sbin/snapmergepuppy (tahrpup)

Dec 27th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.17 KB | None | 0 0
  1. #!/bin/ash
  2. #2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
  3. #Barry Kauler www.puppylinux.com
  4. #called from savepuppyd and rc.shutdown, to save tmpfs layer to permanent flash storage.
  5. #updated 13 Sept 2007. copy-down only. flushing is done in petget.
  6. #updated 24 Sept 2007. removed '-u' option when copy-down files.
  7. #8 oct 2007: screen out /root/tmp when saving.
  8. #4 nov 2007: reintroduce '-u' copy option.
  9. #v4.01 19may2008 BK: now called from /sbin/pup_eventd daemon (savepuppyd now history).
  10. #v4.01 19may2008 BK: if called from pup_eventd then X running: graceful exit if X shutdown.
  11. #v409 BK: XRUNNING test should be 'pup_event_frontend_d'.
  12. #v409 BK: save /var dir. previously screened out to save space, but prevents crontab from running.
  13. #v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
  14. #w000 pup files renamed to woofr555.sfs, woofsave.2fs.
  15. #w003 bug fix for .wh.__dir_opaque whiteout file.
  16. #w003 screened out some dirs in /dev that should not be saved.
  17. #v424 fix for more layers in unionfs/aufs.
  18. #100222 shinobar: possible timezone problem with BOOTCONFIG. more file exclusions.
  19. #100422 added ^root/ftpd exclusion.
  20. #100429 modify 'trash' exclusion.
  21. #100820 added /var/tmp exclusion (apparently filled by opera crashes).
  22. #101221 yaf-splash fix.
  23. #110206 Dougal: clean up some testing. speedup: LANG=C, also change to /bin/ash.
  24. #110212 recent aufs: .wh.__dir_opaque name changed to .wh..wh..opq.
  25. #110212 Jemimah: files may disappear, more efficient calc of free space, fix i/o err.
  26. #110222 shinobar: remove all /dev, allow 'pup_ro10-19', exit code for no space
  27. #110224 BK: revert remove all /dev, for now. 110503 added dev/snd
  28. #110505 support sudo for non-root user.
  29. #111229 rerwin: fix jemimah code (110212).
  30. #120103 rerwin: screen out /.XLOADED when save.
  31. #140102 SFR: various fixes + gettext
  32. #140512 SFR: performance improvements
  33.  
  34. [ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
  35.  
  36. export TEXTDOMAIN=snapmergepuppy
  37. export OUTPUT_CHARSET=UTF-8
  38.  
  39. OLDLANG="$LANG"
  40. export LANG=C #110206 Dougal: I **think** this should not cause problems with filenames
  41.  
  42. #variables created at bootup by 'init' script in initramfs...
  43. . /etc/rc.d/PUPSTATE
  44. . /etc/DISTRO_SPECS #v412
  45. SAVEPART="`echo -n "$PUPSAVE" | cut -f 1 -d ','`"
  46.  
  47. #v3.02 first time, do not use '-u' option when saving, save everything...
  48. NEXTPASSTHRU=""
  49. [ -e /tmp/flagnextpassthru ] && NEXTPASSTHRU="yes"
  50.  
  51. RUNPS="`busybox ps`"
  52. SHUTDOWN="no"
  53. [ "`echo "$RUNPS" | grep 'rc.shutdown'`" != "" ] && SHUTDOWN="yes"
  54. XRUNNING="no"
  55. [ "`echo "$RUNPS" | grep 'pup_event_frontend_d'`" != "" ] && XRUNNING="yes"
  56.  
  57. PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R7/bin"
  58. WD="`pwd`"
  59.  
  60. # all the action takes places inside $SNAP (ex: /initrd/pup_rw)
  61. SNAP="`cat /sys/fs/aufs/si_*/br0 | head -1 | cut -f1 -d'='`"
  62. cd $SNAP || exit 1
  63.  
  64. # files are copied from $SNAP/* to $BASE/ (ex: /initrd/pup_ro1)
  65. BASE="`cat /sys/fs/aufs/si_*/br1 | head -1 | cut -f1 -d'='`"
  66. BASEMTP="`df-FULL --output=target "$BASE" | tail -1`"
  67.  
  68. # Precautions
  69. [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ] || { echo "Wrong PUPMODE ($PUPMODE)!"; exit 1; }
  70. mountpoint -q "$BASEMTP" || { echo "$BASEMTP is not mounted!"; exit 1; }
  71.  
  72. echo "Merging $SNAP onto $BASE..."
  73.  
  74. # =============================================================================
  75. # WHITEOUTS
  76. # =============================================================================
  77.  
  78. for XFL in /sys/fs/aufs/si_*/br[0-9]* #get aufs branch list
  79. do
  80.  XFN="${XFL##*/}"
  81.  [ ${XFN:2} -lt 2 ] && continue
  82.  SFSPoints="${SFSPoints}`cat ${XFL} | cut -f1 -d'='` "
  83. done
  84.  
  85. find . -mount \( -regex '.*/\.wh\.[^/]*' -type f \) | sed -e 's/\.\///;s/\.wh\.//' |
  86. while read -r N
  87. do
  88.  DN="${N%/*}"
  89.  BN="${N##*/}"
  90.  [ "$DN" = "$N" ] || [ "$DN" = "" ] && DN="."
  91.  [ "$DN" = "." ] && continue
  92.  [ "$BN" = ".wh.aufs" ] && continue #w003 aufs has file .wh..wh.aufs in /initrd/pup_rw.
  93.  
  94.  #110212 unionfs and early aufs: '.wh.__dir_opaque' marks ignore all contents in lower layers...
  95.  if [ "$BN" = "__dir_opaque" ];then #w003
  96.   #'.wh.__dir_opaque' marks ignore all contents in lower layers...
  97.   rm -rf "${BASE}/${DN}" 2>/dev/null #wipe anything in save layer. 110212 delete entire dir.
  98.   mkdir -p "${BASE}/${DN}" #jemimah: files sometimes mysteriously reappear if you don't delete and recreate the directory, aufs bug? 111229 rerwin: need -p, may have to create parent dir.
  99.   #also need to save the whiteout file to block all lower layers (may be readonly)...
  100.   touch "${BASE}/${DN}/.wh.__dir_opaque" 2>/dev/null
  101.   rm -f "$SNAP/$DN/.wh.__dir_opaque" #should force aufs layer "reval".
  102.   continue
  103.  fi
  104.  #110212 recent aufs: .wh.__dir_opaque name changed to .wh..wh..opq ...
  105.  if [ "$BN" = ".wh..opq" ] ; then
  106.   rm -rf "${BASE}/${DN}" 2>/dev/null  #wipe anything in save layer.
  107.   mkdir -p "${BASE}/${DN}" #jemimah: files sometimes mysteriously reappear if you don't delete and recreate the directory, aufs bug? 111229 rerwin: need -p, may have to create parent dir.
  108.   #also need to save the whiteout file to block all lower layers (may be readonly)...
  109.   touch "${BASE}/${DN}/.wh..wh..opq" 2>/dev/null
  110.   rm -f "$SNAP/$DN/.wh..wh..opq"  #should force aufs layer "reval".
  111.   continue
  112.  fi
  113.  #comes in here with the '.wh.' prefix stripped off, leaving actual filename...
  114.  rm -rf "$BASE/$N"
  115.  
  116.  #if file exists on a lower layer, have to save the whiteout file...
  117.  #110206 Dougal: speedup and refine the search...
  118.  for P in $SFSPoints
  119.  do
  120.    if [ -e "$P/$N" ] || [ -L "$P/$N" ] ; then   # SFR: broken symlinks also deserve to be processed ( '-e' won't detect them, needs to be also '-L')
  121.      [ -d "${BASE}/${DN}" ] || mkdir -p "${BASE}/${DN}"
  122.      touch "${BASE}/${DN}/.wh.${BN}"
  123.      break
  124.    fi
  125.  done #110206 End Dougal.
  126.  rm -f "$SNAP/$DN/.wh.$BN" #remove whiteout file. should force aufs layer "reval".
  127. done
  128.  
  129. # =============================================================================
  130. # DIRECTORIES
  131. # =============================================================================
  132.  
  133. #110224 BK revert, leave save of /dev in for now, just take out some subdirs... 110503 added dev/snd
  134. find . -mount -type d | tail +2 | sed -e 's/\.\///' | grep -v -E '^mnt|^initrd|^proc|^sys|^tmp|^root/tmp|^\.wh\.|/\.wh\.|^dev/\.|^dev/fd|^dev/pts|^dev/shm|^dev/snd|^var/tmp' |
  135. while read -r N
  136. do
  137.  #v4.01 graceful exit if shutdown X (see /usr/X11R7/bin/restartwm,wmreboot,wmpoweroff)...
  138.  [ "$XRUNNING" = "yes" ] && [ -f /tmp/wmexitmode.txt ] && exit
  139.  mkdir -p "$BASE/$N"
  140.  #i think nathan advised this, to handle non-root user (SFR: improved/simplified)
  141.  chmod "$BASE/$N" --reference="$N"
  142.  OWNERSHIP="`stat --format=%u:%g "$N"`"
  143.  chown $OWNERSHIP "$BASE/$N"
  144.  touch "$BASE/$N" --reference="$N"
  145. done
  146.  
  147. # =============================================================================
  148. # FILES
  149. # =============================================================================
  150.  
  151. #100222 a quick hack: BOOTCONFIG written to in init, before timezone set, can cause trouble...
  152. touch /etc/rc.d/BOOTCONFIG
  153. FREEBASE=`df -B 1 | grep -w "$BASEMTP"| head -n 1 | tr -s ' ' | cut -f 4 -d ' '` #110212 Jemimah #110222 shinobar # SFR: result in bytes (see 'find' below)
  154.  
  155. rm -f /tmp/snapmergepuppy-nospace #110222 shinobar
  156. rm -f /tmp/snapmergepuppy-error   #140102 SFR
  157.  
  158. #Copy Files... v409 remove '^var'. w003 screen out some /dev files. 100222 shinobar: more exclusions. 100422 added ^root/ftpd. 100429 modify 'trash' exclusion. 100820 added var/tmp #110224 BK: revert, leave save of /dev in for now... 120103 rerwin: add .XLOADED # SFR: added dev/snd # SFR: added .crdownload
  159. # SFR: move as much as possible into 'find' itself
  160. # Limitation - files with newline (\n) in its name are processed wrong (but this is not a new issue)
  161. find . -mount \
  162.        -not -path . \
  163.        -not -type d \
  164.        -regextype posix-extended \
  165.        -not \( -regex '.*/\.wh\.[^\]*' -type f \) \
  166.        -not \( -regex '^./mnt.*|^./proc.*|^./sys.*|^./tmp.*|^./pup_.*|^./zdrv_.*|^./root/tmp.*|.*_zdrv_.*|^./dev/\..*|^./dev/fd.*|^./dev/pts.*|^./dev/snd.*|^./dev/shm.*|^./dev/tty.*|^./\.wh\..*|^./var/run.*|^./root/ftpd.*|^./var/tmp.*|.*\.XLOADED$' \) \
  167.        -not \( -regex '.*\.thumbnails.*|.*\.part$|.*\.crdownload$' \) \
  168.        -printf "%s %C@ %P\n" |
  169. while read -r NSIZE NCTIME N
  170. do
  171.  #v4.01 graceful exit if shutdown X (see /usr/X11R7/bin/restartwm,wmreboot,wmpoweroff)...
  172.  [ "$XRUNNING" = "yes" ] && [ -f /tmp/wmexitmode.txt ] && exit
  173.  
  174.  ([ ! -e "$N" ] && [ ! -L "$N" ]) && continue # SFR: skip non-existing files (btw, '-e' won't detect broken symlinks, so '-L' is necessary!)
  175.  
  176.  #stop saving if not enough room left in ${DISTRO_FILE_PREFIX}save file...
  177.  if [ $((NSIZE+204800)) -gt $FREEBASE ]; then   # 204800 = 200K slack space
  178.   FREEBASE=`df -B 1 | grep -w "$BASEMTP" | head -n 1 | tr -s ' ' | cut -f 4 -d ' '` #110212 Jemimah: this is very slow; try not to check every iteration #110222 shinobar: fix for pup_ro10 and more
  179.   if [ $((NSIZE+204800)) -gt $FREEBASE ]; then  #110212 Jemimah.
  180.    touch /tmp/snapmergepuppy-nospace  #110222 shinobar
  181.    break
  182.   fi
  183.  else
  184.   FREEBASE=$((FREEBASE-NSIZE)) #110212 Jemimah: keep track of the worst case
  185.  fi
  186.  
  187.  ##v2.21 -u causes trouble if timezone malconfigured...
  188.  [ -L "$BASE/$N" ] && rm -f "$BASE/$N"
  189.  if [ -L "$N" -o "$NEXTPASSTHRU" = "" ];then
  190.   #link, or first run of snapmergepuppy. no '-u' (update) option.
  191.   [ -L "$N" ] && [ -d "$BASE/$N" ] && rm -rf "$BASE/$N" # SFR: in case if folder has been replaced with a symlink (cp won't overwrite a dir with a symlink)
  192.   cp -a -f "$N" "$BASE/$N" 2>>/tmp/snapmergepuppy-error
  193.   rm "$N"
  194.  else
  195.   [ ! -e "$BASE/$N" ] || [ ${NCTIME%%.*} -gt `stat -c %Z "$BASE/$N"` ] && { cp -a -f "$N" "$BASE/$N" 2>>/tmp/snapmergepuppy-error; rm "$N"; }
  196.  fi
  197.  
  198.  DN="${N%/*}" #111229 rerwin: bugfix for jemimah code (110212) (SFR: improved performance)
  199.  BN="${N##*/}" #111229  "
  200.  [ "$DN" = "$N" ] || [ "$DN" = "" ] && DN="."
  201.  [ -e "$BASE/$DN/.wh.${BN}" ] && rm "$BASE/$DN/.wh.${BN}" #110212 jemimah bugfix - I/O errors if you don't do this
  202.  
  203. done
  204.  
  205. # =============================================================================
  206.  
  207. touch /tmp/flagnextpassthru
  208.  
  209. # SFR: fix for .wh files not being created (in some cases) in pup_rw
  210. # force re-evalution of all the layers
  211. busybox mount -t aufs -o remount,udba=reval unionfs /
  212.  
  213. sync
  214. cd "$WD"
  215.  
  216. # =============================================================================
  217.  
  218. error_msg () {
  219.   if [ "$SHUTDOWN" = "no" -a "$XRUNNING" = "yes" ];then
  220.     export DISPLAY=':0'
  221.     /usr/lib/gtkdialog/box_splash -timeout 30 -close box -icon gtk-dialog-warning -bg red -text "$1"
  222.   else
  223.     echo "$1"
  224.   fi
  225. }
  226.  
  227. export LANG="$OLDLANG"
  228. ERRSTATUS=0
  229.  
  230. if [ -f /tmp/snapmergepuppy-nospace ]; then #110222 shinobar
  231.   ERRMSG="$(gettext 'WARNING!
  232. Unable to save all files. You need to delete some.')"
  233.   error_msg "$ERRMSG"
  234.   ERRSTATUS=1
  235. fi
  236.  
  237. sed -i '/No such file or directory/d' /tmp/snapmergepuppy-error # discard errors caused by bad timing
  238.  
  239. if [ -s /tmp/snapmergepuppy-error ]; then   #140102 SFR
  240.   ERRMSG="$(gettext "WARNING!
  241. There were some errors detected.
  242. (see '/tmp/snapmergepuppy-error' for details)
  243. Filesystem check of the savefile (pfix=fsck) is highly recommended.")"
  244.   error_msg "$ERRMSG"
  245.   ERRSTATUS=1
  246. fi
  247.  
  248. exit $ERRSTATUS
  249.  
  250. # =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment