OSMC run add-on as root

Hi,

I am really new with linux. I would like to create a small add-on in OSMC which can toggle an GPIO in Raspberry Pi2. I can run this in terminal with sudo.
When I modified the code to be able to run as add-on in OSMC, I can not run it, because I get this error:

12:53:57 1139.827148 T:1657795616 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<–
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type ‘exceptions.RuntimeError’>
Error Contents: No access to /dev/mem. Try running as root!
Traceback (most recent call last):
File “/home/osmc/Python/dialog.py”, line 60, in
GPIO.setup(i, GPIO.OUT) # LED pin set as output
RuntimeError: No access to /dev/mem. Try running as root!
–>End of Python script error report<–

How can I run my script as root?

Thanks in advance

No escalating privileges within Kodi unfortunately.

But you can call another python script outside of Kodi using command line instructions and subprocess.call().

Thanks, it is working.
Based on this, I am going to learn a service programming and comminicating an OSMC script with this service.

In new versions of OSMC we do not enforce strict devmem, so you will not need root privileges to interface with GPIO. This has been in for a couple of months or so.

Hi Sam, is this still the case? Sorry I realise your comment is from sometime ago but I’m new to OSMC and need to access the GPIO pins with www-data user and as OSMC does not have the GPIO group I can’t add it. I can call my python scripts with sudo but not with the OSMC user or, more importantly for my usage, with the www-data user from my home webpage. Can you help? Thanks :slight_smile:

It’s a very old post and I’m not sure exactly what you want to do. @sam_nazarko hasn’t yet responded, so I’m unsure what the current state of play is.

I doubt you need access to /dev/mem but if you want read access to /dev/gpiomen, create a file /etc/udev/rules.d/01-gpio-permissions.rules and paste this line into it:

KERNEL=="gpiomem", SUBSYSTEM=="bcm2835-gpiomem", MODE="0640", GROUP="osmc"

then reboot. Afterwards, /dev/gpiomem will have the following permissions and ownership:

crw-r----- 1 root osmc 244, 0 May 17 12:45 /dev/gpiomem

If you add group osmc to user www-data, your web page should now have read access.

I think the www-data user should be able to access it fine