filemnt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. ############## ROX app.: Click image files to mount & unmount. by: Terry Becker aka: SunBurnt
  3. ############## File types = ".2fs, .3fs, .sfs, .iso, & initrd.gz".
  4. #v423 detect wrong squashfs version 3.x or 4.x.
  5. #v424 advise of sfs-version-converter.
  6. #100606 have backported squashfs4 to kernel 2.6.27.47.
  7. #100831 fix KERNELSUBVER for olpc people, have 'uname -r' returning version with '_'.
  8. #101221 yaf-splash fix.
  9. #111016 fix for 3.x kernel.
  10. #111110 handle encrypted save-files.
  11. #111112 small fixes.
  12. #120201 rodin.s: internationalization.
  13. #120220 npierce: improved test and message for already mounted file, also prevent mkdir if file not found, and fix adding pwd prefix
  14. #120220 npierce: add slash to pwd prefix; exit on failed stat. refer: http://murga-linux.com/puppy/viewtopic.php?t=71767&start=450
  15. #120323 replace 'xmessage' with 'pupmessage'.
  16. #120525 take into account shinobar's sfs load-on-the-fly pet.
  17. #121105 added a translation.
  18. #121203 remove directory if mount goes wrong. thanks MHHP: http://murga-linux.com/puppy/viewtopic.php?t=82654
  19. #130204 mavrothal: fix spaces in imgFile.
  20. #130223 BK: fix wonky logic.
  21. export TEXTDOMAIN=filemnt
  22. export OUTPUT_CHARSET=UTF-8
  23. . gettext.sh
  24. #v423...
  25. KERNELVER="`uname -r`"
  26. if vercmp $KERNELVER ge 3.0;then #111016
  27. SFSSTR='squashfs, version 4'
  28. SFSMAJOR=4
  29. else
  30. KERNELSUBVER=`echo -n "$KERNELVER" | cut -f 3 -d '.' | cut -f 1 -d '-' | cut -f 1 -d '_'` #100831
  31. KERNELSUBSUBVER=`echo -n "$KERNELVER" | cut -f 4 -d '.' | cut -f 1 -d '-'`
  32. SFSSTR='squashfs, version 3'
  33. SFSMAJOR=3
  34. if [ $KERNELSUBVER -eq 27 ];then
  35. if [ $KERNELSUBSUBVER -gt 46 ];then #100606
  36. SFSSTR='squashfs, version 4'
  37. SFSMAJOR=4
  38. fi
  39. fi
  40. if [ $KERNELSUBVER -gt 28 ];then
  41. SFSSTR='squashfs, version 4'
  42. SFSMAJOR=4
  43. fi
  44. fi
  45. imgFile="$1"
  46. if [ ! -f "$imgFile" ];then echo "No regular file: $imgFile";exit 1;fi #120220
  47. if [ "`dirname $imgFile`" = '.' ] ;then #120220
  48. imgFile="`pwd`/`echo $imgFile |sed 's/^\.//'`" #120220
  49. fi
  50. #120525 may need to replace with /mnt/home...
  51. HOMELINK="`readlink /mnt/home | grep 'initrd'`" #ex: /initrd/mnt/dev_save
  52. if [ "$HOMELINK" ];then
  53. hPTN="s%${HOMELINK}%/mnt/home%"
  54. imgFile="`echo -n "$imgFile" | sed -e "$hPTN"`"
  55. #ex: ex: /initrd/mnt/dev_save/kernel_src-3.0.7-patched.sfs becomes /mnt/home/kernel_src-3.0.7-patched.sfs becomes
  56. fi
  57. #ex: /mnt/home/kernel_src-3.0.7-patched.sfs becomes /mnt/+mnt+home+kernel_src-3.0.7-patched.sfs
  58. MntPt='/mnt/'"`echo "$imgFile" |sed "s#^\.##g" |sed "s#/#+#g" | tr ' ' '_'`" # SFR, added 'tr'
  59. #130223 BK is it already mounted?
  60. MNTEDLOOP="$(cat /proc/mounts | grep "$MntPt" | cut -f 1 -d ' ')"
  61. imgFileBASE="`basename "$imgFile"`" #BK
  62. imgFileDEV_INODE="`stat -L -c "%d_%i" "$imgFile"`" || exit #120220
  63. if [ "$MNTEDLOOP" = "" ];then #130223 BK
  64. #not mounted on $MntPt.
  65. #BK check if already mounted elsewhere...
  66. for ONELOOP in `mount | grep '^/dev/loop' | cut -f 1 -d ' ' | tr '\n' ' '`
  67. do
  68. MNTDIMG_MNT_PT="`mount | grep "^$ONELOOP" | cut -f 3 -d ' '`" #120220...
  69. MNTDIMG=""
  70. [ "`echo $MNTDIMG_MNT_PT | cut -f 2 -d /`" = "initrd" ] && MNTDIMG="/initrd"
  71. MNTDIMG="${MNTDIMG}`losetup-FULL $ONELOOP | cut -f 2 -d '(' | cut -f 1 -d ')'`"
  72. MNTDIMG_DEV_INODE="`stat -L -c "%d_%i" "$MNTDIMG"`" #120220
  73. #not perfect, but paths may be symlinks...
  74. if [ "$MNTDIMG_DEV_INODE" = "$imgFileDEV_INODE" ];then #120220...
  75. pupmessage -bg '#FF8080' "$(eval_gettext "Sorry \$imgFile is already mounted and in-use by Puppy
  76. on \$MNTDIMG_MNT_PT from \$MNTDIMG")" #120220 121105 add gettext.
  77. exit
  78. fi
  79. done
  80. Ext=`echo "$imgFile" |sed 's/^.*\.//'` # get file type from extention
  81. Ext="`echo $Ext | tr [:upper:] [:lower:]`" # SFR: extension to lowercase (If Bash >= 4 it could be just Ext=${Ext,,} )
  82. if [ "$Ext" = '2fs' ] ;then
  83. Type='ext2'
  84. elif [ "$Ext" = '3fs' ] ;then
  85. Type='ext3'
  86. elif [ "$Ext" = '4fs' ] ;then #v423 planning for the future!
  87. Type='ext4'
  88. elif [ "$Ext" = 'sfs' ] ;then
  89. Type='squashfs'
  90. elif [ "$Ext" = 'iso' ] ;then
  91. Type='iso9660'
  92. fi
  93. #v423 detect wrong squashfs version...
  94. if [ "$Type" = "squashfs" ];then
  95. if [ "`disktype "${imgFile}" | grep "$SFSSTR"`" = "" ];then
  96. if [ $SFSMAJOR -eq 4 ];then #100606
  97. pupmessage -center -bg '#FFC0C0' "`gettext \"NOTICE: This is an older version 3.x squashfs file, not usable.
  98. All Linux kernels 2.6.29 or later require version 4.x squashfs files
  99. (note, some 2.6.27.47+ kernels have Squashfs4 backported to them).
  100. Note, there is an SFS-version-converter in the Utility menu, run that first.\"`"
  101. else
  102. pupmessage -center -bg '#FFC0C0' "`gettext \"NOTICE: This is a newer version 4.x squashfs file, not usable.
  103. All Linux kernels 2.6.28 or earlier require version 3.x squashfs files
  104. (note, an exception is some 2.6.27.47+ kernels that have Squashfs4 backported).
  105. Note, there is an SFS-version-converter in the Utility menu, run that first.\"`"
  106. fi
  107. exit
  108. fi
  109. #120525 take into account shinobar's load-on-the-fly pet...
  110. if [ "`which sfs_load`" != "" ];then
  111. SFSMSG1a="$(eval_gettext "You can choose to mount the \${imgFileBASE} file to view it's contents (read-only), or you can install it.")" #'geany
  112. SFSMSG1b="${SFSMSG1a}\n\n$(gettext 'Regarding the latter, traditionally SFS files are selected by the \ZbBootManager\ZB for loading at bootup (see System menu), which you can do, or you can choose to install (load) it now (which will not require a reboot).\n\nNote1: If you want to edit the contents of an SFS file, you have to use the commandline utilities \Zbunsquashfs\ZB and \Zbmksquashfs\ZB.\nNote2: In the case of a full HD installation, without a layered filesystem, installation is irreversible. For all other installation modes, the BootManager can be used to unload an installed (loaded) SFS file.')"
  113. pupdialog --colors --background '#FFFF80' --backtitle "${imgFileBASE}" --extra-button --yes-label "$(gettext 'View contents')" --no-label "$(gettext 'Install SFS')" --extra-label "$(gettext 'Cancel')" --yesno "${SFSMSG1b}" 0 0
  114. RETVAL=$?
  115. [ $RETVAL -eq 1 ] && exec sfs_load "$1"
  116. [ $RETVAL -ne 0 ] && exit #120527
  117. fi
  118. fi
  119. echo -e "\n ### Mount: $imgFile\n"
  120. mkdir -p "$MntPt"
  121. #111110 handle encrypted save-files...
  122. CRYPTO=""; Err=0
  123. DEVLOOP="`losetup-FULL -f`" #next free loop device.
  124. case $Type in
  125. ext2|ext3|ext4)
  126. case $imgFileBASE in
  127. *_cryptx*) #light encryption.
  128. CRYPTO='light'
  129. modprobe cryptoloop
  130. rxvt -geometry 25x3 -title "$(gettext 'Enter password')" -bg orange -e losetup-FULL -E 1 $DEVLOOP $imgFile
  131. #losetup-FULL -E 1 $DEVLOOP $imgFile
  132. Err=$?
  133. ;;
  134. *_crypt*) #heavy encryption.
  135. CRYPTO='heavy'
  136. modprobe cryptoloop
  137. modprobe aes_generic 2>/dev/null #v407 aes name change.
  138. modprobe aes 2>/dev/null #for older kernel <2.6.25
  139. modprobe crypto_blkcipher 2>/dev/null #v407 blkcipher name change.
  140. modprobe blkcipher 2>/dev/null #old kernel.
  141. modprobe cbc
  142. MYPASS="`pupdialog --title "$(gettext 'Password required')" --stdout --inputbox "$(gettext 'Password required to open') ${imgFileBASE}:" 0 0`"
  143. Err=$?
  144. if [ $Err -eq 0 ];then
  145. echo "$MYPASS" | losetup-FULL -p 0 -e aes $DEVLOOP $imgFile
  146. Err=$?
  147. fi
  148. ;;
  149. esac
  150. ;;
  151. esac
  152. if [ "$CRYPTO" ];then
  153. [ $Err -eq 0 ] && mount -t $Type $DEVLOOP "$MntPt"
  154. Err=$?
  155. else #normal, no encryption... 130204 mavrothal: fix spaces in imgFile...
  156. mount -t $Type -o loop "$imgFile" "$MntPt"
  157. Err=$?
  158. fi
  159. if [ $Err -eq 0 ] ;then
  160. #130223 BK: got a empty window, use -x option to rescan files...
  161. rox -x "$MntPt" -d "$MntPt" # mount good, run rox filer
  162. /usr/lib/gtkdialog/box_splash -timeout 6 -bg green -text "`eval_gettext \"SUCCESS! Click \\\$imgFileBASE icon again to unmount it\"`" & #BK
  163. else
  164. rmdir "$MntPt" #121203
  165. fi
  166. else ### file's mounted, unmount it
  167. rox -D "$MntPt" #BK
  168. sync #111112
  169. umount "$MntPt"
  170. Err=$?
  171. /usr/lib/gtkdialog/box_splash -timeout 3 -text "$(gettext 'Unmounting') $imgFileBASE" #BK
  172. echo -e "\n ### UnMount: $imgFile\n"
  173. sync #111112
  174. rmdir "$MntPt"
  175. fi
  176. if [ $Err -ne 0 ] ;then # ERROR: mount/unmount
  177. #xmessage -title " ERROR..." " Failed mounting or unmounting. "
  178. pupdialog --background red --title "$(gettext 'ERROR')" --msgbox "$(gettext 'Failed mounting or unmounting')" 0 0
  179. fi
  180. ###END###