umount 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #mount-FULL, umount-FULL, losetup-FULL are the full versions.
  11. #The Busybox versions of mount and umount are available but only by:
  12. # # busybox mount ...
  13. # # busybox umount ...
  14. #mount and umount are now scripts.
  15. #if an ntfs partition, puppy uses user-mode ntfs-3g driver.
  16. #the mount and umount scripts allow seamless mounting and unmounting of ntfs f.s.
  17. [ ! $1 ] && exec busybox umount
  18. #sync #paranoid precaution. v2.12 kernel call to modprobe, sync hangs.
  19. . /etc/rc.d/functions4puppy4 #v4.02
  20. #v406 add /dev/hd* support... v407 add fd*...
  21. MNTDDEVS1="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc
  22. #extract all the '-' options, on separate lines...
  23. #v2.12 pathetic bug, need space before $@, see comment /bin/mount... No, use $*...
  24. DASHOPTS="`echo "$*" | tr '\t' ' ' | tr ' ' '\n' | grep '^\-'`"
  25. #v2.12 curses, busybox mount accepts the '-n' option (just ignores it) but umount does not...
  26. if [ ! "`echo " $DASHOPTS" | grep 'n'`" = "" ];then
  27. FULLCMD="`echo -n " $*" | sed -e 's/ \-n / /g'`" #unsatisfactory!
  28. else
  29. FULLCMD="$*"
  30. fi
  31. # -a param means unmount everything...
  32. #if [ ! "`echo "$@" | grep --extended-regexp '\W-a$|\W-a\W'`" = "" ];then
  33. if [ ! "`echo " $DASHOPTS" | grep 'a'`" = "" ];then
  34. MNTFUSE="`busybox mount | grep 'fuse' | head -n 1 | cut -f 3 -d ' '`"
  35. [ ! "$MNTFUSE" = "" ] && fusermount -u $MNTFUSE
  36. busybox umount -d $FULLCMD #v423 -d fix.
  37. exit $?
  38. fi
  39. #remove everything except the device or mntpt...
  40. LASTPARAM="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/'`"
  41. #thanks to jesse for this...
  42. #for ntfs-3g, find the device and mount point...
  43. #note, '\' essential to prevent 'ps' from reporting this invocation line...
  44. NTFSMNTPT="`ps -e | grep -o 'ntfs\-3g.*' | grep "$LASTPARAM" | tr '\t' ' ' | tr -s ' ' | tr ' ' "\n" | grep '^/mnt/'`"
  45. if [ "$NTFSMNTPT" = "" ];then
  46. busybox umount -d $FULLCMD #v423 -d fix to free loop device.
  47. RETVAL=$?
  48. else
  49. #fusermount can only unmount by giving the mount-point...
  50. fusermount -u $NTFSMNTPT
  51. RETVAL=$?
  52. fi
  53. #v403 if there is a desktop icon (see pup_eventd), then refresh it...
  54. if [ $RETVAL -eq 0 ];then
  55. #find which device has been unmounted... v406 add hd*... v407 add fd*...
  56. MNTDDEVS2="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc
  57. if [ "$MNTDDEVS2" = "" ];then
  58. DEVNAME="`echo "$MNTDDEVS1" | head -n 1`" #head is a precaution.
  59. else
  60. DEVNAME="`echo "${MNTDDEVS1}
  61. ${MNTDDEVS2}" | sort | uniq -u | head -n 1`" #head is a precaution.
  62. fi
  63. if [ "$DEVNAME" != "" -a "$DISPLAY" != "" ];then
  64. DRVNAME="`echo -n "$DEVNAME" | cut -b 1-3`" #ex: DRVNAME=sda
  65. #special case, SD card /dev/mmcblk0p1...
  66. [ "$DRVNAME" = "mmc" ] && DRVNAME="`echo -n "$DEVNAME" | sed -e 's/p[0-9]$//'`"
  67. xDRVNAME="$DRVNAME" #v404
  68. [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition. ex: DRVNAME=sda1
  69. if [ -d /root/.pup_event/drive_${DRVNAME} ];then
  70. case $DRVNAME in #v407
  71. fd*)
  72. DRV_CATEGORY="floppy"
  73. ;;
  74. *)
  75. dnPATTERN='/dev/'"${xDRVNAME}"'|'
  76. DRV_CATEGORY="`probedisk | grep "$dnPATTERN" | cut -f 2 -d '|'`"
  77. ;;
  78. esac
  79. #if any partitions still mounted, leave "MNTD" on icon...
  80. drvPATTERN='^/dev/'"${DRVNAME}" #important, no space on end.
  81. #120104 a bug if more than 9 partitions in drive, ex DRVNAME=sda1 xDRVNAME=sda, df test below must ignore sda10 etc...
  82. [ "$DRVNAME" != "$xDRVNAME" ] && drvPATTERN='^/dev/'"${DRVNAME}"' ' #120104 we do want a space on end.
  83. #"MNTD" text is intended as a reminder to user that drive needs to be unmounted,
  84. # but partitions in use by Puppy cannot be unmounted.
  85. if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
  86. if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -E ' /initrd/| /$'`" != "" ];then
  87. #only a partition left mntd that is in use by puppy, change green->yellow...
  88. icon_mounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
  89. else
  90. #redraw icon without "MNTD" text...
  91. icon_unmounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
  92. fi
  93. fi
  94. fi
  95. fi
  96. fi
  97. #v409 now have /etc/mtab a symlink to /proc/mounts so this section not needed...
  98. ##v2.10 do not update /etc/mtab if '-n' option...
  99. #[ ! "`echo " $DASHOPTS" | grep 'n'`" = "" ] && exit $RETVAL
  100. ##busybox does not support /etc/mtab, but some apps (ex: eject
  101. ##and the full mount,umount) need it...
  102. #[ $RETVAL -eq 0 ] && busybox mount | sed -e 's/ on / /g' | sed -e 's/ type / /g' | sed -e 's/ (/ /g' | sed -e 's/)$/ 0 0/g' > /etc/mtab
  103. ##note, it is a long story here. Busybox can be configured to
  104. ##support /etc/mtab, however the information it writes to mtab
  105. ##is incomplete, different, and breaks my scripts.
  106. #v409, instead just make sure the symlink stays there...
  107. if [ ! -L /etc/mtab ];then
  108. rm -f /etc/mtab
  109. ln -s /proc/mounts /etc/mtab
  110. fi
  111. exit $RETVAL