drive_all 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #!/bin/sh
  2. #(c) Copyright 2008 Barry Kauler, puppylinux.com
  3. #2008 Lesser GPL licence v2 (see /usr/share/doc/legal)
  4. #/sbin/pup_event_frontend_d creates a desktop icon and copy of this script for each drive, ex:
  5. # for drive sda: cp -a /usr/local/bin/drive_all /root/.pup_event/drive_sda/AppRun
  6. #this script is launched by pup_event_frontend_d when the icon is first created. also,
  7. #this script is launched when user clicks on deskop icon (former is configurable,
  8. # see /usr/sbin/eventmanager.
  9. #passed param is category of drive: card, floppy, optical, usbdrv, drive.
  10. #v4.0.2 jun08: first write of this script, expanding 'pup_eventd' management.
  11. #v403 icon menu, can call with 'unmount', 'eventmanager' and 'pmount_<DRV_CATEGORY>' params.
  12. #v411 added 'quiet' option for vfat, suppresses error msg when file attribs not preserved on copy.
  13. #w002 left click to unmount a partition.
  14. #w476 fix codepage for vfat f.s.
  15. # 13jun09 by Masaki Shinomiya <shino@pos.to> vfat and so9660 - utf8 option as the default
  16. # 15jun09 : look up /etc/codepage only for vfat utf8 option
  17. # 27jun09 : vfat mount options by Masaki Shinomiya <shino@pos.to>
  18. # 30jun09 I18N
  19. # 08jul09 looking up ONDRVNAME, ask to remove CD
  20. # 16jul09 not ask to remove CD, only open tray
  21. #v424 have added right-click menu item to unmount all partitions (see pup_event_frontend_d)...
  22. #v424 have removed this dlg box. maybe make it optional in the future...
  23. #091223 extra check for invalid desktop drive icon.
  24. #100118 optional defaultcdplayer, defaultdvdplayer.
  25. #100127 codepage fixes. 100131 iso9660 fix.
  26. #100706 BK: bug fixes, introduced with i81n support.
  27. #100719 BK: correct detection of DVD vidoe and launching mediaplayer.
  28. #110126 support /dev/hd* optical device nodes.
  29. #110505 support sudo for non-root user.
  30. #110720 should not open a layer inside aufs, ex: session saved entire partition, desktop drive icon is /initrd/pup_rw.
  31. #110721 detect if have clicked on a close-box of mounted partition.
  32. #120110 running as fido, popup password box changes mouse pointer position, need read before run sudo.
  33. #120129 karl godt: fix ZOMBIES, see http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420. see also rc.shutdown, pmount, pup_event_frontend_d.
  34. #120323 partial replace 'xmessage' with 'pupmessage'.
  35. #120516 pup_event_frontend_d may call here when a drive plugged in, fix handling of automounting.
  36. #120517 syntax error in 120516, quotes needed.
  37. #130128 convert i18n "technosaurus-method" to gettext.
  38. #130128 optical media, if has a f.s., can be either iso9660 or udf (see updated probepart and pup_event_frontend_d).
  39. #130203 change probedisk2 to probedisk.
  40. #130216 add support f2fs.
  41. export TEXTDOMAIN=drive_all
  42. export OUTPUT_CHARSET=UTF-8
  43. . gettext.sh
  44. [ ! -f /tmp/desktop_drives_curpos ] && getcurpos > /tmp/desktop_drives_curpos #120110
  45. [ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
  46. CURPOS="`cat /tmp/desktop_drives_curpos`" #120110
  47. rm -f /tmp/desktop_drives_curpos #120110
  48. export LANG # need to restart pmount with correct locale
  49. #w002 extracted as a func...
  50. unmount_func() {
  51. rox -D $MNTPT
  52. sync
  53. while [ 1 ];do
  54. umount $MNTPT ; umVAL=$?
  55. #fix zombie process prevents partition unmount, invisible to fuser...
  56. if [ $umVAL -ne 0 ];then
  57. #120129 karl godt: fix ZOMBIES, see http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420 ...
  58. ZOMBIES="`ps -H -A | grep '<defunct>' | sed 's/^[[:blank:]]*//;s/ /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
  59. for ONEZOMBIE in $ZOMBIES
  60. do
  61. echo "Killing parentless zombie process $ONEZOMBIE"
  62. kill $ONEZOMBIE
  63. done
  64. umount $MNTPT ; umVAL=$?
  65. fi
  66. #rare ocassion, partition got unmounted before umount runs above...
  67. umPATTERN="${MNTPT} "
  68. [ "`busybox mount | grep "$umPATTERN"`" = "" ] && umVAL=0
  69. if [ $umVAL -ne 0 ];then
  70. EXTRAMSG0="`fuser -v -m $MNTPT`"
  71. if [ "$EXTRAMSG0" = "" ];then
  72. /usr/lib/gtkdialog/box_ok "$(gettext 'Puppy drive manager')" error "$(gettext 'FAILURE! In the case of removable media, the most
  73. common reason is the media is not currently inserted.
  74. If so, please remedy.')"
  75. else
  76. xmessage -bg "#ff99ff" -title "$(gettext 'Puppy drive manager: FAIL')" -center -buttons "$(gettext 'KILL')":10,"$(gettext 'EXIT')":20 "$(gettext 'FAILURE!')
  77. $(gettext 'One or more processes (programs) are currently
  78. using the partition. Here they are:')
  79. ${EXTRAMSG0}
  80. $(gettext "If you press the 'KILL' button, Puppy will attempt to
  81. kill the offending programs for you. Only do this as a
  82. last resort.")
  83. $(gettext "Firstly you should try to close the programs
  84. manually, so do not press 'KILL'.")
  85. $(gettext "PLEASE PRESS THE 'EXIT' BUTTON!")"
  86. if [ $? -eq 10 ];then
  87. fuser -k -m $MNTPT
  88. continue
  89. fi
  90. fi
  91. else
  92. [ "$DRV_CATEGORY" = "optical" ] && eject_cd /dev/$ONEDRVNAME
  93. fi
  94. break
  95. done
  96. }
  97. eject_cd ()
  98. {
  99. eject $1
  100. }
  101. FSTYPE=""
  102. [ $2 ] && FSTYPE="$2" #see pup_event_frontend_d.
  103. PARAM1=""
  104. [ $1 ] && PARAM1="$1"
  105. #extract drive name from end of the script name...
  106. #note, when click on desktop icon, /root/.pup_event/drive_<ONEDRVNAME> is executed,
  107. #however a script may run /root/.pup_event/drive_<ONEDRVNAME>/AppRun ...
  108. ONEDRVNAME=$(dirname $0|sed -e 's/^.*_//')
  109. if [ "`echo "${PARAM1}" | grep 'pmount_'`" != "" ];then #v403 chose pmount from drive icon menu.
  110. DRV_CATEGORY="`echo -n "${PARAM1}" | cut -f 2 -d '_'`"
  111. if [ "`pidof gtkdialog_pmount`" != "" ];then #if pmount running, kill it.
  112. killall gtkdialog_pmount 2>/dev/null
  113. sleep 0.1
  114. fi
  115. exec pmount ${DRV_CATEGORY}
  116. fi
  117. #110721 detect if have clicked on a close-box of mounted partition...
  118. #when left-click desktop icon, param1 passed this script: drive|card|floppy|optical|usbdrv
  119. # and param2 has the fs type...
  120. case $PARAM1 in
  121. drive|card|floppy|optical|usbdrv)
  122. mPATTERN='^/dev/'"$ONEDRVNAME"' '
  123. MNTPT="`df -a| grep -m1 "$mPATTERN" | tr -s ' ' | cut -f 6 -d ' '`" #140702 shinobar
  124. if [ "$MNTPT" ];then
  125. #get coords of desktop icon...
  126. drPTN=">/root/.pup_event/drive_${ONEDRVNAME}<"
  127. PPENTRY="`grep "$drPTN" /root/Choices/ROX-Filer/PuppyPin`"
  128. if [ "$PPENTRY" ];then
  129. xleft="`echo -n "$PPENTRY" | grep -o ' x="[0-9]*"' | cut -f 2 -d '"'`" #'geany
  130. ytop="`echo -n "$PPENTRY" | grep -o ' y="[0-9]*"' | cut -f 2 -d '"'`" #'geany
  131. CURPOSX=`echo -n "$CURPOS" | cut -f 1 -d ' '`
  132. CURPOSY=`echo -n "$CURPOS" | cut -f 2 -d ' '`
  133. #hmmm, have to apply correction to cursor position...
  134. CURPOSX=$(($CURPOSX + 24))
  135. CURPOSY=$(($CURPOSY + 32))
  136. xcloseleft=$(($xleft + 35)) #close-box is 13x13 pixels, want bigger pickup area.
  137. xcloseright=$(($xleft + 53))
  138. yclosetop=$(($ytop - 5))
  139. yclosebottom=$(($ytop + 13))
  140. if [ $CURPOSX -ge $xcloseleft ];then
  141. if [ $CURPOSX -le $xcloseright ];then
  142. if [ $CURPOSY -ge $yclosetop ];then
  143. if [ $CURPOSY -le $yclosebottom ];then
  144. PARAM1="unmount"
  145. fi
  146. fi
  147. fi
  148. fi
  149. fi
  150. fi
  151. ;;
  152. esac
  153. if [ "$PARAM1" = "eventmanager" ];then #v403 chosen from drive icon menu.
  154. #v405 'desktop' param, cutdown tabs for desktop drive icons only...
  155. exec eventmanager desktop
  156. fi
  157. if [ "$PARAM1" = "unmount" ];then #v403 chose unmount from drive icon menu.
  158. mPATTERN='^/dev/'"$ONEDRVNAME"' '
  159. MNTPT="`df -a| grep -m1 "$mPATTERN" | tr -s ' ' | cut -f 6 -d ' '`" #140702 shinobar
  160. DRV_CATEGORY=""
  161. PROBEDISK="probedisk"
  162. DRV_CATEGORY=$($PROBEDISK | grep "^/dev/$ONEDRVNAME|" | cut -d'|' -f 2)
  163. echo "$DRV_CATEGORY" | grep '^cd' && DRV_CATEGORY="optical"
  164. [ "$MNTPT" = "" ] && [ "$DRV_CATEGORY" = "optical" ] && eject_cd /dev/$ONEDRVNAME
  165. [ "$MNTPT" = "" ] && exit #precaution.
  166. if [ "$MNTPT" = "/" -o "`echo "$MNTPT" | grep '/initrd/'`" != "" ];then
  167. /usr/lib/gtkdialog/box_ok "$(gettext 'Puppy drive manager')" error "$(eval_gettext "/dev/\${ONEDRVNAME} mounted on \${MNTPT}
  168. is in use by Puppy. You cannot unmount it.")"
  169. exit
  170. fi
  171. unmount_func
  172. exit
  173. fi
  174. #v424 have added right-click menu item to unmount all partitions (see pup_event_frontend_d)...
  175. if [ "$PARAM1" = "unmountall" ];then
  176. for MNTPT in `mount | grep '^/dev/' | grep -vE '^/dev/loop|^/dev/ram' | cut -f 3 -d ' ' | grep -v '^/initrd/' | grep -v '^/$' | tr '\n' ' '`
  177. do
  178. unmount_func
  179. done
  180. exit
  181. fi
  182. DRV_CATEGORY="${PARAM1}"
  183. [ "$DRV_CATEGORY" = "" ] && DRV_CATEGORY="drive" #precaution.
  184. [ "$DRV_CATEGORY" = "all" ] && DRV_CATEGORY="drive" #precaution.
  185. [ -f /etc/eventmanager ] && . /etc/eventmanager #written by /usr/sbin/eventmanager
  186. [ "$ICONDESK" != "true" -a "$ICONDESK" != "false" ] && ICONDESK="true"
  187. [ ! $RAMSAVEINTERVAL ] && RAMSAVEINTERVAL=30
  188. [ "$HOTPLUGNOISY" != "true" -a "$HOTPLUGNOISY" != "false" ] && HOTPLUGNOISY="false"
  189. [ "$AUTOTARGET" != "true" -a "$AUTOTARGET" != "false" ] && AUTOTARGET="true"
  190. [ "$ICONPARTITIONS" != "true" -a "$ICONPARTITIONS" != "false" ] && ICONPARTITIONS="true"
  191. #if ICONDESK=false (see /etc/eventmanager and /sbin/pup_eventd) then there will only
  192. #be one icon on the desktop, labelled 'drives' and handler script is /usr/local/bin/drive_drives
  193. if [ "$ONEDRVNAME" = "drives" ];then
  194. if [ "`pidof gtkdialog_pmount`" != "" ];then #if pmount running, kill it.
  195. killall gtkdialog_pmount 2>/dev/null
  196. sleep 0.1
  197. fi
  198. exec pmount $DRV_CATEGORY
  199. fi
  200. #decision... we can bring up a "handler" window, that explains things, good for beginners.
  201. #or jump direct to most appropriate target application, or do nothing.
  202. #for now only have AUTOTARGET=true go direct to most appropriate target app,
  203. # =false always start Pmount.
  204. if [ "$AUTOTARGET" = "false" ];then
  205. if [ "`pidof gtkdialog_pmount`" != "" ];then #if pmount running, kill it.
  206. killall gtkdialog_pmount 2>/dev/null
  207. sleep 0.1
  208. fi
  209. exec pmount $DRV_CATEGORY
  210. fi
  211. #oh, now have to analyse the media, decide what app to run...
  212. dPATTERN='^/dev/'"$ONEDRVNAME"' '
  213. FLAGOPTICAL='no'
  214. case $ONEDRVNAME in #110126
  215. sr*)
  216. FLAGOPTICAL='yes'
  217. ;;
  218. hd*)
  219. [ -e /proc/ide/$ONEDRVNAME/media ] && [ "`cat /proc/ide/$ONEDRVNAME/media`" = "cdrom" ] && FLAGOPTICAL='yes'
  220. ;;
  221. esac
  222. if [ "$FLAGOPTICAL" = "yes" ];then
  223. if [ "`busybox mount | grep "$dPATTERN"`" = "" ];then
  224. cddetect -q -d/dev/${ONEDRVNAME}
  225. RETVAL=$?
  226. case $RETVAL in
  227. 2|3) #iso file: normal data cd.
  228. true
  229. ;;
  230. 1) #audio
  231. ln -snf /dev/${ONEDRVNAME} /dev/cdrom
  232. if [ -f /usr/local/bin/defaultcdplayer ];then #100118
  233. exec defaultcdplayer
  234. else
  235. exec defaultmediaplayer cdda://
  236. fi
  237. ;;
  238. 50) #mixed. we don't want to mount it but we'll see if it plays
  239. ln -snf /dev/${ONEDRVNAME} /dev/cdrom
  240. if [ -f /usr/local/bin/defaultcdplayer ];then #100118
  241. exec defaultcdplayer
  242. else
  243. exec defaultmediaplayer cdda://
  244. fi
  245. ;;
  246. 5|8) #video dvd #140712 5|8)
  247. ln -snf /dev/${ONEDRVNAME} /dev/dvd
  248. [ -f /usr/local/bin/defaultdvdplayer ] && exec defaultdvdplayer
  249. [ "`grep '^exec mplayer' /usr/local/bin/defaultmediaplayer`" != "" ] && exec defaultmediaplayer dvdnav:// #100719 #140712 ^exec
  250. exec defaultmediaplayer dvd://
  251. ;;
  252. 0) #failed to detect type.
  253. #cddetect does not work properly for dvd's, try something else...
  254. #100719 properly detect dvd video...
  255. if [ "`df | grep "$dPATTERN"`" = "" ];then #only test if not mounted.
  256. FLAGDVDVIDEO="`dvd+rw-mediainfo /dev/${ONEDRVNAME} | grep 'Mounted Media: .* DVD-ROM'`"
  257. if [ "$FLAGDVDVIDEO" = "" ];then
  258. #mount and look for 'video_ts' directory (dir should contain file video_ts.ifo)...
  259. mkdir -p /mnt/${ONEDRVNAME}
  260. mount -t iso9660 /dev/${ONEDRVNAME} /mnt/${ONEDRVNAME}
  261. if [ $? -eq 0 ];then
  262. [ -d /mnt/${ONEDRVNAME}/video_ts -o -d /mnt/${ONEDRVNAME}/VIDEO_TS ] && FLAGDVDVIDEO="yes"
  263. umount /mnt/${ONEDRVNAME}
  264. fi
  265. fi
  266. if [ "$FLAGDVDVIDEO" != "" ];then
  267. ln -snf /dev/${ONEDRVNAME} /dev/dvd
  268. [ -f /usr/local/bin/defaultdvdplayer ] && exec defaultdvdplayer
  269. [ "`grep '^exec mplayer' /usr/local/bin/defaultmediaplayer`" != "" ] && exec defaultmediaplayer dvdnav:// #140712 ^exec
  270. exec defaultmediaplayer dvd://
  271. fi
  272. fi
  273. ;;
  274. 255) #might be blank, double check it's there #140712
  275. DISK=`cddetect_quick -d/dev/${ONEDRVNAME}`
  276. if [ "$DISK" = "disc inserted" ];then
  277. BURNER="xmessage 'no burner?'"
  278. type pburn &>/dev/null && BURNER=pburn
  279. exec "$BURNER"
  280. fi
  281. ;;
  282. esac
  283. fi
  284. #FSTYPE="iso9660"
  285. #130128 probepart now detects udf, FSTYPE should already be set to that...
  286. [ "$FSTYPE" != "udf" ] && FSTYPE="iso9660" #would already have been iso9660 anyway.
  287. fi
  288. #if dropped down here, it is likely to be a data partition...
  289. #120516 pup_event_frontend_d may call here when a drive plugged in, but only passes one parameter (PARAM1). want FSTYPE also...
  290. # also, for AUTOTARGET=true in /etc/eventmanager will come here, and a plugged in drive may be more than one partition to automount...
  291. if [ "$FSTYPE" != "" ];then
  292. DOPARTS="${ONEDRVNAME}|${FSTYPE}" #120517
  293. else
  294. #ONEDRVNAME is probably a drive name, ex sda...
  295. pPTN="/dev/${ONEDRVNAME}" #no space on end!
  296. DOPARTS="`probepart -m | grep "$pPTN" | cut -f 1,2 -d '|' | cut -f 3 -d '/' | grep -E 'btrfs|ext2|ext3|ext4|f2fs|hfs|iso9660|jfs|minix|msdos|ntfs|reiser|udf|vfat|xfs' | tr '\n' ' '`" #ex: sda1|ext3 sda2|vfat sda3|ext3 130216 added f2fs # SFR: enabled jfs & hfs
  297. fi
  298. #if it is a mountable partition then mount and open with rox. If already mntd then open in rox...
  299. EXITFLAG=no
  300. for ONEDO in $DOPARTS
  301. do
  302. xONEDRVNAME="`echo -n "$ONEDO" | cut -f 1 -d '|'`"
  303. xFSTYPE="`echo -n "$ONEDO" | cut -f 2 -d '|'`"
  304. if [ "`df | grep "$dPATTERN"`" = "" ];then
  305. #not mounted...
  306. mkdir -p /mnt/$xONEDRVNAME
  307. case $xFSTYPE in
  308. ntfs)
  309. #'mount' is a script that takes care of mounting ntfs...
  310. mount -t ntfs /dev/$xONEDRVNAME /mnt/$xONEDRVNAME
  311. RETVAL1=$?
  312. ;;
  313. vfat)
  314. #w476 modify codepage and iocharset (note: this code also in /usr/sbin/pmount)...
  315. #note, could also set iocharset=iso8859-2 (default: codepage=437, iocharset=iso8859-1)
  316. # 27jul09 lookup /etc/codepage and LANG by Masaki Shinomiya <shino@pos.to>
  317. NLS_PARAM=""
  318. if [ -f /etc/codepage ]; then #100127...
  319. grep -q -i '850' /etc/codepage && [ "$(echo $LANG|cut -d'.' -f1)" != "en_US" ] && NLS_PARAM=",codepage=850"
  320. grep -q -i '852' /etc/codepage && NLS_PARAM=",codepage=852,iocharset=iso8859-2"
  321. fi
  322. echo $LANG | grep -q -i '\.utf' && NLS_PARAM="$NLS_PARAM,utf8"
  323. mount -t vfat -o shortname=mixed,quiet${NLS_PARAM} /dev/$xONEDRVNAME /mnt/$xONEDRVNAME #v411
  324. RETVAL1=$?
  325. ;;
  326. #100127 not certain about this (inserted by shinobar)... 100131 shinobar informs needed for cds created in windows...
  327. iso9660|udf) #130128 add udf.
  328. mount -t $xFSTYPE -o iocharset=utf8 /dev/$xONEDRVNAME /mnt/$xONEDRVNAME
  329. RETVAL1=$?
  330. ;;
  331. *)
  332. mount -t $xFSTYPE /dev/$xONEDRVNAME /mnt/$xONEDRVNAME
  333. RETVAL1=$?
  334. ;;
  335. esac
  336. if [ $RETVAL1 -eq 0 ];then
  337. rox -x /mnt/$xONEDRVNAME -d /mnt/$xONEDRVNAME &
  338. EXITFLAG=yes
  339. else
  340. #091223 why did it fail?
  341. dd if=/dev/$xONEDRVNAME of=/dev/null bs=512 count=1 > /dev/null 2>&1
  342. if [ $? -ne 0 ];then
  343. #/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
  344. case $xONEDRVNAME in
  345. mmcblk*)
  346. yONEDRV="`echo -n "$xONEDRVNAME" | cut -c 1-7`"
  347. ;;
  348. *)
  349. yONEDRV="`echo -n "$xONEDRVNAME" | cut -c 1-3`"
  350. ;;
  351. esac
  352. echo "$yONEDRV" > /tmp/pup_event_frontend_block_request
  353. fi
  354. fi
  355. else
  356. #mounted...
  357. sddPATTERN='/dev/'"$ONEDRVNAME"' '
  358. MNTPT="`df -a| grep -m1 "$dPATTERN" | tr -s ' ' | cut -f6 -d ' '`" #140702 shinobar
  359. if [ -h /mnt/home ];then
  360. [ "`readlink /mnt/home`" = "$MNTPT" ] && MNTPT="/mnt/home"
  361. fi
  362. #110720 should not open a layer inside aufs, ex: session saved entire partition, desktop drive icon is /initrd/pup_rw...
  363. case $MNTPT in
  364. /initrd/pup_*)
  365. MNTPT="/" #open rox at top of aufs layers.
  366. ;;
  367. esac
  368. rox -x $MNTPT -d $MNTPT &
  369. EXITFLAG=yes
  370. fi
  371. done
  372. [ "$EXITFLAG" = "yes" ] && exit
  373. #NOTE: if pmount already running, don't really want to kill then restart,
  374. # just want to bring the window up if it's minimised ...how?
  375. if [ "`pidof gtkdialog_pmount`" != "" ];then #if pmount running, kill it.
  376. killall gtkdialog_pmount 2>/dev/null
  377. sleep 0.1
  378. fi
  379. exec pmount ${DRV_CATEGORY}
  380. ###END###