[SOLVED] Linux: how to mount/unmount a partition? lazarus / 32-Linux ---------------------------------------------------------------------- Hartmut · 2018-12-06 12:46 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. ---------------------------------------------------------------------- lucamar · 2018-12-06 13:11 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. ---------------------------------------------------------------------- Hartmut · 2018-12-06 15:12 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function unmount(folder: ansistring): boolean;   var s: ansistring;       ok: boolean;   begin   ok:=RunCommand('/bin/bash', ['-c', 'umount ' + folder], s);   exit(ok);   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 · 2018-12-06 17:47 Quote from: Hartmut on December 06, 2018, 04:12:31 pmBut 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i echo "my password" | sudo --stdin mount [...etc...] the same but executed directly from Pascal code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DoMount: Boolean;const  command  = '/bin/bash -c echo "my password" | sudo --stdin mount whatever';var  outstr: String;begin  Result := RunCommand(command, outstr)end;  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: QuoteCommon for all types of file system are the options [...]``user'' (allow a user to  mount) [...] ---------------------------------------------------------------------- Hartmut · 2018-12-06 19:23 Thank you very much, lucamar, for your help and that idea. It works after I modified it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DoMount: Boolean;const  command = '/bin/bash';  params  = 'echo "my password" | sudo --stdin mount /media/C/';var  outstr: String;begin  Result := RunCommand(command, ['-c', params], outstr);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 · 2018-12-06 20:23 Quote from: Hartmut on December 06, 2018, 08:23:37 pmThank you very much, lucamar, for your help and that idea. It works after I modified it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [... correct code ...] Yeah, sorry: I copied the wrong line from a diff QuoteBut 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. QuoteI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lucamar@selene:~$ uname -srvmoLinux 2.6.38-16-generic #67-Ubuntu SMP Thu Sep 6 18:00:43 UTC 2012 i686 GNU/Linuxlucamar@selene:~$ mount /media/Backs/Unprivileged user can not mount NTFS block devices using the external FUSElibrary. Either mount the volume as root, or rebuild NTFS-3G with integratedFUSE support and make it setuid root. Please see more information athttp://ntfs-3g.org/support.html#unprivileged  It seems mount needs to be sudoed to mount ntfs volumes. ---------------------------------------------------------------------- Hartmut · 2018-12-06 21:21 Quote from: lucamar on December 06, 2018, 09:23:19 pmIt seems mount needs to be sudoed to mount ntfs volumes. Thanks for that information. Must stop for the night now. Will continue tomorrow. ---------------------------------------------------------------------- soerensen3 · 2018-12-06 21:35 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 ---------------------------------------------------------------------- Hartmut · 2018-12-07 19:21 Quote from: lucamar on December 06, 2018, 09:23:19 pmIt 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). Quote from: soerensen3 on December 06, 2018, 10:35:05 pmdoes 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 · 2018-12-07 20:49 Quote from: Hartmut on December 07, 2018, 08:21:07 pmI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ↪ lsblkNAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda      8:0    0 238,5G  0 disk ├─sda1   8:1    0    89M  0 part ├─sda2   8:2    0  76,1G  0 part ├─sda3   8:3    0    28G  0 part /├─sda4   8:4    0     1K  0 part ├─sda5   8:5    0 130,4G  0 part /home└─sda6   8:6    0     4G  0 part [SWAP]sr0     11:0    1  1024M  0 rom   ↪ udisksctl mount -b /dev/sda2Mounted /dev/sda2 at /run/media/johannes/D2F0B901F0B8ECBB. ↪ lsblkNAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda      8:0    0 238,5G  0 disk ├─sda1   8:1    0    89M  0 part ├─sda2   8:2    0  76,1G  0 part /run/media/johannes/D2F0B901F0B8ECBB├─sda3   8:3    0  28G  0 part /├─sda4   8:4    0     1K  0 part ├─sda5   8:5    0 130,4G  0 part /home└─sda6   8:6    0     4G  0 part [SWAP]sr0     11:0    1  1024M  0 rom ---------------------------------------------------------------------- lucamar · 2018-12-07 23:06 Quote from: soerensen3 on December 07, 2018, 09:49:09 pmWhat 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lucamar@selene:~$ udisks --mount /dev/sda3Mounted /org/freedesktop/UDisks/devices/sda3 at /media/Backslucamar@selene:~$ ls /media/BacksArchive.7z          Names.txt                  versalles.txt  xeva-win.txtArchives            Oly.txt                  VIDEOS         zOlyBackups             PortApps                   Virtualbookmarks.html      RECYCLER                   webDocumentos.tar.bz2  System Volume Information  xevalin.txt  Handy little bugger! ---------------------------------------------------------------------- Hartmut · 2018-12-08 12:55 Quote from: soerensen3 on December 07, 2018, 09:49:09 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! Quote from: lucamar on December 08, 2018, 12:06:32 am... 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 · 2018-12-09 11:39 Quote from: Hartmut on December 08, 2018, 01:55:02 pmQuote from: soerensen3 on December 07, 2018, 09:49:09 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! Quote from: lucamar on December 08, 2018, 12:06:32 am... 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!