How to grant the snap app access to the /dev/mem?

I have installed the ubuntu core on a Raspberry3 and made a snap app about operating the rasp’s gpio pins.
But there will be an error ("RuntimeError: No access to /dev/mem. Try running as root!
" ) when I try to run the snap app.
The python file contains a very simple function, which is to turn the gpio pins on and off.

I try to solve it by the following command lines
" sudo adduser myusername gpio
sudo chown myusername /dev/gpiomem
sudo chmod g+rw /dev/gpiomem
"

emmmm… it made no sense.

So how can I solve the problem??? And what’i more ,why it happened???

/dev/mem access is mediated by the physical-memory-control and physical-memory-observe interfaces.

You’ll need to plug one or both of these to get read (observe) or write (control) access to /dev/mem.

See https://docs.snapcraft.io/build-snaps/metadata

gpiomem access is actually granted automatically when using a gpio interface … that said, i think you need to use a pi image from edge to even get these interfaces since the gadget snap in stable does not provide them.

Thinks for your answer, My Pi’s system image is an edge version which is “Ubuntu Core 16 image for Raspberry Pi 3 (edge)”.

Thanks for your careful answer
I have tried adding plugs after the corresponding directives in the snapcraft.yaml, but this doesn;t seem to work,I added some interfaces to the file(gpio-memory-control ; gpio ; physical-memory-control ; physical-memory-observe) in order to grant the snap app access to the /dev/gpiomem and /dev/mem.
Following is the configuration of my app:

apps:
openthewind:
command: openwind
plugs:

  • gpio-memory-control
  • physical-memory-control
  • physical-memory-observe
  • gpio

parts:
my-part:

See ‘snapcraft plugins’

source: .
plugin: python
python-packages: [RPi.GPIO]

emmmm… hahahaha
I come back (⁎⁍̴̛ᴗ⁍̴̛⁎)
I have solved this problem.
After adding the relevant interface description, you still need to use “sudo your-snap-command” to let the app access the gpio pins.
Thanks again for your answer

yup, this is dealt with in:

eventually we should be able to assign all gpio access to a certain group or have it grant access via udev-acl’s so you wont need sudo for your snap command anymore.

1 Like