Help - Zoeken - Gebruikers - Kalender
Volledige versie: NTFS rw mount script
MandrivaClub.NL > MCNLive > MCNLive: International
azenz
Hi guys,

I wrote a little script to facilitate easy ntfs rw mounting using fuse (it mounts on /mnt/ntfs). It's pasted below for comments, ideas, etc. It works from the command line, but the problem is that if I want to run it via the KDE menu it doesn't find the fuse module. With all other commands this can be solved by giving the full path (e.g. initrd/sbin/...), but that doesn't work for the "modprobe fuse" command. I wonder if anyone has an idea what could be done about that, because later I hope to make it executable via double-click from the desktop (and I could image that future MCNlive distributions may benefit from having a script like this one to make life easier for newbies).


-----------------------------
#!/bin/sh
success="1"
until [ $success -eq 0 ]
do
dialog --title "Mounting NTFS" --backtitle "Mount NTFS partition as writable" --inputbox "Partition name (e.g. hda5)" 8 55 2>/tmp/input
sel=$?
if [ $sel -eq 1 ]
then
exit 0
fi

p="/dev/"
location=`cat /tmp/input`
path=$p$location
echo "Checking..."
ls $path
pexist=$?
if [ $pexist -eq 0 ]
then
success="0"
else
success="1"
dialog --title "Error - Invalid Partition" --backtitle "Mount NTFS partition as writable" --msgbox "The partition $location does not exist, please try again." 8 50 2>/tmp/input3
fi
done

mkdir /mnt/ntfs
umount $path
modprobe fuse
mount -t ntfs-3g -o user,umask=0 $path /mnt/ntfs

result=$?
if [ $result -eq 0 ]
then
dialog --title "Mounting NTFS" --backtitle "Mount NTFS partition as writable" --msgbox "$location has been successfully mounted." 8 50 2>/tmp/input3
else
dialog --title "ERROR" --backtitle "Mount NTFS partition as writable" --msgbox "The partition $location could not be mounted." 8 50 2>/tmp/input3
fi

rm -f /tmp/input
rm -f /tmp/input3
kris
You need to run it as root. The script needs a sanity check if it is called as root.
A menu entry for KDE needs: kdesu ......

What kind of 'dialog' is it?
azenz
Dialog is simply a shell command, makes it kind of GUI (a very simple one). Yes it has to be run as root and I have a call-up script that does that, which is what would be placed on the menu or desktop:

echo "Please enter the root (administrator) password to mount an NTFS partition as writable:"
su -c /usr/bin/ntfs-rw

Could you replace su with kdesu?
kris
You need to 'start' the script with kdesu.
azenz
Yes, kdesu works.

The main issue is that scripts that are started from the desktop or the KDE menu panel do not "see" many apps such as mount or mkdir or cp, probably because they are not in the path. This can be solved by giving the full path, e.g. "/initrd/sbin/mount" instead of just "mount".

But I haven't been able to apply this to the command "modprobe fuse", because the command /initrd/sbin/modprobe fuse yields the error that fuse cannot be found, and then the ntfs drive file journal is messed up and one has to run chkdsk before trying again. I have done a search for "fuse" over the system but without success. Any ideas?
holy-shit
(azenz @ Mar 26 2007, 02:47 AM) [snapback]81646[/snapback]

But I haven't been able to apply this to the command "modprobe fuse", because the command /initrd/sbin/modprobe fuse yields the error that fuse cannot be found, and then the ntfs drive file journal is messed up and one has to run chkdsk before trying again. I have done a search for "fuse" over the system but without success. Any ideas?


Just look in the fuse packages:
$ rpm -ql fuse-2.5.3-3mdk
/etc/udev/rules.d/40-fuse.rules
/sbin/mount.fuse
/usr/bin/fusermount
/usr/share/doc/fuse-2.5.3
/usr/share/doc/fuse-2.5.3/AUTHORS
/usr/share/doc/fuse-2.5.3/COPYING.LIB
/usr/share/doc/fuse-2.5.3/ChangeLog
/usr/share/doc/fuse-2.5.3/Filesystems
/usr/share/doc/fuse-2.5.3/NEWS
/usr/share/doc/fuse-2.5.3/README
/usr/share/doc/fuse-2.5.3/README-2.4
/usr/share/doc/fuse-2.5.3/README.NFS

$ rpm -ql libfuse2-2.5.3-3mdk
/usr/lib/libfuse.so.2
/usr/lib/libfuse.so.2.5.3


And there is also a kernel module named /lib/modules/2.6.17-5mdv/kernel/fs/fuse/fuse.ko.gz

Nice script but just one little advise when writing scripts: don't create variables when you're only going to use them once, like the $p and $result variables.

azenz
The script uses the command mount -t ntfs-3g -o user,umask=0 $path /mnt/ntfs and if run from the desktop complains that it cannot find the module fuse. I looked at mount.fuse but it seems to work differently. And the kernel module, well, the issue is how I can do the mount if the module fuse isn't in the default path, and I am not sure how any of the files you listed can help with that, because I don't know that any of them is represents the module "fuse" as used by mount. I can add the full path to mount (/initrd/sbin/mount), but then one can't do the same with the options that mount uses such as ntfs-3g. That is the issue...

QUOTE
Nice script but just one little advise when writing scripts: don't create variables when you're only going to use them once, like the $p and $result variables.


Thanks for the tip, but how do I do without them in this particular script?
holy-shit
(azenz @ Mar 27 2007, 03:55 AM) [snapback]81752[/snapback]

The script uses the command mount -t ntfs-3g -o user,umask=0 $path /mnt/ntfs and if run from the desktop complains that it cannot find the module fuse. I looked at mount.fuse but it seems to work differently. And the kernel module, well, the issue is how I can do the mount if the module fuse isn't in the default path, and I am not sure how any of the files you listed can help with that, because I don't know that any of them is represents the module "fuse" as used by mount. I can add the full path to mount (/initrd/sbin/mount), but then one can't do the same with the options that mount uses such as ntfs-3g. That is the issue...


The module for fuse (fuse.ko.gz) is only needed once. It is loaded via the modprobe command which is in the /sbin directory. This directory is in root's path by default but not in the user's path.
The modprobe command doesn't use the path to find the module but uses the directory /lib/modules/$(uname -r)/kernel/fs/ to find it.

You can make your system load the module when it boots. Just put 'fuse' in the file /etc/modprobe.preload.
azenz
Thanks - I tried adding fuse to the modprobe.preload file. The issue is that I am running a live system and the change in that file doesn't survive even a remaster, the entry "fuse" is just not there anymore.

And I am not sure how to include the directory /lib/modules/$(uname -r)/kernel/fs/ path thing into my script. Would you mind helping again? Thanks smile.gif!
holy-shit
(azenz @ Mar 29 2007, 04:18 AM) [snapback]81938[/snapback]

And I am not sure how to include the directory /lib/modules/$(uname -r)/kernel/fs/ path thing into my script. Would you mind helping again? Thanks smile.gif!


Like I said, you can't. The modprobe command does that by default. You can't change it.
Check if the fuse module is there:
ls -l /lib/modules/$(uname -r)/kernel/fs/fuse

It should be because it's part of the kernel and your script doesn't have any problems when run from root. Your problem lies with getting a full root environment when running your script.

To make changes to Mandriva Live you can find info on this forum. I'm not familiar with it myself.

Refering to your variable question, change

result=$?
if [ $result -eq 0 ]


into

if [ $? -eq 0 ]


Since the return code is only used once there's no need to assign it to a variable.

The same goes for $p.
azenz
QUOTE
It should be because it's part of the kernel and your script doesn't have any problems when run from root. Your problem lies with getting a full root environment when running your script.


The problem is that the script runs fine from konsole if I first changed user to root, but it doesn't when run from the shortcut menu or desktop even though it is called up as a) running in a shell, and cool.gif running with root privileges. The issue is the path, and that can be dealt with for commands like modprobe or mount, but "modprobe fuse" doesn't work in that environment.

QUOTE
Check if the fuse module is there:
ls -l /lib/modules/$(uname -r)/kernel/fs/fuse


Yes, the fuse.ko.gz file is there. "Modprobe fuse" works fine but only when typed in a shell or when used in a script run in a shell by typing the script name and hitting return.


QUOTE
The same goes for $p.


Thanks, I hadn't thought about that.
holy-shit
(azenz @ Mar 30 2007, 01:44 AM) [snapback]82001[/snapback]

The problem is that the script runs fine from konsole if I first changed user to root, but it doesn't when run from the shortcut menu or desktop even though it is called up as a) running in a shell, and cool.gif running with root privileges. The issue is the path, and that can be dealt with for commands like modprobe or mount, but "modprobe fuse" doesn't work in that environment.


Again, the issue is not the path. The path variable is only used to find a command. So to run the modporbe command it is search for using the path variable. When found it is run by the shell. However, the modprobe command itself doesn't use the path variable to locate the module it has to load.

I suspect it has to do with the environment the script is run in. Like you said it works fine when you first become root and then run it. The difference here is that when you become root (for instance by using the command 'su -') then the root environment is set up. Every command you run uses that environment.

However it is also possible to run a command as root without setting up the root environment first. The command is run in the environment of the original user.

Open 2 consoles and run in one of them the command 'su -' and in the other the command 'su'. In both consoles you have become root. Now use the commands 'env' and 'set' to see the differences in the environments.
azenz
QUOTE
I suspect it has to do with the environment the script is run in. Like you said it works fine when you first become root and then run it. The difference here is that when you become root (for instance by using the command 'su -') then the root environment is set up. Every command you run uses that environment.


In one case, I open a shell, type su, and run the script. This works fine. In the other case, one script calls a second script called "ntfs-call" :

konsole -e ntfs-call

The script ntfs-call in turns opens the actual ntfs script as user root through this command:

su -c /usr/bin/ntfs

When activated like this, the script ntfs doesn't find the module fuse. I ran the two shells as you said and typed env. The results look pretty much the same to me, no visible difference. Hmm...I wonder if we are stuck here....

kris
Don't use 'su' in a script. You need to 'run' the script as root.
azenz
But then what is my option of starting the script not from a shell but from the KDE menu or a desktop link? ;).gif

The su command in the script does cause the entire script to run in root mode, e.g. mount commands work fine. Just the modprobe fuse can't find fuse.
holy-shit
Change the su command into:
su -c /usr/bin/ntfs -

The last minus sign tells su to run through root's profile. Without it you run the script as root but with the environment of your user account.

Better is to setup sudo so that your user account can run the script as root without password.
azenz
QUOTE
Better is to setup sudo so that your user account can run the script as root without password.


That sounds very convienent, how do you do that?
holy-shit
First make sure the sudo package is installed. 'which sudo' should give something.

Open a console and become root.
Run the command 'visudo' which starts vi and opens the sudoers file.
Go to the section '# User privilege specification' and add the following line (assuming your account name is 'azenz'):
azenz ALL - NOPASSWD: /usr/bin/ntfs

Save the file and test it by running the command 'sudo /usr/bin/ntfs' from your account.

Do you know how vi works?

azenz
IT WORKED! icon_idea.gif Thanks for persevering on this one with me.

QUOTE
Do you know how vi works?


I googled it, thanks for asking smile.gif

QUOTE
azenz ALL - NOPASSWD: /usr/bin/ntfs


When I insert that I get "sudoers file: syntax error, line 20".
holy-shit
(azenz @ Apr 3 2007, 05:23 AM) [snapback]82246[/snapback]

When I insert that I get "sudoers file: syntax error, line 20".

The minus sign after 'ALL' should be an equal sign.
azenz ALL = NOPASSWD: /usr/bin/ntfs

azenz
I now get no error message upon exiting visudo, but the entry doesn't work somehow, i.e. various error messages indicate that ntfs does not run with root privileges when started from a shell without typing su.
holy-shit
(azenz @ Apr 3 2007, 02:11 PM) [snapback]82266[/snapback]

I now get no error message upon exiting visudo, but the entry doesn't work somehow, i.e. various error messages indicate that ntfs does not run with root privileges when started from a shell without typing su.

Are you using the command 'sudo ntfs'?
azenz
That's it, works now. Thanks! smile.gif
Dit is een "Print" versie van onze forums. Om de volledige versie met meer informatie, afbeeldingen en opmaakte bekijken, a.u.b. klik hier.