Recent

Author Topic: [SOLVED] Linux: how to mount/unmount a partition?  (Read 5097 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
[SOLVED] Linux: how to mount/unmount a partition?
« on: December 06, 2018, 01:46:26 pm »
In a console program I want to mount and later unmount a Windows-partition. This Windows-partition is prepared in /etc/fstab via:

Code: [Select]
LABEL=Win7 /media/C ntfs defaults,users,noauto,windows_names 0 0
Are there free pascal commands or functions to mount/unmount such a partition? Or must I call the Linux commands "mount/umount" via RunCommand or TProcess.Execute?

I'm using FPC 3.0.4 on Ubuntu 18.04. Thanks in advance.
« Last Edit: December 08, 2018, 01:55:25 pm by Hartmut »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to mount/unmount a partition?
« Reply #1 on: December 06, 2018, 02:11:06 pm »
You could always examine the source of mount to see how it does it :) Just joking (although it's a good idea): in your case I would use a TProcess to call  mount/unmount rathr than replicate them. (un)Mounting volumes is not an speciallly delicate operation but it's best to leave it to the "professionals"

ETA: In case you still want to do it in code, investigate the kernel calls mount() and umount(): use man 2 mount and man 2 umount for starters. I couldn't find them in Free Pascal RTL, though.
« Last Edit: December 06, 2018, 02:36:37 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to mount/unmount a partition?
« Reply #2 on: December 06, 2018, 04:12:31 pm »
If I understand lucamar correctly, then there is no free pascal command or function to mount/unmount a partition. Unmounting via RunCommand() is not difficult:

Code: Pascal  [Select][+][-]
  1. function unmount(folder: ansistring): boolean;
  2.    var s: ansistring;
  3.        ok: boolean;
  4.    begin
  5.    ok:=RunCommand('/bin/bash', ['-c', 'umount ' + folder], s);
  6.    exit(ok);
  7.    end;  
   
But for mounting a partition I need sudo and it's password, but I don't want the need to enter the password all the time.

Is it possible, to configure my /etc/fstab (see above) in another way, so that no password is neccessary for mounting? I thought that parameter "users" has that meaning, but in my case (Ubuntu 18.04) it does not work (mounting requests sudo and password). I'm a beginner to Linux. Any ideas?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to mount/unmount a partition?
« Reply #3 on: December 06, 2018, 06:47:11 pm »
But for mounting a partition I need sudo and it's password, but I don't want the need to enter the password all the time.

You can use use sudo --stdin to be able to pass the password on the (redirected) standard input. For example on a bash script it could be something like:
Code: Bash  [Select][+][-]
  1. echo "my password" | sudo --stdin mount [...etc...]

the same but executed directly from Pascal code:

Code: Pascal  [Select][+][-]
  1. function DoMount: Boolean;
  2. const
  3.   command  = '/bin/bash -c echo "my password" | sudo --stdin mount whatever';
  4. var
  5.   outstr: String;
  6. begin
  7.   Result := RunCommand(command, outstr)
  8. end;
  9.  

But to fully realize all the posibilities you should really use TProcess. A little more work but a lot more flexibility. Some more info in the page Executing External Programs of the wiki.

Oh, BTW:

Quote
[..] I thought that parameter "users" has that meaning [...]

The parameter is "user", in singular, not "users" (plural). From man fstab:

Quote
Common for all types of file system are the options [...]``user'' (allow a user to  mount) [...]
« Last Edit: December 06, 2018, 06:58:28 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to mount/unmount a partition?
« Reply #4 on: December 06, 2018, 08:23:37 pm »
Thank you very much, lucamar, for your help and that idea. It works after I modified it to:

Code: Pascal  [Select][+][-]
  1. function DoMount: Boolean;
  2. const
  3.   command = '/bin/bash';
  4.   params  = 'echo "my password" | sudo --stdin mount /media/C/';
  5. var
  6.   outstr: String;
  7. begin
  8.   Result := RunCommand(command, ['-c', params], outstr);
  9. end;  

But in this case I must compile my current Linux password in my programs. I would be not very happy to do this. I would prefer a solution by changing my /etc/fstab, so that I don't need a password for mounting, if somehow possible.

I changed "users" into "user" in /etc/fstab (and rebooted Linux), but I still need sudo to mount a partition.

"man mount" says: "Only the user that mounted a filesystem can unmount it again. If any user should be able to unmount it, then use users instead of user in the fstab line".
And "users" does appear in https://help.ubuntu.com/community/Fstab.

So from my knowledge both options exist: "user" and "users". But both do not have the expected result on my system :-(

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to mount/unmount a partition?
« Reply #5 on: December 06, 2018, 09:23:19 pm »
Thank you very much, lucamar, for your help and that idea. It works after I modified it to:
Code: Pascal  [Select][+][-]
  1. [... correct code ...]

Yeah, sorry: I copied the wrong line from a diff

Quote
But in this case I must compile my current Linux password in my programs

There are lots of other options: getting it from the command line, from a (temp) file, etc.

Quote
I changed "users" into "user" in /etc/fstab (and rebooted Linux), but I still need sudo to mount a partition.

"man mount" says: "Only the user that mounted a filesystem can unmount it again. If any user should be able to unmount it, then use users instead of user in the fstab line".
And "users" does appear in https://help.ubuntu.com/community/Fstab.

So from my knowledge both options exist: "user" and "users". But both do not have the expected result on my system :-(

Oh, I see. Sorry again: I'm on an Ubuntu 11 box which doesn't seem to know about "users" :)

In theory, adding "user" (or "users") to fstab should be enough but you may be finding some kind of interaction with other security features of the system ... for example, that mount  itself can't be run by normal users or something like that.

ETA

I've been investigating a little and finally found this by a simple try:
Code: Bash  [Select][+][-]
  1. lucamar@selene:~$ uname -srvmo
  2. Linux 2.6.38-16-generic #67-Ubuntu SMP Thu Sep 6 18:00:43 UTC 2012 i686 GNU/Linux
  3. lucamar@selene:~$ mount /media/Backs/
  4. Unprivileged user can not mount NTFS block devices using the external FUSE
  5. library. Either mount the volume as root, or rebuild NTFS-3G with integrated
  6. FUSE support and make it setuid root. Please see more information at
  7. http://ntfs-3g.org/support.html#unprivileged
  8.  

It seems mount needs to be sudoed to mount ntfs volumes.
« Last Edit: December 06, 2018, 09:45:04 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to mount/unmount a partition?
« Reply #6 on: December 06, 2018, 10:21:11 pm »
It seems mount needs to be sudoed to mount ntfs volumes.

Thanks for that information. Must stop for the night now. Will continue tomorrow.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to mount/unmount a partition?
« Reply #8 on: December 07, 2018, 08:21:07 pm »
It seems mount needs to be sudoed to mount ntfs volumes.

I checked it and you are right: for a NTFS volume I need sudo, for a VFAT partition i don't need sudo (both tried with "users" and "user" options in /etc/fstab).

does that maybe help?
https://unix.stackexchange.com/questions/96625/how-to-allow-non-superusers-to-mount-any-filesystem
or maybe this: https://askubuntu.com/questions/299294/how-do-i-give-root-privileges-to-an-app

I read both articles, but for me as a beginner to Linux a lot of stuff was to hard to understand and/or to dangerous to try it. Anyway thank you for trying to help me.

If I find no better solution, I will compile my Linux password into my programs, as lucamar suggested.

But: in Ubuntu filemanagers like Nautilus (Gnome) and Dolphin (KDE-Plasma) I can mount NTFS volumes with 1 mouseclick without entering a password. How can they do it without sudo? Does anybody know that?

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Linux: how to mount/unmount a partition?
« Reply #9 on: December 07, 2018, 09:49:09 pm »
I checked it and you are right: for a NTFS volume I need sudo, for a VFAT partition i don't need sudo (both tried with "users" and "user" options in /etc/fstab).
This is not completely true. I can mount a NTFS partition with any file manager without root/sudo.
What the first links is saying that you can mount a partition into media by not using mount directly but with for example udisks which is a daemon for mounting partitions into the media folder. Type udisksctrl into a terminal to see if that is available. If so you can use it to mount your partition: https://wiki.archlinux.org/index.php/udisks
It's pretty straightforward if you know mount (as a normal user try this):
Code: Bash  [Select][+][-]
  1. ↪ lsblk
  2. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
  3. sda      8:0    0 238,5G  0 disk
  4. ├─sda1   8:1    0    89M  0 part
  5. ├─sda2   8:2    0  76,1G  0 part
  6. ├─sda3   8:3    0    28G  0 part /
  7. ├─sda4   8:4    0     1K  0 part
  8. ├─sda5   8:5    0 130,4G  0 part /home
  9. └─sda6   8:6    0     4G  0 part [SWAP]
  10. sr0     11:0    1  1024M  0 rom  
  11.  
  12. ↪ udisksctl mount -b /dev/sda2
  13. Mounted /dev/sda2 at /run/media/johannes/D2F0B901F0B8ECBB.
  14.  
  15. ↪ lsblk
  16. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
  17. sda      8:0    0 238,5G  0 disk
  18. ├─sda1   8:1    0    89M  0 part
  19. ├─sda2   8:2    0  76,1G  0 part /run/media/johannes/D2F0B901F0B8ECBB
  20. ├─sda3   8:3    0    28G  0 part /
  21. ├─sda4   8:4    0     1K  0 part
  22. ├─sda5   8:5    0 130,4G  0 part /home
  23. └─sda6   8:6    0     4G  0 part [SWAP]
  24. sr0     11:0    1  1024M  0 rom  
  25.  
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to mount/unmount a partition?
« Reply #10 on: December 08, 2018, 12:06:32 am »
What the first links is saying that you can mount a partition into media by not using mount directly but with for example udisks which is a daemon for mounting partitions into the media folder. Type udisksctrl into a terminal [. . .]

Well! I didn't know that, lots of thanks! This system (Ubuntu 11) is probably too old but it has the (I suppose) equivalent command udisks, so I can do:
Code: Bash  [Select][+][-]
  1. lucamar@selene:~$ udisks --mount /dev/sda3
  2. Mounted /org/freedesktop/UDisks/devices/sda3 at /media/Backs
  3. lucamar@selene:~$ ls /media/Backs
  4. Archive.7z          Names.txt                  versalles.txt  xeva-win.txt
  5. Archives            Oly.txt                    VIDEOS         zOly
  6. Backups             PortApps                   Virtual
  7. bookmarks.html      RECYCLER                   web
  8. Documentos.tar.bz2  System Volume Information  xevalin.txt
  9.  

Handy little bugger! :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to mount/unmount a partition?
« Reply #11 on: December 08, 2018, 01:55:02 pm »
... Type udisksctrl into a terminal to see if that is available ...
I think there is a little typo, it must be "udisksctl" instead of "udisksctrl".

"udisksctl" works perfectly on my Ubuntu 18.04! Now I can mount even a NTFS partition without sudo. I'm happy!

... This system (Ubuntu 11) is probably too old but it has the (I suppose) equivalent command udisks ...
On my Ubuntu 18.04 "udisks" does not exist. But "udisksctl" does the job.

Thank you very much to all who helped me. This is a great forum.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: [SOLVED] Linux: how to mount/unmount a partition?
« Reply #12 on: December 09, 2018, 12:39:10 pm »
... Type udisksctrl into a terminal to see if that is available ...
I think there is a little typo, it must be "udisksctl" instead of "udisksctrl".

"udisksctl" works perfectly on my Ubuntu 18.04! Now I can mount even a NTFS partition without sudo. I'm happy!

... This system (Ubuntu 11) is probably too old but it has the (I suppose) equivalent command udisks ...
On my Ubuntu 18.04 "udisks" does not exist. But "udisksctl" does the job.

Thank you very much to all who helped me. This is a great forum.
Oh sorry, my bad! But in the example it was written correctly..
Glad you could solve your problem though!
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

 

TinyPortal © 2005-2018