Flutter quick-usb on core

I’m trying to use the flutter quick-usb package in core. Does anyone know what I need to do to make the usb bus visible/usable?
I’ve tried adding the raw-usb plug into my snap but no luck.

I’m new to snaps/snapcraft and my knowledge is limited so any help would be much appreciated

Hi @laughing-gravy ,

Generally speaking, USB Printers can be reached from /dev/usb/lpx. Is that also the case for you? If so, this is already exposed as part of the raw-usb plug.

What I could suggest is that, please run your snap at Ubuntu Desktop first. Probably, you would need to add some additional plugs to your snap. To understand what would be the problem, please run tail -f /var/log/syslog | grep audit in another terminal screen while running your snap to see if there is any permission issue due to missing plugs.

In the meantime, you could try running cups to check if it is possible to print by using your printer.

https://forum.snapcraft.io/t/call-for-testing-openprintings-cups-snap/

Lastly, it could be nice to provide a sample application with a snapcraft.yaml so that people can see and react.

Thank for your response @bugraaydogar. I have the snap working in desktop mode. This is the output from syslog. This looks like my app discovering the usb hub then searching through for the device:

Apr 26 15:51:34 developer-lubuntu kernel: [28653.000360] audit: type=1400 audit(1619448694.369:13002): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/sys/bus/usb/devices/" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:51:34 developer-lubuntu kernel: [28653.000726] audit: type=1400 audit(1619448694.369:13003): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/run/udev/data/+usb:2-2.4:1.0" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:51:34 developer-lubuntu kernel: [28653.001044] audit: type=1400 audit(1619448694.369:13004): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/run/udev/data/c189:256" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:51:34 developer-lubuntu kernel: [28653.001288] audit: type=1400 audit(1619448694.369:13005): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/run/udev/data/c189:133" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:51:34 developer-lubuntu kernel: [28653.001455] audit: type=1400 audit(1619448694.369:13006): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/run/udev/data/+usb:1-2:1.0" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:51:34 developer-lubuntu kernel: [28653.001608] audit: type=1400 audit(1619448694.369:13007): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/run/udev/data/c189:0" pid=132976 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 26 15:52:35 developer-lubuntu kernel: [28713.653965] audit: type=1400 audit(1619448755.022:13064): apparmor="ALLOWED" operation="open" profile="snap.testprint.testprint" name="/dev/bus/usb/002/009" pid=132976 comm="io.flutter.ui" requested_mask="wr" denied_mask="wr" fsuid=0 ouid=0

Based on this have you got some pointers on what I would need to put in my snapcraft.yaml to get this working on core??

Thanks

hmmm. There is no permission issue within this snap. Just to verify, what is your confinement in the snapcraft.yaml while running on desktop? Is it classic, devmode or strict? I assume it is strict.

I also realize that your printer is using /dev/bus/usb/… instead of /dev/usb/lpx. But, that’s not a problem since /dev/bus/usb is also exposed by usb-raw.

Is there any logs from the application that could be compared with Desktop and Core?
Did you have time to check if you could print anything by using CUPS on Core? And finally, when you install your snap in devmode by using snap install yoursnap --devmode, can you print?

Hello @bugraaydogar I should have made clear in my original post that I’m running core on a NUC device.
I’m running both snaps (desktop and core) in devmode. So just to confirm, the desktop snap works, the snap on core (which is running on a NUC device) does not.
The application doesn’t really give me anything useful other than it seems it is unable to instantiate the ‘_libusb’ variable it needs to perform any usb device discovery

does your snap ship the lsusb binary (stage package: usbutils) ? and libusb-1.0-0 as well ?

i just recently packaged OSCAM that uses lsusb extensively internally and works well with the snapcraft.yaml at:

perhaps that gives you some ideas …

Thanks @ogra. I’ve now got the snap running on the desktop in strict mode and I’m now able to replicate the issue that I’m having on the NUC but now I have some new logs:

Apr 27 11:41:59 developer-lubuntu kernel: [12565.576776] audit: type=1400 audit(1619520119.154:460): apparmor="DENIED" operation="open" profile="snap.testprint.testprint" name="/proc/sys/vm/max_map_count" pid=54678 comm="test_print_app" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
Apr 27 11:41:59 developer-lubuntu kernel: [12565.902069] audit: type=1400 audit(1619520119.482:461): apparmor="DENIED" operation="open" profile="snap.testprint.testprint" name="/home/developer/flutter-workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so" pid=54678 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=1000

It looks like there is some sort of permissions error but why is the snap referencing the build source? How do I package the built flutter artifact into the snap so it can find it?

are you able/allowed to share your snapcraft.yaml ?

Hey @laughing-gravy,

We are making good progress here. Probably you need a home plug and one additional plug.
I’m assuming you cannot share the source, what you could do is using snappy-debug to give you some hint about missing plug configurations.

In Desktop, please install,
snap install snappy-debug

Before running your application run the following command in a terminal;
sudo journalctl --output=short --follow --all | sudo snappy-debug

Run your application, you can see what would be missing plug in the terminal of the above command.

@bugraaydogar Here’s the output after doing what you said (note I’ve added home to plugs):

= AppArmor =
Time: Apr 27 13:50:24
Log: apparmor="DENIED" operation="open" profile="snap.testprint.testprint" 
name="/proc/sys/vm/max_map_count" pid=85148 comm="test_print_app" 
requested_mask="r" denied_mask="r" fsuid=0 ouid=0
File: /proc/sys/vm/max_map_count (read)
Suggestion:
* adjust program to not access '@{PROC}/sys/vm/max_map_count'
= AppArmor =
Time: Apr 27 13:50:24
Log: apparmor="DENIED" operation="open" profile="snap.testprint.testprint" 
name="/home/developer/flutter- 
workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so" 
pid=85148 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 
ouid=1000
File: /home/developer/flutter- 
workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so (read)
Suggestion:
* add 'home' to 'plugs'

@ogra

name: testprint
version: ‘1.0.0.testprint’
summary: testprint
description: testprint
confinement: strict
base: core18
grade: stable

apps:

daemon:
daemon: simple
restart-condition: always
command-chain:
- bin/run-daemon
- bin/wayland-launch
command: bin/test_print_app
extensions: [flutter-master] # Where “master” defines which Flutter channel to use for the build
plugs: [raw-usb, network-control, home]

testprint:
command-chain:
- bin/wayland-launch
command: bin/test_print_app
extensions: [flutter-master]

parts:
testprint:
source: .
plugin: flutter
flutter-target: lib/main.dart
stage-packages:
- libusb-1.0-0
- usbutils

mir-kiosk-snap-launch:
plugin: dump
source: https://github.com/MirServer/mir-kiosk-snap-launch.git
override-build: $SNAPCRAFT_PART_BUILD/build-with-plugs.sh opengl pulseaudio wayland
stage-packages:
- inotify-tools

assets:
plugin: nil
stage-packages:
# - dmz-cursor-theme
- fonts-dejavu
- fonts-freefont-ttf
- fonts-ubuntu

layout:
/usr/share/fonts:
bind: $SNAP/usr/share/fonts
/etc/fonts:
bind: $SNAP/etc/fonts
/usr/share/icons:
bind: $SNAP/usr/share/icons

coudl you put triple backtics ``` above and below your paste so the indendation does not get lost ?

name: testprint
version: '1.0.0.testprint'
summary: testprint
description: testprint
confinement: strict
base: core18
grade: stable

apps:

  daemon:
    daemon: simple
    restart-condition: always
    command-chain:
      - bin/run-daemon
      - bin/wayland-launch
    command: bin/test_print_app
    extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build
    plugs: [raw-usb, network-control, home]

  testprint:
    command-chain:
      - bin/wayland-launch
    command: bin/test_print_app
    extensions: [flutter-master]

parts:
  testprint:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart
    stage-packages:
      - libusb-1.0-0
      - usbutils

  mir-kiosk-snap-launch:
    plugin: dump
    source: https://github.com/MirServer/mir-kiosk-snap-launch.git
    override-build:  $SNAPCRAFT_PART_BUILD/build-with-plugs.sh opengl pulseaudio wayland
    stage-packages:
      - inotify-tools

  assets:
    plugin: nil
    stage-packages:
      #    - dmz-cursor-theme
      - fonts-dejavu
      - fonts-freefont-ttf
      - fonts-ubuntu

layout:
  /usr/share/fonts:
    bind: $SNAP/usr/share/fonts
  /etc/fonts:
    bind: $SNAP/etc/fonts
  /usr/share/icons:
    bind: $SNAP/usr/share/icons```

@laughing-gravy thanks for sharing these.
It seems like /sys/vm/max_map_count is not exposed by any interface that is available. There is no plug available for that purpose. I also assume that, you have no idea why quick_usb or your application would like to reach that.

@ogra: Can they use system-files ?

i dont think you can use system-files for anything in /sys, /proc or /dev … but i also think the /sys/vm/max_map_count is a red herring …

hmm, i wonder if moving these to the assets part instead would help to not have the app look for them in a weird path …

the manually started app seems to be missing all plugs … it should have the same set of plugs listed as the daemon to give it the same set of permissions …

name: testprint
version: '1.0.0.testprint'
summary: testprint
description: testprint
confinement: strict
base: core18
grade: stable

apps:

  daemon:
    daemon: simple
    restart-condition: always
    command-chain:
      - bin/run-daemon
      - bin/wayland-launch
    command: bin/test_print_app
    extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build
    plugs: [raw-usb, network-control, home]

  testprint:
    command-chain:
      - bin/wayland-launch
    command: bin/test_print_app
    extensions: [flutter-master]
    plugs: [raw-usb, network-control, home]

parts:
  testprint:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart

  mir-kiosk-snap-launch:
    plugin: dump
    source: https://github.com/MirServer/mir-kiosk-snap-launch.git
    override-build:  $SNAPCRAFT_PART_BUILD/build-with-plugs.sh opengl pulseaudio wayland
    stage-packages:
      - inotify-tools

  assets:
    plugin: nil
    stage-packages:
      - libusb-1.0-0
      - usbutils
      - fonts-dejavu
      - fonts-freefont-ttf
      - fonts-ubuntu

layout:
  /usr/share/fonts:
    bind: $SNAP/usr/share/fonts
  /etc/fonts:
    bind: $SNAP/etc/fonts
  /usr/share/icons:
    bind: $SNAP/usr/share/icons```

I've modified the yaml as suggested but I get exactly the same errors:
= AppArmor =
Time: Apr 27 15:08:32
Log: apparmor="DENIED" operation="open" profile="snap.testprint.testprint" name="/proc/sys/vm/max_map_count" pid=105546 comm="test_print_app" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
File: /proc/sys/vm/max_map_count (read)
Suggestion:
* adjust program to not access '@{PROC}/sys/vm/max_map_count'

= AppArmor =
Time: Apr 27 15:08:33
Log: apparmor="DENIED" operation="open" profile="snap.testprint.testprint" name="/home/developer/flutter-workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so" pid=105546 comm="io.flutter.ui" requested_mask="r" denied_mask="r" fsuid=0 ouid=1000
File: /home/developer/flutter-workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so (read)
Suggestion:
* add 'home' to 'plugs'

@laughing-gravy: Did you hard-coded the path of libusb-1.0.23.so in your flutter application?
I believe you don’t have a path like /home/developer/flutter-workspace/test_print_app/build/linux/debug/bundle/lib/libusb-1.0.23.so in your UC.

Would it be possible to use the library from /usr/lib/x86_64-linux-gnu?

or preferably have it respect LD_LIBRARY_PATH :slight_smile:

@bugraaydogar It isn’t hard coded, it is put there by the flutter build.
I alluded to this earlier. I suspect the problem here is that this path doesn’t exist in the snap. Perhaps in devmode it can access that folder? However, I don’t know why it is included when the flutter plugin builds the app. I would have thought that the flutter app (which is from the bundle directory down) would be included somewhere in the snap rather than there being a reference to my source directory on my build machine. Do you know anything about the flutter plugin and how to achieve this?