Adding an OSMC menu entry to run an arbitrary bash script

Hi,

I’ve been googling this on and off for a while. I’m certain it is possible, but I keep disappearing down rabbit holes of overly complex solutions, or those using long defunct add-ons.

I simply want to add menu entries to run arbitrary bash scripts, to save needing to ssh in to mount and dismount veracrypted volumes on external drives. Nothing fancy required, although display of a simple message dialog would be useful.

Can anybody point me to a guide to achieve this?

Thanks in advance

I can’t think of a simple way to do that other than writing a custom add-on.

Is there a reason why something like systemd automounts, or autofs couldn’t be setup to mount these drives when connected automatically? Just like a regular drive.

1 Like

Hi,

No reason other than lack of knowledge on my part…
My scripts aready uniquely identify the drive which is connected, so I’ll investigate the options you’ve suggested.

Thanks for your advice

Is there a reason why a script has to run when you connect the drives?

They’re encrypted with veracrypt, so the appropriate partition must be mounted with the correct key.

Can you give an example of what command would need to be run? (with the keys obscured of course :wink: )

Sure

(It’s 4 am here so I may not respond until later if I finally get some sleep, but my insomnia is pretty bad!)

The core command in the script is as follows

veracrypt --non-interactive --mount-options ro --mount --password="$thepassword" -k "" /dev/disk/by-id/usb-WD_Elements_XXXX_XXXXXXXXXXXXXXXXXXXXXXXX-0:0-part1 /mnt/vc/veracrypt3

Before the vero is shut down, a clean dismount is performed via

veracrypt --dismount

(For all mounted partitions, or they can be individually specified)

The only issue here is if a drive has powered off due to a sleep mode timer (in the drive firmware), however issuing a command such as

sudo fdisk -l

will enumerate all connected drives, before the dismount takes place (otherwise the command fails, leaving the veracrypt partition mounted)

I understand the insomnia bit as I can’t get to sleep many days until 3-4AM and have to be up at 8AM to feed horses…

I’ll toss some ideas around with other team members. I suspect we may be able to come up with a solution.

1 Like

Thanks, much obliged. Sorry to hear you suffer insomnia too. It’s not something I’d wish on anybody.

Do you perhaps just want to run these commands on startup and shutdown, or do you need to control when they are ran?

Sam

As long as you’re using systemd-based fstab mounts, I’d have thought a systemd service “unit” would do the trick (though you’ll probably need one unit file per disk).

In the /etc/fstab you’d need to associate the disk’s UUID with its mount point. See here.

Then for each mount point, create a .service file in /etc/systemd/system. I think the name can be arbitrary but it’s better to associate it with the mount point name, so I’ve called this one mnt-test.service since it’s for a mount point of /mnt/test.

osmc@osmc:~$ cat /etc/systemd/system/mnt-test.service 
[Unit]
Description=Run script when /mnt/test is mounted
Requires=mnt-test.mount
After=mnt-test.mount

[Service]
ExecStart=/home/osmc/logit.sh
Type=forking

[Install]
WantedBy=mnt-test.mount

My script logit.sh just writes a message to the system journal, BTW.

Remember to enable it (sudo systemctl enable mnt-test.sh).

I can’t guarantee that it’ll work first time but it should be a step in the right direction. One caveat: if you’re using autofs, then this isn’t going to work.

Ive done something similar, using the osmc skin you can just add an entry to the sub menu of one of the main menu items…eg, Settings.

From there you can select ‘Change action’, then ‘Custom item’, then enter:

XBMC.RunScript(/home/osmc/path/to/script.py)

One caveat is I think its a Kodi limitation that only python scripts can be run like this, but you can simply call your bash script from the python script via:

import os

os.system('path/to/bash.sh')

or something similar…adding a Kodi notification via the script is pretty straightforward as well.

@retroresolution for your use, are your disks completely veracrypt’ed, or are they files on a normally formatted disk? From your example script, I’m guessing that the partition is encrypted?

Ideally I’d like to run them via a menu, but otherwise they’d need to run on connection of a usb device (with a switch statement to select the correct decryption key for the appropriate device), and on shutdown.

You could use a udev rule to do it on connection of a USB device.
Otherwise it’s not unfeasible to do it via a Kodi menu option. But putting it on the main menu will be skin dependent.

The entire partition is encrypted. On a couple of disks there are two partitions, encrypted with unique keys. Some have an additional small unencrypted fat32 40MB spacer partition.

Any pointers to adding it to a kodi menu? - it doesn’t need to be the main menu

Thanks for the suggestion and examples. I’ll take a closer look when my head’s clearer. Cheers

Thank you for your comprehensive answer.I’ll definitely have a look at this in detail when I’m feeling better than at present. Ta

If you write a Python script, it can appear under Programs, see https://kodi.wiki/view/HOW-TO:Script_addon

Sam

1 Like