rc.shutdown 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #!/bin/sh
  2. #(c) Copyright bkauler 2006,2007 www.puppylinux.com
  3. #2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
  4. #updated aug 2007 for v2.20, save in a folder.
  5. #BK updated 20,22 sept 2007 v2.22, multisession bugfix.
  6. #v3.01 BK 10 oct 2007, attempted bugfixes for funny CD drive.
  7. #v3.01 BK 11 oct 2007, removed multisavefunc() to functions4puppy.
  8. #v3.02 BK 1 nov 2007, can change folder of pup_save file.
  9. #v3.97 BK feb 2008: restore support for pcmcia-cs package.
  10. #v3.97 BK 5 mar 2008, handle PSAVEMARK, see /etc/rc.d/PUPSTATE, init script.
  11. #REMOVEDv3.98 BK 2 apr 2008: 'mke2fs -O none' for encrypted pup_save, fix mntg bug.
  12. #v3.99 7apr2008: fix zombie process prevents partition unmount.
  13. #v411 run rc.alsa as it also unloads modules (if not done, prevents shutdown some systems).
  14. #v411 bring down network interfaces, as sometimes prevent poweroff.
  15. #v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
  16. #w002 pup files renamed to format upup-555.sfs, upupsave.2fs.
  17. #w468 alsa shutdown now handled in /etc/init.d/alsa.
  18. #w481 if low-ram, save-file is on a fast media, and no swap file/partition, then create swapfile.
  19. #w482 use xorriso instead of cdrtools/cdrkit if installed.
  20. #v431 tulindo: fix saving to ext4 partition.
  21. #091117 weird shutdown bug. would not shutdown when a partition mounted, x restarted.
  22. #091222 added support for saving to /dev/mmcblk* devices.
  23. #100107 would not shutdown if ntfs partition mounted. Added timeout in shutdown dlg.
  24. #100301 brought code to stop network interfaces further down, also consider 'defaultconnect'.
  25. #100309 network shutdown improvement.
  26. #100315 improper shutdown f.s. check.
  27. #100401 choose ext2/3/4 for pupsave file. 100410 offer ext2/3 only.
  28. #100604 shel: Unload the Ethernet drivers on shutdown to prevent battery drain.
  29. #100615 Patriot: suggested code to enable save-layer to remount ro.
  30. #100719 cannot save session to entire partition if puppy installed in a subdirectory.
  31. #100814 log the cumulative bytes transmitted on network.
  32. #100820 delete /var/tmp/* (apparently filled by opera crashes).
  33. #100902 rerwin: log the cumulative bytes transmitted on dialup.
  34. #100911 fsckme.err filename changed to fsckme.flg. see also initNEW, rc.sysinit, init (initramfs).
  35. #100912 no longer have FASTPARTS variable in /etc/rc.d/PUPSTATE, use ATADRIVES instead.
  36. #100912 if copy .sfs's from cd to hd, copy into a subdir, DISTRO_IDSTRING truncated to 8 chars.
  37. #100915 booting cd, put save-file in same subdir (as 100912).
  38. #100917 booting usb, if drive has more than one partition, allow choice where save session.
  39. #100920 reverse 100915. Fix SAVEPATH.
  40. #100926 crap, recent versions of dialog do not work with --ok-label, --cancel-label, if no extra button...
  41. #101013 dir name to be 8+3 chars ex w0901010.083 (<fileprefix1stchar><version><year><month>.<day><kernel3rdchar>)
  42. #101020 if pup installed in a sub-directory, allow save-file in another partition (in same drive).
  43. #101211 shinobar: bugfix.
  44. #110217 bad hack, see /etc/rc.d/rc.update.
  45. #110405 fix 'free' applet output format different in later versions of busybox.
  46. #110424 do not copy puppy*.sfs into a sub-dir (reverse of 101013). see also 'init'.
  47. #110429 create save-file non-root.
  48. #110430 added warning multisession does not work with laptop optical drives. changed dlg timeout from 60 to 90.
  49. #110503 set owner/permission of dirs when 1st save. this is important when choose to run as fido on 1st shutdown.
  50. #110524 notice not to choose fido, as experimental status only.
  51. #110808 L18L: if geany still running at shutdown, delete socket.
  52. #110919 test for usb interface invalid for kernel with inbuilt usb drivers.
  53. #110919 moved creation of save-file code to /usr/sbin/shutdownconfig.
  54. #110919 unlock rfkill. jemimah has this in fluppy. if don't do this, may be locked at next boot.
  55. #v2.20b make shutdown less verbose...
  56. #110923 L18L: i18n conversion. please see /usr/share/doc/nls/shutdownconfig
  57. #110928 fixed, reboots when choose shutdown. very old bug, dates back to 2009.
  58. #110928 modified i18n conversion, only for echo to /dev/console.
  59. #111106 do not execute fuser if network share mount.
  60. #111107 ldolse: unmount network shares before taking down the network
  61. #120103 karl godt: error unmounting stray partitions. 120103 karl godt: more tweaks.
  62. #120129 karl godt: need to rearrange order, refer http://murga-linux.com/puppy/viewtopic.php?t=71767&start=405.
  63. #120129 karl godt: improved ZOMBIES, see http://www.murga-linux.com/puppy/viewtopic.php?t=73122
  64. #120130 rodin.s: update for gettext (line 91).
  65. #120218 changed TEXTDOMAIN=etcrcd___rc.shutdown to TEXTDOMAIN=rc.shutdown (unlikely to be any other file same name in $PATH).
  66. #120219 zekebaby: unmount network shares, alternate method. 120220 another alternate method.
  67. #120409 experimenting with minit, i discovered "rm -rf /tmp/*" is killing mingetty. initrd.gz wipes it, have added code to wipe for full hd install at bootup...
  68. #120427 01micko: support ext4 save-file.
  69. #120514 WARNING: i don't have this problem, as no longer deleting /tmp/* (120409), however Karl Godt reported that 'fuser -k -m' may kill rc.shutdown then if no /tmp/bootcnt.txt then X restarts, ref: http://bkhome.org/blog/?viewDetailed=02827
  70. #120514 Karl Godt: 1st shutdown: test save-file mounted rw, not ro. 120522 removed.
  71. #120514 shinobar: avoid killing this script.
  72. #120522 precise puppy with aufs, have two dirs .wh..wh.orph, .wh..wh.plnk, in /initrd/pup_rw, filter out.
  73. #120629 raspberry pi does not have a hw clock. save date here, read at bootup (see rc.country).
  74. #121104 saving to entire partition, must save /var/local. fixed some text translations.
  75. #121104 move some code up, to before saving session, as some files were not getting saved in some PUPMODEs.
  76. #121104 official 'fuser' output format changed, when save to entire partiton 'fuser' kills too much prevents shutdown.
  77. #121125 umount is a script, this line also unmounts ntfs partition (if mounted) -- which is a bug in script -- use umount-FULL.
  78. #130128 support udf f.s.
  79. #130217 01micko: relocate umount of network shares.
  80. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  81. #110923
  82. . /usr/bin/gettext.sh # enables use of eval_gettext (several named variables) and ngettext (plurals)
  83. export TEXTDOMAIN=rc.shutdown
  84. export OUTPUT_CHARSET=UTF-8
  85. clear
  86. exec 1> /dev/null 2>&1
  87. which rfkill &>/dev/null && rfkill unblock all #110919 jemimah has this in fluppy. if don't do this, may be locked at next boot.
  88. #PUPPYVERSION="`cat /etc/puppyversion`"
  89. #variables created at bootup by init script in initrd...
  90. . /etc/rc.d/PUPSTATE #v2.02
  91. . /etc/DISTRO_SPECS #v412
  92. . /etc/eventmanager # 140417, SFR
  93. ORIGPUPMODE=$PUPMODE #v2.22
  94. #puppy.sfs is in a subdirectory, default to saving in it...
  95. PUPSFSFILE="`echo "$PUPSFS" | cut -f 3 -d ','`"
  96. PSUBDIR="`dirname "$PUPSFSFILE"`"
  97. [ "$PSUBDIR" = "/" ] && PSUBDIR=""
  98. [ $PUPMODE -eq 5 ] && [ "$DEV1FS" = "iso9660" ] && PSUBDIR="" #100920 booting off cd, always default have savefile at /.
  99. [ $PUPMODE -eq 5 ] && [ "$DEV1FS" = "udf" ] && PSUBDIR="" #130128 ditto, for udf f.s.
  100. [ "$DEV1FS" = "msdos" ] && DEV1FS="vfat" #110919
  101. #for a full hd install, only setting PUPMODE (=2). no /initrd, no initrd.gz.
  102. #w482 use xorriso if it is installed (see also functions4puppy)...
  103. CDRECORD='cdrecord'
  104. MKISOFS='mkisofs'
  105. if [ "`which xorriso`" != "" ];then
  106. CDRECORD='xorrecord'
  107. MKISOFS='xorrisofs' #growisofs reads this variable.
  108. fi
  109. export CDRECORD MKISOFS
  110. . /etc/rc.d/functions4puppy #v3.01 has waittrayclosed_func multisavefunc needs CDRECORD MKISOFS
  111. killzombies() {
  112. ZOMBIES="`/bin/ps -H -A | grep '<defunct>' | sed 's/^[[:blank:]]*//g' | cut -f 1 -d ' ' | sort -gr | tr '\n' ' '`" #120129 karl godt: improved, see http://www.murga-linux.com/puppy/viewtopic.php?t=73122
  113. for ONEZOMBIE in $ZOMBIES
  114. do
  115. #echo "`eval_gettext \"Killing parentless zombie process \\\${ONEZOMBIE}\"`"
  116. echo "Killing parentless zombie process $ONEZOMBIE"
  117. /bin/ps --no-header -p $ONEZOMBIE && kill $ONEZOMBIE
  118. done
  119. }
  120. echo "${DISTRO_NAME} ${DISTRO_VERSION} $(gettext 'is now shutting down...')" > /dev/console #120130
  121. echo "Executing /etc/rc.d/rc.shutdown..."
  122. #100315 improper shutdown check. see /etc/rc.d/rc.sysinit, /init in initramfs, and /sbin/init...
  123. if [ -f /fsckme.flg ];then
  124. if [ -f /tmp/dmesg_e2fsck_warnings1 ];then #120717 rc.sysinit creates this file for full installation.
  125. if [ "`grep -G "(${PDEV1})" /tmp/dmesg_e2fsck_warnings1`" = "" ];then
  126. rm -f /fsckme.flg
  127. else
  128. echo -n 'MAXIMAL' >> /fsckme.flg #read by /sbin/initNEW
  129. fi
  130. else
  131. rm -f /fsckme.flg
  132. fi
  133. fi
  134. [ -f /initrd${PUP_HOME}/fsckme.flg ] && rm -f /initrd${PUP_HOME}/fsckme.flg
  135. #v2.0.0 there could be a save tmpfs->persistent-storage running...
  136. while [ ! "`pidof snapmergepuppy | grep '[0-9]'`" = "" ];do
  137. sleep 1
  138. done
  139. killall savepuppyd
  140. retired_close_sound_devices(){
  141. #rc.local0 can read this...
  142. setvol 0 | tr "\n" " " > /etc/.mixervolume
  143. #v2.10 instead, probably do it this way...
  144. /usr/sbin/alsactl store
  145. #v4.00 ...yes, using 'alsactl restore' in rc.sysinit (fast boot) and rc.local0.
  146. #v411 run rc.alsa as it also unloads modules (if not done, prevents shutdown some systems)...
  147. /etc/rc.d/rc.alsa stop
  148. }
  149. cd /
  150. sync
  151. #just a precaution...
  152. echo "Killing X and all X apps..."
  153. killall -9 X > /dev/null 2>&1
  154. sleep 1
  155. killall -3 X > /dev/null 2>&1
  156. sync
  157. #MU warns may need to do this for dillo...
  158. killall file.dpi 2>/dev/null
  159. killall bookmarks.dpi 2>/dev/null
  160. killall dpid 2>/dev/null
  161. #100902 log the cumulative bytes transmitted on dialup...
  162. modemdisconnect #(if connected)
  163. #100814 log the cumulative bytes transmitted on network...
  164. # (see also /etc/rc.d/rc.sysinit and network_tray)
  165. ACTIVE_INTERFACE=""
  166. [ -f /tmp/sns_interface_success ] && ACTIVE_INTERFACE="`cat /tmp/sns_interface_success`" #SNS
  167. [ ! "$ACTIVE_INTERFACE" ] && ACTIVE_INTERFACE="`ifconfig | grep '^[a-z]' | grep -v '^lo' | grep 'Link encap:Ethernet' | cut -f 1 -d ' ' | head -n 1`"
  168. if [ "$ACTIVE_INTERFACE" ];then
  169. if [ -d /sys/class/net/${ACTIVE_INTERFACE}/statistics ];then
  170. RX_BYTES="`cat /sys/class/net/${ACTIVE_INTERFACE}/statistics/rx_bytes`"
  171. TX_BYTES="`cat /sys/class/net/${ACTIVE_INTERFACE}/statistics/tx_bytes`"
  172. echo -n "$RX_BYTES" > /var/local/sns/rx_bytes_session
  173. echo -n "$TX_BYTES" > /var/local/sns/tx_bytes_session
  174. RX_BYTES_MONTH=`cat /var/local/sns/rx_bytes_month`
  175. RX_BYTES_MONTH=`expr $RX_BYTES_MONTH + $RX_BYTES`
  176. echo -n "$RX_BYTES_MONTH" > /var/local/sns/rx_bytes_month
  177. TX_BYTES_MONTH=`cat /var/local/sns/tx_bytes_month`
  178. TX_BYTES_MONTH=`expr $TX_BYTES_MONTH + $TX_BYTES`
  179. echo -n "$TX_BYTES_MONTH" > /var/local/sns/tx_bytes_month
  180. fi
  181. fi
  182. #v2.16 some packages have a service script that requires stopping...
  183. for service_script in /etc/init.d/*
  184. do
  185. [ -x $service_script ] && $service_script stop
  186. done
  187. #note, /etc/rc.d/rc.services does same, with 'start' parameter.
  188. undecided_unmount_network_shares(){
  189. ##111107 ldolse: unmount network shares before taking down the network
  190. ##(see 111106, need to do it sooner, but 111106 will remount read-only if failed to umount here)
  191. for MOUNTPOINT in `mount | grep '^//' | cut -d ' ' -f 3 | tr '\n' ' '`
  192. do
  193. umount -f $MOUNTPOINT
  194. done
  195. ##120219 hmmm, zekebaby reports above does not work for him. i looked up an ex: mount -t nfs boffo.clowns.org:/home/bozo /mnt/bozo
  196. ##so this is zekebaby's patch: (see http://murga-linux.com/puppy/viewtopic.php?p=605451#605451)
  197. for MOUNTPOINT in `mount | grep ':' | cut -d ' ' -f 3 | tr '\n' ' '`
  198. do
  199. umount -f $MOUNTPOINT
  200. done
  201. }
  202. #130217 01micko: moved this below stopping service-scripts...
  203. #121125 umount is a script, this line also unmounts ntfs partition (if mounted) -- which is a bug in script -- use umount-FULL...
  204. umount-FULL -a -t cifs,smbfs,nfs,sshfs #120220 121125
  205. #100301 brought down below call to 'stop' service scripts, needed for lamesmbxplorer.
  206. #bring down network interfaces (prevents shutdown sometimes)...
  207. [ "`pidof wpa_supplicant`" != "" ] && wpa_cli terminate #100309 kills any running wpa_supplicant.
  208. if [ "`grep 'net-setup.sh' /usr/local/bin/defaultconnect`" = "" ];then #see connectwizard and connectwizard_2nd.
  209. for ONENETIF in `ifconfig | grep -E '^wifi[0-9]|^wlan[0-9]|^eth[0-9]' | cut -f 1 -d ' ' | tr '\n' ' '`
  210. do
  211. ifconfig $ONENETIF down 2> /dev/null
  212. [ "`iwconfig | grep "^${ONENETIF}" | grep "ESSID"`" != "" ] && iwconfig $ONENETIF essid off #100309
  213. dhcpcd --release $ONENETIF 2>/dev/null #100309
  214. done
  215. else
  216. /etc/rc.d/rc.network stop
  217. fi
  218. ## quick hack, maybe shutdown problem if ath_pci left loaded...
  219. #[ "`lsmod | grep '^ath_pci'`" != "" ] && rmmod ath_pci
  220. #100604 reported by shel: http://murga-linux.com/puppy/viewtopic.php?t=56238
  221. # Unload the Ethernet drivers on shutdown to prevent battery drain.
  222. for i in `lsmod | cut -d' ' -f1 | tr '\n' ' '`
  223. do
  224. if grep -q "^$i " /etc/networkmodules; then
  225. modprobe -r "$i"
  226. fi
  227. done
  228. #v3.97 deprecated, for pcmcia-cs package...
  229. if [ -f /sbin/cardmgr ];then #pcmcia-cs package installed.
  230. if [ "`lsmod | grep -E '^i82092|^tcic|^i82365|^yenta_socket'`" != "" ];then
  231. #got this out of rc.pcmcia, modified...
  232. #echo -n $(gettext "Shutting down PCMCIA services: ")
  233. echo -n "Shutting down PCMCIA services: "
  234. if [ -s /var/run/cardmgr.pid ] ; then
  235. PID=`cat /var/run/cardmgr.pid`
  236. kill $PID
  237. for N in 1 2 3 4 ; do
  238. kill -0 $PID 2>/dev/null || break #-0 is test only.
  239. sleep 1
  240. done
  241. fi
  242. if [ -s /var/lib/pcmcia/cardmgr.pid ] ; then
  243. PID=`cat /var/lib/pcmcia/cardmgr.pid`
  244. kill $PID
  245. for N in 1 2 3 4 ; do
  246. kill -0 $PID 2>/dev/null || break #-0 is test only.
  247. sleep 1
  248. done
  249. fi
  250. killall -3 "CardBus Watcher" #BK bug, changed -q to -3.
  251. /sbin/rmmod tcic 2>/dev/null || /sbin/rmmod i82365 2>/dev/null || /sbin/rmmod yenta_socket 2>/dev/null || /sbin/rmmod i82092 2>/dev/null
  252. echo "done."
  253. rm -f /var/lock/subsys/pcmcia
  254. fi
  255. fi
  256. killall udevd > /dev/null 2>&1 #100615
  257. #first time booted puppy, there may not have been any persistent storage...
  258. if [ $PUPMODE -eq 5 ];then #ifpupmode5
  259. #110919 code moved out to /usr/sbin/shutdownconfig.
  260. #note, shutdownconfig normally called via /usr/bin/wmreboot or wmpoweroff when want shutdown from X.
  261. unset PUPSAVE SAVEFS SAVEPART SAVEFILE NAMEONLY SAVEPATH MYPASSWORD SFEXT xPDEV xDEVFS
  262. if [ ! -f /tmp/shutdownconfig_results ];then
  263. shutdownconfig
  264. fi
  265. if [ -s /tmp/shutdownconfig_results ];then
  266. . /tmp/shutdownconfig_results #supplies variables PUPMODE SAVEFS PUPSAVE SAVEPART SAVEFILE NAMEONLY SAVEPATH MYPASSWORD SFEXT
  267. rm -f /tmp/shutdownconfig_results #precaution.
  268. fi
  269. fi #end ifpupmode5
  270. #w481 if low-ram, save-file is on a fast media, and no swap file/partition, then create one...
  271. #note, rc.sysinit in puppy 3.01 had code to create a pupswap.swp, not in 4.x series.
  272. swapfile_func() {
  273. localSMNTPT="$1"
  274. localSAVEPART="$2"
  275. if [ ! -f ${localSMNTPT}/pupswap.swp ];then
  276. if [ "$ATADRIVES" ];then #see /etc/rc.d/PUPSTATE 100912
  277. TOTALRAM=`free | grep -o 'Mem: .*' | tr -s ' ' | cut -f 2 -d ' '` #110405
  278. if [ $TOTALRAM -lt 262145 ];then #was 128000, bump up <=256MB.
  279. TOTALSWAP=`free | grep -o 'Swap: .*' | tr -s ' ' | cut -f 2 -d ' '` #110405
  280. if [ $TOTALSWAP -eq 0 ];then
  281. spPATTERN="`echo -n "$localSAVEPART" | sed -e 's/[0-9]*$//'` " #100912 ex: 'sda '
  282. if [ "`echo "$ATADRIVES" | grep "$spPATTERN"`" != "" ];then #100912
  283. smPATTERN=' '"$localSMNTPT"'$'
  284. FREESAVEM=`df -m | grep "$smPATTERN" | tr -s ' ' | cut -f 4 -d ' '`
  285. SWPSIZE=""
  286. [ $FREESAVEM -gt 100 ] && SWPSIZE="50"
  287. [ $FREESAVEM -gt 200 ] && SWPSIZE="100"
  288. [ $FREESAVEM -gt 500 ] && SWPSIZE="200"
  289. if [ "$SWPSIZE" ];then
  290. echo >/dev/console
  291. echo "$(eval_gettext "A swapfile named 'pupswap.swp' is being created in partition \${localSAVEPART},")" >/dev/console
  292. echo "$(eval_gettext "of size \${SWPSIZE}MB. This will be loaded at next boot, to ease the low-RAM")" >/dev/console
  293. echo "$(gettext "situation of your PC. Creating now...")" >/dev/console
  294. dd if=/dev/zero of=${localSMNTPT}/pupswap.swp bs=1048576 count=$SWPSIZE
  295. if [ $? -eq 0 ];then
  296. sync
  297. mkswap ${localSMNTPT}/pupswap.swp
  298. fi
  299. if [ $? -ne 0 ];then
  300. rm -f ${localSMNTPT}/pupswap.swp 2>/dev/null
  301. echo "$(gettext "...error, unable to create swapfile.")" >/dev/console
  302. #echo "...error, unable to create swapfile." >/dev/console
  303. fi
  304. sync
  305. fi
  306. fi
  307. fi
  308. fi
  309. fi
  310. fi
  311. } #end swapfile_func
  312. if [ "$PUPSAVE" != "" ];then
  313. #f.s. and partition where ${DISTRO_FILE_PREFIX}save.2fs is located...
  314. SAVEFS="`echo -n "$PUPSAVE" | cut -f 2 -d ','`"
  315. SAVEPART="`echo -n "$PUPSAVE" | cut -f 1 -d ','`"
  316. SAVEFILE="`echo -n "$PUPSAVE" | cut -f 3 -d ','`"
  317. fi
  318. clear >/dev/console
  319. #121104 move some code up, to before saving session...
  320. cp -f /etc/profile /var/local/etc_profile_at_shutdown #110217 bad hack, see /etc/rc.d/rc.update.
  321. #120629 raspberry pi does not have a hw clock. save here, read at bootup (see rc.country)...
  322. DATESAVE="`date -R | cut -f 2-5 -d ' ' | cut -f 1 -d '.' | tr '[a-z]' '[A-Z]'`" #ex: 29 JUN 2012 08:39:07
  323. echo -n "$DATESAVE" > /var/local/shutdown_date_saved
  324. #when the working files run in tmpfs in ram, they are saved (below) and /tmp and /var
  325. #are screened out. however, some PUPMODES mount ${DISTRO_FILE_PREFIX}save.2fs directly on /initrd/pup_rw,
  326. #the top unionfs layer, meaning that there is no intermediary tmpfs in ram for working
  327. #files, hence everything is saved directly, ditto for PUPMODE=2 a full h.d. install.
  328. #hence need to do some explicit wiping here...
  329. echo -n "" > /var/log/messages #delete, as it keeps growing.(note choosepartfunc uses this)
  330. rm -f /var/log/X*
  331. #120409 experimenting with minit, i discovered this line is killing mingetty. initrd.gz wipes it, have added code to wipe for full hd install at bootup...
  332. #rm -rf /tmp/*
  333. rm -f /var/lock/LCK*
  334. rm -f /var/run/*.pid
  335. rm -rf /root/tmp 2> /dev/null #...note, not screening this out in any of above save modes.
  336. rm -rf /root/.thumbnails/* 2> /dev/null
  337. [ -d /var/tmp ] && rm -rf /var/tmp/* 2> /dev/null #100820
  338. #110808 L18L if geany still running at shutdown, need these...
  339. rm -f /tmp/geany_socket* 2>/dev/null
  340. [ -d /root/.config/geany ] && rm -f /root/.config/geany/geany_socket* 2>/dev/null
  341. [ -f /etc/rc.d/MORESFS ] && rm /etc/rc.d/MORESFS
  342. sync
  343. # 140417, SFR
  344. asktosave_func () {
  345. if [ "$ASKTOSAVE" = "false" ]; then
  346. RETVAL=0
  347. else
  348. dialog --timeout 60 --yes-label "$(gettext 'SAVE')" --no-label "$(gettext 'NO SAVE')" --yesno "$(gettext 'Press ENTER key to save session...
  349. Or, press TAB then ENTER to not save session...
  350. Or, wait 60 seconds to shutdown without saving session...')" 0 0 >/dev/console
  351. RETVAL=$?
  352. fi
  353. [ $RETVAL -ne 0 ] && echo "$(gettext "Session not saved")" >/dev/console
  354. return $RETVAL
  355. }
  356. #save session...
  357. case $PUPMODE in
  358. 5) #v2.02 first boot.
  359. echo "$(gettext "Session not saved")" >/dev/console
  360. #echo "Session not saved" >/dev/console
  361. sync
  362. #ha ha, before had this, if aborted save after choosing save-partition, code
  363. #further down wiped all of the partition (it was mntd on /tmp/savepup)...
  364. [ ! "$SMNTPT" = "" ] && umount $SMNTPT 2>/dev/null
  365. #...well, fixed it, changed mntpt so not in /tmp. Leave above line here.
  366. ;;
  367. 32) #first shutdown, save back to PDEV1. v3.97: xPDEV
  368. echo "$(eval_gettext "Saving session to \${xPDEV}...")" >/dev/console #121104
  369. DEV1MNT=`grep "/dev/${xPDEV} " /proc/mounts | cut -f 2 -d " "`
  370. if [ "$DEV1MNT" = "" ];then
  371. mkdir -p /mnt/$xPDEV
  372. mount -t $xDEVFS /dev/$xPDEV /mnt/$xPDEV
  373. if [ ! $? -eq 0 ];then
  374. echo "$(eval_gettext "ERROR: unable to mount /dev/\${xPDEV}, cannot save.")" >/dev/console #121104
  375. #echo "ERROR: unable to mount /dev/$xPDEV, cannot save." >/dev/console
  376. exit
  377. fi
  378. DEV1MNT="/mnt/$xPDEV"
  379. fi
  380. RDIRS="`find /initrd/pup_rw/ -maxdepth 1 -mount -type d | grep -v "/$" |grep -v "/mnt"|grep -v "/tmp"|grep -v "/proc"|grep -v "/sys"|grep -v "/var"|grep -v "/dev" | grep -v "/lost" |tr "\n" " "`"
  381. for ONEDIR in $RDIRS
  382. do
  383. cp -a $ONEDIR ${DEV1MNT}/
  384. done
  385. mkdir -p ${DEV1MNT}/var #121104
  386. cp -a /initrd/pup_rw/var/local ${DEV1MNT}/var/ #121104 puppy data here that must be saved.
  387. #DISTRO_SPECS must be saved, init script looks for it to determine if
  388. #there is a saved session...
  389. cp -af /etc/DISTRO_SPECS ${DEV1MNT}/etc/
  390. sync
  391. #w481 if low-ram, save-file is on a fast media, and no swap file/partition, then create one...
  392. swapfile_func $DEV1MNT $xPDEV
  393. umount $DEV1MNT 2> /dev/null
  394. ;;
  395. 128) #1st shutdown, save to ${DISTRO_FILE_PREFIX}save.2fs.
  396. #partition already mounted on $SMNTPT.
  397. echo "$(eval_gettext "Saving session to \${SAVEFILE} file on \${SAVEPART} partition...")" >/dev/console
  398. #echo "Saving session to $SAVEFILE file on $SAVEPART partition..." >/dev/console
  399. mkdir /tmp/save1stpup
  400. #echo -n $(gettext "Mounting ${SAVEFILE}...")
  401. echo -n "Mounting ${SAVEFILE}..."
  402. FILEFS="ext3"
  403. [ "`echo -n "$SAVEFILE" | grep "2fs"`" != "" ] && FILEFS="ext2"
  404. [ "`echo -n "$SAVEFILE" | grep "4fs"`" != "" ] && FILEFS="ext4" #120427 01micko
  405. if [ "$CRYPTO" = "" ];then
  406. if [ -d $SMNTPT$SAVEFILE ]; then
  407. mount -o bind $SMNTPT$SAVEFILE /tmp/save1stpup
  408. else
  409. mount -t $FILEFS -o noatime,rw,loop $SMNTPT$SAVEFILE /tmp/save1stpup
  410. fi
  411. else
  412. #note: loop2 is kept available for scripts to use. but, do this to find free one...
  413. DEVLOOP="`losetup-FULL -f`"
  414. #'-p 0' means read password from stdin...
  415. #v2.17 crap, '-p 0' works for aes, not for xor encryption....
  416. if [ "$CRYPTO" = '-E 1' ];then #light xor encr.
  417. echo "$(gettext "Note, a bug in one of the Linux utility programs requires you to reenter")" >/dev/console
  418. echo "$(gettext "the password in the case of light encryption...")" >/dev/console
  419. #echo "Note, a bug in one of the Linux utility programs requires you to reenter" >/dev/console
  420. #echo "the password in the case of light encryption..." >/dev/console
  421. fi
  422. echo "$MYPASSWORD" | losetup-FULL -p 0 $CRYPTO $DEVLOOP ${SMNTPT}$SAVEFILE >/dev/console
  423. [ ! $? -eq 0 ] && exit 1
  424. mount -t $FILEFS -o noatime,rw $DEVLOOP /tmp/save1stpup
  425. fi
  426. #floppy disk has little space. snapmergepuppy handles this for subsequent saves, but for now,
  427. #be radical, only save /etc...
  428. if [ "$SAVEPART" = "fd0" ];then
  429. RDIRS="/initrd/pup_rw/etc"
  430. else
  431. #110503 remove /var exclusion...
  432. #120522 testing precise puppy with aufs, have two dirs here .wh..wh.orph, .wh..wh.plnk, filter out...
  433. RDIRS="`find /initrd/pup_rw/ -mindepth 1 -maxdepth 1 -mount -type d | grep -v '/\.wh\.' | grep -v -E '/$|/mnt|/tmp|/proc|/sys|/dev|/lost' | tr '\n' ' '`"
  434. fi
  435. for ONEDIR in $RDIRS
  436. do
  437. BASENAME="`basename $ONEDIR`"
  438. if [ ! -d /tmp/save1stpup/${BASENAME} ];then #110503
  439. mkdir -p /tmp/save1stpup/${BASENAME}
  440. #110503 got this from /usr/sbin/snapmergepuppy...
  441. chmod "/tmp/save1stpup/${BASENAME}" --reference="$ONEDIR"
  442. OWNER="`stat --format=%U "$ONEDIR"`"
  443. chown $OWNER "/tmp/save1stpup/${BASENAME}"
  444. GRP="`stat --format=%G "$ONEDIR"`"
  445. chgrp $GRP "/tmp/save1stpup/${BASENAME}"
  446. touch "/tmp/save1stpup/${BASENAME}" --reference="$ONEDIR"
  447. fi
  448. cp -a $ONEDIR/* /tmp/save1stpup/${BASENAME}/ #v2.16exp3
  449. [ "$BASENAME" = "root" ] && cp -a $ONEDIR/.[0-9a-zA-Z]* /tmp/save1stpup/${BASENAME}/ #v2.16exp4
  450. done
  451. sync
  452. umount /tmp/save1stpup
  453. #w481 if low-ram, save-file is on a fast media, and no swap file/partition, then create one...
  454. swapfile_func $SMNTPT $SAVEPART
  455. umount $SMNTPT 2> /dev/null
  456. #100917 booted from usbflash, saved to partition other than boot...
  457. #101020 also allowing in case of frugal install in a sub-directory...
  458. if [ "$expBOOTDRV" ];then #see choosepartfunc.
  459. if [ "$SAVEPART" != "$PDEV1" ];then
  460. SAVEMARK="`echo -n "$SAVEPART" | rev | sed -e 's%[a-z].*%%' | rev`" #ex: sdc2 becomes 2.
  461. aPATTERN="/dev/$PDEV1 "
  462. aMNTPT=`grep "$aPATTERN" /proc/mounts | cut -f 2 -d ' '`
  463. mkdir -p /mnt/$PDEV1
  464. if [ "$aMNTPT" = "" ];then
  465. mount -t $DEV1FS /dev/$PDEV1 /mnt/$PDEV1
  466. aMNTPT="/mnt/$PDEV1"
  467. fUMNT='yes'
  468. fi
  469. echo -n "$SAVEMARK" > ${aMNTPT}${xPSUBDIR}/SAVEMARK #file in boot partition. 'init' script reads this. 101020
  470. [ "$fUMNT" = "yes" ] && umount /dev/$PDEV1
  471. fi
  472. fi
  473. ;;
  474. 69) #save to folder on multisession CD/DVD (including 1st shutdown). (5+64=69) NOT used
  475. multisavefunc
  476. ;;
  477. 77) #save to folder on multisession CD/DVD (including 1st shutdown). (13+64=77)
  478. multisavefunc
  479. ;;
  480. 2)
  481. echo "$(eval_gettext "\${PDEV1} mounted directly, session already saved.")" >/dev/console
  482. #echo "$PDEV1 mounted directly, session already saved." >/dev/console
  483. ;;
  484. 6)
  485. echo "$(eval_gettext "\${PDEV1} mounted directly top layer, session already saved.")" >/dev/console
  486. #echo "$PDEV1 mounted directly top layer, session already saved." >/dev/console
  487. ;;
  488. 12)
  489. TXT=`basename $SAVEFILE`
  490. echo "$(eval_gettext "\${TXT} mounted directly top layer, session already saved.")" >/dev/console
  491. #echo "`basename $SAVEFILE` mounted directly top layer, session already saved." >/dev/console
  492. ;;
  493. 3) #PDEV1.
  494. asktosave_func
  495. if [ $? -eq 0 ]; then
  496. echo "$(eval_gettext "Saving session to \${PDEV1}...")" >/dev/console
  497. /usr/sbin/snapmergepuppy /initrd/pup_ro1 /initrd/pup_rw
  498. fi
  499. ;;
  500. 7) #PDEV1 and PUPSFS.
  501. asktosave_func
  502. if [ $? -eq 0 ]; then
  503. echo "$(eval_gettext "Saving session to \${PDEV1}...")" >/dev/console
  504. /usr/sbin/snapmergepuppy /initrd/pup_ro1 /initrd/pup_rw
  505. fi
  506. ;;
  507. 13) #PDEV1 and PUPSFS and PUPSAVE
  508. #/initrd/pup_rw has tmpfs, pup_ro1 has ${DISTRO_FILE_PREFIX}save.2fs file (PUPSAVE), pup_ro2 has PUPSFS file.
  509. #the above are in unionfs at /.
  510. asktosave_func
  511. if [ $? -eq 0 ]; then
  512. echo "$(eval_gettext "Saving session to \${SAVEFILE} (\${SAVEPART})...")" >/dev/console
  513. /usr/sbin/snapmergepuppy /initrd/pup_ro1 /initrd/pup_rw
  514. fi
  515. ;;
  516. *)
  517. echo "$(gettext "Session not saved")" >/dev/console
  518. #echo "Session not saved" >/dev/console
  519. sleep 2
  520. ;;
  521. esac
  522. retired_unmount_partitions(){
  523. #120129 karl godt: need to rearrange order, refer http://murga-linux.com/puppy/viewtopic.php?t=71767&start=405 ...
  524. MNTDPARTS="`mount`"
  525. MNTDPARTS="`echo $MNTDPARTS |rev|sed 's# )#\n)#g' |rev`" #reverses order of lines.
  526. #echo
  527. #echo $(gettext "Unmounting stray filesystems:")
  528. echo "Unmounting stray filesystems:"
  529. #091117 110928 if partition mounted, when choose shutdown, pc rebooted. found that param given to fuser must be mount-point, not /dev/*...
  530. STRAYPARTL="`echo "$MNTDPARTS" | grep ' /mnt/' |grep -v -E '/dev/pts|/proc|/sys|tmpfs|rootfs|on / |/dev/root|usbfs|unionfs|aufs|/initrd'`"
  531. STRAYPARTD="`echo "$STRAYPARTL" | cut -f 1 -d ' ' | tr '\n' ' '`"
  532. #111106 do not execute fuser if network share mount (may hang, see http://murga-linux.com/puppy/viewtopic.php?t=73122)...
  533. STRAYPARTandMNT="`echo "$STRAYPARTL" | cut -f 1,3 -d ' ' | tr ' ' '|' | tr '\n' ' '`"
  534. for ONESTRAY in $STRAYPARTandMNT
  535. do
  536. FLAGCIFS="`echo -n ${ONESTRAY} | grep '^//'`"
  537. ONESTRAYMNT="`echo -n ${ONESTRAY} | cut -f 2 -d '|'`"
  538. #echo "`eval_gettext \"Unmounting \\\${ONESTRAY}...\"`"
  539. echo "Unmounting $ONESTRAYMNT..."
  540. if [ "$FLAGCIFS" = "" ];then
  541. xFUSER="`fuser -m $ONESTRAYMNT 2>/dev/null`"
  542. [ "$xFUSER" != "" ] && fuser -k -m $ONESTRAYMNT 2>/dev/null
  543. fi
  544. killzombies #v3.99
  545. sync
  546. umount -r $ONESTRAYMNT #120103 karl godt.
  547. done
  548. }
  549. # Karl Godt: 2013-12-14 reworked the whole unmount block
  550. MOUNTED=`tac /proc/mounts | grep -vE '/dev |/dev/root | rootfs | / | usbfs | aufs | unionfs | tmpfs ' | cut -f2 -d' '`
  551. STRAY_MOUNTPOINTS=`echo "$MOUNTED" | grep -vE '/proc|/sys|/initrd|/dev |/dev/pts'`
  552. #echo
  553. #echo $(gettext "Unmounting stray filesystems:")
  554. [ "$STRAY_MOUNTPOINTS" ] && echo "Unmounting stray filesystems:" >/dev/console
  555. for MOUNT_POINT in $STRAY_MOUNTPOINTS;
  556. do
  557. MOUNT_POINT=`busybox echo -e "$MOUNT_POINT"` # formats escaped chars like \040 to literal like ' ' (space)
  558. #echo "`eval_gettext \"Unmounting \\\${MOUNT_POINT}...\"`"
  559. echo "Unmounting '$MOUNT_POINT' ..." >/dev/console
  560. FLAGCIFS=`echo -n "${MOUNT_POINT}" | grep '^//'`
  561. if [ ! "$FLAGCIFS" ]; then
  562. xFUSER=`fuser -m "$MOUNT_POINT" 2>/dev/null`
  563. [ "$xFUSER" ] && fuser -k -m "$MOUNT_POINT"
  564. fi
  565. killzombies #v3.99
  566. sync
  567. umount -r "$MOUNT_POINT" #120103 karl godt.
  568. done
  569. swapoff -a #works only if swaps are in mtab or ftab
  570. #v2.13 menno suggests this improvement...
  571. STRAYPARTD="`cat /proc/swaps | grep "/dev/" | cut -f 1 -d " " | tr "\n" " "`"
  572. for ONESTRAY in $STRAYPARTD
  573. do
  574. #echo "`eval_gettext \"Swapoff \\\${ONESTRAY}\"`"
  575. echo "Swapoff $ONESTRAY"
  576. swapoff $ONESTRAY
  577. done
  578. sync
  579. #rm -f /tmp/wmexitmode.txt
  580. #note, there is a problem with unmounting, especially ntfs as it cannot be remounted
  581. #ro (?). A ntfs part with ${DISTRO_FILE_PREFIX}save.2fs cannot be unmounted because of the mounted
  582. #${DISTRO_FILE_PREFIX}save.2fs.
  583. #at least, attempt to kill anything running in the ntfs partition...
  584. #(i don't think anything will be, only /dev/loop1 (${DISTRO_FILE_PREFIX}save.2fs), but just in case)
  585. BPS="`busybox ps -A`" #121104
  586. SHID="`echo "$BPS" | grep ' -sh$' | head -n 1 | sed -e 's%^ %%g' | cut -f 1 -d ' '`" #121104
  587. MYPID=$$ #120514
  588. PARENT=$PPID #120514 id of parent process. variable provided by system.
  589. ABSPUPHOME="" #100107
  590. [ "$PUP_HOME" ] && ABSPUPHOME="/initrd${PUP_HOME}" #v2.16rc
  591. [ ! "$ABSPUPHOME" ] && ABSPUPHOME="/initrd/mnt/dev_save" #v2.16rc
  592. if [ "`busybox mount | grep "$ABSPUPHOME"`" != "" ];then
  593. #121104 full 'fuser' outputs pids with "m" on end, ex: 6418m 6632m 9169m 9170m
  594. #busybox 'fuser' does not. well, could remove the "m"...
  595. BADPIDS="`fuser -m $ABSPUPHOME 2>/dev/null | tr -d 'm'`" #100107 121104
  596. for ONEBAD in $BADPIDS
  597. do
  598. [ "$ONEBAD" = "$MYPID" ] && continue #120514 precaution.
  599. [ "$ONEBAD" = "$PARENT" ] && continue #120514 precaution.
  600. [ "$ONEBAD" = "$SHID" ] && continue #121104 save to entire partition, shutdown was aborting.
  601. echo "Killing process $ONEBAD..."
  602. kill $ONEBAD
  603. sleep 1
  604. kill -9 $ONEBAD 2>/dev/null
  605. sync
  606. done
  607. killzombies #v3.99
  608. fi
  609. #v2.16rc try this too... SAVE_LAYER defined in /etc/rc.d/PUPSTATE...
  610. if [ "$SAVE_LAYER" ];then
  611. sync
  612. SAVEDEV=`grep "/initrd${SAVE_LAYER}" /proc/mounts | cut -f 1 -d ' '`
  613. SAVEFS=`grep "/initrd${SAVE_LAYER}" /proc/mounts | cut -f 3 -d ' '`
  614. #100615 Patriot: suggested this code to enable save-layer to remount ro...
  615. uniFS=$(awk '/unionfs/ {print $3}' /proc/mounts) #gets fstype, ex: aufs
  616. if [ "$uniFS" == "aufs" -a "$SAVE_LAYER" == "/pup_rw" ]; then
  617. #i think only work if prepended dir is a separate f.s...
  618. if [ "`mount | grep '^tmpfs on /tmp '`" != "" ];then #created by /init in initrd.gz
  619. mkdir -p /tmp/unrootfs
  620. busybox mount -o remount,prepend:/tmp/unrootfs,xino=/tmp/unrootfs/xino -t $uniFS / /
  621. sync
  622. fi
  623. fi
  624. busybox mount -t $SAVEFS -o remount,ro $SAVEDEV /initrd${SAVE_LAYER} 2>/dev/null
  625. umount-FULL -i -n -l /initrd${SAVE_LAYER} 2>/dev/null #-l is lazy unmount.
  626. fi
  627. #v2.16 try one more thing for ntfs... lazy unmount, suggested by GuestToo...
  628. MNTFUSE="`busybox mount | grep 'fuse' | head -n 1 | cut -f 3 -d ' '`"
  629. if [ "$MNTFUSE" != "" ];then
  630. #v2.17 hairywill found the -u is required with -z...
  631. fusermount -z -u $MNTFUSE
  632. fi
  633. busybox umount -ar > /dev/null 2>&1
  634. #the end#