How can I safely eject USB disks in Openmediavault?

  • I have an external hard drive connected by usb to a PC with the Openmediavault Nas system. I'd like to be able to eject the drive out of the way and unplug it from power via a wifi plug when I'm not using it.

    The bad thing is that to eject a disk in OMV you have to end the processes that are using it, and I use that disk to access it from Windows via SMB. When I want to eject it, I have to remove the SMB share and delete the shared folder on the drive, then it does let me eject it. This is torture to have to do it this way, in my previous Synology with a simple click you ejected the disk safely.

    As I have been researching, in OMV it is impossible to eject the disk if there are shared folders. It seems crazy to me because an external drive is something portable that you can take anywhere. I do not understand how in OMV it is not possible to eject the disk without first deleting the shared folder or having to do the crazy thing of shutting down the server.

    -Isn't there a way to automate this whole process in OMV so that it ejects the disk through a script or a command? Is there a program where you can do this? (without having to delete the shared folder).

    -Are there ways for the drive to park its heads (or go into hibernation) so I can unplug it without damaging the drive?

    -Can you create a script that does the whole process automatically? Create folder, add privileges to a user group, share it via SMB.

    • Official Post

    OMV is open source. Everything is possible. But you have to do it on your own.

    The design is such that hard drives are always connected. Only the usb-backup plugin allows to run a backup by plugging in the drive and removing it after the backup is completed.


    Are you not able to just spin down the drive when not used?

    BTW: did you check, what the power consumption of the wifi plug is? Might be more than the one of the power supply with hdd spun down.

  • Thank you very much mate. The disk has sensitive information about my work and I would not like to have it always connected. That's why when I need it I connect the power to it remotely and when I'm done I eject it safely and disconnect the power.


    If the disk goes into hibernation, can it be safely disconnected from the power supply? How would I know when the disk has gone into hibernation? would there be any way to force hibernation by some command immediately?


    I'm new to OPV and I have many doubts hehe, are there ways to share folders, add users, etc by command line? where can i get information?

    • Official Post

    OMV is based on Debian. So anything which works on Debian can be used. BUT it may interfere with how OMV works.


    You can power off a drive with

    hdparm -Y /dev/sdX

    to check the status

    hdparm -C /dev/sdX but this can wake up the drive. In that case try

    smartctl -i -n standby /dev/sdX


    but OMV does not like if a drive is missing. You will not be able to make any configuration changes with a missing drive.

  • What would you expect to happen, if you unplug (unmount) the disk and still have the smb shares or other services using it defined?

    If you got help in the forum and want to give something back to the project click here (omv) or here (scroll down) (plugins) and write up your solution for others.

  • Thank you very much. I'll try the commands to see if the disk hibernates.


    I am also going to test if the system reacts when a disk is missing, if I still have access to the folders on the other drives, if I can continue adding shared folders, etc. When I test it, I'll comment on the news again.


    What would you expect to happen, if you unplug (unmount) the disk and still have the smb shares or other services using it defined?

    I haven't tried it yet, but the only thing I'm going to use that disk for is to copy data that I need in windows, jpg images and that kind of thing. Once I have the data I need, I'd like to hibernate it and unplug it from power. I won't be using that drive for any apps, cloud downloads or anything like that. The behavior I expect is if I unplug it, it will make the drive inaccessible for copying or reading data. It is the only thing that I hope, to see if there is luck.

  • I haven't tried it yet, but the only thing I'm going to use that disk for is to copy data that I need in windows, jpg images and that kind of thing. Once I have the data I need, I'd like to hibernate it and unplug it from power. I won't be using that drive for any apps, cloud downloads or anything like that. The behavior I expect is if I unplug it, it will make the drive inaccessible for copying or reading data. It is the only thing that I hope, to see if there is luck.

    So your idea is to have something like inactive configurations, which keep the definition, but are not rendered to the config files and can be activated again, once you plug the drive back.

    With the omv-prc command line tool, you can make all changes, which can be done by the ui.


    Maybe it is worth a feature request.

    If you got help in the forum and want to give something back to the project click here (omv) or here (scroll down) (plugins) and write up your solution for others.

  • Hello partners.


    I tried the code to hibernate the disks but as you said there seems to be no way to know if they are hibernating or not. The drive supposedly goes into sleep mode.


    I have also found out that the disk can be unmounted from the console (from the web panel it does not leave) and that works very well. But the disk identifier (esdf1) changes when you unplug and plug the disk back in. So that it always recognizes the disk even if you remove it and put it in another USB connector, you have to identify it in another way.


    With the command sudo lsblk you will get a list of the drives and in my case this would be the identifier of my external usb drive:


    /srv/dev-disk-by-uuid-669E8B469E8B0B8D


    Then to unmount it:


    sudo umount /srv/dev-disk-by-uuid-669E8B469E8B0B8D


    This safely unmounts the drive and you can safely unplug the drive from power.


    In my case I have two external disks, I have added that command in the OMV task scheduler to be able to disconnect any of the two. To identify which task is the one that removes a certain disk you can add a comment.


    fcFnM9h.jpg



    But there is the problem of shared folders that remain in OMV without a drive. In my case, having only two disks, it would be two accesses to the root directory of the "/" disks and share them from SMB. Because my intention is to see all the content of the disks when I connect them from Windows. It would be great if OMV allowed this, as it happens in other nas systems, but it doesn't and if the drives are not connected it won't let you add more shared folders on other drives (gives an error of type Failed to execute command 'export PATH =...), in short, if you don't have all the disks online, OMV won't let you create any shared folder, it gives an error because you haven't deleted those folders and you have them shared in SMB.



    For now with the zero knowledge I have of Linux (I have always used Windows) it is the only place I have been able to reach. Macon and Zoki, thanks for your help.


    Zoki, where could I find more information about the omv-prc command line tool? Maybe as you say I could build a function to create and delete the shared folders (by turning the drives on and off from the scheduler). I think the file where all these OMV references are stored is /etc/openmediavault/config.xml. If you can modify the file by command (create folders, give permissions, share via SMB).


    Thanks, I'll let you know what's new.

  • Zoki, where could I find more information about the omv-prc command line tool? Maybe as you say I could build a function to create and delete the shared folders (by turning the drives on and off from the scheduler). I think the file where all these OMV references are stored is /etc/openmediavault/config.xml. If you can modify the file by command (create folders, give permissions, share via SMB).

    You can find it in the docs: https://openmediavault.readthe…opment/tools/omv_rpc.html


    Having such a job should be pretty easy given the info there.

    If you got help in the forum and want to give something back to the project click here (omv) or here (scroll down) (plugins) and write up your solution for others.

  • If I hear that there are more secret things from work on a USB drive, I would at least put them in an encrypted container/folder. I see several advantages there:

    - If you lose the drive, not everyone can read everything.

    - You open an encrypted folder when you need to, but you close it again afterwards, so that no one else can open it.

  • You can find it in the docs: https://openmediavault.readthe…opment/tools/omv_rpc.html


    Having such a job should be pretty easy given the info there.

    Hello fellow Zoki. Made some progress thanks to your help, but got a bit stuck on this part: This is how I can delete the SMB share. The method needs the uuid of the share to remove it:


    omv-rpc -u admin 'smb' 'deleteShare' '{"uuid": "0be7e06a-a888-436d-8088-5ec63963cc50"}'


    But now I need to add it back, but I don't know what parameters to send to the setShare method to write it to the database. I have tried like this:


    Code
    omv-rpc -u admin 'smb' 'setShare' '{"uuid":"0be7e06a-a888-436d-8088-5ec63963cc50","enable":true,"sharedfolderref":"626db680-b317-42b9-a312-d30392bd5e36","comment":"","guest":"no","readonly":false,"browseable":true,"recyclebin":true,"recyclemaxsize":0,"recyclemaxage":0,"hidedotfiles":true,"inheritacls":true,"inheritpermissions":false,"easupport":false,"storedosattributes":false,"hostsallow":"","hostsdeny":"","audit":false,"timemachine":false,"extraoptions":""}'


    But it gives an error, I think the reason is because the uuid is not created and I don't know if at some point this number is randomly generated or refers to shaderedfolderref in some way.


    Code
    {"response":null,"error":{"code":0,"message":"Failed to execute XPath query '\/\/services\/smb\/shares\/share[uuid='0be7e06a-a888-436d-8088-5ec63963cc50']'.","trace":"OMV\\Config\\DatabaseException: Failed to execute XPath query '\/\/services\/smb\/shares\/share[uuid='0be7e06a-a888-436d-8088-5ec63963cc50']'. in \/usr\/share\/php\/openmediavault\/config\/database.inc:78\nStack trace:\n#0 \/usr\/share\/php\/openmediavault\/config\/database.inc(108): OMV\\Config\\Database->get('conf.service.sm...', '0be7e06a-a888-4...')\n#1 \/usr\/share\/php\/openmediavault\/config\/database.inc(255): OMV\\Config\\Database->getAssoc('conf.service.sm...', '0be7e06a-a888-4...')\n#2 \/usr\/share\/openmediavault\/engined\/rpc\/smb.inc(187): OMV\\Config\\Database->set(Object(OMV\\Config\\ConfigObject))\n#3 [internal function]: Engined\\Rpc\\Smb->setShare(Array, Array)\n#4 \/usr\/share\/php\/openmediavault\/rpc\/serviceabstract.inc(123): call_user_func_array(Array, Array)\n#5 \/usr\/share\/php\/openmediavault\/rpc\/rpc.inc(86): OMV\\Rpc\\ServiceAbstract->callMethod('setShare', Array, Array)\n#6 \/usr\/sbin\/omv-engined(537): OMV\\Rpc\\Rpc::call('smb', 'setShare', Array, Array, 1)\n#7 {main}"}}


    In the documentation it does not clarify what parameters it needs, although if I send it without uuid it gives an error that the uuid is missing. Let's see if you can give me a hand.




    Here the documentation of the smb class:


    https://github.com/openmediava…vault/engined/rpc/smb.inc

  • If I hear that there are more secret things from work on a USB drive, I would at least put them in an encrypted container/folder. I see several advantages there:

    - If you lose the drive, not everyone can read everything.

    - You open an encrypted folder when you need to, but you close it again afterwards, so that no one else can open it.

    It also seems like a good idea to me, but the bad thing I see is having the disk working 24/7 (also because of the energy consumption that, although it is very little, if there are two disks or more a year, it supposes an extra cost). There are weeks that I don't use it, or I only use it once to get some data from it, I think it's best to leave it off and turn it on remotely when needed.


    I also need to unplug it from time to time to use it on other computers outside of my house. But I will also keep it in mind to encrypt it to secure your data. Thanks mate.

  • In the documentation it does not clarify what parameters it needs, although if I send it without uuid it gives an error that the uuid is missing. Let's see if you can give me a hand.

    If you take your Browser and open the web tools panel (F12), you can see the requests the OMV frontend makes to the backend. There will be lots of calls to rpc.php.

    If you look at the payload of one of these requests, you will see the json sent to the servern. This should help you build up the calls.

    So what you have to do is to execute the operation on the UI, find the corresponding call to rpc.php and take the payload for your call to omv-rpc.


    Take a close look on which call you use, the UI does quite a lot calls to getInformation ...


    This is a call to create a shared folder:

    This is a call to create a smb share on that shared folder:

    And this is the apply:

    Code
    {
      "service": "Config",
      "method": "applyChangesBg",
      "params": {
        "modules": [],
        "force": false
      },
      "options": null
    }

    So the uuid is the uuid of the shared folder

    You can check the structure of the database in /etc/openmediavault/config.xml

    If you got help in the forum and want to give something back to the project click here (omv) or here (scroll down) (plugins) and write up your solution for others.

  • Good Zoki, thanks to your help I have managed to complete the puzzle. Zoki, it seems that every time I unmount the disk and remount the uuid changes, so it's completely random. The uuid of the shared folder and the uuid of the SMB resource are also completely random. But I've figured out how to get them on the fly and then when unmounting the drive. The only stable thing that is always maintained is the disk id, luckily...


    Now everything works ok, but only through SSH, in the task scheduler there is no way to make it work and I don't know why. I have put the script in a .sh file and I am executing it in this way:

    sh /usr/sbin/comandos/tarea.sh


    But when I run it, it tells me that omv-rpc was not found. I have no idea about Debian or why the script works perfectly from SSH and in task scheduler it doesn't work at all, the only three commands I've tried, all fail. Lets see if you can lend me a hand.


    sh:


    Bash
    #!/bin/bash
    omv-rpc -u admin 'FileSystemMgmt' 'mount' '{"id": "/dev/disk/by-uuid/269E8B449E8B0B8D", "fstab":true}'
    uuid_disco_JS=$(sudo omv-rpc -u admin 'fstab' 'getByFsName' '{"fsname": "/dev/disk/by-uuid/269E8B449E8B0B8D"}')
    uuid_disco=$(sudo bash jq -r '.uuid' <<< $uuid_disco_JS)


    Code
    Failed to execute command 'export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export LANG=C.UTF-8; export SHELL=/bin/sh; sudo --shell --non-interactive --user='Nasito' -- /var/lib/openmediavault/cron.d/userdefined-8902b8a9-bc53-4ff0-bb37-72d8ceeb5031 2>&1' with exit code '2': /usr/sbin/comandos/tarea.sh: 2: /usr/sbin/comandos/tarea.sh: omv-rpc: not found /usr/sbin/comandos/tarea.sh: 3: /usr/sbin/comandos/tarea.sh: : not found /usr/sbin/comandos/tarea.sh: 4: /usr/sbin/comandos/tarea.sh: Syntax error: redirection unexpected


    EDIT: I'm finding things out as I go. I'm sorry but I have never used shell commands nor do I know what a bash is, or a bin, etc.


    It seems that the SSH commands are included in bash functions and now it seems to work. But I have had to add a PATH to the file:



    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


    It looks like you can now access the omv-rpc service


    Now it looks like this:


    Bash
    #!/bin/bash
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    sampleFunction(){
    omv-rpc -u admin 'FileSystemMgmt' 'mount' '{"id": "/dev/disk/by-uuid/269E8B449E8B0B8D", "fstab":true}'
    uuid_disco_JS=$(omv-rpc -u admin 'fstab' 'getByFsName' '{"fsname": "/dev/disk/by-uuid/269E8B449E8B0B8D"}')
    uuid_disco=$(printf '%s\n' "$uuid_disco_JS" | jq '.uuid' -r)
    }
    sampleFunction


    Now it unmounts the disk fine, but errors:


    Code
    Failed to execute command 'export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export LANG=C.UTF-8; export SHELL=/bin/sh; sudo --shell --non-interactive --user='Nasito' -- /var/lib/openmediavault/cron.d/userdefined-8902b8a9-bc53-4ff0-bb37-72d8ceeb5031 2>&1' with exit code '127': null cb0284ca-d7ad-4c9b-90d3-75363fa14039 /usr/sbin/comandos/tarea.sh: 9: /usr/sbin/comandos/tarea.sh: } : not found /usr/sbin/comandos/tarea.sh: 3: /usr/sbin/comandos/tarea.sh: { : not found


    Do you know what can cause these errors?


    I will continue to test and investigate. Thank you very much partners.

  • Some terminology:

    ssh is only a means of opening a connection to your server. In the background it starts a shell, usually bash.

    This bash has more capabilities as the one cron use. The shell for cron has limited capabilities.

    A script is only a sequence of commands, you could have typed into the shell. But rules how " and ' are treated inside a script are different.


    Your script is more complicated than it has to be. You do not need a function:



    Make sure, you have unix line breaks (only lf, not cr/lf as on windows). If you cut-and-paste you may have to adjust in the editor of your choice.

    If you got help in the forum and want to give something back to the project click here (omv) or here (scroll down) (plugins) and write up your solution for others.

  • Thank you friend Zoki. Thanks to you I have solved it completely. It works perfect, finally. I am happy with the result.

    I'm going to make a guide in case someone needs it.

  • Here I leave the .sh to mount disk, create shared folders, user privileges and SMB resources. Thanks to Zoki and the fellow contributors to the thread, you're all great.


    MOUNT DISC:



    REMOVE DISC:



    If you don't want me to show you the debugged code in the process, you can comment the line set -x # allow debug output. That way you will only get the echo.

    To get the disk id you can use the command sudo lsblk via SSH as root to show the drives. The disk id is something like this:


     /srv/dev-disk-by-uuid-269E8B449E8B0B8D


    Also change the NAME of the shared folder and shared resources to the ones you want, also the options and the users with privileges to said folders (everything is in the code and you can see how the JSON that are sent as parameters to the functions of omv-rpc.


    You must create a folder and assign privileges to it via SSH with the command chmod 777 /tmp/foo and then put both files with the .sh extension in those folders. You can also edit in the .sh the path where you are going to store the json temporarily (I have done it that way, I don't know if there will be another more elegant one).


    Then, for the task scheduler to read the .sh files, you must add the sh command and the path in this way:


     sh /usr/sbin/comandos/desmontar.sh

    If you have any doubt, put it in this thread.



Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!