umount 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. #Barry Kauler 2006 www.puppylinux.com
  3. #2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
  4. #v406 support for old kernel /dev/hd*.
  5. #v407 fix for floppy drive icon.
  6. #v409 gparted create ext3 part. failed, fixed by making /etc/mtab a symlink.
  7. #v423 recent version busybox umount, default for umount is to not free loop device, needs -d.
  8. #120104 a bug if more than 9 partitions in drive, desktop icons may display incorrect mounted status.
  9. #130203 change probedisk2 to probedisk.
  10. [ ! $1 ] && exec busybox umount
  11. #sync #paranoid precaution. v2.12 kernel call to modprobe, sync hangs.
  12. . /etc/rc.d/functions4puppy4 #v4.02
  13. #v406 add /dev/hd* support... v407 add fd*...
  14. MNTDDEVS1="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc
  15. #extract all the '-' options, on separate lines...
  16. #v2.12 pathetic bug, need space before $@, see comment /bin/mount... No, use $*...
  17. DASHOPTS="`echo "$*" | tr '\t' ' ' | tr ' ' '\n' | grep '^\-'`"
  18. #v2.12 curses, busybox mount accepts the '-n' option (just ignores it) but umount does not...
  19. if [ ! "`echo " $DASHOPTS" | grep 'n'`" = "" ];then
  20. FULLCMD="`echo -n " $*" | sed -e 's/ \-n / /g'`" #unsatisfactory!
  21. else
  22. FULLCMD="$*"
  23. fi
  24. # -a param means unmount everything...
  25. #if [ ! "`echo "$@" | grep --extended-regexp '\W-a$|\W-a\W'`" = "" ];then
  26. if [ ! "`echo " $DASHOPTS" | grep 'a'`" = "" ];then
  27. MNTFUSE="`busybox mount | grep 'fuse' | head -n 1 | cut -f 3 -d ' '`"
  28. [ ! "$MNTFUSE" = "" ] && fusermount -u $MNTFUSE
  29. busybox umount -d $FULLCMD #v423 -d fix.
  30. exit $?
  31. fi
  32. #remove everything except the device or mntpt...
  33. LASTPARAM="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/'`"
  34. #thanks to jesse for this...
  35. #for ntfs-3g, find the device and mount point...
  36. #note, '\' essential to prevent 'ps' from reporting this invocation line...
  37. NTFSMNTPT="`ps -e | grep -o 'ntfs\-3g.*' | grep "$LASTPARAM" | tr '\t' ' ' | tr -s ' ' | tr ' ' "\n" | grep '^/mnt/'`"
  38. if [ "$NTFSMNTPT" = "" ];then
  39. busybox umount -d $FULLCMD #v423 -d fix to free loop device.
  40. RETVAL=$?
  41. else
  42. #fusermount can only unmount by giving the mount-point...
  43. fusermount -u $NTFSMNTPT
  44. RETVAL=$?
  45. fi
  46. #v403 if there is a desktop icon (see pup_eventd), then refresh it...
  47. if [ $RETVAL -eq 0 ];then
  48. #find which device has been unmounted... v406 add hd*... v407 add fd*...
  49. MNTDDEVS2="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc
  50. if [ "$MNTDDEVS2" = "" ];then
  51. DEVNAME="`echo "$MNTDDEVS1" | head -n 1`" #head is a precaution.
  52. else
  53. DEVNAME="`echo "${MNTDDEVS1}
  54. ${MNTDDEVS2}" | sort | uniq -u | head -n 1`" #head is a precaution.
  55. fi
  56. if [ "$DEVNAME" != "" -a "$DISPLAY" != "" ];then
  57. DRVNAME="`echo -n "$DEVNAME" | cut -b 1-3`" #ex: DRVNAME=sda
  58. #special case, SD card /dev/mmcblk0p1...
  59. [ "$DRVNAME" = "mmc" ] && DRVNAME="`echo -n "$DEVNAME" | sed -e 's/p[0-9]$//'`"
  60. xDRVNAME="$DRVNAME" #v404
  61. [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition. ex: DRVNAME=sda1
  62. if [ -d /root/.pup_event/drive_${DRVNAME} ];then
  63. case $DRVNAME in #v407
  64. fd*)
  65. DRV_CATEGORY="floppy"
  66. ;;
  67. *)
  68. dnPATTERN='/dev/'"${xDRVNAME}"'|'
  69. DRV_CATEGORY="`probedisk | grep "$dnPATTERN" | cut -f 2 -d '|'`"
  70. ;;
  71. esac
  72. #if any partitions still mounted, leave "MNTD" on icon...
  73. drvPATTERN='^/dev/'"${DRVNAME}" #important, no space on end.
  74. #120104 a bug if more than 9 partitions in drive, ex DRVNAME=sda1 xDRVNAME=sda, df test below must ignore sda10 etc...
  75. [ "$DRVNAME" != "$xDRVNAME" ] && drvPATTERN='^/dev/'"${DRVNAME}"' ' #120104 we do want a space on end.
  76. #"MNTD" text is intended as a reminder to user that drive needs to be unmounted,
  77. # but partitions in use by Puppy cannot be unmounted.
  78. if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
  79. if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -E ' /initrd/| /$'`" != "" ];then
  80. #only a partition left mntd that is in use by puppy, change green->yellow...
  81. icon_mounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
  82. else
  83. #redraw icon without "MNTD" text...
  84. icon_unmounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
  85. fi
  86. fi
  87. fi
  88. fi
  89. fi
  90. #v409, instead just make sure the symlink stays there...
  91. if [ ! -L /etc/mtab ];then
  92. rm -f /etc/mtab
  93. ln -s /proc/mounts /etc/mtab
  94. fi
  95. exit $RETVAL